ControlGoods.php 6.7 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\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. 'category_id' => $data['category_id'],
  61. 'insert_time' => time(),
  62. ];
  63. $ControlGoods_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. 'control_product_logid' => $ControlGoods_id,
  70. 'company_id' => $company_id,
  71. ];
  72. }
  73. $ControlGoodsCompanyModel->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 editControlGoods_content($where, $data)
  93. {
  94. $ControlGoods = $this->where($where)->first();
  95. if (!$ControlGoods) {
  96. return false;
  97. }
  98. DB::beginTransaction();
  99. try {
  100. $ControlGoodsCompanyModel = new ControlGoodsCompanyModel();
  101. $store_scope = $data['store_scope'] !='' ? 2 : 1; //店铺范围1=全部店铺 2=指定店铺
  102. $company_scope = $data['company_scope'] !='' ? 2 : 1; //公司范围1=全部公司 2=指定公司
  103. $ControlGoods->platform = $data['platform'];
  104. $ControlGoods->product_name = $data['product_name'];
  105. $ControlGoods->product_specs = $data['product_specs'];
  106. $ControlGoods->store_scope = $store_scope;
  107. $ControlGoods->company_scope = $company_scope;
  108. $ControlGoods->category_id = $data['category_id'];
  109. $ControlGoods->update_time = time();
  110. $ControlGoods->save();
  111. $ControlGoodsCompanyModel->where('control_product_logid', $ControlGoods->id)->delete();
  112. if ($ControlGoods->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. 'control_product_logid' => $ControlGoods->id,
  118. 'company_id' => $company_id,
  119. ];
  120. }
  121. $ControlGoodsCompanyModel->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 updateControlGoods($where, $data)
  141. {
  142. DB::beginTransaction();
  143. try {
  144. $this->editControlGoods_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. $ControlGoods = $this->where($where)->first();
  166. if (!$ControlGoods) {
  167. return false;
  168. }
  169. $ControlGoods->status = $status;
  170. $ControlGoods->update_time = time();
  171. $ControlGoods->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 deleteControlGoods($where)
  183. {
  184. $ControlGoods = $this->where($where)->first();
  185. if (!$ControlGoods) {
  186. return false;
  187. }
  188. DB::beginTransaction();
  189. try {
  190. $ControlGoodsCompanyModel = new ControlGoodsCompanyModel();
  191. $company_id_log = $ControlGoodsCompanyModel->where('control_product_logid', $ControlGoods->id)->get();
  192. if (!empty($company_id_log)) {
  193. $ControlGoodsCompanyModel->where('control_product_logid', $ControlGoods->id)->delete();
  194. }
  195. $ControlGoods->delete();
  196. DB::commit();
  197. return true;
  198. // 成功处理...
  199. } catch (\Exception $e) {
  200. DB::rollBack();
  201. // 错误处理...
  202. return false;
  203. }
  204. }
  205. }