ViolationProduct.php 12 KB

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