ViolationProduct.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  1. <?php
  2. namespace App\Http\Controllers\Manager\WashConfig;
  3. use App\Http\Controllers\Controller;
  4. use App\Http\Requests\Manager\WashConfig\ViolationProduct as Request;
  5. use App\Models\Manager\WashConfig\ViolationProduct as ViolationProductModel;
  6. use App\Models\Manager\WashConfig\ViolationStore as ViolationStoreModel;
  7. use App\Models\Manager\WashConfig\ViolationProductCompany as ViolationProductCompanyModel;
  8. use App\Models\Manager\WashConfig\ProductCategory as ProductCategoryModel;
  9. use Illuminate\Support\Facades\DB;
  10. use App\Models\Manager\WashConfig\LowPriceGoods as LowPriceGoodsModel;
  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-03
  21. */
  22. class ViolationProduct extends Controller
  23. {
  24. /**
  25. * 列表
  26. * @author 唐远望
  27. * @version 1.0
  28. * @date 2025-12-03
  29. *
  30. */
  31. public function list(Request $request, ViolationProductModel $ViolationProductModel, ViolationProductCompanyModel $ViolationProductCompanyModel, 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. $ViolationProductModel = $ViolationProductModel->whereIn('id', function ($query) use ($platform_ids) {
  69. $query->select('product_id')
  70. ->from('washconfig_violation_product_platform')
  71. ->whereIn('platform_id', $platform_ids)
  72. ->distinct('product_id');;
  73. });
  74. }
  75. if ($company_name) {
  76. $ViolationProductModel = $ViolationProductModel->whereIn('id', function ($query) use ($company_name) {
  77. $query->select('washconfig_violation_product_company.violation_product_logid')
  78. ->from('washconfig_violation_product_company')
  79. ->join('washconfig_violation_store', 'washconfig_violation_store.id', '=', 'washconfig_violation_product_company.company_id')
  80. ->where('washconfig_violation_store.company_name', 'like', "%$company_name%")
  81. ->distinct('washconfig_violation_product_company.company_id');
  82. });
  83. }
  84. $result = $ViolationProductModel
  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 (trim($value['store_scope']) == '') {
  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 = $ViolationProductCompanyModel->where('violation_product_logid', $value['id'])
  107. ->join('washconfig_violation_store', 'washconfig_violation_store.id', '=', 'washconfig_violation_product_company.company_id')
  108. ->select(['washconfig_violation_store.id', 'washconfig_violation_store.company_name', 'washconfig_violation_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-03
  121. */
  122. public function detail(Request $request, ViolationProductModel $ViolationProductModel, ViolationProductCompanyModel $ViolationProductCompanyModel, 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 = $ViolationProductModel->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 = $ViolationProductCompanyModel->where('violation_product_logid', $data->id)
  150. ->join('washconfig_violation_store', 'washconfig_violation_store.id', '=', 'washconfig_violation_product_company.company_id')
  151. ->select(['washconfig_violation_store.id', 'washconfig_violation_store.company_name', 'washconfig_violation_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-03
  165. *
  166. */
  167. public function add(Request $request, 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. $status_where = ['status' => 0];
  174. if ($is_admin != 1 && $company_id != 0) {
  175. $status_where['company_id'] = $company_id;
  176. } else {
  177. $status_where['company_id'] = $admin_company_id;
  178. }
  179. //获取禁止商品启动数量
  180. $violation_product_count = $ViolationProductModel->where($status_where)->count();
  181. //获取低价挂网商品启用数量
  182. $lowprice_product_count = $ViolationProductModel->where($status_where)->count();
  183. //计算总数量
  184. $product_totle = $violation_product_count + $lowprice_product_count;
  185. // 接收数据
  186. $all_data = request()->all();
  187. $product_brand = request('product_brand', '');
  188. $product_keyword = request('product_keyword', '');
  189. $product_specs = request('product_specs', '');
  190. $all_data['product_specs'] = $product_specs;
  191. $all_data['product_brand'] = $product_brand;
  192. $all_data['product_keyword'] = $product_keyword;
  193. $enable_full_quantity = request('enable_full_quantity', 1); //全量,0启用,1禁用
  194. $all_data['enable_full_quantity'] = $enable_full_quantity;
  195. if ($enable_full_quantity == 1 && !isset($all_data['product_specs'])) {
  196. return json_send(['code' => 'error', 'msg' => '非全量清洗时,商品规格不能为空']);
  197. }
  198. if(trim($product_keyword) !=''){
  199. $product_keyword_count = count(explode(',', $product_keyword));
  200. if ($product_keyword_count > 5) {
  201. return json_send(['code' => 'error', 'msg' => '商品关键词不能超过5个']);
  202. }
  203. }
  204. $store_scope = request('store_scope', '');
  205. $all_data['store_scope'] = $store_scope;
  206. $platform_string = request('platform', '');
  207. $company_scope = request('company_scope', '');
  208. $all_data['company_scope'] = $company_scope;
  209. $category_id = request('category_id', '0');
  210. $all_data['category_id'] = $category_id;
  211. $specify_responsible_person = request('specify_responsible_person', '0');
  212. $all_data['specify_responsible_person'] = $specify_responsible_person;
  213. //查询是否存在
  214. $map = ['product_brand' => $all_data['product_brand'], 'product_name' => $all_data['product_name']];
  215. if(isset($all_data['product_specs']) && $all_data['product_specs'] !=''){
  216. $map['product_specs'] = $all_data['product_specs'];
  217. }
  218. if ($is_admin != 1 && $company_id != 0) {
  219. $map['company_id'] = $company_id;
  220. $all_data['company_id'] = $company_id;
  221. } else {
  222. $map['company_id'] = $admin_company_id;
  223. $all_data['company_id'] = $admin_company_id;
  224. }
  225. $data = $ViolationProductModel->where($map)->first();
  226. if (!empty($data) && $enable_full_quantity == 1) {
  227. //继续校验规格是否存在
  228. $product_specs_tring = $all_data['product_specs'];
  229. $product_specs = $product_specs_tring ? explode(',', $product_specs_tring) : '';
  230. $product_specs = $product_specs ? array_unique($product_specs) : '';
  231. //继续校平台是否存在
  232. $platforms = $platform_string ? explode(',', $platform_string) : '';
  233. $platforms = $platforms ? array_unique($platforms) : '';
  234. $platform_data = $ViolationProductModel->platform_index_data();
  235. foreach ($platforms as $platform) {
  236. $product_specs_log = $ViolationProductModel
  237. ->whereRaw("FIND_IN_SET(?, platform)", [$platform])
  238. ->where(['product_brand' => $product_brand, 'product_name' => $all_data['product_name'], 'status' => '0'])
  239. ->where(function ($query) use ($product_specs) {
  240. // 平台条件(固定)
  241. $query->where(function ($query_li) use ($product_specs) {
  242. foreach ($product_specs as $product_spec) {
  243. if (empty($product_specs)) continue;
  244. $query_li->orWhereRaw("FIND_IN_SET(?, product_specs)", [$product_spec]);
  245. }
  246. });
  247. })->first();
  248. $platform_name = isset($platform_data[$platform]) ? $platform_data[$platform] : '';
  249. if (!empty($product_specs_log)) {
  250. return json_send(['code' => 'error', 'msg' => $platform_name . '存在重复的商品规格记录']);
  251. }
  252. }
  253. } else if (!empty($data) && $enable_full_quantity == 0) {
  254. $platforms = $platform_string ? explode(',', $platform_string) : '';
  255. $platforms = $platforms ? array_unique($platforms) : '';
  256. $platform_data = $ViolationProductModel->platform_index_data();
  257. foreach ($platforms as $platform) {
  258. $product_specs_log = $ViolationProductModel
  259. ->whereRaw("FIND_IN_SET(?, platform)", [$platform])
  260. ->where(['product_brand' => $product_brand, 'product_name' => $all_data['product_name'], 'status' => '0'])
  261. ->first();
  262. $platform_name = isset($platform_data[$platform]) ? $platform_data[$platform] : '';
  263. if (!empty($product_specs_log)) {
  264. return json_send(['code' => 'error', 'msg' => $platform_name . '存在重复的商品记录']);
  265. }
  266. }
  267. }
  268. // 写入数据表
  269. $result = $ViolationProductModel->addViolationProduct($all_data);
  270. // 如果操作失败
  271. if (!$result) return json_send(['code' => 'error', 'msg' => '新增失败']);
  272. // 记录行为
  273. $admin_id = request('access_token.uid', 0); //用户ID
  274. $table_name = $ViolationProductModel->getTable();
  275. $notes_type = 1; //操作类型,1添加,2修改,3=删除
  276. $this->addAdminHistory('清洗配置-禁止商品管理', $company_id, $admin_id, $is_admin, $table_name, $notes_type, [], $all_data, '新增了禁止商品' . $all_data['product_name'] . '信息');
  277. // 告知结果
  278. return json_send(['code' => 'success', 'msg' => '新增成功']);
  279. }
  280. /**
  281. * 修改
  282. * @author 唐远望
  283. * @version 1.0
  284. * @date 2025-12-03
  285. *
  286. */
  287. public function edit(Request $request, ViolationProductModel $ViolationProductModel)
  288. {
  289. $request->scene('edit')->validate();
  290. $admin_company_id = request('admin_company_id', '0');
  291. $company_id = request('access_token.company_id', '0');
  292. $is_admin = request('access_token.is_admin', '0'); //是否管理员操作 0=是1=否
  293. // 接收参数
  294. $id = request('id', 0);
  295. // 接收数据
  296. $all_data = request()->all();
  297. $product_brand = request('product_brand', '');
  298. $product_keyword = request('product_keyword', '');
  299. $product_specs = request('product_specs', '');
  300. $all_data['product_specs'] = $product_specs;
  301. $all_data['product_brand'] = $product_brand;
  302. $all_data['product_keyword'] = $product_keyword;
  303. $enable_full_quantity = request('enable_full_quantity', 1); //全量,0启用,1禁用
  304. $all_data['enable_full_quantity'] = $enable_full_quantity;
  305. if ($enable_full_quantity == 1 && !isset($all_data['product_specs'])) {
  306. return json_send(['code' => 'error', 'msg' => '非全量清洗时,商品规格不能为空']);
  307. }
  308. if(trim($product_keyword) !=''){
  309. $product_keyword_count = count(explode(',', $product_keyword));
  310. if ($product_keyword_count > 5) {
  311. return json_send(['code' => 'error', 'msg' => '商品关键词不能超过5个']);
  312. }
  313. }
  314. $store_scope = request('store_scope', '');
  315. $all_data['store_scope'] = $store_scope;
  316. $company_scope = request('company_scope', '');
  317. $platform_string = request('platform', '');
  318. $all_data['company_scope'] = $company_scope;
  319. $category_id = request('category_id', '0');
  320. $all_data['category_id'] = $category_id;
  321. $specify_responsible_person = request('specify_responsible_person', '0');
  322. $all_data['specify_responsible_person'] = $specify_responsible_person;
  323. //查询是否存在
  324. $map = ['product_brand' => $all_data['product_brand'], 'product_name' => $all_data['product_name']];
  325. if(isset($all_data['product_specs']) && $all_data['product_specs'] !=''){
  326. $map['product_specs'] = $all_data['product_specs'];
  327. }
  328. if ($is_admin != 1 && $company_id != 0) {
  329. $map['company_id'] = $company_id;
  330. $all_data['company_id'] = $company_id;
  331. } else {
  332. $map['company_id'] = $admin_company_id;
  333. $all_data['company_id'] = $admin_company_id;
  334. }
  335. $data = $ViolationProductModel->where($map)->where('id', '!=', $id)->first();
  336. if (!empty($data) && $enable_full_quantity == 1) {
  337. //继续校验规格是否存在
  338. $product_specs_tring = $all_data['product_specs'];
  339. $product_specs = $product_specs_tring ? explode(',', $product_specs_tring) : '';
  340. $product_specs = $product_specs ? array_unique($product_specs) : '';
  341. //继续校平台是否存在
  342. $platforms = $platform_string ? explode(',', $platform_string) : '';
  343. $platforms = $platforms ? array_unique($platforms) : '';
  344. $platform_data = $ViolationProductModel->platform_index_data();
  345. foreach ($platforms as $platform) {
  346. $product_specs_log = $ViolationProductModel
  347. ->whereRaw("FIND_IN_SET(?, platform)", [$platform])
  348. ->where(['product_brand' => $product_brand, 'product_name' => $all_data['product_name'], 'status' => '0'])
  349. ->where('id', '!=', $id)
  350. ->where(function ($query) use ($product_specs) {
  351. // 平台条件(固定)
  352. $query->where(function ($query_li) use ($product_specs) {
  353. foreach ($product_specs as $product_spec) {
  354. if (empty($product_specs)) continue;
  355. $query_li->orWhereRaw("FIND_IN_SET(?, product_specs)", [$product_spec]);
  356. }
  357. });
  358. })->first();
  359. $platform_name = isset($platform_data[$platform]) ? $platform_data[$platform] : '';
  360. if (!empty($product_specs_log)) {
  361. return json_send(['code' => 'error', 'msg' => $platform_name . '存在重复的商品规格记录']);
  362. }
  363. }
  364. } else if (!empty($data) && $enable_full_quantity == 0) {
  365. $platforms = $platform_string ? explode(',', $platform_string) : '';
  366. $platforms = $platforms ? array_unique($platforms) : '';
  367. $platform_data = $ViolationProductModel->platform_index_data();
  368. foreach ($platforms as $platform) {
  369. $product_specs_log = $ViolationProductModel
  370. ->whereRaw("FIND_IN_SET(?, platform)", [$platform])
  371. ->where(['product_brand' => $product_brand, 'product_name' => $all_data['product_name'], 'status' => '0'])
  372. ->first();
  373. $platform_name = isset($platform_data[$platform]) ? $platform_data[$platform] : '';
  374. if (!empty($product_specs_log)) {
  375. return json_send(['code' => 'error', 'msg' => $platform_name . '存在重复的商品记录']);
  376. }
  377. }
  378. }
  379. // 更新数据表
  380. $where = ['id' => $id];
  381. if ($is_admin != 1 && $company_id != 0) {
  382. $where['company_id'] = $company_id;
  383. } else {
  384. $where['company_id'] = $admin_company_id;
  385. }
  386. $ViolationProduct = $ViolationProductModel->where($where)->first();
  387. if (!$ViolationProduct) return json_send(['code' => 'error', 'msg' => '记录不存在']);
  388. $oldData = $ViolationProduct->toarray();
  389. $result = $ViolationProductModel->editViolationProduct_content($ViolationProduct, $all_data);
  390. // 如果操作失败
  391. if (!$result) return json_send(['code' => 'error', 'msg' => '修改失败']);
  392. // 记录行为
  393. $admin_id = request('access_token.uid', 0); //用户ID
  394. $table_name = $ViolationProductModel->getTable();
  395. $notes_type = 2; //操作类型,1添加,2修改,3=删除
  396. $this->addAdminHistory('清洗配置-禁止商品管理', $company_id, $admin_id, $is_admin, $table_name, $notes_type, $oldData, $all_data, '修改了禁止商品' . $oldData['product_name'] . '信息');
  397. // 告知结果
  398. return json_send(['code' => 'success', 'msg' => '修改成功']);
  399. }
  400. /**
  401. * 修改状态
  402. * @author 唐远望
  403. * @version 1.0
  404. * @date 2025-12-03
  405. *
  406. */
  407. public function set_status(Request $request, ViolationProductModel $ViolationProductModel)
  408. {
  409. // 验证参数
  410. $request->scene('set_status')->validate();
  411. $admin_company_id = request('admin_company_id', '0');
  412. $company_id = request('access_token.company_id', '0');
  413. $is_admin = request('access_token.is_admin', '0'); //是否管理员操作 0=是1=否
  414. // 接收数据
  415. $id = request('id', 0);
  416. $status = request('status', 0);
  417. if ($status == 0) {
  418. //获取禁止商品启动数量
  419. $violation_product_count = $ViolationProductModel->where('status', 0)->count();
  420. //获取低价挂网商品启用数量
  421. $lowprice_product_count = $ViolationProductModel->where('status', 0)->count();
  422. //计算总数量
  423. $product_totle = $violation_product_count + $lowprice_product_count;
  424. }
  425. // 查询用户
  426. $where = ['id' => $id];
  427. if ($is_admin != 1 && $company_id != 0) {
  428. $where['company_id'] = $company_id;
  429. } else {
  430. $where['company_id'] = $admin_company_id;
  431. }
  432. $ViolationProduct = $ViolationProductModel->where($where)->first();
  433. if (!$ViolationProduct) return json_send(['code' => 'error', 'msg' => '记录不存在']);
  434. if (!empty($ViolationProduct) && $ViolationProduct->enable_full_quantity == 1) {
  435. //继续校验规格是否存在
  436. $product_specs_tring = $ViolationProduct->product_specs;
  437. $product_specs = $product_specs_tring ? explode(',', $product_specs_tring) : '';
  438. $product_specs = $product_specs ? array_unique($product_specs) : '';
  439. //继续校平台是否存在
  440. $platform_string = $ViolationProduct->platform;
  441. $platforms = $platform_string ? explode(',', $platform_string) : '';
  442. $platforms = $platforms ? array_unique($platforms) : '';
  443. $platform_data = $ViolationProductModel->platform_index_data();
  444. foreach ($platforms as $platform) {
  445. $product_specs_log = $ViolationProductModel
  446. ->whereRaw("FIND_IN_SET(?, platform)", [$platform])
  447. ->where(['product_brand' => $ViolationProduct->product_brand, 'product_name' => $ViolationProduct->product_name, 'status' => '0'])
  448. ->where('id', '!=', $id)
  449. ->where(function ($query) use ($product_specs) {
  450. // 平台条件(固定)
  451. $query->where(function ($query_li) use ($product_specs) {
  452. foreach ($product_specs as $product_spec) {
  453. if (empty($product_specs)) continue;
  454. $query_li->orWhereRaw("FIND_IN_SET(?, product_specs)", [$product_spec]);
  455. }
  456. });
  457. })->first();
  458. $platform_name = isset($platform_data[$platform]) ? $platform_data[$platform] : '';
  459. if (!empty($product_specs_log)) {
  460. return json_send(['code' => 'error', 'msg' => $platform_name . '存在重复的商品规格启用记录']);
  461. }
  462. }
  463. } else if (!empty($data) && $ViolationProduct->enable_full_quantity == 0) {
  464. $platform_string = $ViolationProduct->platform;
  465. $platforms = $platform_string ? explode(',', $platform_string) : '';
  466. $platforms = $platforms ? array_unique($platforms) : '';
  467. $platform_data = $ViolationProductModel->platform_index_data();
  468. foreach ($platforms as $platform) {
  469. $product_specs_log = $ViolationProductModel
  470. ->whereRaw("FIND_IN_SET(?, platform)", [$platform])
  471. ->where(['product_brand' => $ViolationProduct->product_brand, 'product_name' => $ViolationProduct->product_name, 'status' => '0'])
  472. ->where('id', '!=', $id)->first();
  473. $platform_name = isset($platform_data[$platform]) ? $platform_data[$platform] : '';
  474. if (!empty($product_specs_log)) {
  475. return json_send(['code' => 'error', 'msg' => $platform_name . '存在重复的商品启用记录']);
  476. }
  477. }
  478. }
  479. // 执行修改
  480. $result = $ViolationProductModel->changeStatus($ViolationProduct, $status);
  481. // 提示新增失败
  482. if (!$result) return json_send(['code' => 'error', 'msg' => '设置失败']);
  483. // 记录行为
  484. $admin_id = request('access_token.uid', 0); //用户ID
  485. $table_name = $ViolationProductModel->getTable();
  486. $notes_type = 2; //操作类型,1添加,2修改,3=删除
  487. $this->addAdminHistory('清洗配置-禁止商品管理', $company_id, $admin_id, $is_admin, $table_name, $notes_type, [], ['status' => $status], '修改了禁止商品' . $ViolationProduct->product_name . '状态');
  488. // 告知结果
  489. return json_send(['code' => 'success', 'msg' => '设置成功']);
  490. }
  491. /**
  492. * 删除
  493. * @author 唐远望
  494. * @version 1.0
  495. * @date 2025-12-03
  496. *
  497. */
  498. public function delete(Request $request, ViolationProductModel $ViolationProductModel)
  499. {
  500. // 验证参数
  501. $request->scene('delete')->validate();
  502. $admin_company_id = request('admin_company_id', '0');
  503. $company_id = request('access_token.company_id', '0');
  504. $is_admin = request('access_token.is_admin', '0'); //是否管理员操作 0=是1=否
  505. // 接收数据
  506. $id = request('id', 0);
  507. // 查询用户
  508. $where = ['id' => $id];
  509. if ($is_admin != 1 && $company_id != 0) {
  510. $where['company_id'] = $company_id;
  511. } else {
  512. $where['company_id'] = $admin_company_id;
  513. }
  514. // 执行删除
  515. $ViolationProduct = $ViolationProductModel->where($where)->first();
  516. if (!$ViolationProduct) {
  517. return false;
  518. }
  519. $ViolationProduct_log = $ViolationProduct->toArray();
  520. DB::beginTransaction();
  521. try {
  522. $ViolationProductCompanyModel = new ViolationProductCompanyModel();
  523. $company_id_log = $ViolationProductCompanyModel->where('violation_product_logid', $ViolationProduct->id)->get();
  524. if (!empty($company_id_log)) {
  525. $ViolationProductCompanyModel->where('violation_product_logid', $ViolationProduct->id)->delete();
  526. }
  527. $ViolationProductModel->where($where)->delete();
  528. // 记录行为
  529. $admin_id = request('access_token.uid', 0); //用户ID
  530. $table_name = $ViolationProductModel->getTable();
  531. $notes_type = 3; //操作类型,1添加,2修改,3=删除
  532. $this->addAdminHistory('清洗配置-禁止商品管理', $company_id, $admin_id, $is_admin, $table_name, $notes_type, $ViolationProduct_log, [], '删除了禁止商品' . $ViolationProduct_log['product_name'] . '信息');
  533. // 告知结果
  534. DB::commit();
  535. return json_send(['code' => 'success', 'msg' => '删除成功']);
  536. // 成功处理...
  537. } catch (\Exception $e) {
  538. DB::rollBack();
  539. // 错误处理...
  540. return json_send(['code' => 'error', 'msg' => '删除失败', 'data' => $e->getMessage(), 'k' => $ViolationProduct_log]);
  541. }
  542. }
  543. /**
  544. * 下载导入模板
  545. * @author 唐远望
  546. * @version 1.0
  547. * @date 2026-02-28
  548. *
  549. */
  550. public function download_template()
  551. {
  552. // 创建一个新的 Spreadsheet 对象
  553. $spreadsheet = new Spreadsheet();
  554. $sheet = $spreadsheet->getActiveSheet();
  555. //合并单元格
  556. $sheet->mergeCells('A1:R1');
  557. $sheet->setCellValue('A1', '禁止挂网商品清洗导入模板'); // 设置合并后的单元格内容
  558. // 获取合并后的单元格样式对象
  559. $style = $sheet->getStyle('A1');
  560. // 设置水平居中和垂直居中
  561. $style->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER)->setVertical(Alignment::VERTICAL_CENTER);
  562. // 然后设置行高以适应两行文本
  563. $sheet->getRowDimension(1)->setRowHeight(40); // 设置行高,单位是磅(point)
  564. // 设置表头
  565. $sheet->setCellValue('A2', '商品名称*');
  566. $sheet->setCellValue('B2', '商品分类名称');
  567. $sheet->setCellValue('C2', '商品规格*');
  568. $sheet->setCellValue('D2', '平台名称(多个逗号隔开,为空全部)');
  569. $sheet->setCellValue('E2', '指定公司(多个逗号隔开,为空全部)');
  570. $sheet->setCellValue('F2', '指派责任人(是/否)*');
  571. // 生成 Excel 文件
  572. $writer = new Xlsx($spreadsheet);
  573. // 直接输出到浏览器(下载)
  574. $filename = '禁止挂网商品清洗导入模板.xlsx';
  575. header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  576. header('Content-Disposition: attachment;filename="' . $filename . '"');
  577. header('Cache-Control: max-age=0');
  578. $writer->save('php://output');
  579. exit;
  580. }
  581. /**
  582. * 导入Excel数据
  583. * @author 唐远望
  584. * @version 1.0
  585. * @date 2026-02-28
  586. *
  587. */
  588. public function import_data(Request $request, ViolationProductModel $ViolationProductModel, ViolationStoreModel $ViolationStoreModel, ProductCategoryModel $ProductCategoryModel)
  589. {
  590. $request->scene('import_data')->validate();
  591. $admin_company_id = request('admin_company_id', '0');
  592. $company_id = request('access_token.company_id', '0');
  593. $is_admin = request('access_token.is_admin', '0'); //是否管理员操作 0=是1=否
  594. $file = $request->file('file');
  595. // 加载Excel文件
  596. $spreadsheet = IOFactory::load($file->getPathname());
  597. $sheet = $spreadsheet->getActiveSheet();
  598. // 获取所有数据
  599. $data = $sheet->toArray();
  600. if (empty($data) || count($data) < 2) return json_send(['code' => 'error', 'msg' => '导入数据为空']);
  601. $platform_data = $ViolationProductModel->platform_data();
  602. DB::beginTransaction();
  603. try {
  604. foreach ($data as $key => $item) {
  605. if ($key < 2) continue;
  606. //强制必传参数校验
  607. $res_data = $this->import_data_check($key, $item);
  608. if ($res_data) return json_send($res_data);
  609. $category_id = '0';
  610. if (isset($item[1])) {
  611. $category_data = $ProductCategoryModel->where('name', $item[1])->first();
  612. $category_id = $category_data ? $category_data->id : '0';
  613. }
  614. $platform_text = isset($item[3]) ? explode(',', $item[3]) : '0'; // 平台
  615. $platform_id_text = '';
  616. if (!empty($platform_text)) {
  617. foreach ($platform_text as $key => $value) {
  618. if (isset($platform_data[$value])) {
  619. $platform_id_text = $platform_data[$value] . ',' . $platform_id_text;
  620. }
  621. }
  622. }
  623. $company_scope = isset($item[4]) ? explode(',', $item[4]) : '0'; // 公司范围
  624. $company_id_text = '';
  625. if (!empty($company_scope)) {
  626. //查询所有公司ID逗号隔开
  627. $company_id_text = $ViolationStoreModel->whereIn('company_name', $company_scope)->pluck('id')->implode(',');
  628. }
  629. // 权限判断
  630. if ($is_admin != 1 && $company_id != 0) {
  631. $insert_product_data['company_id'] = $company_id;
  632. } else {
  633. $insert_product_data['company_id'] = $admin_company_id;
  634. }
  635. $insert_product_data['product_name'] = $item[0]; // 商品名称
  636. $insert_product_data['category_id'] =$category_id; // 商品分类
  637. $insert_product_data['product_specs'] = $item[2]; // 商品规格
  638. $insert_product_data['platform'] = $platform_id_text != '' ? substr($platform_id_text, 0, -1) : '0'; // 平台:0=全部,1=淘宝,2=京东,3=拼多多,4=美团,5=药师帮,6=1药城,7=药久久
  639. $insert_product_data['store_scope'] = ''; // 店铺范围(为空时全部,指定时为店铺ID多个逗号隔开)
  640. $insert_product_data['company_scope'] = $company_id_text; // 公司范围(为空时全部,指定时为公司ID多个逗号隔开)
  641. $insert_product_data['specify_responsible_person'] = trim($item[5]) == '是' ? 0 : 1; // 指派责任人 0=开启 1=关闭
  642. //插入数据
  643. $ViolationProductModel->addViolationProduct($insert_product_data);
  644. }
  645. DB::commit();
  646. return json_send(['code' => 'success', 'msg' => '导入成功']);
  647. // 成功处理...
  648. } catch (\Exception $e) {
  649. DB::rollBack();
  650. // 错误处理...
  651. return json_send(['code' => 'error', 'msg' => '导入失败', 'data' => $e->getMessage()]);
  652. }
  653. }
  654. /**
  655. * 导入Excel数据必传参数校验
  656. * @author 唐远望
  657. * @version 1.0
  658. * @date 2025-12-31
  659. *
  660. */
  661. private function import_data_check($key, $item)
  662. {
  663. $key = $key + 1;
  664. if (!$item[0]) {
  665. return ['code' => 'error', 'msg' => "第{$key}行商品名称不能为空", 'data' => $item];
  666. }
  667. if (!$item[2]) {
  668. return ['code' => 'error', 'msg' => "第{$key}行商品规格不能为空", 'data' => $item];
  669. }
  670. if (!$item[5]) {
  671. return ['code' => 'error', 'msg' => "第{$key}行指派责任人不能为空", 'data' => $item];
  672. }
  673. }
  674. }