2023-03-08 10:26:24 +08:00

259 lines
8.6 KiB
PHP

<?php
/**
* @copyright Copyright (c) 2021 勾股工作室
* @license https://opensource.org/licenses/Apache-2.0
* @link https://www.gougucms.com
*/
namespace app\admin\controller\supplychain;
use app\admin\BaseController;
use think\facade\Db;
use think\facade\View;
use app\admin\model\Merchant; // 商户模型
use app\admin\model\GeoCity; // 省市模型
use app\admin\model\GeoArea; // 区域模型
use app\admin\model\GeoStreet; // 街道模型
use app\admin\model\SupplyChain; // 供应链模型
use app\api\model\Area as AreaModel; // 市场区域模型
use app\api\model\AreaManager as AreaManagerModel; // 区域负责人模型
class Index extends BaseController
{
public function __construct()
{
$this->adminInfo = get_login_admin();
$this->category_id=354;
$this->url=[
'/admin/supplychain.index/index?category_id='.$this->category_id,
'/admin/supplychain.index/add',
'/admin/supplychain.index/edit',
'/admin/supplychain.index/delete',
'/admin/supplychain.merchant/index',
'/admin/supplychain.merchant/bill',
];
}
/**
*
* 供应链团队列表
*
*/
public function index()
{
if (request()->isAjax()) {
$params= get_params();
$where[]= ['status','=',0];
if (isset($params['keywords']) && !empty($params['keywords'])){
$where[]= ['name','like','%'.$params['keywords'].'%'];
}
if($this->adminInfo['position_id'] != 1){ //不是超级管理员
$www['admin_id'] = $this->adminInfo['id'];
$user_address = Db::table('fa_szxc_information_useraddress')->where($www)->find();
if ($user_address){
if($user_address['auth_range'] == 1){
$where[] = ['village_id','=',$user_address['village_id']];
}elseif ($user_address['auth_range'] == 2){
$where[] = ['street_id','=',$user_address['street_id']];
}elseif ($user_address['auth_range'] == 3){
$where[] = ['area_id','=',$user_address['area_id']];
}else{
$where[] = ['village_id','=',$user_address['village_id']];
}
}else{
$where[] = ['village_id','=',''];
}
}
$total = SupplyChain::where($where)->count();
$list = SupplyChain::with(['merchant', 'street', 'area'])->order('id desc')->select();
View::assign('url', $this->url);
View::assign('list', $list);
$result = ['total' => $total, 'data' => $list];
return table_assign(0, '', $result);
}else{
$list = SupplyChain::select();
View::assign('url', $this->url);
View::assign('list', $list);
return view();
}
}
/**
*
* 新增
*
*/
public function add()
{
if (request()->isAjax()) {
$params = get_params();
$data['user_id'] = $this->adminInfo['id']; // 操作用户ID
$data['name'] = $params['title']; // 团队名称
$data['tel'] = $params['phone']; // 联系电话
$data['shareRate'] = $params['shareRate']; // 分润比例
$data['mer_id_list'] = json_encode($params['mer_id']); // 已选商户
$data['street_id'] = $params['street_id']; // 街道ID
$street = GeoStreet::where('street_id', $data['street_id'])->find(); // 街道数据
$data['lng'] = $street['lng']; // 经度
$data['lat'] = $street['lat']; // 纬度
$area = $street->area; // 区数据
$data['area_id'] = $area['area_id']; // 区县id
$city = $area->city; // 获取市级
$data['address'] = $city['city_name'] . $area['area_name'] . $street['street_name']; // 实际地址
$data['create_time'] = date('Y-m-d H:i:s');
// 数据入库
$res = SupplyChain::create($data);
// 关联数据入库
foreach ($params['mer_id'] as $v) {
$dataLink = [
'eb_merchant_id' => $v, // 商户ID
'user_id' => $data['user_id'],
'create_time' => $data['create_time'],
];
$res->linkMerchant()->save($dataLink); // 插入关联数据
}
if ($res){
return to_assign(0,'操作成功',['aid'=>$res]);
}
return to_assign(1, '操作失败,原因:'.$res);
}else{
// 取出正常的商家
$merchant = Merchant::where('status', 1)->column('mer_id, real_name');
// 区域模型
$arealist = GeoArea::where('city_code', '510500')->select();
View::assign('editor', get_system_config('other','editor'));
View::assign('arealist', $arealist);
View::assign('merchant', $merchant);
View::assign('url', $this->url);
return view();
}
}
/**
*
* 编辑
*
*/
public function edit()
{
$id = get_params("id");
if(!$id) return to_assign(1, '非法操作!');
if (request()->isAjax()) {
$params = get_params();
$data['id'] = $params['id']; // 当前ID
$data['user_id'] = $this->adminInfo['id']; // 操作用户ID
$data['name'] = $params['title']; // 团队名称
$data['tel'] = $params['phone']; // 联系电话
$data['shareRate'] = $params['shareRate']; // 分润比例
$data['mer_id_list'] = isset($params['mer_id']) ? json_encode($params['mer_id']) : null; // 已选商户
$data['street_id'] = $params['street_id']; // 街道ID
$street = GeoStreet::where('street_id', $data['street_id'])->find(); // 街道数据
$data['lng'] = $street['lng']; // 经度
$data['lat'] = $street['lat']; // 纬度
$area = $street->area; // 区数据
$data['area_id'] = $area['area_id']; // 区县id
$city = $area->city; // 获取市级
$data['address'] = $city['city_name'] . $area['area_name'] . $street['street_name']; // 实际地址
$data['create_time'] = date('Y-m-d H:i:s');
// 数据更新
$supplyChain = SupplyChain::with(['linkMerchant'])->find($data['id']);
$res = $supplyChain->update($data);
// 获取关联数据一对一---曲线救国
$linkMerchant = $supplyChain['linkMerchant'];
// $linkMerchantArr = $linkMerchant->column('id');
// 先删除关联数据-- 曲线救国
$linkMerchant->delete();
// 关联商户状态
if($data['mer_id_list'])
{
// 再重新将关联数据入库
foreach ($params['mer_id'] as $v) {
$dataLink = [
'eb_merchant_id' => $v, // 商户ID
'user_id' => $data['user_id'],
'create_time' => $data['create_time'],
];
$supplyChain->linkMerchant()->save($dataLink);
}
}
if ($res){
return to_assign(0,'操作成功',['aid'=>$res]);
}
return to_assign(1, '操作失败,原因:'.$res);
}else{
$supplyChain = SupplyChain::with(['merchant', 'street', 'area'])->find($id); // 取出当前供应链数据
View::assign('detail', $supplyChain);
// 取出正常的商家
$merchant = Merchant::where('status', 1)->column('mer_id, real_name');
// 区域模型
$arealist = GeoArea::where('city_code', '510500')->select();
View::assign('arealist', $arealist);
View::assign('merchant', $merchant);
View::assign('url', $this->url);
return view();
}
}
/**
*
* 删除
*
*/
public function delete()
{
$id = get_params("id");
if(!$id) return to_assign(1, '非法操作!');
$supplyChain = SupplyChain::with(['linkMerchant'])->find($id);
// 删除关联模型
$res = $supplyChain->together(['linkMerchant'])->delete();
if ($res){
return to_assign(0,'操作成功',['aid'=>$res]);
}
return to_assign(1, '操作失败,原因:'.$res);
}
}