ImportList.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. $shops_num = $this->model
  93. ->where('status', 1)
  94. ->sum('status');
  95. $specs_num = $this->model
  96. ->where('status', 2)
  97. ->sum('status');
  98. $result = ['total' => $list->total(), 'rows' => $list->items(), 'shops_num' => $shops_num, 'specs_num' => $specs_num];
  99. return json($result);
  100. }
  101. /**
  102. * 关联店铺
  103. */
  104. #[Route('GET,POST', 'shops')]
  105. public function shops()
  106. {
  107. if (false === $this->request->isPost()) {
  108. $ids = $this->request->get('ids/s', '');
  109. $shopList = new ShopList();
  110. $sql_restul = $shopList->alias('s')
  111. ->join("yun_customer c", "s.customer_id = c.id", "INNER")
  112. ->where(['s.status' => 1, 'c.status' => 1])
  113. ->field(['s.id', 's.shop_id', 's.name', 'c.name' => 'nickname'])
  114. ->select();
  115. $arr = [];
  116. foreach ($sql_restul as $item) {
  117. $arr[$item['id']] = $item['name'] . '(' . '店铺id:' . $item['shop_id'] . ',客户:' . $item['nickname'] . ')';
  118. }
  119. $this->assign('shopList', $arr);
  120. return $this->fetch();
  121. }
  122. $params = array_merge($this->request->post("row/a"), $this->postParams);
  123. if (empty($params)) {
  124. $this->error(__('提交的参数不能为空'));
  125. }
  126. // if (!$this->request->checkToken('__token__', ['__token__' => $this->request->post('__token__')])) {
  127. // $this->error(__('token错误,请刷新页面重试'));
  128. // }
  129. foreach ($params as &$value) {
  130. if (is_array($value)) {
  131. $value = implode(',', $value);
  132. }
  133. if ($value === '') {
  134. $value = null;
  135. }
  136. }
  137. $ids = $this->request->get('ids/s', '');
  138. if (empty($params['shop_id'])) $this->error(__('请选择店铺'));
  139. $result = false;
  140. Db::startTrans();
  141. try {
  142. //更新店铺
  143. ShopList::where('id', $params['shop_id'])->update(['shop_id' => $ids]);
  144. //更新状态
  145. $result = $this->model->where('shop_id', $ids)->update(['status' => 2]);
  146. if ($this->callback) {
  147. $callback = $this->callback;
  148. $callback($this->model);
  149. }
  150. Db::commit();
  151. } catch (\Exception $e) {
  152. Db::rollback();
  153. $this->error($e->getMessage());
  154. }
  155. if ($result === false) {
  156. $this->error(__('没有新增任何数据'));
  157. }
  158. $this->success();
  159. }
  160. /**
  161. * 关联规格
  162. */
  163. #[Route('GET,POST', 'specs')]
  164. public function specs(mixed $row = null)
  165. {
  166. $ids = $this->request->param('ids');
  167. $importSku = new ImportSku();
  168. if (!$row || is_array($row)) {
  169. $row = $this->model->find($ids);
  170. }
  171. if (!$row) {
  172. $this->error(__('没有找到记录'));
  173. }
  174. if (count($this->volidateFields) > 0) {
  175. foreach ($this->volidateFields as $field => $value) {
  176. if ($row[$field] != $value) {
  177. $this->error(__('没有操作权限'));
  178. }
  179. }
  180. }
  181. if (false === $this->request->isPost()) {
  182. $shop_id = $row['shop_id'];
  183. $shopListModel = new ShopListModel();
  184. $rows = $shopListModel::where('shop_id', $shop_id)->where('status', 1)->find();
  185. //修改规格
  186. $importSku = new ImportSku();
  187. $sql_data = $importSku->where(['shop_id' => $row['shop_id'], 'sku_id' => $row['sku_id']])->find();
  188. $spec_id = 0;
  189. if (!empty($sql_data)) {
  190. $this->tabValue = $sql_data['variety_id'];
  191. $spec_id = $sql_data['spec_id'];
  192. }
  193. $this->assign('tabValue', $this->tabValue . '');
  194. $this->assign('spec_id', $spec_id);
  195. $this->assign('rows', $rows->type_spec ?? '');
  196. $this->assign('row', $row);
  197. return $this->fetch();
  198. }
  199. $params = $this->request->post("");
  200. if (empty($params['all_data'])) $this->error(__('请选择规格'));
  201. $count = 0;
  202. Db::startTrans();
  203. try {
  204. $status = $row['status'];
  205. $time = time();
  206. $shopList = new ShopList();
  207. $productConfig = new ProductConfig();
  208. $shopDelivery = new ShopDelivery();
  209. $customerSpec = new CustomerSpec();
  210. $shopDelivery_list = [];
  211. $import_list = [];
  212. $shopDelivery_updata_list = [];
  213. $todayStart = date('Y-m-d', $time);
  214. $todayStart = $todayStart . ' 00:00:00';
  215. switch ($status) {
  216. case 2:
  217. //插入规格
  218. ImportSku::insertSpecs($row['shop_id'], $row['sku_id'], (int)$params['all_data'][0]['type_id'], (int)$params['all_data'][0]['id']);
  219. //查看有多少规格
  220. $list = $this->model::where('shop_id', $row['shop_id'])->where('sku_id', $row['sku_id'])->where('status', 2)->select();
  221. foreach ($list as $item) {
  222. //插入发货数据
  223. $res = JuShuiTanService::setAdditionalPrice(
  224. $importSku,
  225. $shopList,
  226. $productConfig,
  227. $shopDelivery,
  228. $customerSpec,
  229. $item['shop_id'],
  230. $item['sku_id'],
  231. $params['all_data'][0]['type_id'],
  232. $params['all_data'][0]['id'],
  233. $params['all_data'][0]['name'],
  234. $item
  235. );
  236. $getPackSpecsFee = $res['getPackSpecsFee'];
  237. $shopDelivery_list[] = $res['shopDelivery'];
  238. $import_list_item = [
  239. 'id' => $item['id'],
  240. 'pack_specs_id' => $getPackSpecsFee['data']['id'],
  241. 'labor_cost_money' => $getPackSpecsFee['data']['labor_cost_money'],
  242. 'one_surcharge_money' => $getPackSpecsFee['one_surcharge_money'],
  243. 'two_surcharge_money' => $getPackSpecsFee['two_surcharge_money'],
  244. 'status' => 3
  245. ];
  246. $import_list[] = $import_list_item;
  247. $count++;
  248. }
  249. $this->model->saveAll($import_list);
  250. $shopDelivery->saveAll($shopDelivery_list);
  251. break;
  252. case 3:
  253. //修改规格
  254. $importSku = new ImportSku();
  255. $sql_data = $importSku->where(['shop_id' => $row['shop_id'], 'sku_id' => $row['sku_id']])->find();
  256. $result = $sql_data->save([
  257. 'variety_id' => (int)$params['all_data'][0]['type_id'],
  258. 'spec_id' => (int)$params['all_data'][0]['id']
  259. ]);
  260. if (!$result) $this->error(__('没有数据被更新'));
  261. //查看有多少规格
  262. $where = [];
  263. $where[] = ['shop_id', '=', $row['shop_id']];
  264. $where[] = ['sku_id', '=', $row['sku_id']];
  265. $where[] = ['consign_time', '>=', $todayStart];
  266. $whereOr = [];
  267. $whereOr[] = ['shop_id', '=', $row['shop_id']];
  268. $whereOr[] = ['sku_id', '=', $row['sku_id']];
  269. $whereOr[] = ['status', '=', 2];
  270. $list = $this->model::where($where)->whereOr($whereOr)->select();
  271. foreach ($list as $item) {
  272. //插入发货数据
  273. $res = JuShuiTanService::setAdditionalPrice(
  274. $importSku,
  275. $shopList,
  276. $productConfig,
  277. $shopDelivery,
  278. $customerSpec,
  279. $item['shop_id'],
  280. $item['sku_id'],
  281. $params['all_data'][0]['type_id'],
  282. $params['all_data'][0]['id'],
  283. $params['all_data'][0]['name'],
  284. $item
  285. );
  286. $getPackSpecsFee = $res['getPackSpecsFee'];
  287. if ($item['status'] == 2) {
  288. $shopDelivery_list[] = $res['shopDelivery'];
  289. } else if ($item['status'] == 3) {
  290. $waybill_no = $res['shopDelivery']['waybill_no'];
  291. $shopDelivery_id = JuShuiTanService::get_shopDelivery_id($waybill_no);
  292. if ($shopDelivery_id > 0) {
  293. $res['shopDelivery']['id'] = $shopDelivery_id;
  294. $res['shopDelivery']['updatetime'] = $time;
  295. $shopDelivery_updata_list[] = $res['shopDelivery'];
  296. }
  297. }
  298. $import_list_item = [
  299. 'id' => $item['id'],
  300. 'pack_specs_id' => $getPackSpecsFee['data']['id'],
  301. 'labor_cost_money' => $getPackSpecsFee['data']['labor_cost_money'],
  302. 'one_surcharge_money' => $getPackSpecsFee['one_surcharge_money'],
  303. 'two_surcharge_money' => $getPackSpecsFee['two_surcharge_money'],
  304. 'status' => 3
  305. ];
  306. $import_list[] = $import_list_item;
  307. $count++;
  308. }
  309. $this->model->saveAll($import_list);
  310. $shopDelivery->saveAll($shopDelivery_list);
  311. $shopDelivery->saveAll($shopDelivery_updata_list);
  312. break;
  313. default:
  314. # code...
  315. break;
  316. }
  317. Db::commit();
  318. } catch (\Exception $e) {
  319. Db::rollback();
  320. $this->error($e->getMessage());
  321. }
  322. if ($count) {
  323. return resp_json(200, '操作成功');
  324. }
  325. $this->error(__('没有数据被更新'));
  326. }
  327. //删除
  328. #[Route("GET,POST", "del")]
  329. public function del()
  330. {
  331. //通过定义callback回调函数来执行删除后的操作
  332. $this->callback = function ($ids) {};
  333. return $this->_del();
  334. }
  335. }