ViolationProduct.php 7.7 KB

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