ShopGoodsLogic.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeadmin快速开发前后端分离管理后台(PHP版)
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
  8. // | github下载:https://github.com/likeshop-github/likeadmin
  9. // | 访问官网:https://www.likeadmin.cn
  10. // | likeadmin团队 版权所有 拥有最终解释权
  11. // +----------------------------------------------------------------------
  12. // | author: likeadminTeam
  13. // +----------------------------------------------------------------------
  14. namespace app\adminapi\logic\shops;
  15. use app\common\model\shops\ShopGoods;
  16. use app\common\logic\BaseLogic;
  17. use app\common\model\shops\ShopGoodSpecsInventory;
  18. use think\facade\Db;
  19. /**
  20. * ShopGoods逻辑
  21. * Class ShopGoodsLogic
  22. * @package app\adminapi\logic\shops
  23. */
  24. class ShopGoodsLogic extends BaseLogic
  25. {
  26. /**
  27. * @notes 添加
  28. * @param array $params
  29. * @return bool
  30. * @author likeadmin
  31. * @date 2024/08/04 11:07
  32. */
  33. public static function add(array $params): bool
  34. {
  35. Db::startTrans();
  36. try {
  37. if(isset($params['custom_attribute_items']) && !empty($params['custom_attribute_items'])){
  38. foreach($params['custom_attribute_items'] as &$item){
  39. if(intval($item['img_type']) === 0){
  40. foreach($item['spec_items'] as $k => $v){
  41. unset($item['spec_items'][$k]['spec_img']);
  42. }
  43. }
  44. }
  45. }
  46. $params['goods_category_id'] = end($params['goods_category_ids']);
  47. $model = ShopGoods::create([
  48. 'delivery_type' => $params['delivery_type'],
  49. 'shop_goods_type' => $params['shop_goods_type'],
  50. 'goods_category_ids' => $params['goods_category_ids'],
  51. 'goods_category_id' => $params['goods_category_id'],
  52. 'goods_name' => $params['goods_name'],
  53. 'company_name' => $params['company_name'],
  54. 'good_unit' => $params['good_unit'],
  55. 'goods_image' => $params['goods_image'],
  56. 'goods_banners' => !empty( $params['goods_banners']) ? $params['goods_banners'] : null,
  57. 'description' => $params['description'],
  58. 'goods_status' => $params['goods_status'],
  59. 'is_recommend' => $params['is_recommend'],
  60. 'recommend_weight' => $params['recommend_weight'],
  61. 'specs_type' => $params['specs_type'] ?? 1,
  62. 'custom_attribute_items' => $params['specs_type'] ==3 ? $params['custom_attribute_items'] : null,
  63. ]);
  64. if(!empty($params['goodSpecsInventory'])){
  65. $data = [];
  66. foreach ($params['goodSpecsInventory'] as $item){
  67. $val = [];
  68. $val['specs'] = $item['specs'];
  69. $val['inventory'] = $item['inventory'];
  70. $val['remaining_inventory'] = $item['remaining_inventory'];
  71. $val['service_total'] = $item['service_total'];
  72. $val['service_fee'] = $item['service_fee'];
  73. $val['shop_goods_id'] = $model->id;
  74. $data[] = $val;
  75. }
  76. $model->goodSpecsInventory()->saveAll($data);
  77. }
  78. Db::commit();
  79. return true;
  80. } catch (\Exception $e) {
  81. Db::rollback();
  82. self::setError($e->getMessage());
  83. return false;
  84. }
  85. }
  86. /**
  87. * @notes 编辑
  88. * @param array $params
  89. * @return bool
  90. * @author likeadmin
  91. * @date 2024/08/04 11:07
  92. */
  93. public static function edit(array $params): bool
  94. {
  95. Db::startTrans();
  96. try {
  97. if(isset($params['custom_attribute_items']) && !empty($params['custom_attribute_items'])){
  98. foreach($params['custom_attribute_items'] as &$item){
  99. if(intval($item['img_type']) === 0){
  100. foreach($item['spec_items'] as $k => $v){
  101. unset($item['spec_items'][$k]['spec_img']);
  102. }
  103. }
  104. }
  105. }
  106. ShopGoods::where('id', $params['id'])->update([
  107. 'delivery_type' => $params['delivery_type'],
  108. 'shop_goods_type' => $params['shop_goods_type'],
  109. 'goods_category_ids' => $params['goods_category_ids'],
  110. 'goods_category_id' => $params['goods_category_id'],
  111. 'goods_name' => $params['goods_name'],
  112. 'company_name' => $params['company_name'],
  113. 'goods_image' => $params['goods_image'],
  114. 'goods_banners' => !empty($params['goods_banners']) ? json_encode($params['goods_banners'],true) : null,
  115. 'description' => $params['description'],
  116. 'goods_status' => $params['goods_status'],
  117. 'is_recommend' => $params['is_recommend'],
  118. 'recommend_weight' => $params['recommend_weight'],
  119. 'specs_type' => $params['specs_type'] ?? 1,
  120. 'custom_attribute_items' => $params['specs_type']==3 ? json_encode($params['custom_attribute_items'],true) : null,
  121. ]);
  122. $ids = ShopGoodSpecsInventory::where('shop_goods_id',$params['id'])->column('id');
  123. $saveIds = [];
  124. if(!empty($ids)){
  125. $saveIds = array_reverse(array_slice($ids,0,count($params['goodSpecsInventory'])));
  126. $deleteIds = array_slice($ids,count($params['goodSpecsInventory']));
  127. ShopGoodSpecsInventory::destroy($deleteIds);
  128. }
  129. $model = ShopGoods::find($params['id']);
  130. $data = [];
  131. foreach($params['goodSpecsInventory'] as $item){
  132. $val = [];
  133. if(!empty($saveIds)){
  134. $id = array_pop($saveIds);
  135. $val['id'] = $id;
  136. }
  137. $val['specs'] = $item['specs'];
  138. $val['inventory'] = $item['inventory'];
  139. $val['remaining_inventory'] = $item['remaining_inventory'];
  140. $val['service_total'] = $item['service_total'];
  141. $val['service_fee'] = $item['service_fee'];
  142. $val['shop_goods_id'] = $model->id;
  143. $data[] = $val;
  144. }
  145. $model->goodSpecsInventory()->saveAll($data);
  146. Db::commit();
  147. return true;
  148. } catch (\Exception $e) {
  149. Db::rollback();
  150. self::setError($e->getMessage());
  151. return false;
  152. }
  153. }
  154. /**
  155. * @notes 删除
  156. * @param array $params
  157. * @return bool
  158. * @author likeadmin
  159. * @date 2024/08/04 11:07
  160. */
  161. public static function delete(array $params): bool
  162. {
  163. ShopGoodSpecsInventory::where('shop_goods_id',$params['id'])->delete();
  164. return ShopGoods::destroy($params['id']);
  165. }
  166. /**
  167. * @notes 获取详情
  168. * @param $params
  169. * @return array
  170. * @author likeadmin
  171. * @date 2024/08/04 11:07
  172. */
  173. public static function detail($params): array
  174. {
  175. return ShopGoods::findOrEmpty($params['id'])->toArray();
  176. }
  177. }