GoodsLogic.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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\goods;
  15. use app\common\model\goods\Goods;
  16. use app\common\logic\BaseLogic;
  17. use app\common\model\goods_category\GoodsCategory;
  18. use app\common\model\performance\PerformanceRules;
  19. use think\Exception;
  20. use think\facade\Db;
  21. /**
  22. * Goods逻辑
  23. * Class GoodsLogic
  24. * @package app\adminapi\logic\goods
  25. */
  26. class GoodsLogic extends BaseLogic
  27. {
  28. /**
  29. * @notes 添加
  30. * @param array $params
  31. * @return bool
  32. * @author likeadmin
  33. * @date 2024/07/07 18:37
  34. */
  35. public static function add(array $params): bool
  36. {
  37. Db::startTrans();
  38. try {
  39. $params['goods_category_id'] = end($params['goods_category_ids']);
  40. Goods::create([
  41. 'goods_category_ids' => $params['goods_category_ids'],
  42. 'category_type' => GoodsCategory::where('id',$params['goods_category_id'])->value('category_type'),
  43. 'goods_category_id' => $params['goods_category_id'],
  44. 'goods_name' => $params['goods_name'],
  45. 'goods_image' => $params['goods_image'],
  46. 'goods_video' => $params['goods_video'],
  47. 'goods_banners' => $params['goods_banners'],
  48. 'goods_number' => $params['goods_number'],
  49. 'good_unit' => $params['good_unit'],
  50. 'goods_size' => $params['goods_size'],
  51. 'goods_type' => $params['goods_type'],
  52. 'goods_brand' => $params['goods_brand'],
  53. 'install_guide' => $params['install_guide'],
  54. 'goods_payment_type'=>$params['goods_payment_type'],
  55. 'base_service_fee' => $params['base_service_fee'],
  56. 'service_total' => $params['service_total'],
  57. 'service_fee' => $params['service_fee'],
  58. 'service_image' => $params['service_image'],
  59. 'warranty_period'=>$params['warranty_period'],
  60. 'fee_schedule' => $params['fee_schedule'],
  61. 'goods_status' => 0,
  62. 'is_recommend' => $params['is_recommend'] ?:0,
  63. 'recommend_weight' => $params['recommend_weight'] ?:0,
  64. 'is_top' => $params['is_top'] ?:0,
  65. 'top_weight' => $params['top_weight'] ?:0,
  66. 'is_hot' => $params['is_hot'] ?:0,
  67. 'hot_weight' => $params['hot_weight'] ?:0,
  68. ]);
  69. Db::commit();
  70. return true;
  71. } catch (\Exception $e) {
  72. Db::rollback();
  73. self::setError($e->getMessage());
  74. return false;
  75. }
  76. }
  77. /**
  78. * @notes 编辑
  79. * @param array $params
  80. * @return bool
  81. * @author likeadmin
  82. * @date 2024/07/07 18:37
  83. */
  84. public static function edit(array $params): bool
  85. {
  86. Db::startTrans();
  87. try {
  88. if((!$params['type'] || !$params['rate']) && $params['goods_status']==1){
  89. throw new Exception('商品规格的绩效未配置,不允许上架');
  90. }
  91. $params['goods_category_id'] = end($params['goods_category_ids']);
  92. Goods::where('id', $params['id'])->update([
  93. 'category_type' => GoodsCategory::where('id',$params['goods_category_id'])->value('category_type'),
  94. 'goods_category_ids' => json_encode($params['goods_category_ids'],true),
  95. 'goods_category_id' => $params['goods_category_id'],
  96. 'goods_name' => $params['goods_name'],
  97. 'goods_image' => $params['goods_image'],
  98. 'goods_video' => $params['goods_video'],
  99. 'goods_banners' => !empty($params['goods_banners']) ? json_encode($params['goods_banners'],true) : null,
  100. 'goods_number' => $params['goods_number'],
  101. 'good_unit' => $params['good_unit'],
  102. 'goods_size' => $params['goods_size'],
  103. 'goods_type' => $params['goods_type'],
  104. 'goods_brand' => $params['goods_brand'],
  105. 'install_guide' => $params['install_guide'],
  106. 'goods_payment_type'=>$params['goods_payment_type'],
  107. 'base_service_fee' => $params['base_service_fee'],
  108. 'service_total' => $params['service_total'],
  109. 'service_fee' => $params['service_fee'],
  110. 'service_image' => $params['service_image'],
  111. 'warranty_period'=>$params['warranty_period'],
  112. 'fee_schedule' => $params['fee_schedule'],
  113. 'goods_status' => $params['goods_status'],
  114. 'is_recommend' => $params['is_recommend'] ?:0,
  115. 'recommend_weight' => $params['recommend_weight'] ?:0,
  116. 'is_top' => $params['is_top'] ?:0,
  117. 'top_weight' => $params['top_weight'] ?:0,
  118. 'is_hot' => $params['is_hot'] ?:0,
  119. 'hot_weight' => $params['hot_weight'] ?:0,
  120. ]);
  121. //更新绩效规则
  122. if(in_array($params['type'],[0,1,2])){
  123. // 0-1
  124. if($params['rate']>1){
  125. throw new Exception('商品绩效比率不能大于1');
  126. }
  127. }
  128. if(in_array($params['type'],[3])){
  129. // ≥1
  130. if($params['rate']<1){
  131. throw new Exception('金额不能小于1');
  132. }
  133. }
  134. $rule = PerformanceRules::where(['goods_id'=>$params['id']])->findOrEmpty();
  135. if(!$rule->isEmpty()){
  136. $rule->type = $params['type'];
  137. $rule->rate = $params['rate'];
  138. }else{
  139. $rule = new PerformanceRules();
  140. $rule->goods_id = $params['id'];
  141. $rule->type = $params['type'];
  142. $rule->rate = $params['rate'];
  143. }
  144. $rule->save();
  145. Db::commit();
  146. return true;
  147. } catch (\Exception $e) {
  148. Db::rollback();
  149. self::setError($e->getMessage());
  150. return false;
  151. }
  152. }
  153. /**
  154. * @notes 删除
  155. * @param array $params
  156. * @return bool
  157. * @author likeadmin
  158. * @date 2024/07/07 18:37
  159. */
  160. public static function delete(array $params): bool
  161. {
  162. return Goods::destroy($params['id']);
  163. }
  164. /**
  165. * @notes 获取详情
  166. * @param $params
  167. * @return array
  168. */
  169. public static function detail($params): array
  170. {
  171. return Goods::with('performanceRules')->findOrEmpty($params['id'])->toArray();
  172. }
  173. /**
  174. * @notes 批量规则配置
  175. * @param $params
  176. * @return array
  177. */
  178. public static function rulesBatch($params): bool
  179. {
  180. Db::startTrans();
  181. try {
  182. if(in_array($params['type'],[0,1,2])){
  183. // 0-1
  184. if($params['rate']>1){
  185. throw new Exception('商品绩效比率不能大于1');
  186. }
  187. }
  188. if(in_array($params['type'],[3])){
  189. // ≥1
  190. if($params['rate']<1){
  191. throw new Exception('金额不能小于1');
  192. }
  193. }
  194. foreach ($params['ids'] as $id){
  195. $rule = null;
  196. //更新绩效规则
  197. $rule = PerformanceRules::where(['goods_id'=>$id])->findOrEmpty();
  198. if(!$rule->isEmpty()){
  199. $rule->type = $params['type'];
  200. $rule->rate = $params['rate'];
  201. }else{
  202. $rule = new PerformanceRules();
  203. $rule->goods_id = $id;
  204. $rule->type = $params['type'];
  205. $rule->rate = $params['rate'];
  206. }
  207. $rule->save();
  208. }
  209. Db::commit();
  210. return true;
  211. } catch (\Exception $e) {
  212. Db::rollback();
  213. self::setError($e->getMessage());
  214. return false;
  215. }
  216. }
  217. }