ControlGoods.php 7.1 KB

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