ImportList.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. if (!$row || is_array($row)) {
  158. $row = $this->model->find($ids);
  159. }
  160. if (!$row) {
  161. $this->error(__('没有找到记录'));
  162. }
  163. if (count($this->volidateFields) > 0) {
  164. foreach ($this->volidateFields as $field => $value) {
  165. if ($row[$field] != $value) {
  166. $this->error(__('没有操作权限'));
  167. }
  168. }
  169. }
  170. if (false === $this->request->isPost()) {
  171. $shop_id = $row['shop_id'];
  172. $shopListModel = new ShopListModel();
  173. $rows = $shopListModel::where('shop_id', $shop_id)->where('status', 1)->find();
  174. $this->assign('rows', $rows->type_spec ?? '');
  175. $this->assign('row', $row);
  176. return $this->fetch();
  177. }
  178. $params = $this->request->post("");
  179. if (empty($params['all_data'])) $this->error(__('请选择规格'));
  180. $count = 0;
  181. Db::startTrans();
  182. try {
  183. //插入规格
  184. ImportSku::insertSpecs($row['shop_id'], $row['sku_id'], (int)$params['all_data'][0]['type_id'], (int)$params['all_data'][0]['id']);
  185. //查看有多少规格
  186. $list = $this->model::where('shop_id', $row['shop_id'])->where('sku_id', $row['sku_id'])->where('status', 2)->select();
  187. $shopList = new ShopList();
  188. $productConfig = new ProductConfig();
  189. $shopDelivery = new ShopDelivery();
  190. $customerSpec = new CustomerSpec();
  191. $importSku = new ImportSku();
  192. $shopDelivery_list=[];
  193. $import_list=[];
  194. foreach ($list as $item) {
  195. //插入发货数据
  196. $res=JuShuiTanService::setAdditionalPrice(
  197. $importSku,
  198. $shopList,
  199. $productConfig,
  200. $shopDelivery,
  201. $customerSpec,
  202. $item['shop_id'],
  203. $item['sku_id'],
  204. $params['all_data'][0]['type_id'],
  205. $params['all_data'][0]['id'],
  206. $params['all_data'][0]['name'],
  207. $item
  208. );
  209. $getPackSpecsFee=$res['getPackSpecsFee'];
  210. $shopDelivery_list[]=$res['shopDelivery'];
  211. $import_list_item=[
  212. 'id'=>$item['id'],
  213. 'pack_specs_id'=>$getPackSpecsFee['data']['id'],
  214. 'labor_cost_money'=>$getPackSpecsFee['data']['labor_cost_money'],
  215. 'one_surcharge_money'=>$getPackSpecsFee['one_surcharge_money'],
  216. 'two_surcharge_money'=>$getPackSpecsFee['two_surcharge_money'],
  217. 'status' => 3
  218. ];
  219. $import_list[]=$import_list_item;
  220. $count++;
  221. // $count += $item->save(['pack_specs_id'=>$getPackSpecsFee['data']['id'],'labor_cost_money'=>$getPackSpecsFee['data']['labor_cost_money'],'one_surcharge_money'=>$getPackSpecsFee['one_surcharge_money'],'two_surcharge_money'=>$getPackSpecsFee['two_surcharge_money'],'status' => 3]);
  222. }
  223. $this->model->saveAll($import_list);
  224. $shopDelivery->saveAll($shopDelivery_list);
  225. if ($this->callback) {
  226. $callback = $this->callback;
  227. $callback($ids);
  228. }
  229. Db::commit();
  230. } catch (\Exception $e) {
  231. Db::rollback();
  232. $this->error($e->getMessage());
  233. }
  234. if ($count) {
  235. return resp_json(200, '操作成功');
  236. }
  237. $this->error(__('没有数据被更新'));
  238. }
  239. //删除
  240. #[Route("GET,POST", "del")]
  241. public function del()
  242. {
  243. //通过定义callback回调函数来执行删除后的操作
  244. $this->callback = function ($ids) {};
  245. return $this->_del();
  246. }
  247. }