GoodsLogic.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. 'is_agent' => $params['is_agent'] ?:0,
  69. 'property_activity_id' => $params['property_activity_id'] ??0
  70. ]);
  71. Db::commit();
  72. return true;
  73. } catch (\Exception $e) {
  74. Db::rollback();
  75. self::setError($e->getMessage());
  76. return false;
  77. }
  78. }
  79. /**
  80. * @notes 编辑
  81. * @param array $params
  82. * @return bool
  83. * @author likeadmin
  84. * @date 2024/07/07 18:37
  85. */
  86. public static function edit(array $params): bool
  87. {
  88. Db::startTrans();
  89. try {
  90. if((!$params['type'] || !$params['rate']) && $params['goods_status']==1){
  91. throw new Exception('商品规格的绩效未配置,不允许上架');
  92. }
  93. $params['goods_category_id'] = end($params['goods_category_ids']);
  94. Goods::where('id', $params['id'])->update([
  95. 'category_type' => GoodsCategory::where('id',$params['goods_category_id'])->value('category_type'),
  96. 'goods_category_ids' => json_encode($params['goods_category_ids'],true),
  97. 'goods_category_id' => $params['goods_category_id'],
  98. 'goods_name' => $params['goods_name'],
  99. 'goods_image' => $params['goods_image'],
  100. 'goods_video' => $params['goods_video'],
  101. 'goods_banners' => !empty($params['goods_banners']) ? json_encode($params['goods_banners'],true) : null,
  102. 'goods_number' => $params['goods_number'],
  103. 'good_unit' => $params['good_unit'],
  104. 'goods_size' => $params['goods_size'],
  105. 'goods_type' => $params['goods_type'],
  106. 'goods_brand' => $params['goods_brand'],
  107. 'install_guide' => $params['install_guide'],
  108. 'goods_payment_type'=>$params['goods_payment_type'],
  109. 'base_service_fee' => $params['base_service_fee'],
  110. 'service_total' => $params['service_total'],
  111. 'service_fee' => $params['service_fee'],
  112. 'service_image' => $params['service_image'],
  113. 'warranty_period'=>$params['warranty_period'],
  114. 'fee_schedule' => $params['fee_schedule'],
  115. 'goods_status' => $params['goods_status'],
  116. 'is_recommend' => $params['is_recommend'] ?:0,
  117. 'recommend_weight' => $params['recommend_weight'] ?:0,
  118. 'is_top' => $params['is_top'] ?:0,
  119. 'top_weight' => $params['top_weight'] ?:0,
  120. 'is_hot' => $params['is_hot'] ?:0,
  121. 'hot_weight' => $params['hot_weight'] ?:0,
  122. 'is_agent' => $params['is_agent'] ?:0,
  123. 'property_activity_id' => $params['property_activity_id'] ??0
  124. ]);
  125. //更新绩效规则
  126. if(in_array($params['type'],[0,1,2])){
  127. // 0-1
  128. if($params['rate']>1){
  129. throw new Exception('商品绩效比率不能大于1');
  130. }
  131. }
  132. if(in_array($params['type'],[3])){
  133. // ≥1
  134. if($params['rate']<1){
  135. throw new Exception('金额不能小于1');
  136. }
  137. }
  138. $rule = PerformanceRules::where(['goods_id'=>$params['id']])->findOrEmpty();
  139. if(!$rule->isEmpty()){
  140. $rule->type = $params['type'];
  141. $rule->rate = $params['rate'];
  142. }else{
  143. $rule = new PerformanceRules();
  144. $rule->goods_id = $params['id'];
  145. $rule->type = $params['type'];
  146. $rule->rate = $params['rate'];
  147. }
  148. $rule->save();
  149. Db::commit();
  150. return true;
  151. } catch (\Exception $e) {
  152. Db::rollback();
  153. self::setError($e->getMessage());
  154. return false;
  155. }
  156. }
  157. /**
  158. * @notes 删除
  159. * @param array $params
  160. * @return bool
  161. * @author likeadmin
  162. * @date 2024/07/07 18:37
  163. */
  164. public static function delete(array $params): bool
  165. {
  166. return Goods::destroy($params['id']);
  167. }
  168. /**
  169. * @notes 获取详情
  170. * @param $params
  171. * @return array
  172. */
  173. public static function detail($params): array
  174. {
  175. return Goods::with('performanceRules')->findOrEmpty($params['id'])->toArray();
  176. }
  177. /**
  178. * @notes 批量规则配置
  179. * @param $params
  180. * @return array
  181. */
  182. public static function rulesBatch($params): bool
  183. {
  184. Db::startTrans();
  185. try {
  186. if(in_array($params['type'],[0,1,2])){
  187. // 0-1
  188. if($params['rate']>1){
  189. throw new Exception('商品绩效比率不能大于1');
  190. }
  191. }
  192. if(in_array($params['type'],[3])){
  193. // ≥1
  194. if($params['rate']<1){
  195. throw new Exception('金额不能小于1');
  196. }
  197. }
  198. foreach ($params['ids'] as $id){
  199. $rule = null;
  200. //更新绩效规则
  201. $rule = PerformanceRules::where(['goods_id'=>$id])->findOrEmpty();
  202. if(!$rule->isEmpty()){
  203. $rule->type = $params['type'];
  204. $rule->rate = $params['rate'];
  205. }else{
  206. $rule = new PerformanceRules();
  207. $rule->goods_id = $id;
  208. $rule->type = $params['type'];
  209. $rule->rate = $params['rate'];
  210. }
  211. $rule->save();
  212. }
  213. Db::commit();
  214. return true;
  215. } catch (\Exception $e) {
  216. Db::rollback();
  217. self::setError($e->getMessage());
  218. return false;
  219. }
  220. }
  221. }