ViolationProduct.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. 'company_id' => $data['company_id'],
  59. 'platform' => $data['platform'],
  60. 'product_name' => $data['product_name'],
  61. 'product_specs' => $data['product_specs'],
  62. 'store_scope' => $data['store_scope'] != '' ? 2 : 1, //店铺范围1=全部店铺 2=指定店铺
  63. 'company_scope' => $data['company_scope'] != '' ? 2 : 1, //公司范围1=全部公司 2=指定公司
  64. 'category_id' => $data['category_id'],
  65. 'specify_responsible_person' => $data['specify_responsible_person'],
  66. 'insert_time' => time(),
  67. ];
  68. $ViolationProduct_id = $this->insertGetId($insert_data);
  69. if ($insert_data['company_scope'] == 2) {
  70. $insert_company_data = [];
  71. $company_scope = explode(',', $data['company_scope']);
  72. foreach ($company_scope as $company_id) {
  73. $insert_company_data[] = [
  74. 'violation_product_logid' => $ViolationProduct_id,
  75. 'company_id' => $company_id,
  76. ];
  77. }
  78. $ViolationProductCompanyModel->insert($insert_company_data);
  79. }
  80. DB::commit();
  81. return true;
  82. // 成功处理...
  83. } catch (\Exception $e) {
  84. DB::rollBack();
  85. // 错误处理...
  86. return false;
  87. }
  88. }
  89. /**
  90. * 编辑内容
  91. * @author 唐远望
  92. * @version 1.0
  93. * @date 2025-12-03
  94. * @param $data
  95. * @return bool
  96. */
  97. public function editViolationProduct_content($ViolationProduct, $data)
  98. {
  99. DB::beginTransaction();
  100. try {
  101. $ViolationProductCompanyModel = new ViolationProductCompanyModel();
  102. $store_scope = $data['store_scope'] != '' ? 2 : 1; //店铺范围1=全部店铺 2=指定店铺
  103. $company_scope = $data['company_scope'] != '' ? 2 : 1; //公司范围1=全部公司 2=指定公司
  104. $ViolationProduct->company_id = $data['company_id'];
  105. $ViolationProduct->platform = $data['platform'];
  106. $ViolationProduct->product_name = $data['product_name'];
  107. $ViolationProduct->product_specs = $data['product_specs'];
  108. $ViolationProduct->store_scope = $store_scope;
  109. $ViolationProduct->company_scope = $company_scope;
  110. $ViolationProduct->category_id = $data['category_id'];
  111. $ViolationProduct->specify_responsible_person = $data['specify_responsible_person'];
  112. $ViolationProduct->update_time = time();
  113. $ViolationProduct->save();
  114. $ViolationProductCompanyModel->where('violation_product_logid', $ViolationProduct->id)->delete();
  115. if ($company_scope == 2) {
  116. $insert_company_data = [];
  117. $company_scope = explode(',', $data['company_scope']);
  118. foreach ($company_scope as $company_id) {
  119. $insert_company_data[] = [
  120. 'violation_product_logid' => $ViolationProduct->id,
  121. 'company_id' => $company_id,
  122. ];
  123. }
  124. $ViolationProductCompanyModel->insert($insert_company_data);
  125. }
  126. DB::commit();
  127. return true;
  128. // 成功处理...
  129. } catch (\Exception $e) {
  130. DB::rollBack();
  131. // 错误处理...
  132. return false;
  133. }
  134. }
  135. /**
  136. * 更新数据
  137. * @author 唐远望
  138. * @version 1.0
  139. * @date 2025-12-03
  140. * @param $data
  141. * @return bool
  142. */
  143. public function updateViolationProduct($where, $data)
  144. {
  145. DB::beginTransaction();
  146. try {
  147. $this->editViolationProduct_content($where, $data);
  148. DB::commit();
  149. return true;
  150. // 成功处理...
  151. } catch (\Exception $e) {
  152. DB::rollBack();
  153. // 错误处理...
  154. return false;
  155. }
  156. }
  157. /**
  158. * 修改状态
  159. * @author 唐远望
  160. * @version 1.0
  161. * @date 2025-12-03
  162. * @param $id
  163. * @param $status
  164. * @return bool
  165. */
  166. public function changeStatus($ViolationProduct, $status)
  167. {
  168. $ViolationProduct->status = $status;
  169. $ViolationProduct->update_time = time();
  170. $ViolationProduct->save();
  171. return true;
  172. }
  173. /**
  174. * 删除数据
  175. * @author 唐远望
  176. * @version 1.0
  177. * @date 2025-12-03
  178. * @param $id
  179. * @return bool
  180. */
  181. public function deleteViolationProduct($where)
  182. {
  183. $ViolationProduct = $this->where($where)->first();
  184. if (!$ViolationProduct) {
  185. return false;
  186. }
  187. DB::beginTransaction();
  188. try {
  189. $ViolationProductCompanyModel = new ViolationProductCompanyModel();
  190. $company_id_log = $ViolationProductCompanyModel->where('violation_product_logid', $ViolationProduct->id)->get();
  191. if (!empty($company_id_log)) {
  192. $ViolationProductCompanyModel->where('violation_product_logid', $ViolationProduct->id)->delete();
  193. }
  194. $ViolationProduct->delete();
  195. DB::commit();
  196. return true;
  197. // 成功处理...
  198. } catch (\Exception $e) {
  199. DB::rollBack();
  200. // 错误处理...
  201. return false;
  202. }
  203. }
  204. }