ImportList.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. <?php
  2. declare(strict_types=1);
  3. namespace app\admin\controller\goods;
  4. use app\common\controller\Backend;
  5. use app\admin\traits\Actions;
  6. use think\annotation\route\Group;
  7. use think\annotation\route\Route;
  8. use think\facade\Db;
  9. use app\common\model\ImportSku;
  10. use app\common\model\ShopList;
  11. use app\common\model\StockConfig;
  12. use app\common\model\ShopDelivery;
  13. use app\admin\service\JuShuiTanService;
  14. use app\common\model\CustomerSpec;
  15. use app\common\model\ProductConfig;
  16. use app\common\model\ImportList as ImportListModel;
  17. use app\common\model\ShopList as ShopListModel;
  18. #[Group("goods/import_list")]
  19. class ImportList extends Backend
  20. {
  21. //app\admin\controller\goods\ImportList
  22. use Actions {
  23. index as private _index;
  24. del as private _del;
  25. }
  26. private $typeList=null;
  27. private $tabValue=null;
  28. protected function _initialize()
  29. {
  30. parent::_initialize();
  31. $this->model = new ImportListModel();
  32. $this->typeList=StockConfig::where('type_id', 'variety_name')->order('sort desc')->column('title', 'id');
  33. $this->tabValue=array_keys($this->typeList)[0]?? null;
  34. // $this->assign('shopList', ShopList::where('status', 1)->column('name', 'id'));
  35. $this->assign('typeList', $this->typeList);
  36. $this->assign('packingList', StockConfig::where('type_id', 'packing_box')->column('title', 'id'));
  37. }
  38. // 获取店铺
  39. #[Route('GET,POST,JSON', 'getShopList')]
  40. public function getShopList()
  41. {
  42. $shopList = new ShopList();
  43. $sql_restul = $shopList->alias('s')
  44. ->join("yun_customer c", "s.customer_id = c.id", "INNER")
  45. ->where(['s.status' => 1, 'c.status' => 1])
  46. ->field(['s.id', 's.shop_id', 's.name', 'c.name' => 'nickname'])
  47. ->select();
  48. $arr = [];
  49. foreach ($sql_restul as $item) {
  50. $arr[$item['id']] = $item['name'] . '(' . '店铺id:' . $item['shop_id'] . ',客户:' . $item['nickname'] . ')';
  51. }
  52. return $this->jsonSuccess('', $arr);
  53. }
  54. /**
  55. * 查看
  56. */
  57. #[Route('GET,JSON', 'index')]
  58. public function index()
  59. {
  60. if (false === $this->request->isAjax()) {
  61. return $this->fetch();
  62. }
  63. if ($this->request->post('selectpage')) {
  64. return $this->selectpage();
  65. }
  66. [$where, $order, $limit, $with] = $this->buildparams();
  67. $list = $this->model
  68. ->withJoin($with, 'left')
  69. //如果没有使用operate filter过滤的情况下,推荐使用with关联,可以提高查询效率
  70. //->with($with)
  71. ->where($where)
  72. ->order($order)
  73. ->paginate($limit)
  74. ->each(function ($item, $key) {
  75. if (empty($item['type_id'])) {
  76. $item['type_id'] = 0;
  77. }
  78. $trade_from_list = [];
  79. if ($item['type_id'] == 1) {
  80. $feng_su_trade_from_list = site_config("addonsd.feng_su_trade_from_list");
  81. $trade_from_list = $feng_su_trade_from_list;
  82. } else {
  83. $ju_shui_tan_trade_from_list = site_config("addonsd.ju_shui_tan_trade_from_list");
  84. $trade_from_list = $ju_shui_tan_trade_from_list;
  85. }
  86. $trade_from = $item['trade_from'];
  87. $trade_from = isset($trade_from_list[$trade_from]) ? $trade_from_list[$trade_from] : $item['trade_from'];
  88. $item['trade_from'] = $trade_from;
  89. // $item['trade_from']=empty($item['trade_from'])?'无':$trade_from_list[$trade_from];
  90. return $item;
  91. });
  92. $result = ['total' => $list->total(), 'rows' => $list->items()];
  93. return json($result);
  94. }
  95. /**
  96. * 关联店铺
  97. */
  98. #[Route('GET,POST', 'shops')]
  99. public function shops()
  100. {
  101. if (false === $this->request->isPost()) {
  102. $ids = $this->request->get('ids/s', '');
  103. $shopList = new ShopList();
  104. $sql_restul = $shopList->alias('s')
  105. ->join("yun_customer c", "s.customer_id = c.id", "INNER")
  106. ->where(['s.status' => 1, 'c.status' => 1])
  107. ->field(['s.id', 's.shop_id', 's.name', 'c.name' => 'nickname'])
  108. ->select();
  109. $arr = [];
  110. foreach ($sql_restul as $item) {
  111. $arr[$item['id']] = $item['name'] . '(' . '店铺id:' . $item['shop_id'] . ',客户:' . $item['nickname'] . ')';
  112. }
  113. $this->assign('shopList', $arr);
  114. return $this->fetch();
  115. }
  116. $params = array_merge($this->request->post("row/a"), $this->postParams);
  117. if (empty($params)) {
  118. $this->error(__('提交的参数不能为空'));
  119. }
  120. // if (!$this->request->checkToken('__token__', ['__token__' => $this->request->post('__token__')])) {
  121. // $this->error(__('token错误,请刷新页面重试'));
  122. // }
  123. foreach ($params as &$value) {
  124. if (is_array($value)) {
  125. $value = implode(',', $value);
  126. }
  127. if ($value === '') {
  128. $value = null;
  129. }
  130. }
  131. $ids = $this->request->get('ids/s', '');
  132. if (empty($params['shop_id'])) $this->error(__('请选择店铺'));
  133. $result = false;
  134. Db::startTrans();
  135. try {
  136. //更新店铺
  137. ShopList::where('id', $params['shop_id'])->update(['shop_id' => $ids]);
  138. //更新状态
  139. $result = $this->model->where('shop_id', $ids)->update(['status' => 2]);
  140. if ($this->callback) {
  141. $callback = $this->callback;
  142. $callback($this->model);
  143. }
  144. Db::commit();
  145. } catch (\Exception $e) {
  146. Db::rollback();
  147. $this->error($e->getMessage());
  148. }
  149. if ($result === false) {
  150. $this->error(__('没有新增任何数据'));
  151. }
  152. $this->success();
  153. }
  154. /**
  155. * 关联规格
  156. */
  157. #[Route('GET,POST', 'specs')]
  158. public function specs(mixed $row = null)
  159. {
  160. $ids = $this->request->param('ids');
  161. $importSku = new ImportSku();
  162. if (!$row || is_array($row)) {
  163. $row = $this->model->find($ids);
  164. }
  165. if (!$row) {
  166. $this->error(__('没有找到记录'));
  167. }
  168. if (count($this->volidateFields) > 0) {
  169. foreach ($this->volidateFields as $field => $value) {
  170. if ($row[$field] != $value) {
  171. $this->error(__('没有操作权限'));
  172. }
  173. }
  174. }
  175. if (false === $this->request->isPost()) {
  176. $shop_id = $row['shop_id'];
  177. $shopListModel = new ShopListModel();
  178. $rows = $shopListModel::where('shop_id', $shop_id)->where('status', 1)->find();
  179. //修改规格
  180. $importSku = new ImportSku();
  181. $sql_data = $importSku->where(['shop_id' => $row['shop_id'], 'sku_id' => $row['sku_id']])->find();
  182. $spec_id=0;
  183. if(!empty($sql_data)){
  184. $this->tabValue=$sql_data['variety_id'];
  185. $spec_id=$sql_data['spec_id'];
  186. }
  187. $this->assign('tabValue',$this->tabValue.'');
  188. $this->assign('spec_id',$spec_id);
  189. $this->assign('rows', $rows->type_spec ?? '');
  190. $this->assign('row', $row);
  191. return $this->fetch();
  192. }
  193. $params = $this->request->post("");
  194. if (empty($params['all_data'])) $this->error(__('请选择规格'));
  195. $count = 0;
  196. Db::startTrans();
  197. try {
  198. $status = $row['status'];
  199. $time = time();
  200. $shopList = new ShopList();
  201. $productConfig = new ProductConfig();
  202. $shopDelivery = new ShopDelivery();
  203. $customerSpec = new CustomerSpec();
  204. $shopDelivery_list = [];
  205. $import_list = [];
  206. $shopDelivery_updata_list=[];
  207. $todayStart = date('Y-m-d', $time);
  208. $todayStart=$todayStart.' 00:00:00';
  209. switch ($status) {
  210. case 2:
  211. //插入规格
  212. ImportSku::insertSpecs($row['shop_id'], $row['sku_id'], (int)$params['all_data'][0]['type_id'], (int)$params['all_data'][0]['id']);
  213. //查看有多少规格
  214. $list = $this->model::where('shop_id', $row['shop_id'])->where('sku_id', $row['sku_id'])->where('status', 2)->select();
  215. foreach ($list as $item) {
  216. //插入发货数据
  217. $res = JuShuiTanService::setAdditionalPrice(
  218. $importSku,
  219. $shopList,
  220. $productConfig,
  221. $shopDelivery,
  222. $customerSpec,
  223. $item['shop_id'],
  224. $item['sku_id'],
  225. $params['all_data'][0]['type_id'],
  226. $params['all_data'][0]['id'],
  227. $params['all_data'][0]['name'],
  228. $item
  229. );
  230. $getPackSpecsFee = $res['getPackSpecsFee'];
  231. $shopDelivery_list[] = $res['shopDelivery'];
  232. $import_list_item = [
  233. 'id' => $item['id'],
  234. 'pack_specs_id' => $getPackSpecsFee['data']['id'],
  235. 'labor_cost_money' => $getPackSpecsFee['data']['labor_cost_money'],
  236. 'one_surcharge_money' => $getPackSpecsFee['one_surcharge_money'],
  237. 'two_surcharge_money' => $getPackSpecsFee['two_surcharge_money'],
  238. 'status' => 3
  239. ];
  240. $import_list[] = $import_list_item;
  241. $count++;
  242. }
  243. $this->model->saveAll($import_list);
  244. $shopDelivery->saveAll($shopDelivery_list);
  245. break;
  246. case 3:
  247. //修改规格
  248. $importSku = new ImportSku();
  249. $sql_data = $importSku->where(['shop_id' => $row['shop_id'], 'sku_id' => $row['sku_id']])->find();
  250. $result = $sql_data->save([
  251. 'variety_id' => (int)$params['all_data'][0]['type_id'],
  252. 'spec_id' => (int)$params['all_data'][0]['id']
  253. ]);
  254. if (!$result) $this->error(__('没有数据被更新'));
  255. //查看有多少规格
  256. $where=[];
  257. $where[] = ['shop_id', '=', $row['shop_id']];
  258. $where[] = ['sku_id', '=', $row['sku_id']];
  259. $where[] = ['consign_time','>=',$todayStart];
  260. $whereOr=[];
  261. $whereOr[] = ['shop_id', '=', $row['shop_id']];
  262. $whereOr[] = ['sku_id', '=', $row['sku_id']];
  263. $whereOr[] = ['status','=',2];
  264. $list = $this->model::where($where)->whereOr($whereOr)->select();
  265. foreach ($list as $item) {
  266. //插入发货数据
  267. $res = JuShuiTanService::setAdditionalPrice(
  268. $importSku,
  269. $shopList,
  270. $productConfig,
  271. $shopDelivery,
  272. $customerSpec,
  273. $item['shop_id'],
  274. $item['sku_id'],
  275. $params['all_data'][0]['type_id'],
  276. $params['all_data'][0]['id'],
  277. $params['all_data'][0]['name'],
  278. $item
  279. );
  280. $getPackSpecsFee = $res['getPackSpecsFee'];
  281. if($item['status']==2){
  282. $shopDelivery_list[] = $res['shopDelivery'];
  283. }else if($item['status']==3){
  284. $waybill_no=$res['shopDelivery']['waybill_no'];
  285. $shopDelivery_id=JuShuiTanService::get_shopDelivery_id($waybill_no);
  286. if($shopDelivery_id>0){
  287. $res['shopDelivery']['id']=$shopDelivery_id;
  288. $res['shopDelivery']['updatetime']=$time;
  289. $shopDelivery_updata_list[]=$res['shopDelivery'];
  290. }
  291. }
  292. $import_list_item = [
  293. 'id' => $item['id'],
  294. 'pack_specs_id' => $getPackSpecsFee['data']['id'],
  295. 'labor_cost_money' => $getPackSpecsFee['data']['labor_cost_money'],
  296. 'one_surcharge_money' => $getPackSpecsFee['one_surcharge_money'],
  297. 'two_surcharge_money' => $getPackSpecsFee['two_surcharge_money'],
  298. 'status' => 3
  299. ];
  300. $import_list[] = $import_list_item;
  301. $count++;
  302. }
  303. $this->model->saveAll($import_list);
  304. $shopDelivery->saveAll($shopDelivery_list);
  305. $shopDelivery->saveAll($shopDelivery_updata_list);
  306. break;
  307. default:
  308. # code...
  309. break;
  310. }
  311. Db::commit();
  312. } catch (\Exception $e) {
  313. Db::rollback();
  314. $this->error($e->getMessage());
  315. }
  316. if ($count) {
  317. return resp_json(200, '操作成功');
  318. }
  319. $this->error(__('没有数据被更新'));
  320. }
  321. //删除
  322. #[Route("GET,POST", "del")]
  323. public function del()
  324. {
  325. //通过定义callback回调函数来执行删除后的操作
  326. $this->callback = function ($ids) {};
  327. return $this->_del();
  328. }
  329. }