ImportList.php 21 KB

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