ImportList.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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\FengsuSku;
  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. * 查看
  36. */
  37. #[Route('GET,JSON', 'index')]
  38. public function index()
  39. {
  40. if (false === $this->request->isAjax()) {
  41. return $this->fetch();
  42. }
  43. if ($this->request->post('selectpage')) {
  44. return $this->selectpage();
  45. }
  46. [$where, $order, $limit, $with] = $this->buildparams();
  47. $list = $this->model
  48. ->withJoin($with, 'left')
  49. //如果没有使用operate filter过滤的情况下,推荐使用with关联,可以提高查询效率
  50. //->with($with)
  51. ->where($where)
  52. ->order($order)
  53. ->paginate($limit)
  54. ->each(function ($item, $key) {
  55. if(empty($item['type_id'])){
  56. $item['type_id']=0;
  57. }
  58. $trade_from_list=site_config("addonsd.ju_shui_tan_trade_from_list");
  59. $trade_from=$item['trade_from'];
  60. $item['trade_from']=empty($item['trade_from'])?'无':$trade_from_list[$trade_from];
  61. return $item;
  62. });
  63. $result = ['total' => $list->total(), 'rows' => $list->items()];
  64. return json($result);
  65. }
  66. /**
  67. * 关联店铺
  68. */
  69. #[Route('GET,POST', 'shops')]
  70. public function shops()
  71. {
  72. if (false === $this->request->isPost()) {
  73. $ids = $this->request->get('ids/s', '');
  74. return $this->fetch();
  75. }
  76. $params = array_merge($this->request->post("row/a"), $this->postParams);
  77. if (empty($params)) {
  78. $this->error(__('提交的参数不能为空'));
  79. }
  80. if (!$this->request->checkToken('__token__', ['__token__' => $this->request->post('__token__')])) {
  81. $this->error(__('token错误,请刷新页面重试'));
  82. }
  83. foreach ($params as &$value) {
  84. if (is_array($value)) {
  85. $value = implode(',', $value);
  86. }
  87. if ($value === '') {
  88. $value = null;
  89. }
  90. }
  91. $ids = $this->request->get('ids/s', '');
  92. if (empty($params['shop_id'])) $this->error(__('请选择店铺'));
  93. $result = false;
  94. Db::startTrans();
  95. try {
  96. //更新店铺
  97. ShopList::where('id', $params['shop_id'])->update(['shop_id' => $ids]);
  98. //更新状态
  99. $result = $this->model->where('shop_id', $ids)->update(['status' => 2]);
  100. if ($this->callback) {
  101. $callback = $this->callback;
  102. $callback($this->model);
  103. }
  104. Db::commit();
  105. } catch (\Exception $e) {
  106. Db::rollback();
  107. $this->error($e->getMessage());
  108. }
  109. if ($result === false) {
  110. $this->error(__('没有新增任何数据'));
  111. }
  112. $this->success();
  113. }
  114. /**
  115. * 关联规格
  116. */
  117. #[Route('GET,POST', 'specs')]
  118. public function specs(mixed $row = null)
  119. {
  120. $ids = $this->request->param('ids');
  121. if (!$row || is_array($row)) {
  122. $row = $this->model->find($ids);
  123. }
  124. if (!$row) {
  125. $this->error(__('没有找到记录'));
  126. }
  127. if (count($this->volidateFields) > 0) {
  128. foreach ($this->volidateFields as $field => $value) {
  129. if ($row[$field] != $value) {
  130. $this->error(__('没有操作权限'));
  131. }
  132. }
  133. }
  134. if (false === $this->request->isPost()) {
  135. $shop_id=$row['shop_id'];
  136. $shopListModel=new ShopListModel();
  137. $rows = $shopListModel::where('shop_id', $shop_id)->where('status', 1)->find();
  138. $this->assign('rows', $rows->type_spec??'');
  139. $this->assign('row', $row);
  140. return $this->fetch();
  141. }
  142. $params = $this->request->post("");
  143. if (empty($params['all_data'])) $this->error(__('请选择规格'));
  144. $count = 0;
  145. Db::startTrans();
  146. try {
  147. //插入规格
  148. FengsuSku::insertSpecs($row['shop_id'], $row['sku_id'], (int)$params['all_data'][0]['type_id'], (int)$params['all_data'][0]['id']);
  149. //查看有多少规格
  150. $list = $this->model::where('shop_id', $row['shop_id'])->where('sku_id', $row['sku_id'])->where('status', 2)->select();
  151. $shopList = new ShopList();
  152. $productConfig = new ProductConfig();
  153. $shopDelivery = new ShopDelivery();
  154. $customerSpec = new CustomerSpec();
  155. $fengsuSku = new FengsuSku();
  156. foreach ($list as $item) {
  157. //插入发货数据
  158. JuShuiTanService::setAdditionalPrice(
  159. $fengsuSku,
  160. $shopList,
  161. $productConfig,
  162. $shopDelivery,
  163. $customerSpec,
  164. $row['shop_id'],
  165. $row['sku_id'],
  166. $params['all_data'][0]['type_id'],
  167. $params['all_data'][0]['id'],
  168. $params['all_data'][0]['name'],
  169. $row
  170. );
  171. $count += $item->save(['status' => 3]);
  172. }
  173. if ($this->callback) {
  174. $callback = $this->callback;
  175. $callback($ids);
  176. }
  177. Db::commit();
  178. } catch (\Exception $e) {
  179. Db::rollback();
  180. $this->error($e->getMessage());
  181. }
  182. if ($count) {
  183. return resp_json(200, '操作成功');
  184. }
  185. $this->error(__('没有数据被更新'));
  186. }
  187. //删除
  188. #[Route("GET,POST", "del")]
  189. public function del()
  190. {
  191. //通过定义callback回调函数来执行删除后的操作
  192. $this->callback = function ($ids) {};
  193. return $this->_del();
  194. }
  195. }