ViolationProduct.php 7.0 KB

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