ControlGoods.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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' => $data['store_scope'] !='' ? 2 : 1, //店铺范围1=全部店铺 2=指定店铺
  59. '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'] !='' ? 2 : 1; //店铺范围1=全部店铺 2=指定店铺
  104. $ControlGoods->company_scope = $data['company_scope'] !='' ? 2 : 1; //公司范围1=全部公司 2=指定公司
  105. $ControlGoods->update_time = time();
  106. $ControlGoods->save();
  107. $ControlGoodsCompanyModel->where('control_product_logid', $ControlGoods->id)->delete();
  108. if ($ControlGoods->company_scope == 2) {
  109. $insert_company_data = [];
  110. $company_scope = explode(',', $data['company_scope']);
  111. foreach ($company_scope as $company_id) {
  112. $insert_company_data[] = [
  113. 'control_product_logid' => $ControlGoods->id,
  114. 'company_id' => $company_id,
  115. ];
  116. }
  117. $ControlGoodsCompanyModel->insert($insert_company_data);
  118. }
  119. DB::commit();
  120. return true;
  121. // 成功处理...
  122. } catch (\Exception $e) {
  123. DB::rollBack();
  124. // 错误处理...
  125. return false;
  126. }
  127. }
  128. /**
  129. * 更新数据
  130. * @author 唐远望
  131. * @version 1.0
  132. * @date 2025-12-03
  133. * @param $data
  134. * @return bool
  135. */
  136. public function updateControlGoods($where, $data)
  137. {
  138. DB::beginTransaction();
  139. try {
  140. $this->editControlGoods_content($where, $data);
  141. DB::commit();
  142. return true;
  143. // 成功处理...
  144. } catch (\Exception $e) {
  145. DB::rollBack();
  146. // 错误处理...
  147. return false;
  148. }
  149. }
  150. /**
  151. * 修改状态
  152. * @author 唐远望
  153. * @version 1.0
  154. * @date 2025-12-03
  155. * @param $id
  156. * @param $status
  157. * @return bool
  158. */
  159. public function changeStatus($where, $status)
  160. {
  161. $ControlGoods = $this->where($where)->first();
  162. if (!$ControlGoods) {
  163. return false;
  164. }
  165. $ControlGoods->status = $status;
  166. $ControlGoods->update_time = time();
  167. $ControlGoods->save();
  168. return true;
  169. }
  170. /**
  171. * 删除数据
  172. * @author 唐远望
  173. * @version 1.0
  174. * @date 2025-12-03
  175. * @param $id
  176. * @return bool
  177. */
  178. public function deleteControlGoods($where)
  179. {
  180. $ControlGoods = $this->where($where)->first();
  181. if (!$ControlGoods) {
  182. return false;
  183. }
  184. DB::beginTransaction();
  185. try {
  186. $ControlGoodsCompanyModel = new ControlGoodsCompanyModel();
  187. $company_id_log = $ControlGoodsCompanyModel->where('control_product_logid', $ControlGoods->id)->get();
  188. if (!empty($company_id_log)) {
  189. $ControlGoodsCompanyModel->where('control_product_logid', $ControlGoods->id)->delete();
  190. }
  191. $ControlGoods->delete();
  192. DB::commit();
  193. return true;
  194. // 成功处理...
  195. } catch (\Exception $e) {
  196. DB::rollBack();
  197. // 错误处理...
  198. return false;
  199. }
  200. }
  201. }