ViolationProduct.php 33 KB

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