| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368 |
- <?php
- declare(strict_types=1);
- namespace app\admin\controller\goods;
- use app\common\controller\Backend;
- use app\admin\traits\Actions;
- use think\annotation\route\Group;
- use think\annotation\route\Route;
- use think\facade\Db;
- use app\common\model\ImportSku;
- use app\common\model\ShopList;
- use app\common\model\StockConfig;
- use app\common\model\ShopDelivery;
- use app\admin\service\JuShuiTanService;
- use app\common\model\CustomerSpec;
- use app\common\model\ProductConfig;
- use app\common\model\ImportList as ImportListModel;
- use app\common\model\ShopList as ShopListModel;
- #[Group("goods/import_list")]
- class ImportList extends Backend
- {
- //app\admin\controller\goods\ImportList
- use Actions {
- index as private _index;
- del as private _del;
-
- }
- private $typeList=null;
- private $tabValue=null;
-
- protected function _initialize()
- {
- parent::_initialize();
- $this->model = new ImportListModel();
- $this->typeList=StockConfig::where('type_id', 'variety_name')->order('sort desc')->column('title', 'id');
- $this->tabValue=array_keys($this->typeList)[0]?? null;
- // $this->assign('shopList', ShopList::where('status', 1)->column('name', 'id'));
- $this->assign('typeList', $this->typeList);
- $this->assign('packingList', StockConfig::where('type_id', 'packing_box')->column('title', 'id'));
- }
- // 获取店铺
- #[Route('GET,POST,JSON', 'getShopList')]
- public function getShopList()
- {
- $shopList = new ShopList();
- $sql_restul = $shopList->alias('s')
- ->join("yun_customer c", "s.customer_id = c.id", "INNER")
- ->where(['s.status' => 1, 'c.status' => 1])
- ->field(['s.id', 's.shop_id', 's.name', 'c.name' => 'nickname'])
- ->select();
- $arr = [];
- foreach ($sql_restul as $item) {
- $arr[$item['id']] = $item['name'] . '(' . '店铺id:' . $item['shop_id'] . ',客户:' . $item['nickname'] . ')';
- }
- return $this->jsonSuccess('', $arr);
- }
- /**
- * 查看
- */
- #[Route('GET,JSON', 'index')]
- public function index()
- {
- if (false === $this->request->isAjax()) {
- return $this->fetch();
- }
- if ($this->request->post('selectpage')) {
- return $this->selectpage();
- }
- [$where, $order, $limit, $with] = $this->buildparams();
- $list = $this->model
- ->withJoin($with, 'left')
- //如果没有使用operate filter过滤的情况下,推荐使用with关联,可以提高查询效率
- //->with($with)
- ->where($where)
- ->order($order)
- ->paginate($limit)
- ->each(function ($item, $key) {
- if (empty($item['type_id'])) {
- $item['type_id'] = 0;
- }
- $trade_from_list = [];
- if ($item['type_id'] == 1) {
- $feng_su_trade_from_list = site_config("addonsd.feng_su_trade_from_list");
- $trade_from_list = $feng_su_trade_from_list;
- } else {
- $ju_shui_tan_trade_from_list = site_config("addonsd.ju_shui_tan_trade_from_list");
- $trade_from_list = $ju_shui_tan_trade_from_list;
- }
- $trade_from = $item['trade_from'];
- $trade_from = isset($trade_from_list[$trade_from]) ? $trade_from_list[$trade_from] : $item['trade_from'];
- $item['trade_from'] = $trade_from;
- // $item['trade_from']=empty($item['trade_from'])?'无':$trade_from_list[$trade_from];
- return $item;
- });
- $result = ['total' => $list->total(), 'rows' => $list->items()];
- return json($result);
- }
- /**
- * 关联店铺
- */
- #[Route('GET,POST', 'shops')]
- public function shops()
- {
- if (false === $this->request->isPost()) {
- $ids = $this->request->get('ids/s', '');
- $shopList = new ShopList();
- $sql_restul = $shopList->alias('s')
- ->join("yun_customer c", "s.customer_id = c.id", "INNER")
- ->where(['s.status' => 1, 'c.status' => 1])
- ->field(['s.id', 's.shop_id', 's.name', 'c.name' => 'nickname'])
- ->select();
- $arr = [];
- foreach ($sql_restul as $item) {
- $arr[$item['id']] = $item['name'] . '(' . '店铺id:' . $item['shop_id'] . ',客户:' . $item['nickname'] . ')';
- }
- $this->assign('shopList', $arr);
- return $this->fetch();
- }
- $params = array_merge($this->request->post("row/a"), $this->postParams);
- if (empty($params)) {
- $this->error(__('提交的参数不能为空'));
- }
- // if (!$this->request->checkToken('__token__', ['__token__' => $this->request->post('__token__')])) {
- // $this->error(__('token错误,请刷新页面重试'));
- // }
- foreach ($params as &$value) {
- if (is_array($value)) {
- $value = implode(',', $value);
- }
- if ($value === '') {
- $value = null;
- }
- }
- $ids = $this->request->get('ids/s', '');
- if (empty($params['shop_id'])) $this->error(__('请选择店铺'));
- $result = false;
- Db::startTrans();
- try {
- //更新店铺
- ShopList::where('id', $params['shop_id'])->update(['shop_id' => $ids]);
- //更新状态
- $result = $this->model->where('shop_id', $ids)->update(['status' => 2]);
- if ($this->callback) {
- $callback = $this->callback;
- $callback($this->model);
- }
- Db::commit();
- } catch (\Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- if ($result === false) {
- $this->error(__('没有新增任何数据'));
- }
- $this->success();
- }
- /**
- * 关联规格
- */
- #[Route('GET,POST', 'specs')]
- public function specs(mixed $row = null)
- {
- $ids = $this->request->param('ids');
- $importSku = new ImportSku();
- if (!$row || is_array($row)) {
- $row = $this->model->find($ids);
- }
- if (!$row) {
- $this->error(__('没有找到记录'));
- }
- if (count($this->volidateFields) > 0) {
- foreach ($this->volidateFields as $field => $value) {
- if ($row[$field] != $value) {
- $this->error(__('没有操作权限'));
- }
- }
- }
- if (false === $this->request->isPost()) {
- $shop_id = $row['shop_id'];
- $shopListModel = new ShopListModel();
- $rows = $shopListModel::where('shop_id', $shop_id)->where('status', 1)->find();
-
- //修改规格
- $importSku = new ImportSku();
- $sql_data = $importSku->where(['shop_id' => $row['shop_id'], 'sku_id' => $row['sku_id']])->find();
- $spec_id=0;
- if(!empty($sql_data)){
- $this->tabValue=$sql_data['variety_id'];
- $spec_id=$sql_data['spec_id'];
- }
- $this->assign('tabValue',$this->tabValue.'');
- $this->assign('spec_id',$spec_id);
- $this->assign('rows', $rows->type_spec ?? '');
- $this->assign('row', $row);
-
- return $this->fetch();
- }
- $params = $this->request->post("");
- if (empty($params['all_data'])) $this->error(__('请选择规格'));
- $count = 0;
- Db::startTrans();
- try {
- $status = $row['status'];
- $time = time();
- $shopList = new ShopList();
- $productConfig = new ProductConfig();
- $shopDelivery = new ShopDelivery();
- $customerSpec = new CustomerSpec();
-
- $shopDelivery_list = [];
- $import_list = [];
- $shopDelivery_updata_list=[];
- $todayStart = date('Y-m-d', $time);
- $todayStart=$todayStart.' 00:00:00';
- switch ($status) {
- case 2:
- //插入规格
- ImportSku::insertSpecs($row['shop_id'], $row['sku_id'], (int)$params['all_data'][0]['type_id'], (int)$params['all_data'][0]['id']);
- //查看有多少规格
- $list = $this->model::where('shop_id', $row['shop_id'])->where('sku_id', $row['sku_id'])->where('status', 2)->select();
- foreach ($list as $item) {
- //插入发货数据
- $res = JuShuiTanService::setAdditionalPrice(
- $importSku,
- $shopList,
- $productConfig,
- $shopDelivery,
- $customerSpec,
- $item['shop_id'],
- $item['sku_id'],
- $params['all_data'][0]['type_id'],
- $params['all_data'][0]['id'],
- $params['all_data'][0]['name'],
- $item
- );
- $getPackSpecsFee = $res['getPackSpecsFee'];
- $shopDelivery_list[] = $res['shopDelivery'];
- $import_list_item = [
- 'id' => $item['id'],
- 'pack_specs_id' => $getPackSpecsFee['data']['id'],
- 'labor_cost_money' => $getPackSpecsFee['data']['labor_cost_money'],
- 'one_surcharge_money' => $getPackSpecsFee['one_surcharge_money'],
- 'two_surcharge_money' => $getPackSpecsFee['two_surcharge_money'],
- 'status' => 3
- ];
- $import_list[] = $import_list_item;
- $count++;
- }
- $this->model->saveAll($import_list);
- $shopDelivery->saveAll($shopDelivery_list);
- break;
- case 3:
- //修改规格
- $importSku = new ImportSku();
- $sql_data = $importSku->where(['shop_id' => $row['shop_id'], 'sku_id' => $row['sku_id']])->find();
- $result = $sql_data->save([
- 'variety_id' => (int)$params['all_data'][0]['type_id'],
- 'spec_id' => (int)$params['all_data'][0]['id']
- ]);
- if (!$result) $this->error(__('没有数据被更新'));
- //查看有多少规格
- $where=[];
- $where[] = ['shop_id', '=', $row['shop_id']];
- $where[] = ['sku_id', '=', $row['sku_id']];
- $where[] = ['consign_time','>=',$todayStart];
- $whereOr=[];
- $whereOr[] = ['shop_id', '=', $row['shop_id']];
- $whereOr[] = ['sku_id', '=', $row['sku_id']];
- $whereOr[] = ['status','=',2];
- $list = $this->model::where($where)->whereOr($whereOr)->select();
-
- foreach ($list as $item) {
- //插入发货数据
- $res = JuShuiTanService::setAdditionalPrice(
- $importSku,
- $shopList,
- $productConfig,
- $shopDelivery,
- $customerSpec,
- $item['shop_id'],
- $item['sku_id'],
- $params['all_data'][0]['type_id'],
- $params['all_data'][0]['id'],
- $params['all_data'][0]['name'],
- $item
- );
- $getPackSpecsFee = $res['getPackSpecsFee'];
-
- if($item['status']==2){
- $shopDelivery_list[] = $res['shopDelivery'];
- }else if($item['status']==3){
- $waybill_no=$res['shopDelivery']['waybill_no'];
- $shopDelivery_id=JuShuiTanService::get_shopDelivery_id($waybill_no);
- if($shopDelivery_id>0){
- $res['shopDelivery']['id']=$shopDelivery_id;
- $res['shopDelivery']['updatetime']=$time;
- $shopDelivery_updata_list[]=$res['shopDelivery'];
- }
-
- }
- $import_list_item = [
- 'id' => $item['id'],
- 'pack_specs_id' => $getPackSpecsFee['data']['id'],
- 'labor_cost_money' => $getPackSpecsFee['data']['labor_cost_money'],
- 'one_surcharge_money' => $getPackSpecsFee['one_surcharge_money'],
- 'two_surcharge_money' => $getPackSpecsFee['two_surcharge_money'],
- 'status' => 3
- ];
-
- $import_list[] = $import_list_item;
- $count++;
- }
- $this->model->saveAll($import_list);
- $shopDelivery->saveAll($shopDelivery_list);
- $shopDelivery->saveAll($shopDelivery_updata_list);
-
- break;
- default:
- # code...
- break;
- }
- Db::commit();
- } catch (\Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- if ($count) {
- return resp_json(200, '操作成功');
- }
- $this->error(__('没有数据被更新'));
- }
- //删除
- #[Route("GET,POST", "del")]
- public function del()
- {
- //通过定义callback回调函数来执行删除后的操作
- $this->callback = function ($ids) {};
- return $this->_del();
- }
- }
|