LowPriceGoods.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. <?php
  2. namespace App\Http\Controllers\Manager\WashConfig;
  3. use App\Http\Controllers\Controller;
  4. use App\Http\Requests\Manager\WashConfig\LowPriceGoods as Request;
  5. use App\Models\Manager\WashConfig\LowPriceGoods as LowPriceGoodsModel;
  6. use App\Models\Manager\WashConfig\ViolationStore as ViolationStoreModel;
  7. use App\Models\Manager\WashConfig\LowPriceGoodsCompany as LowPriceGoodsCompanyModel;
  8. use App\Models\Manager\WashConfig\ProductCategory as ProductCategoryModel;
  9. use Illuminate\Support\Facades\DB;
  10. use App\Models\Manager\WashConfig\ViolationProduct as ViolationProductModel;
  11. use Illuminate\Support\Carbon;
  12. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  13. use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
  14. use PhpOffice\PhpSpreadsheet\Style\Alignment;
  15. use PhpOffice\PhpSpreadsheet\IOFactory;
  16. /**
  17. * 数据清洗-低价产品配置
  18. * @author 唐远望
  19. * @version 1.0
  20. * @date 2025-12-02
  21. */
  22. class LowPriceGoods extends Controller
  23. {
  24. /**
  25. * 列表
  26. * @author 唐远望
  27. * @version 1.0
  28. * @date 2025-12-02
  29. *
  30. */
  31. public function list(Request $request, LowPriceGoodsModel $LowPriceGoodsModel, LowPriceGoodsCompanyModel $LowPriceGoodsCompanyModel, ProductCategoryModel $ProductCategoryModel)
  32. {
  33. $request->scene('list')->validate();
  34. $admin_company_id = request('admin_company_id', '0');
  35. $company_id = request('access_token.company_id', '0');
  36. $is_admin = request('access_token.is_admin', '0'); //是否管理员操作 0=是1=否
  37. // 查询条件
  38. $map = [];
  39. $limit = request('limit', config('page_num', 10));
  40. $status = request('status', '');
  41. $start_time = request('start_time', '');
  42. $end_time = request('end_time', '');
  43. $product_name = request('product_name', '');
  44. $platform = request('platform', '');
  45. $store_scope = request('store_scope', '');
  46. $company_scope = request('company_scope', '');
  47. $category_id = request('category_id', '');
  48. $product_brand = request('product_brand', '');
  49. $company_name = request('company_name', '');
  50. // 时间条件
  51. if ($start_time) $map[] = ['insert_time', '>=', strtotime($start_time)];
  52. if ($end_time) $map[] = ['insert_time', '<=', strtotime($end_time)];
  53. // 其他条件
  54. if (is_numeric($status)) $map[] = ['status', '=', $status];
  55. if ($product_name) $map[] = ['product_name', 'like', "%$product_name%"];
  56. if ($store_scope) $map[] = ['store_scope', '=', $store_scope];
  57. if ($company_scope) $map[] = ['company_scope', '=', $company_scope];
  58. if ($category_id) $map[] = ['category_id', '=', $category_id];
  59. if ($product_brand) $map[] = ['product_brand', 'like', "%$product_brand%"];
  60. // 查询数据
  61. if ($is_admin != 1 && $company_id != 0) {
  62. $map[] = ['company_id', '=', $company_id];
  63. } else {
  64. $map[] = ['company_id', '=', $admin_company_id];
  65. }
  66. if ($platform != '') {
  67. $platform_ids = explode(',', $platform);
  68. $LowPriceGoodsModel = $LowPriceGoodsModel->whereIn('id', function ($query) use ($platform_ids) {
  69. $query->select('product_id')
  70. ->from('washconfig_lowprice_product_platform')
  71. ->whereIn('platform_id', $platform_ids)
  72. ->distinct('product_id');
  73. });
  74. }
  75. if ($company_name) {
  76. $LowPriceGoodsModel = $LowPriceGoodsModel->whereIn('id', function ($query) use ($company_name) {
  77. $query->select('washconfig_lowprice_product_company.lowprice_product_logid')
  78. ->from('washconfig_lowprice_product_company')
  79. ->join('washconfig_violation_store', 'washconfig_violation_store.id', '=', 'washconfig_lowprice_product_company.company_id')
  80. ->where('washconfig_violation_store.company_name', 'like', "%$company_name%")
  81. ->distinct('washconfig_lowprice_product_company.company_id');
  82. });
  83. }
  84. $result = $LowPriceGoodsModel
  85. ->where($map)
  86. ->with(['product_keyword'])
  87. ->orderByDesc('id')
  88. ->paginate($limit)->toarray();
  89. // 分配数据
  90. if (!$result) return json_send(['code' => 'success', 'msg' => '获取成功', 'data' => []]);
  91. if (isset($result['data']) && count($result['data']) > 0) {
  92. foreach ($result['data'] as $key => $value) {
  93. $result['data'][$key]['platform'] = isset($value['platform']) ? explode(',', $value['platform']) : '';
  94. $category_name = $value['category_id'] > 0 ? $ProductCategoryModel->where('id', $value['category_id'])->value('name') : '';
  95. $result['data'][$key]['category_name'] = $category_name;
  96. //查询店铺名称
  97. if ($value['store_scope'] == '1') {
  98. $result['data'][$key]['store_name'] = ['全部店铺'];
  99. } else {
  100. $result['data'][$key]['store_name'] = '';
  101. }
  102. //查询公司名称
  103. if ($value['company_scope'] == '1') {
  104. $result['data'][$key]['company_name'] = ['全部公司'];
  105. } else {
  106. $company_data = $LowPriceGoodsCompanyModel->where('lowprice_product_logid', $value['id'])
  107. ->join('washconfig_violation_store', 'washconfig_violation_store.id', '=', 'washconfig_lowprice_product_company.company_id')
  108. ->select(['washconfig_violation_store.id', 'washconfig_violation_store.company_name', 'washconfig_lowprice_product_company.company_id'])->get()->toArray();
  109. $result['data'][$key]['company_name'] = !empty($company_data) ? array_column($company_data, 'company_name') : '';
  110. }
  111. }
  112. }
  113. // 加载模板
  114. return json_send(['code' => 'success', 'msg' => '获取成功', 'data' => $result]);
  115. }
  116. /**
  117. * 详情
  118. * @author 唐远望
  119. * @version 1.0
  120. * @date 2025-12-02
  121. */
  122. public function detail(Request $request, LowPriceGoodsModel $LowPriceGoodsModel, LowPriceGoodsCompanyModel $LowPriceGoodsCompanyModel, ProductCategoryModel $ProductCategoryModel)
  123. {
  124. $request->scene('detail')->validate();
  125. $admin_company_id = request('admin_company_id', '0');
  126. $company_id = request('access_token.company_id', '0');
  127. $is_admin = request('access_token.is_admin', '0'); //是否管理员操作 0=是1=否
  128. // 接收参数
  129. $id = request('id', 0);
  130. $map = ['id' => $id];
  131. if ($is_admin != 1 && $company_id != 0) {
  132. $map['company_id'] = $company_id;
  133. } else {
  134. $map['company_id'] = $admin_company_id;
  135. }
  136. $data = $LowPriceGoodsModel->where($map)->with(['product_keyword'])->first();
  137. if (!$data) return json_send(['code' => 'error', 'msg' => '记录不存在']);
  138. //查询店铺名称
  139. if (trim($data['store_scope']) == '') {
  140. $data['store_name'] = ['全部店铺'];
  141. } else {
  142. $data['store_name'] = '';
  143. }
  144. //查询公司名称
  145. if ($data->company_scope == '1') {
  146. $data->company_name = ['全部公司'];
  147. $data->company_ids = '';
  148. } else {
  149. $company_data = $LowPriceGoodsCompanyModel->where('lowprice_product_logid', $data->id)
  150. ->join('washconfig_violation_store', 'washconfig_violation_store.id', '=', 'washconfig_lowprice_product_company.company_id')
  151. ->select(['washconfig_violation_store.id', 'washconfig_violation_store.company_name', 'washconfig_lowprice_product_company.company_id'])->get()->toArray();
  152. $data->company_name = !empty($company_data) ? array_column($company_data, 'company_name') : '';
  153. $data->company_ids = !empty($company_data) ? array_column($company_data, 'company_id') : '';
  154. }
  155. $data->platform = isset($data->platform) ? explode(',', $data->platform) : '';
  156. $data->category_name = $data->category_id > 0 ? $ProductCategoryModel->where('id', $data->category_id)->value('name') : '';
  157. // 加载模板
  158. return json_send(['code' => 'success', 'msg' => '获取成功', 'data' => $data]);
  159. }
  160. /**
  161. * 添加
  162. * @author 唐远望
  163. * @version 1.0
  164. * @date 2025-12-02
  165. *
  166. */
  167. public function add(Request $request, LowPriceGoodsModel $LowPriceGoodsModel, ViolationProductModel $ViolationProductModel)
  168. {
  169. $request->scene('add')->validate();
  170. $admin_company_id = request('admin_company_id', '0');
  171. $company_id = request('access_token.company_id', '0');
  172. $is_admin = request('access_token.is_admin', '0'); //是否管理员操作 0=是1=否
  173. //获取禁止商品启动数量
  174. $violation_product_count = $ViolationProductModel->where('status', 0)->count();
  175. //获取低价挂网商品启用数量
  176. $lowprice_product_count = $LowPriceGoodsModel->where('status', 0)->count();
  177. //计算总数量
  178. $product_totle = $violation_product_count + $lowprice_product_count;
  179. // 接收数据
  180. $all_data = request()->all();
  181. $product_brand = request('product_brand', '');
  182. $product_keyword = request('product_keyword', '');
  183. $product_specs = request('product_specs', '');
  184. $all_data['product_specs'] = $product_specs;
  185. $all_data['product_brand'] = $product_brand;
  186. $all_data['product_keyword'] = $product_keyword;
  187. $enable_full_quantity = request('enable_full_quantity', 1); //全量,0启用,1禁用
  188. $all_data['enable_full_quantity'] = $enable_full_quantity;
  189. if ($enable_full_quantity == 1 && !isset($all_data['product_specs'])) {
  190. return json_send(['code' => 'error', 'msg' => '非全量清洗时,商品规格不能为空']);
  191. }
  192. if(trim($product_keyword) !=''){
  193. $product_keyword_count = count(explode(',', $product_keyword));
  194. if ($product_keyword_count > 5) {
  195. return json_send(['code' => 'error', 'msg' => '商品关键词不能超过5个']);
  196. }
  197. }
  198. $store_scope = request('store_scope', '');
  199. $all_data['store_scope'] = $store_scope;
  200. $company_scope = request('company_scope', '');
  201. $all_data['company_scope'] = $company_scope;
  202. $category_id = request('category_id', '0');
  203. $all_data['category_id'] = $category_id;
  204. $specify_responsible_person = request('specify_responsible_person', '0');
  205. $all_data['specify_responsible_person'] = $specify_responsible_person;
  206. //查询是否存在
  207. $map = ['product_name' => $all_data['product_name'], 'platform' => $all_data['platform']];
  208. if(isset($all_data['product_specs']) && $all_data['product_specs'] !=''){
  209. $map['product_specs'] = $all_data['product_specs'];
  210. }
  211. if ($is_admin != 1 && $company_id != 0) {
  212. $map['company_id'] = $company_id;
  213. $all_data['company_id'] = $company_id;
  214. } else {
  215. $map['company_id'] = $admin_company_id;
  216. $all_data['company_id'] = $admin_company_id;
  217. }
  218. $data = $LowPriceGoodsModel->where($map)->first();
  219. if ($data) return json_send(['code' => 'error', 'msg' => '记录已存在']);
  220. // 写入数据表
  221. $result = $LowPriceGoodsModel->addLowProduct($all_data);
  222. // 如果操作失败
  223. if (!$result) return json_send(['code' => 'error', 'msg' => '新增失败']);
  224. // 记录行为
  225. $admin_id = request('access_token.uid', 0); //用户ID
  226. $is_admin = request('access_token.is_admin'); //是否管理员操作 0=是1=否
  227. $table_name = $LowPriceGoodsModel->getTable();
  228. $notes_type = 1; //操作类型,1添加,2修改,3=删除
  229. $this->addAdminHistory('清洗配置-低价商品管理', $company_id, $admin_id, $is_admin, $table_name, $notes_type, [], $all_data, '新增了低价商品' . $all_data['product_name'] . '信息');
  230. // 告知结果
  231. return json_send(['code' => 'success', 'msg' => '新增成功']);
  232. }
  233. /**
  234. * 修改
  235. * @author 唐远望
  236. * @version 1.0
  237. * @date 2025-12-02
  238. *
  239. */
  240. public function edit(Request $request, LowPriceGoodsModel $LowPriceGoodsModel)
  241. {
  242. $request->scene('edit')->validate();
  243. $admin_company_id = request('admin_company_id', '0');
  244. $company_id = request('access_token.company_id', '0');
  245. $is_admin = request('access_token.is_admin', '0'); //是否管理员操作 0=是1=否
  246. // 接收参数
  247. $id = request('id', 0);
  248. // 接收数据
  249. $all_data = request()->all();
  250. $product_brand = request('product_brand', '');
  251. $product_keyword = request('product_keyword', '');
  252. $product_specs = request('product_specs', '');
  253. $all_data['product_specs'] = $product_specs;
  254. $all_data['product_brand'] = $product_brand;
  255. $all_data['product_keyword'] = $product_keyword;
  256. $enable_full_quantity = request('enable_full_quantity', 1); //全量,0启用,1禁用
  257. $all_data['enable_full_quantity'] = $enable_full_quantity;
  258. if ($enable_full_quantity == 1 && !isset($all_data['product_specs'])) {
  259. return json_send(['code' => 'error', 'msg' => '非全量清洗时,商品规格不能为空']);
  260. }
  261. if(trim($product_keyword) !=''){
  262. $product_keyword_count = count(explode(',', $product_keyword));
  263. if ($product_keyword_count > 5) {
  264. return json_send(['code' => 'error', 'msg' => '商品关键词不能超过5个']);
  265. }
  266. }
  267. $store_scope = request('store_scope', '');
  268. $all_data['store_scope'] = $store_scope;
  269. $company_scope = request('company_scope', '');
  270. $all_data['company_scope'] = $company_scope;
  271. $category_id = request('category_id', '0');
  272. $all_data['category_id'] = $category_id;
  273. $specify_responsible_person = request('specify_responsible_person', '0');
  274. $all_data['specify_responsible_person'] = $specify_responsible_person;
  275. //查询是否存在
  276. $map = ['product_name' => $all_data['product_name'], 'platform' => $all_data['platform']];
  277. if(isset($all_data['product_specs']) && $all_data['product_specs'] !=''){
  278. $map['product_specs'] = $all_data['product_specs'];
  279. }
  280. if ($is_admin != 1 && $company_id != 0) {
  281. $map['company_id'] = $company_id;
  282. } else {
  283. $map['company_id'] = $admin_company_id;
  284. }
  285. $data = $LowPriceGoodsModel->where($map)->where('id', '!=', $id)->first();
  286. if ($data) return json_send(['code' => 'error', 'msg' => '记录已存在']);
  287. // 更新数据表
  288. $where = ['id' => $id];
  289. if ($is_admin != 1 && $company_id != 0) {
  290. $where['company_id'] = $company_id;
  291. $all_data['company_id'] = $company_id;
  292. } else {
  293. $where['company_id'] = $admin_company_id;
  294. $all_data['company_id'] = $admin_company_id;
  295. }
  296. $LowProduct = $LowPriceGoodsModel->where($where)->first();
  297. if (!$LowProduct) return json_send(['code' => 'error', 'msg' => '记录不存在']);
  298. $oldData = $LowProduct->toarray();
  299. $result = $LowPriceGoodsModel->editLowProduct_content($LowProduct, $all_data);
  300. // 如果操作失败
  301. if (!$result) return json_send(['code' => 'error', 'msg' => '修改失败']);
  302. // 记录行为
  303. $admin_id = request('access_token.uid', 0); //用户ID
  304. $table_name = $LowPriceGoodsModel->getTable();
  305. $notes_type = 2; //操作类型,1添加,2修改,3=删除
  306. $this->addAdminHistory('清洗配置-低价商品管理', $company_id, $admin_id, $is_admin, $table_name, $notes_type, $oldData, $all_data, '修改了低价商品' . $oldData['product_name'] . '信息');
  307. // 告知结果
  308. return json_send(['code' => 'success', 'msg' => '修改成功']);
  309. }
  310. /**
  311. * 修改状态
  312. * @author 唐远望
  313. * @version 1.0
  314. * @date 2025-12-02
  315. *
  316. */
  317. public function set_status(Request $request, LowPriceGoodsModel $LowPriceGoodsModel, ViolationProductModel $ViolationProductModel)
  318. {
  319. // 验证参数
  320. $request->scene('set_status')->validate();
  321. $admin_company_id = request('admin_company_id', '0');
  322. $company_id = request('access_token.company_id', '0');
  323. $is_admin = request('access_token.is_admin', '0'); //是否管理员操作 0=是1=否
  324. // 接收数据
  325. $id = request('id', 0);
  326. $status = request('status', 0);
  327. if ($status == 0) {
  328. //获取禁止商品启动数量
  329. $status_where = ['status' => 0];
  330. if ($is_admin != 1 && $company_id != 0) {
  331. $status_where['company_id'] = $company_id;
  332. } else {
  333. $status_where['company_id'] = $admin_company_id;
  334. }
  335. $violation_product_count = $ViolationProductModel->where($status_where)->count();
  336. //获取低价挂网商品启用数量
  337. $lowprice_product_count = $LowPriceGoodsModel->where($status_where)->count();
  338. //计算总数量
  339. $product_totle = $violation_product_count + $lowprice_product_count;
  340. }
  341. // 查询用户
  342. $where = ['id' => $id];
  343. if ($is_admin != 1 && $company_id != 0) {
  344. $where['company_id'] = $company_id;
  345. } else {
  346. $where['company_id'] = $admin_company_id;
  347. }
  348. $LowProduct = $LowPriceGoodsModel->where($where)->first();
  349. if (!$LowProduct) return json_send(['code' => 'error', 'msg' => '记录不存在']);
  350. // 执行修改
  351. $result = $LowPriceGoodsModel->changeStatus($LowProduct, $status);
  352. // 提示新增失败
  353. if (!$result) return json_send(['code' => 'error', 'msg' => '设置失败']);
  354. // 记录行为
  355. $admin_id = request('access_token.uid', 0); //用户ID
  356. $table_name = $LowPriceGoodsModel->getTable();
  357. $notes_type = 2; //操作类型,1添加,2修改,3=删除
  358. $this->addAdminHistory('清洗配置-低价商品管理', $company_id, $admin_id, $is_admin, $table_name, $notes_type, [], ['status' => $status], '修改了低价商品' . $LowProduct->product_name . '状态');
  359. // 告知结果
  360. return json_send(['code' => 'success', 'msg' => '设置成功']);
  361. }
  362. /**
  363. * 删除
  364. * @author 唐远望
  365. * @version 1.0
  366. * @date 2025-12-02
  367. *
  368. */
  369. public function delete(Request $request, LowPriceGoodsModel $LowPriceGoodsModel)
  370. {
  371. // 验证参数
  372. $request->scene('delete')->validate();
  373. $admin_company_id = request('admin_company_id', '0');
  374. $company_id = request('access_token.company_id', '0');
  375. $is_admin = request('access_token.is_admin', '0'); //是否管理员操作 0=是1=否
  376. // 接收数据
  377. $id = request('id', 0);
  378. // 查询用户
  379. $where = ['id' => $id];
  380. if ($is_admin != 1 && $company_id != 0) {
  381. $where['company_id'] = $company_id;
  382. } else {
  383. $where['company_id'] = $admin_company_id;
  384. }
  385. // 执行删除
  386. $LowProduct = $LowPriceGoodsModel->where($where)->first();
  387. if (!$LowProduct) {
  388. return json_send(['code' => 'error', 'msg' => '记录不存在']);
  389. }
  390. DB::beginTransaction();
  391. try {
  392. $LowPriceGoodsCompanyModel = new LowPriceGoodsCompanyModel();
  393. $company_id_log = $LowPriceGoodsCompanyModel->where('lowprice_product_logid', $LowProduct->id)->get();
  394. if (!empty($company_id_log)) {
  395. $LowPriceGoodsCompanyModel->where('lowprice_product_logid', $LowProduct->id)->delete();
  396. }
  397. $LowProduct->delete();
  398. // 记录行为
  399. $admin_id = request('access_token.uid', 0); //用户ID
  400. $is_admin = request('access_token.is_admin'); //是否管理员操作 0=是1=否
  401. $table_name = $LowPriceGoodsModel->getTable();
  402. $notes_type = 3; //操作类型,1添加,2修改,3=删除
  403. $this->addAdminHistory('清洗配置-低价商品管理', $company_id, $admin_id, $is_admin, $table_name, $notes_type, $LowProduct->toarray(), [], '删除了低价商品' . $LowProduct->product_name . '信息');
  404. DB::commit();
  405. // 告知结果
  406. return json_send(['code' => 'success', 'msg' => '删除成功']);
  407. // 成功处理...
  408. } catch (\Exception $e) {
  409. DB::rollBack();
  410. // 错误处理...
  411. return json_send(['code' => 'error', 'msg' => '删除失败']);
  412. }
  413. }
  414. /**
  415. * 下载导入模板
  416. * @author 唐远望
  417. * @version 1.0
  418. * @date 2026-02-28
  419. *
  420. */
  421. public function download_template()
  422. {
  423. // 创建一个新的 Spreadsheet 对象
  424. $spreadsheet = new Spreadsheet();
  425. $sheet = $spreadsheet->getActiveSheet();
  426. //合并单元格
  427. $sheet->mergeCells('A1:R1');
  428. $sheet->setCellValue('A1', '低价挂网商品清洗导入模板'); // 设置合并后的单元格内容
  429. // 获取合并后的单元格样式对象
  430. $style = $sheet->getStyle('A1');
  431. // 设置水平居中和垂直居中
  432. $style->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER)->setVertical(Alignment::VERTICAL_CENTER);
  433. // 然后设置行高以适应两行文本
  434. $sheet->getRowDimension(1)->setRowHeight(40); // 设置行高,单位是磅(point)
  435. // 设置表头
  436. $sheet->setCellValue('A2', '商品名称*');
  437. $sheet->setCellValue('B2', '商品分类名称');
  438. $sheet->setCellValue('C2', '商品规格*');
  439. $sheet->setCellValue('D2', '监控价格*');
  440. $sheet->setCellValue('E2', '平台名称(多个逗号隔开,为空全部)');
  441. $sheet->setCellValue('F2', '指定公司(多个逗号隔开,为空全部)');
  442. $sheet->setCellValue('G2', '指派责任人(是/否)*');
  443. // 生成 Excel 文件
  444. $writer = new Xlsx($spreadsheet);
  445. // 直接输出到浏览器(下载)
  446. $filename = '低价挂网商品清洗导入模板.xlsx';
  447. header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  448. header('Content-Disposition: attachment;filename="' . $filename . '"');
  449. header('Cache-Control: max-age=0');
  450. $writer->save('php://output');
  451. exit;
  452. }
  453. /**
  454. * 导入Excel数据
  455. * @author 唐远望
  456. * @version 1.0
  457. * @date 2026-02-28
  458. *
  459. */
  460. public function import_data(Request $request, LowPriceGoodsModel $LowPriceGoodsModel, ViolationStoreModel $ViolationStoreModel, ProductCategoryModel $ProductCategoryModel)
  461. {
  462. $request->scene('import_data')->validate();
  463. $admin_company_id = request('admin_company_id', '0');
  464. $company_id = request('access_token.company_id', '0');
  465. $is_admin = request('access_token.is_admin', '0'); //是否管理员操作 0=是1=否
  466. $file = $request->file('file');
  467. // 加载Excel文件
  468. $spreadsheet = IOFactory::load($file->getPathname());
  469. $sheet = $spreadsheet->getActiveSheet();
  470. // 获取所有数据
  471. $data = $sheet->toArray();
  472. if (empty($data) || count($data) < 2) return json_send(['code' => 'error', 'msg' => '导入数据为空']);
  473. $platform_data = $LowPriceGoodsModel->platform_data();
  474. DB::beginTransaction();
  475. try {
  476. foreach ($data as $key => $item) {
  477. if ($key < 2) continue;
  478. //强制必传参数校验
  479. $res_data = $this->import_data_check($key, $item);
  480. if ($res_data) return json_send($res_data);
  481. $category_id = '0';
  482. if (isset($item[1])) {
  483. $category_data = $ProductCategoryModel->where('name', $item[1])->first();
  484. $category_id = $category_data ? $category_data->id : '0';
  485. }
  486. $platform_text = isset($item[4]) ? explode(',', $item[4]) : '0'; // 平台
  487. $platform_id_text = '';
  488. if (!empty($platform_text)) {
  489. foreach ($platform_text as $key => $value) {
  490. if (isset($platform_data[$value])) {
  491. $platform_id_text = $platform_data[$value] . ',' . $platform_id_text;
  492. }
  493. }
  494. }
  495. $company_scope = isset($item[5]) ? explode(',', $item[5]) : '0'; // 公司范围
  496. $company_id_text = '';
  497. if (!empty($company_scope)) {
  498. //查询所有公司ID逗号隔开
  499. $company_id_text = $ViolationStoreModel->whereIn('company_name', $company_scope)->pluck('id')->implode(',');
  500. }
  501. // 权限判断
  502. if ($is_admin != 1 && $company_id != 0) {
  503. $insert_product_data['company_id'] = $company_id;
  504. } else {
  505. $insert_product_data['company_id'] = $admin_company_id;
  506. }
  507. $insert_product_data['product_name'] = $item[0]; // 商品名称
  508. $insert_product_data['category_id'] = $category_id; // 商品分类
  509. $insert_product_data['product_specs'] = $item[2]; // 商品规格
  510. $insert_product_data['suggested_price'] = $item[3]; // 指导价格
  511. $insert_product_data['platform'] = $platform_id_text != '' ? substr($platform_id_text, 0, -1) : '0'; // 平台:0=全部,1=淘宝,2=京东,3=拼多多,4=美团,5=药师帮,6=1药城,7=药久久
  512. $insert_product_data['store_scope'] = ''; // 店铺范围(为空时全部,指定时为店铺ID多个逗号隔开)
  513. $insert_product_data['company_scope'] = $company_id_text; // 公司范围(为空时全部,指定时为公司ID多个逗号隔开)
  514. $insert_product_data['specify_responsible_person'] = trim($item[6]) == '是' ? 0 : 1; // 指派责任人 0=开启 1=关闭
  515. //插入数据
  516. $LowPriceGoodsModel->addLowProduct($insert_product_data);
  517. }
  518. DB::commit();
  519. return json_send(['code' => 'success', 'msg' => '导入成功']);
  520. // 成功处理...
  521. } catch (\Exception $e) {
  522. DB::rollBack();
  523. // 错误处理...
  524. return json_send(['code' => 'error', 'msg' => '导入失败', 'data' => $e->getMessage()]);
  525. }
  526. }
  527. /**
  528. * 导入Excel数据必传参数校验
  529. * @author 唐远望
  530. * @version 1.0
  531. * @date 2025-12-31
  532. *
  533. */
  534. private function import_data_check($key, $item)
  535. {
  536. $key = $key + 1;
  537. if (!$item[0]) {
  538. return ['code' => 'error', 'msg' => "第{$key}行商品名称不能为空", 'data' => $item];
  539. }
  540. if (!$item[2]) {
  541. return ['code' => 'error', 'msg' => "第{$key}行商品规格不能为空", 'data' => $item];
  542. }
  543. if (!$item[3]) {
  544. return ['code' => 'error', 'msg' => "第{$key}行监控价格不能为空", 'data' => $item];
  545. }
  546. if (!$item[6]) {
  547. return ['code' => 'error', 'msg' => "第{$key}行指派责任人不能为空", 'data' => $item];
  548. }
  549. }
  550. }