ImportList.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. * 查看
  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=[];
  59. if($item['type_id']==1){
  60. $feng_su_trade_from_list=site_config("addonsd.feng_su_trade_from_list");
  61. $trade_from_list=$feng_su_trade_from_list;
  62. }else{
  63. $ju_shui_tan_trade_from_list=site_config("addonsd.ju_shui_tan_trade_from_list");
  64. $trade_from_list=$ju_shui_tan_trade_from_list;
  65. }
  66. $trade_from=$item['trade_from'];
  67. $trade_from=isset($trade_from_list[$trade_from])?$trade_from_list[$trade_from]:$item['trade_from'];
  68. $item['trade_from']=$trade_from;
  69. // $item['trade_from']=empty($item['trade_from'])?'无':$trade_from_list[$trade_from];
  70. return $item;
  71. });
  72. $result = ['total' => $list->total(), 'rows' => $list->items()];
  73. return json($result);
  74. }
  75. /**
  76. * 关联店铺
  77. */
  78. #[Route('GET,POST', 'shops')]
  79. public function shops()
  80. {
  81. if (false === $this->request->isPost()) {
  82. $ids = $this->request->get('ids/s', '');
  83. $shopList=new ShopList();
  84. $sql_restul=$shopList->alias('s')
  85. ->join("yun_customer c", "s.customer_id = c.id", "INNER")
  86. ->where(['s.status' => 1, 'c.status' => 1])
  87. ->field(['s.id','s.name','c.name'=>'nickname'])
  88. ->select();
  89. $arr=[];
  90. foreach ($sql_restul as $item) {
  91. $arr[$item['id']]=$item['name'].'(客户:'.$item['nickname'].')';
  92. }
  93. $this->assign('shopList',$arr );
  94. return $this->fetch();
  95. }
  96. $params = array_merge($this->request->post("row/a"), $this->postParams);
  97. if (empty($params)) {
  98. $this->error(__('提交的参数不能为空'));
  99. }
  100. if (!$this->request->checkToken('__token__', ['__token__' => $this->request->post('__token__')])) {
  101. $this->error(__('token错误,请刷新页面重试'));
  102. }
  103. foreach ($params as &$value) {
  104. if (is_array($value)) {
  105. $value = implode(',', $value);
  106. }
  107. if ($value === '') {
  108. $value = null;
  109. }
  110. }
  111. $ids = $this->request->get('ids/s', '');
  112. if (empty($params['shop_id'])) $this->error(__('请选择店铺'));
  113. $result = false;
  114. Db::startTrans();
  115. try {
  116. //更新店铺
  117. ShopList::where('id', $params['shop_id'])->update(['shop_id' => $ids]);
  118. //更新状态
  119. $result = $this->model->where('shop_id', $ids)->update(['status' => 2]);
  120. if ($this->callback) {
  121. $callback = $this->callback;
  122. $callback($this->model);
  123. }
  124. Db::commit();
  125. } catch (\Exception $e) {
  126. Db::rollback();
  127. $this->error($e->getMessage());
  128. }
  129. if ($result === false) {
  130. $this->error(__('没有新增任何数据'));
  131. }
  132. $this->success();
  133. }
  134. /**
  135. * 关联规格
  136. */
  137. #[Route('GET,POST', 'specs')]
  138. public function specs(mixed $row = null)
  139. {
  140. $ids = $this->request->param('ids');
  141. if (!$row || is_array($row)) {
  142. $row = $this->model->find($ids);
  143. }
  144. if (!$row) {
  145. $this->error(__('没有找到记录'));
  146. }
  147. if (count($this->volidateFields) > 0) {
  148. foreach ($this->volidateFields as $field => $value) {
  149. if ($row[$field] != $value) {
  150. $this->error(__('没有操作权限'));
  151. }
  152. }
  153. }
  154. if (false === $this->request->isPost()) {
  155. $shop_id=$row['shop_id'];
  156. $shopListModel=new ShopListModel();
  157. $rows = $shopListModel::where('shop_id', $shop_id)->where('status', 1)->find();
  158. $this->assign('rows', $rows->type_spec??'');
  159. $this->assign('row', $row);
  160. return $this->fetch();
  161. }
  162. $params = $this->request->post("");
  163. if (empty($params['all_data'])) $this->error(__('请选择规格'));
  164. $count = 0;
  165. Db::startTrans();
  166. try {
  167. //插入规格
  168. ImportSku::insertSpecs($row['shop_id'], $row['sku_id'], (int)$params['all_data'][0]['type_id'], (int)$params['all_data'][0]['id']);
  169. //查看有多少规格
  170. $list = $this->model::where('shop_id', $row['shop_id'])->where('sku_id', $row['sku_id'])->where('status', 2)->select();
  171. $shopList = new ShopList();
  172. $productConfig = new ProductConfig();
  173. $shopDelivery = new ShopDelivery();
  174. $customerSpec = new CustomerSpec();
  175. $importSku = new ImportSku();
  176. foreach ($list as $item) {
  177. //插入发货数据
  178. JuShuiTanService::setAdditionalPrice(
  179. $importSku,
  180. $shopList,
  181. $productConfig,
  182. $shopDelivery,
  183. $customerSpec,
  184. $row['shop_id'],
  185. $row['sku_id'],
  186. $params['all_data'][0]['type_id'],
  187. $params['all_data'][0]['id'],
  188. $params['all_data'][0]['name'],
  189. $row
  190. );
  191. $count += $item->save(['status' => 3]);
  192. }
  193. if ($this->callback) {
  194. $callback = $this->callback;
  195. $callback($ids);
  196. }
  197. Db::commit();
  198. } catch (\Exception $e) {
  199. Db::rollback();
  200. $this->error($e->getMessage());
  201. }
  202. if ($count) {
  203. return resp_json(200, '操作成功');
  204. }
  205. $this->error(__('没有数据被更新'));
  206. }
  207. //删除
  208. #[Route("GET,POST", "del")]
  209. public function del()
  210. {
  211. //通过定义callback回调函数来执行删除后的操作
  212. $this->callback = function ($ids) {};
  213. return $this->_del();
  214. }
  215. }