ViolationProduct.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. <?php
  2. namespace App\Models\Manager\WashConfig;
  3. use Illuminate\Database\Eloquent\Factories\HasFactory;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Support\Facades\DB;
  6. use App\Models\Manager\WashConfig\ViolationProductCompany as ViolationProductCompanyModel;
  7. use App\Models\Manager\WashConfig\ViolationProductPlatform as ViolationProductPlatformModel;
  8. use App\Models\Manager\WashConfig\ViolationProductKeyword as ViolationProductKeywordModel;
  9. /**
  10. * 清洗配置-违规商品
  11. * @author: 唐远望
  12. * @version: 1.0
  13. * @date: 2025-12-03
  14. */
  15. class ViolationProduct extends Model
  16. {
  17. use HasFactory;
  18. // 与模型关联的表名
  19. protected $table = 'washconfig_violation_product';
  20. // 是否主动维护时间戳
  21. public $timestamps = false;
  22. // 定义时间戳字段名
  23. // const CREATED_AT = 'insert_time';
  24. // const UPDATED_AT = 'update_time';
  25. /**
  26. * 添加
  27. * @author 唐远望
  28. * @version 1.0
  29. * @date 2025-12-03
  30. */
  31. public function addViolationProduct_content($data)
  32. {
  33. $insert_data = [
  34. 'company_id' => $data['company_id'],
  35. 'platform' => $data['platform'],
  36. 'product_brand' => $data['product_brand'],
  37. 'product_name' => $data['product_name'],
  38. 'product_specs' => $data['product_specs'],
  39. 'store_scope' => $data['store_scope'] != '' ? 2 : 1, //店铺范围1=全部店铺 2=指定店铺
  40. 'company_scope' => $data['company_scope'] != '' ? 2 : 1, //公司范围1=全部公司 2=指定公司
  41. 'category_id' => $data['category_id'],
  42. 'specify_responsible_person' => $data['specify_responsible_person'],
  43. 'product_brand' => $data['product_brand'],
  44. 'insert_time' => time(),
  45. ];
  46. $ViolationProduct_id = $this->insertGetId($insert_data);
  47. return $ViolationProduct_id;
  48. }
  49. /**
  50. * 写入数据
  51. * @author 唐远望
  52. * @version 1.0
  53. * @date 2025-12-03
  54. * @param $data
  55. * @return bool
  56. */
  57. public function addViolationProduct($data)
  58. {
  59. DB::beginTransaction();
  60. try {
  61. $insert_data = [
  62. 'company_id' => $data['company_id'],
  63. 'platform' => $data['platform'],
  64. 'product_name' => $data['product_name'],
  65. 'product_specs' => $data['product_specs'],
  66. 'store_scope' => $data['store_scope'] != '' ? 2 : 1, //店铺范围1=全部店铺 2=指定店铺
  67. 'company_scope' => $data['company_scope'] != '' ? 2 : 1, //公司范围1=全部公司 2=指定公司
  68. 'category_id' => $data['category_id'],
  69. 'specify_responsible_person' => $data['specify_responsible_person'],
  70. 'insert_time' => time(),
  71. ];
  72. $ViolationProduct_id = $this->insertGetId($insert_data);
  73. if ($insert_data['company_scope'] == 2) {
  74. $ViolationProductCompanyModel = new ViolationProductCompanyModel();
  75. $insert_company_data = [];
  76. $company_scope = explode(',', $data['company_scope']);
  77. foreach ($company_scope as $company_id) {
  78. $insert_company_data[] = [
  79. 'violation_product_logid' => $ViolationProduct_id,
  80. 'company_id' => $company_id,
  81. ];
  82. }
  83. $ViolationProductCompanyModel->insert($insert_company_data);
  84. }
  85. if ($data['platform'] != 0) {
  86. $ViolationProductPlatformModel = new ViolationProductPlatformModel();
  87. $insert_platform_data = [];
  88. $platform_scope = explode(',', $data['platform']);
  89. foreach ($platform_scope as $platform_id) {
  90. $insert_platform_data[] = [
  91. 'company_id' => $data['company_id'],
  92. 'product_id' => $ViolationProduct_id,
  93. 'platform_id' => $platform_id,
  94. ];
  95. }
  96. $ViolationProductPlatformModel->insert($insert_platform_data);
  97. }
  98. // 如果存在采集配置的关键字
  99. $product_keyword = $data['product_keyword'];
  100. if (trim($product_keyword) != '') {
  101. $ProductKeywordModel = new ViolationProductKeywordModel();
  102. $product_keyword_data = explode(',', $product_keyword);
  103. $keyword_data = [];
  104. foreach ($product_keyword_data as $key => $value) {
  105. if (trim($value) == '') continue;
  106. $keyword_data[] = [
  107. 'violation_product_id' => $ViolationProduct_id,
  108. 'keyword' => $value,
  109. ];
  110. }
  111. $ProductKeywordModel->insert($keyword_data);
  112. }
  113. DB::commit();
  114. return true;
  115. // 成功处理...
  116. } catch (\Exception $e) {
  117. DB::rollBack();
  118. // 错误处理...
  119. return false;
  120. }
  121. }
  122. /**
  123. * 编辑内容
  124. * @author 唐远望
  125. * @version 1.0
  126. * @date 2025-12-03
  127. * @param $data
  128. * @return bool
  129. */
  130. public function editViolationProduct_content($ViolationProduct, $data)
  131. {
  132. DB::beginTransaction();
  133. try {
  134. $ViolationProductCompanyModel = new ViolationProductCompanyModel();
  135. $ViolationProductPlatformModel = new ViolationProductPlatformModel();
  136. $store_scope = $data['store_scope'] != '' ? 2 : 1; //店铺范围1=全部店铺 2=指定店铺
  137. $company_scope = $data['company_scope'] != '' ? 2 : 1; //公司范围1=全部公司 2=指定公司
  138. $ViolationProduct->company_id = $data['company_id'];
  139. $ViolationProduct->platform = $data['platform'];
  140. $ViolationProduct->product_brand = $data['product_brand'];
  141. $ViolationProduct->product_name = $data['product_name'];
  142. $ViolationProduct->product_specs = $data['product_specs'];
  143. $ViolationProduct->store_scope = $store_scope;
  144. $ViolationProduct->company_scope = $company_scope;
  145. $ViolationProduct->category_id = $data['category_id'];
  146. $ViolationProduct->specify_responsible_person = $data['specify_responsible_person'];
  147. $ViolationProduct->update_time = time();
  148. $ViolationProduct->save();
  149. $violation_product_companycount = $ViolationProductCompanyModel->where('violation_product_logid', $ViolationProduct->id)->count();
  150. if ($violation_product_companycount > 0) {
  151. $ViolationProductCompanyModel->where('violation_product_logid', $ViolationProduct->id)->delete();
  152. }
  153. if ($company_scope == 2) {
  154. $insert_company_data = [];
  155. $company_scope = explode(',', $data['company_scope']);
  156. foreach ($company_scope as $company_id) {
  157. $insert_company_data[] = [
  158. 'violation_product_logid' => $ViolationProduct->id,
  159. 'company_id' => $company_id,
  160. ];
  161. }
  162. $ViolationProductCompanyModel->insert($insert_company_data);
  163. }
  164. $violation_product_platformcount = $ViolationProductPlatformModel->where('product_id', $ViolationProduct->id)->count();
  165. if ($violation_product_platformcount > 0) {
  166. $ViolationProductPlatformModel->where('product_id', $ViolationProduct->id)->delete();
  167. }
  168. if ($data['platform'] != 0) {
  169. $insert_platform_data = [];
  170. $platform_scope = explode(',', $data['platform']);
  171. foreach ($platform_scope as $platform_id) {
  172. $insert_platform_data[] = [
  173. 'company_id' => $data['company_id'],
  174. 'product_id' => $ViolationProduct->id,
  175. 'platform_id' => $platform_id,
  176. ];
  177. }
  178. $ViolationProductPlatformModel->insert($insert_platform_data);
  179. }
  180. // 如果存在采集配置的关键字
  181. $product_keyword = $data['product_keyword'];
  182. if (trim($product_keyword) != '') {
  183. $ProductKeywordModel = new ViolationProductKeywordModel();
  184. //查询是否配置了关键字,如果有则删除重新添加
  185. $keyword_count = $ProductKeywordModel->where('violation_product_id', $ViolationProduct->id)->count();
  186. if ($keyword_count > 0) {
  187. $ProductKeywordModel->where('violation_product_id', $ViolationProduct->id)->delete();
  188. }
  189. $product_keyword_data = explode(',', $product_keyword);
  190. $keyword_data = [];
  191. foreach ($product_keyword_data as $key => $value) {
  192. if (trim($value) == '') continue;
  193. $keyword_data[] = [
  194. 'violation_product_id' => $ViolationProduct->id,
  195. 'keyword' => $value,
  196. ];
  197. }
  198. $ProductKeywordModel->insert($keyword_data);
  199. }
  200. DB::commit();
  201. return true;
  202. // 成功处理...
  203. } catch (\Exception $e) {
  204. DB::rollBack();
  205. // 错误处理...
  206. return false;
  207. }
  208. }
  209. /**
  210. * 更新数据
  211. * @author 唐远望
  212. * @version 1.0
  213. * @date 2025-12-03
  214. * @param $data
  215. * @return bool
  216. */
  217. public function updateViolationProduct($where, $data)
  218. {
  219. DB::beginTransaction();
  220. try {
  221. $this->editViolationProduct_content($where, $data);
  222. DB::commit();
  223. return true;
  224. // 成功处理...
  225. } catch (\Exception $e) {
  226. DB::rollBack();
  227. // 错误处理...
  228. return false;
  229. }
  230. }
  231. /**
  232. * 修改状态
  233. * @author 唐远望
  234. * @version 1.0
  235. * @date 2025-12-03
  236. * @param $id
  237. * @param $status
  238. * @return bool
  239. */
  240. public function changeStatus($ViolationProduct, $status)
  241. {
  242. $ViolationProduct->status = $status;
  243. $ViolationProduct->update_time = time();
  244. $ViolationProduct->save();
  245. return true;
  246. }
  247. /**
  248. * 删除数据
  249. * @author 唐远望
  250. * @version 1.0
  251. * @date 2025-12-03
  252. * @param $id
  253. * @return bool
  254. */
  255. public function deleteViolationProduct($where)
  256. {
  257. $ViolationProduct = $this->where($where)->first();
  258. if (!$ViolationProduct) {
  259. return false;
  260. }
  261. DB::beginTransaction();
  262. try {
  263. $ViolationProductCompanyModel = new ViolationProductCompanyModel();
  264. $company_id_log = $ViolationProductCompanyModel->where('violation_product_logid', $ViolationProduct->id)->get();
  265. if (!empty($company_id_log)) {
  266. $ViolationProductCompanyModel->where('violation_product_logid', $ViolationProduct->id)->delete();
  267. }
  268. $ViolationProduct->delete();
  269. DB::commit();
  270. return true;
  271. // 成功处理...
  272. } catch (\Exception $e) {
  273. DB::rollBack();
  274. // 错误处理...
  275. return false;
  276. }
  277. }
  278. /**
  279. * 平台定义
  280. * @author 唐远望
  281. * @version 1.0
  282. * @date 2025-12-31
  283. */
  284. public function platform_data()
  285. {
  286. $platform_data = [
  287. '全部' => '0',
  288. '淘宝' => '1',
  289. '京东' => '2',
  290. '拼多多' => '3',
  291. '美团' => '4',
  292. '药师帮' => '5',
  293. '1药城' => '6',
  294. '药九九' => '7',
  295. '药易购' => '8',
  296. '药帮忙' => '9',
  297. '熊猫药药' => '10'
  298. ];
  299. return $platform_data;
  300. }
  301. }