ViolationProduct.php 6.8 KB

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