ViolationProduct.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. /**
  8. * 清洗配置-违规商品
  9. * @author: 唐远望
  10. * @version: 1.0
  11. * @date: 2025-12-03
  12. */
  13. class ViolationProduct extends Model
  14. {
  15. use HasFactory;
  16. // 与模型关联的表名
  17. protected $table = 'washconfig_violation_product';
  18. // 是否主动维护时间戳
  19. public $timestamps = false;
  20. // 定义时间戳字段名
  21. // const CREATED_AT = 'insert_time';
  22. // const UPDATED_AT = 'update_time';
  23. /**
  24. * 添加
  25. * @author 唐远望
  26. * @version 1.0
  27. * @date 2025-12-03
  28. */
  29. public function addViolationProduct_content($data)
  30. {
  31. $insert_data = [
  32. 'platform' => $data['platform'],
  33. 'product_name' => $data['product_name'],
  34. 'product_specs' => $data['product_specs'],
  35. 'store_scope' => $data['store_scope'] != '' ? 2 : 1, //店铺范围1=全部店铺 2=指定店铺
  36. 'company_scope' => $data['company_scope'] != '' ? 2 : 1, //公司范围1=全部公司 2=指定公司
  37. 'category_id' => $data['category_id'],
  38. 'specify_responsible_person' => $data['specify_responsible_person'],
  39. 'insert_time' => time(),
  40. ];
  41. $ViolationProduct_id = $this->insertGetId($insert_data);
  42. return $ViolationProduct_id;
  43. }
  44. /**
  45. * 写入数据
  46. * @author 唐远望
  47. * @version 1.0
  48. * @date 2025-12-03
  49. * @param $data
  50. * @return bool
  51. */
  52. public function addViolationProduct($data)
  53. {
  54. DB::beginTransaction();
  55. try {
  56. $ViolationProductCompanyModel = new ViolationProductCompanyModel();
  57. $insert_data = [
  58. 'platform' => $data['platform'],
  59. 'product_name' => $data['product_name'],
  60. 'product_specs' => $data['product_specs'],
  61. 'store_scope' => $data['store_scope'] != '' ? 2 : 1, //店铺范围1=全部店铺 2=指定店铺
  62. 'company_scope' => $data['company_scope'] != '' ? 2 : 1, //公司范围1=全部公司 2=指定公司
  63. 'category_id' => $data['category_id'],
  64. 'specify_responsible_person' => $data['specify_responsible_person'],
  65. 'insert_time' => time(),
  66. ];
  67. $ViolationProduct_id = $this->insertGetId($insert_data);
  68. if ($insert_data['company_scope'] == 2) {
  69. $insert_company_data = [];
  70. $company_scope = explode(',', $data['company_scope']);
  71. foreach ($company_scope as $company_id) {
  72. $insert_company_data[] = [
  73. 'violation_product_logid' => $ViolationProduct_id,
  74. 'company_id' => $company_id,
  75. ];
  76. }
  77. $ViolationProductCompanyModel->insert($insert_company_data);
  78. }
  79. DB::commit();
  80. return true;
  81. // 成功处理...
  82. } catch (\Exception $e) {
  83. DB::rollBack();
  84. // 错误处理...
  85. return false;
  86. }
  87. }
  88. /**
  89. * 编辑内容
  90. * @author 唐远望
  91. * @version 1.0
  92. * @date 2025-12-03
  93. * @param $data
  94. * @return bool
  95. */
  96. public function editViolationProduct_content($ViolationProduct, $data)
  97. {
  98. DB::beginTransaction();
  99. try {
  100. $ViolationProductCompanyModel = new ViolationProductCompanyModel();
  101. $store_scope = $data['store_scope'] != '' ? 2 : 1; //店铺范围1=全部店铺 2=指定店铺
  102. $company_scope = $data['company_scope'] != '' ? 2 : 1; //公司范围1=全部公司 2=指定公司
  103. $ViolationProduct->platform = $data['platform'];
  104. $ViolationProduct->product_name = $data['product_name'];
  105. $ViolationProduct->product_specs = $data['product_specs'];
  106. $ViolationProduct->store_scope = $store_scope;
  107. $ViolationProduct->company_scope = $company_scope;
  108. $ViolationProduct->category_id = $data['category_id'];
  109. $ViolationProduct->specify_responsible_person = $data['specify_responsible_person'];
  110. $ViolationProduct->update_time = time();
  111. $ViolationProduct->save();
  112. $ViolationProductCompanyModel->where('violation_product_logid', $ViolationProduct->id)->delete();
  113. if ($company_scope == 2) {
  114. $insert_company_data = [];
  115. $company_scope = explode(',', $data['company_scope']);
  116. foreach ($company_scope as $company_id) {
  117. $insert_company_data[] = [
  118. 'violation_product_logid' => $ViolationProduct->id,
  119. 'company_id' => $company_id,
  120. ];
  121. }
  122. $ViolationProductCompanyModel->insert($insert_company_data);
  123. }
  124. DB::commit();
  125. return true;
  126. // 成功处理...
  127. } catch (\Exception $e) {
  128. DB::rollBack();
  129. // 错误处理...
  130. return false;
  131. }
  132. }
  133. /**
  134. * 更新数据
  135. * @author 唐远望
  136. * @version 1.0
  137. * @date 2025-12-03
  138. * @param $data
  139. * @return bool
  140. */
  141. public function updateViolationProduct($where, $data)
  142. {
  143. DB::beginTransaction();
  144. try {
  145. $this->editViolationProduct_content($where, $data);
  146. DB::commit();
  147. return true;
  148. // 成功处理...
  149. } catch (\Exception $e) {
  150. DB::rollBack();
  151. // 错误处理...
  152. return false;
  153. }
  154. }
  155. /**
  156. * 修改状态
  157. * @author 唐远望
  158. * @version 1.0
  159. * @date 2025-12-03
  160. * @param $id
  161. * @param $status
  162. * @return bool
  163. */
  164. public function changeStatus($ViolationProduct, $status)
  165. {
  166. $ViolationProduct->status = $status;
  167. $ViolationProduct->update_time = time();
  168. $ViolationProduct->save();
  169. return true;
  170. }
  171. /**
  172. * 删除数据
  173. * @author 唐远望
  174. * @version 1.0
  175. * @date 2025-12-03
  176. * @param $id
  177. * @return bool
  178. */
  179. public function deleteViolationProduct($where)
  180. {
  181. $ViolationProduct = $this->where($where)->first();
  182. if (!$ViolationProduct) {
  183. return false;
  184. }
  185. DB::beginTransaction();
  186. try {
  187. $ViolationProductCompanyModel = new ViolationProductCompanyModel();
  188. $company_id_log = $ViolationProductCompanyModel->where('violation_product_logid', $ViolationProduct->id)->get();
  189. if (!empty($company_id_log)) {
  190. $ViolationProductCompanyModel->where('violation_product_logid', $ViolationProduct->id)->delete();
  191. }
  192. $ViolationProduct->delete();
  193. DB::commit();
  194. return true;
  195. // 成功处理...
  196. } catch (\Exception $e) {
  197. DB::rollBack();
  198. // 错误处理...
  199. return false;
  200. }
  201. }
  202. }