ImportList.php 13 KB

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