ControlGoods.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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\ControlGoodsCompany as ControlGoodsCompanyModel;
  7. /**
  8. * 清洗配置-强管控商品模型
  9. * @author: 唐远望
  10. * @version: 1.0
  11. * @date: 2025-12-03
  12. */
  13. class ControlGoods extends Model
  14. {
  15. use HasFactory;
  16. // 与模型关联的表名
  17. protected $table = 'washconfig_control_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 addControlGoods_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. $ControlGoods_id = $this->insertGetId($insert_data);
  39. return $ControlGoods_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 addControlGoods($data)
  50. {
  51. DB::beginTransaction();
  52. try {
  53. $ControlGoodsCompanyModel = new ControlGoodsCompanyModel();
  54. $insert_data = [
  55. 'platform' => $data['platform'],
  56. 'product_name' => $data['product_name'],
  57. 'product_specs' => $data['product_specs'],
  58. 'store_scope' => isset($data['store_scope']) && $data['store_scope'] !='' ? 2 : 1, //店铺范围1=全部店铺 2=指定店铺
  59. 'company_scope' => isset($data['company_scope']) && $data['company_scope'] !='' ? 2 : 1, //公司范围1=全部公司 2=指定公司
  60. 'insert_time' => time(),
  61. ];
  62. $ControlGoods_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. 'control_product_logid' => $ControlGoods_id,
  69. 'company_id' => $company_id,
  70. ];
  71. }
  72. $ControlGoodsCompanyModel->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 editControlGoods_content($where, $data)
  92. {
  93. $ControlGoods = $this->where($where)->first();
  94. if (!$ControlGoods) {
  95. return false;
  96. }
  97. DB::beginTransaction();
  98. try {
  99. $ControlGoodsCompanyModel = new ControlGoodsCompanyModel();
  100. $ControlGoods->platform = $data['platform'];
  101. $ControlGoods->product_name = $data['product_name'];
  102. $ControlGoods->product_specs = $data['product_specs'];
  103. $ControlGoods->store_scope = $data['store_scope'];
  104. $ControlGoods->update_time = time();
  105. $ControlGoods->save();
  106. $ControlGoodsCompanyModel->where('control_product_logid', $ControlGoods->id)->delete();
  107. if ($data['company_scope'] == 2) {
  108. $insert_company_data = [];
  109. $company_scope = explode(',', $data['company_scope']);
  110. foreach ($company_scope as $company_id) {
  111. $insert_company_data[] = [
  112. 'control_product_logid' => $ControlGoods->id,
  113. 'company_id' => $company_id,
  114. ];
  115. }
  116. $ControlGoodsCompanyModel->insert($insert_company_data);
  117. }
  118. DB::commit();
  119. return true;
  120. // 成功处理...
  121. } catch (\Exception $e) {
  122. DB::rollBack();
  123. // 错误处理...
  124. return false;
  125. }
  126. }
  127. /**
  128. * 更新数据
  129. * @author 唐远望
  130. * @version 1.0
  131. * @date 2025-12-03
  132. * @param $data
  133. * @return bool
  134. */
  135. public function updateControlGoods($where, $data)
  136. {
  137. DB::beginTransaction();
  138. try {
  139. $this->editControlGoods_content($where, $data);
  140. DB::commit();
  141. return true;
  142. // 成功处理...
  143. } catch (\Exception $e) {
  144. DB::rollBack();
  145. // 错误处理...
  146. return false;
  147. }
  148. }
  149. /**
  150. * 修改状态
  151. * @author 唐远望
  152. * @version 1.0
  153. * @date 2025-12-03
  154. * @param $id
  155. * @param $status
  156. * @return bool
  157. */
  158. public function changeStatus($where, $status)
  159. {
  160. $ControlGoods = $this->where($where)->first();
  161. if (!$ControlGoods) {
  162. return false;
  163. }
  164. $ControlGoods->status = $status;
  165. $ControlGoods->update_time = time();
  166. $ControlGoods->save();
  167. return true;
  168. }
  169. /**
  170. * 删除数据
  171. * @author 唐远望
  172. * @version 1.0
  173. * @date 2025-12-03
  174. * @param $id
  175. * @return bool
  176. */
  177. public function deleteControlGoods($where)
  178. {
  179. $ControlGoods = $this->where($where)->first();
  180. if (!$ControlGoods) {
  181. return false;
  182. }
  183. $ControlGoods->delete();
  184. return true;
  185. }
  186. }