ImportList.php 21 KB

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