ViolationProduct.php 6.7 KB

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