378 lines
12 KiB
PHP
378 lines
12 KiB
PHP
<?php
|
||
/**
|
||
* @copyright Copyright (c) 2021 勾股工作室
|
||
* @license https://opensource.org/licenses/Apache-2.0
|
||
* @link https://www.gougucms.com
|
||
*/
|
||
namespace app\admin\controller\product;
|
||
|
||
use app\admin\BaseController;
|
||
use think\facade\Db;
|
||
use think\facade\View;
|
||
use app\admin\model\Merchant; // 商户模型
|
||
use app\admin\model\EbStoreProduct; // 商品模型
|
||
use app\admin\model\StoreCategory; // 商品分类模型
|
||
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; // 区域负责人模型
|
||
use app\common\controller\FormatList;
|
||
|
||
/**
|
||
*
|
||
* 商品控制器
|
||
*
|
||
*/
|
||
class Product extends BaseController
|
||
{
|
||
|
||
protected $adminInfo;
|
||
protected $url;
|
||
protected $product;
|
||
|
||
public function __construct(EbStoreProduct $product)
|
||
{
|
||
$this->adminInfo = get_login_admin();
|
||
$this->product = $product;
|
||
$this->url=[
|
||
'/admin/product.product/index',
|
||
'/admin/product.product/add',
|
||
'/admin/product.product/edit',
|
||
'/admin/product.product/delete',
|
||
'/admin/product.product/index',
|
||
];
|
||
}
|
||
|
||
protected function auth()
|
||
{
|
||
$where = [];
|
||
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','=',''];
|
||
}
|
||
}
|
||
|
||
return $where;
|
||
}
|
||
|
||
/**
|
||
*
|
||
* 供应链团队列表
|
||
*
|
||
*/
|
||
public function index(StoreCategory $category)
|
||
{
|
||
if (true) {
|
||
// request()->isAjax()
|
||
// Ajax 前端获取数据
|
||
$params= get_params();
|
||
|
||
$where = self::auth();
|
||
|
||
if (isset($params['keywords']) && !empty($params['keywords'])){
|
||
// $where[]= ['sotre_name','like','%'.$params['keywords'].'%'];
|
||
$where['keywords'] = $where['store_name'] = $params['keywords'];
|
||
}
|
||
|
||
$page = empty($params['page'])?1:$params['page'];
|
||
$limit = empty($params['limit'])?10:$params['limit'];
|
||
|
||
// 平台商品分类id
|
||
$where['cate_id'] = empty($params['plate_cate'])?'':$params['plate_cate'];
|
||
|
||
// 商户商品分类id
|
||
$where['mer_cate_id'] = empty($params['store_cate'])?'':$params['store_cate'];
|
||
$where['is_trader'] = isset($params['is_trader'])?$params['is_trader']:0; // 是否自营 1自营, 0 不是自营
|
||
|
||
// product
|
||
$where['is_gift_bag'] = empty($params['is_gift'])?'':$params['is_gift'];
|
||
$where['is_show'] = empty($params['state'])?'':$params['state'];
|
||
$where['temp_id'] = empty($params['shipping_tem'])?'':$params['shipping_tem'];
|
||
$where['labels'] = empty($params['tag'])?'':$params['tag'];
|
||
$where['type'] = 1;//实体商品:0 虚拟商品:1
|
||
$where['status'] = 0;//管理、审核、通过
|
||
|
||
|
||
// $where['is_ficti'] = 1;//是否虚拟销量
|
||
// $where['product_id'] = 0;
|
||
// ['order','sort']
|
||
// soft //软删除
|
||
if (!empty(get_params('mer_id'))) {
|
||
$mer_id = get_params('mer_id');
|
||
$where = array_merge($where, $this->switchType($where['type'],$mer_id,0));
|
||
}
|
||
$mer_id = 77;
|
||
|
||
$this->product->getList($mer_id, $where, $page, $limit);
|
||
|
||
return;
|
||
|
||
$total = EbStoreProduct::where($where)->count();
|
||
|
||
|
||
|
||
$list = EbStoreProduct::with(['merchant' => ['merchantType', 'category']])->order('product_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::with(['merchant' => ['merchantType', 'category']])->select();
|
||
|
||
// 初始空页面展示
|
||
$where['mer_id'] = 0;
|
||
$where['is_show'] = 0;
|
||
$cate_list = $category->getList($where);
|
||
$cate_list = FormatList::DropDownMenu($cate_list);
|
||
|
||
View::assign('cate_list', $cate_list);
|
||
View::assign('url', $this->url);
|
||
View::assign('list', $list);
|
||
return view();
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 与类型相对应的sql字段
|
||
*
|
||
* @Author:Liuxiaoquan
|
||
* @Date: 2020/5/18
|
||
* @param $type 商
|
||
* @param int|null $merId 商户id
|
||
* @param int|null $productType 产品类型
|
||
* @return array $where 条件
|
||
*/
|
||
public function switchType($type, ?int $merId = 0, $productType = 0)
|
||
{
|
||
$stock = 0;
|
||
// 获得库存
|
||
// if ($merId) $stock = merchantConfig($merId, 'mer_store_stock');
|
||
switch ($type) {
|
||
case 1:
|
||
$where = ['is_show' => 1, 'status' => 1,];
|
||
break;
|
||
case 2:
|
||
$where = ['is_show' => 0, 'status' => 1];
|
||
break;
|
||
case 3:
|
||
$where = ['is_show' => 1, 'stock' => 0, 'status' => 1];
|
||
break;
|
||
case 4:
|
||
$where = ['stock' => $stock ? $stock : 0, 'status' => 1];
|
||
break;
|
||
case 5:
|
||
$where = ['soft' => true];
|
||
break;
|
||
case 6:
|
||
$where = ['status' => 0];
|
||
break;
|
||
case 7:
|
||
$where = ['status' => -1];
|
||
break;
|
||
case 20:
|
||
$where = ['status' => 1];
|
||
break;
|
||
default:
|
||
// $where = ['is_show' => 1, 'status' => 1];
|
||
break;
|
||
}
|
||
if ($productType == 0) {
|
||
$where['product_type'] = $productType;
|
||
if (!$merId) $where['is_gift_bag'] = 0;
|
||
}
|
||
if ($productType == 1) {
|
||
$where['product_type'] = $productType;
|
||
}
|
||
if ($productType == 10) {
|
||
$where['is_gift_bag'] = 1;
|
||
}
|
||
if (!$merId) $where['star'] = '';
|
||
return $where;
|
||
}
|
||
|
||
/**
|
||
*
|
||
* 新增
|
||
*
|
||
*/
|
||
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['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['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);
|
||
|
||
}
|
||
|
||
} |