ImportList.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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. use think\facade\Cache;
  19. #[Group("goods/import_list")]
  20. class ImportList extends Backend
  21. {
  22. //app\admin\controller\goods\ImportList
  23. use Actions {
  24. index as private _index;
  25. del as private _del;
  26. }
  27. private $typeList = null;
  28. private $tabValue = null;
  29. protected function _initialize()
  30. {
  31. parent::_initialize();
  32. $this->model = new ImportListModel();
  33. $this->typeList = StockConfig::where('type_id', 'variety_name')->order('sort desc')->column('title', 'id');
  34. $this->tabValue = array_keys($this->typeList)[0] ?? null;
  35. // $this->assign('shopList', ShopList::where('status', 1)->column('name', 'id'));
  36. $this->assign('typeList', $this->typeList);
  37. $this->assign('packingList', StockConfig::where('type_id', 'packing_box')->column('title', 'id'));
  38. }
  39. // 获取店铺
  40. #[Route('GET,POST,JSON', 'getShopList')]
  41. public function getShopList()
  42. {
  43. $shopList = new ShopList();
  44. $sql_restul = $shopList->alias('s')
  45. ->join("yun_customer c", "s.customer_id = c.id", "INNER")
  46. ->where(['s.status' => 1, 'c.status' => 1])
  47. ->field(['s.id', 's.shop_id', 's.name', 'c.name' => 'nickname'])
  48. ->select();
  49. $arr = [];
  50. foreach ($sql_restul as $item) {
  51. $arr[$item['id']] = $item['name'] . '(' . '店铺id:' . $item['shop_id'] . ',客户:' . $item['nickname'] . ')';
  52. }
  53. return $this->jsonSuccess('', $arr);
  54. }
  55. /**
  56. * 查看
  57. */
  58. #[Route('GET,JSON', 'index')]
  59. public function index()
  60. {
  61. if (false === $this->request->isAjax()) {
  62. return $this->fetch();
  63. }
  64. if ($this->request->post('selectpage')) {
  65. return $this->selectpage();
  66. }
  67. [$where, $order, $limit, $with] = $this->buildparams();
  68. $list = $this->model
  69. ->withJoin($with, 'left')
  70. //如果没有使用operate filter过滤的情况下,推荐使用with关联,可以提高查询效率
  71. //->with($with)
  72. ->where($where)
  73. ->order($order)
  74. ->paginate($limit)
  75. ->each(function ($item, $key) {
  76. if (empty($item['type_id'])) {
  77. $item['type_id'] = 0;
  78. }
  79. $trade_from_list = [];
  80. if ($item['type_id'] == 1) {
  81. $feng_su_trade_from_list = site_config("addonsd.feng_su_trade_from_list");
  82. $trade_from_list = $feng_su_trade_from_list;
  83. } else {
  84. $ju_shui_tan_trade_from_list = site_config("addonsd.ju_shui_tan_trade_from_list");
  85. $trade_from_list = $ju_shui_tan_trade_from_list;
  86. }
  87. $trade_from = $item['trade_from'];
  88. $trade_from = isset($trade_from_list[$trade_from]) ? $trade_from_list[$trade_from] : $item['trade_from'];
  89. $item['trade_from'] = $trade_from;
  90. // $item['trade_from']=empty($item['trade_from'])?'无':$trade_from_list[$trade_from];
  91. return $item;
  92. });
  93. $shops_num = $this->model
  94. ->where('status', 1)
  95. ->sum('status');
  96. $specs_num = $this->model
  97. ->where('status', 2)
  98. ->sum('status');
  99. $result = ['total' => $list->total(), 'rows' => $list->items(), 'shops_num' => $shops_num, 'specs_num' => $specs_num];
  100. return json($result);
  101. }
  102. /**
  103. * 关联店铺
  104. */
  105. #[Route('GET,POST', 'shops')]
  106. public function shops()
  107. {
  108. if (false === $this->request->isPost()) {
  109. $ids = $this->request->get('ids/s', '');
  110. $shopList = new ShopList();
  111. $sql_restul = $shopList->alias('s')
  112. ->join("yun_customer c", "s.customer_id = c.id", "INNER")
  113. ->where(['s.status' => 1, 'c.status' => 1])
  114. ->field(['s.id', 's.shop_id', 's.name', 'c.name' => 'nickname'])
  115. ->select();
  116. $arr = [];
  117. foreach ($sql_restul as $item) {
  118. $arr[$item['id']] = $item['name'] . '(' . '店铺id:' . $item['shop_id'] . ',客户:' . $item['nickname'] . ')';
  119. }
  120. $this->assign('shopList', $arr);
  121. return $this->fetch();
  122. }
  123. $params = array_merge($this->request->post("row/a"), $this->postParams);
  124. if (empty($params)) {
  125. $this->error(__('提交的参数不能为空'));
  126. }
  127. // if (!$this->request->checkToken('__token__', ['__token__' => $this->request->post('__token__')])) {
  128. // $this->error(__('token错误,请刷新页面重试'));
  129. // }
  130. foreach ($params as &$value) {
  131. if (is_array($value)) {
  132. $value = implode(',', $value);
  133. }
  134. if ($value === '') {
  135. $value = null;
  136. }
  137. }
  138. $ids = $this->request->get('ids/s', '');
  139. if (empty($params['shop_id'])) $this->error(__('请选择店铺'));
  140. $result = false;
  141. Db::startTrans();
  142. try {
  143. //更新店铺
  144. ShopList::where('id', $params['shop_id'])->update(['shop_id' => $ids]);
  145. //更新状态
  146. $result = $this->model->where('shop_id', $ids)->update(['status' => 2]);
  147. if ($this->callback) {
  148. $callback = $this->callback;
  149. $callback($this->model);
  150. }
  151. Db::commit();
  152. } catch (\Exception $e) {
  153. Db::rollback();
  154. $this->error($e->getMessage());
  155. }
  156. if ($result === false) {
  157. $this->error(__('没有新增任何数据'));
  158. }
  159. $this->success();
  160. }
  161. /**
  162. * 关联规格
  163. */
  164. #[Route('GET,POST', 'specs')]
  165. public function specs(mixed $row = null)
  166. {
  167. $ids = $this->request->param('ids');
  168. $importSku = new ImportSku();
  169. if (!$row || is_array($row)) {
  170. $row = $this->model->find($ids);
  171. }
  172. if (!$row) {
  173. $this->error(__('没有找到记录'));
  174. }
  175. if (count($this->volidateFields) > 0) {
  176. foreach ($this->volidateFields as $field => $value) {
  177. if ($row[$field] != $value) {
  178. $this->error(__('没有操作权限'));
  179. }
  180. }
  181. }
  182. if (false === $this->request->isPost()) {
  183. $shop_id = $row['shop_id'];
  184. $shopListModel = new ShopListModel();
  185. $rows = $shopListModel::where('shop_id', $shop_id)->where('status', 1)->find();
  186. //修改规格
  187. $importSku = new ImportSku();
  188. $sql_data = $importSku->where(['shop_id' => $row['shop_id'], 'sku_id' => $row['sku_id']])->find();
  189. $spec_id = 0;
  190. if (!empty($sql_data)) {
  191. $this->tabValue = $sql_data['variety_id'];
  192. $spec_id = $sql_data['spec_id'];
  193. }
  194. $this->assign('tabValue', $this->tabValue . '');
  195. $this->assign('spec_id', $spec_id);
  196. $this->assign('rows', $rows->type_spec ?? '');
  197. $this->assign('row', $row);
  198. return $this->fetch();
  199. }
  200. $params = $this->request->post("");
  201. if (empty($params['all_data'])) $this->error(__('请选择规格'));
  202. $count = 0;
  203. Db::startTrans();
  204. try {
  205. $status = $row['status'];
  206. $time = time();
  207. $shopList = new ShopList();
  208. $productConfig = new ProductConfig();
  209. $shopDelivery = new ShopDelivery();
  210. $customerSpec = new CustomerSpec();
  211. $shopDelivery_list = [];
  212. $import_list = [];
  213. $shopDelivery_updata_list = [];
  214. $todayStart = date('Y-m-d', $time);
  215. $todayStart = $todayStart . ' 00:00:00';
  216. switch ($status) {
  217. case 2:
  218. //插入规格
  219. ImportSku::insertSpecs($row['shop_id'], $row['sku_id'], (int)$params['all_data'][0]['type_id'], (int)$params['all_data'][0]['id']);
  220. //查看有多少规格
  221. $list = $this->model::where('shop_id', $row['shop_id'])->where('sku_id', $row['sku_id'])->where('status', 2)->select();
  222. foreach ($list as $item) {
  223. //插入发货数据
  224. $res = JuShuiTanService::setAdditionalPrice(
  225. $importSku,
  226. $shopList,
  227. $productConfig,
  228. $shopDelivery,
  229. $customerSpec,
  230. $item['shop_id'],
  231. $item['sku_id'],
  232. $params['all_data'][0]['type_id'],
  233. $params['all_data'][0]['id'],
  234. $params['all_data'][0]['name'],
  235. $item
  236. );
  237. $getPackSpecsFee = $res['getPackSpecsFee'];
  238. $shopDelivery_list[] = $res['shopDelivery'];
  239. $import_list_item = [
  240. 'id' => $item['id'],
  241. 'specs_name' => $getPackSpecsFee['data']['title'],
  242. 'pack_specs_id' => $getPackSpecsFee['data']['id'],
  243. 'labor_cost_money' => $getPackSpecsFee['data']['labor_cost_money'],
  244. 'one_surcharge_money' => $getPackSpecsFee['one_surcharge_money'],
  245. 'two_surcharge_money' => $getPackSpecsFee['two_surcharge_money'],
  246. 'status' => 3
  247. ];
  248. $import_list[] = $import_list_item;
  249. $count++;
  250. }
  251. $this->model->saveAll($import_list);
  252. $shopDelivery->saveAll($shopDelivery_list);
  253. break;
  254. case 3:
  255. //修改规格
  256. $importSku = new ImportSku();
  257. $sql_data = $importSku->where(['shop_id' => $row['shop_id'], 'sku_id' => $row['sku_id']])->find();
  258. $result = $sql_data->save([
  259. 'variety_id' => (int)$params['all_data'][0]['type_id'],
  260. 'spec_id' => (int)$params['all_data'][0]['id']
  261. ]);
  262. if (!$result) $this->error(__('没有数据被更新'));
  263. //查看有多少规格
  264. $where = [];
  265. $where[] = ['shop_id', '=', $row['shop_id']];
  266. $where[] = ['sku_id', '=', $row['sku_id']];
  267. $where[] = ['consign_time', '>=', $todayStart];
  268. $whereOr = [];
  269. $whereOr[] = ['shop_id', '=', $row['shop_id']];
  270. $whereOr[] = ['sku_id', '=', $row['sku_id']];
  271. $whereOr[] = ['status', '=', 2];
  272. $list = $this->model::where($where)->whereOr($whereOr)->select();
  273. foreach ($list as $item) {
  274. //插入发货数据
  275. $res = JuShuiTanService::setAdditionalPrice(
  276. $importSku,
  277. $shopList,
  278. $productConfig,
  279. $shopDelivery,
  280. $customerSpec,
  281. $item['shop_id'],
  282. $item['sku_id'],
  283. $params['all_data'][0]['type_id'],
  284. $params['all_data'][0]['id'],
  285. $params['all_data'][0]['name'],
  286. $item
  287. );
  288. $getPackSpecsFee = $res['getPackSpecsFee'];
  289. if ($item['status'] == 2) {
  290. $shopDelivery_list[] = $res['shopDelivery'];
  291. } else if ($item['status'] == 3) {
  292. $waybill_no = $res['shopDelivery']['waybill_no'];
  293. $shopDelivery_id = JuShuiTanService::get_shopDelivery_id($waybill_no);
  294. if ($shopDelivery_id > 0) {
  295. $res['shopDelivery']['id'] = $shopDelivery_id;
  296. $res['shopDelivery']['updatetime'] = $time;
  297. $shopDelivery_updata_list[] = $res['shopDelivery'];
  298. }
  299. }
  300. $import_list_item = [
  301. 'id' => $item['id'],
  302. 'specs_name' => $getPackSpecsFee['data']['title'],
  303. 'pack_specs_id' => $getPackSpecsFee['data']['id'],
  304. 'labor_cost_money' => $getPackSpecsFee['data']['labor_cost_money'],
  305. 'one_surcharge_money' => $getPackSpecsFee['one_surcharge_money'],
  306. 'two_surcharge_money' => $getPackSpecsFee['two_surcharge_money'],
  307. 'status' => 3
  308. ];
  309. $import_list[] = $import_list_item;
  310. $count++;
  311. }
  312. $this->model->saveAll($import_list);
  313. $shopDelivery->saveAll($shopDelivery_list);
  314. $shopDelivery->saveAll($shopDelivery_updata_list);
  315. break;
  316. default:
  317. # code...
  318. break;
  319. }
  320. Db::commit();
  321. } catch (\Exception $e) {
  322. Db::rollback();
  323. $this->error($e->getMessage());
  324. }
  325. if ($count) {
  326. return resp_json(200, '操作成功');
  327. }
  328. $this->error(__('没有数据被更新'));
  329. }
  330. //删除
  331. #[Route("GET,POST", "del")]
  332. public function del()
  333. {
  334. //通过定义callback回调函数来执行删除后的操作
  335. $this->callback = function ($ids) {};
  336. return $this->_del();
  337. }
  338. /**
  339. * 聚水潭同步数据设置
  340. */
  341. #[Route('GET,POST,JSON', 'set_jushuitan')]
  342. public function set_jushuitan()
  343. {
  344. if (false === $this->request->isPost()) {
  345. $row=[];
  346. $startTime=null;
  347. $jushuitan=Cache::get('jushuitan');
  348. if(!empty($jushuitan)||$jushuitan!=null){
  349. $startTime=$jushuitan['startTime'];
  350. $startTime=date('Y-m-d H:i:s', $startTime);
  351. }
  352. $this->assign('row', $row);
  353. $this->assign('startTime', $startTime);
  354. return $this->fetch();
  355. }
  356. $params = $this->request->post("");
  357. if (empty($params['startTime'])) $this->error(__('请选择开始时间'));
  358. //时间间隔
  359. $start_end_space_time=10*60;
  360. //下一次时间间隔
  361. $next_time_space=7*60;
  362. //将查询分为,"2026-01-05 00:00:00~2026-01-05 00:05:00",查询该区间的订单,但是该时间区间单独数量可能超过50条,为了性能又不能一次性查询太多,从而进行分页查询,
  363. //查询不到数据,就往下一个时间区间查询
  364. $startTime_stamp = strtotime($params['startTime']);
  365. $endTime_stamp=$startTime_stamp+$start_end_space_time;
  366. $pageNum=1;
  367. $jushuitan=[
  368. 'startTime'=>$startTime_stamp,
  369. 'endTime'=>$endTime_stamp,
  370. 'pageNum'=>$pageNum,
  371. 'next_time_space'=>$next_time_space
  372. ];
  373. $jushuitan=[
  374. 'startTime'=>$startTime_stamp,
  375. 'endTime'=>$endTime_stamp,
  376. 'pageNum'=>$pageNum,
  377. 'next_time_space'=>$next_time_space
  378. ];
  379. $result=Cache::set('jushuitan',$jushuitan);
  380. if($result) return $this->jsonSuccess('设置成功');
  381. return $this->jsonError('设置失败');
  382. }
  383. /**
  384. * 风速同步数据设置
  385. */
  386. #[Route('GET,POST,JSON', 'set_fengsu')]
  387. public function set_fengsu()
  388. {
  389. if (false === $this->request->isPost()) {
  390. $row=[];
  391. $startTime=null;
  392. $fengsu=Cache::get('fengsu');
  393. if(!empty($fengsu)||$fengsu!=null){
  394. $startTime=$fengsu['startTime'];
  395. $startTime=date('Y-m-d H:i:s', $startTime);
  396. }
  397. $this->assign('row', $row);
  398. $this->assign('startTime', $startTime);
  399. return $this->fetch();
  400. }
  401. $params = $this->request->post("");
  402. if (empty($params['startTime'])) $this->error(__('请选择开始时间'));
  403. //时间间隔
  404. $start_end_space_time=10*60;
  405. //下一次时间间隔
  406. $next_time_space=7*60;
  407. //将查询分为,"2026-01-05 00:00:00~2026-01-05 00:05:00",查询该区间的订单,但是该时间区间单独数量可能超过50条,为了性能又不能一次性查询太多,从而进行分页查询,
  408. //查询不到数据,就往下一个时间区间查询
  409. $startTime_stamp = strtotime($params['startTime']);
  410. $endTime_stamp=$startTime_stamp+$start_end_space_time;
  411. $pageNum=1;
  412. $fengsu=[
  413. 'startTime'=>$startTime_stamp,
  414. 'endTime'=>$endTime_stamp,
  415. 'pageNum'=>$pageNum,
  416. 'next_time_space'=>$next_time_space
  417. ];
  418. $fengsu=[
  419. 'startTime'=>$startTime_stamp,
  420. 'endTime'=>$endTime_stamp,
  421. 'pageNum'=>$pageNum,
  422. 'next_time_space'=>$next_time_space
  423. ];
  424. $result=Cache::set('fengsu',$fengsu);
  425. if($result) return $this->jsonSuccess('设置成功');
  426. return $this->jsonError('设置失败');
  427. }
  428. }