GoodsLogic.php 8.9 KB

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