GoodsLogic.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. use think\facade\Log;
  22. /**
  23. * Goods逻辑
  24. * Class GoodsLogic
  25. * @package app\adminapi\logic\goods
  26. */
  27. class GoodsLogic extends BaseLogic
  28. {
  29. /**
  30. * @notes 添加
  31. * @param array $params
  32. * @return bool
  33. * @author likeadmin
  34. * @date 2024/07/07 18:37
  35. */
  36. public static function add(array $params): bool
  37. {
  38. Db::startTrans();
  39. try {
  40. if((!$params['type'] || !$params['rate']) && $params['goods_status']==1){
  41. throw new Exception('商品规格的绩效未配置,不允许上架');
  42. }
  43. if(!in_array($params['type'],[1,3])) throw new Exception('商品绩效应为固定提点或工单结算金额');
  44. $params['goods_category_id'] = end($params['goods_category_ids']);
  45. if ($params['is_type'] == 1) {
  46. $params['is_agent'] = 1;
  47. $params['is_activity'] = 0;
  48. } else if ($params['is_type'] == 2) {
  49. $params['is_agent'] = 0;
  50. $params['is_activity'] = 1;
  51. } else {
  52. $params['is_agent'] = 0;
  53. $params['is_activity'] = 0;
  54. }
  55. $goods = Goods::create([
  56. 'goods_category_ids' => $params['goods_category_ids'],
  57. 'category_type' => GoodsCategory::where('id',$params['goods_category_id'])->value('category_type'),
  58. 'goods_category_id' => $params['goods_category_id'],
  59. 'goods_name' => $params['goods_name'],
  60. 'goods_image' => $params['goods_image'],
  61. 'goods_video' => $params['goods_video'],
  62. 'goods_banners' => $params['goods_banners'],
  63. 'goods_number' => $params['goods_number'],
  64. 'good_unit' => $params['good_unit'],
  65. 'goods_size' => $params['goods_size'],
  66. 'goods_type' => $params['goods_type'],
  67. 'goods_brand' => $params['goods_brand'],
  68. 'install_guide' => $params['install_guide'],
  69. 'goods_payment_type'=>$params['goods_payment_type'],
  70. 'base_service_fee' => $params['base_service_fee'],
  71. 'service_total' => $params['service_total'],
  72. 'service_fee' => $params['service_fee'],
  73. 'service_image' => $params['service_image'],
  74. 'warranty_period'=>$params['warranty_period'],
  75. 'fee_schedule' => $params['fee_schedule'],
  76. 'goods_status' => 0,
  77. 'is_recommend' => $params['is_recommend'] ?:0,
  78. 'recommend_weight' => $params['recommend_weight'] ?:0,
  79. 'is_top' => $params['is_top'] ?:0,
  80. 'top_weight' => $params['top_weight'] ?:0,
  81. 'is_hot' => $params['is_hot'] ?:0,
  82. 'hot_weight' => $params['hot_weight'] ?:0,
  83. 'is_agent' => $params['is_agent'] ?:0,
  84. 'is_activity' => $params['is_activity'] ??0,
  85. 'property_activity_id' => $params['property_activity_id'] ??0,
  86. 'labels' => (isset($params['labels']) && $params['labels'])?implode(',',$params['labels']):'',
  87. 'activity_service_fee' => $params['activity_service_fee'] ??'',
  88. 'sell_num'=>!empty($params['sell_num'])?$params['sell_num']:0,
  89. 'platform_value'=>$params['platform_value']??0,
  90. ]);
  91. //更新绩效规则
  92. if(in_array($params['type'],[0,1,2])){
  93. // 0-1
  94. if($params['rate']>1){
  95. throw new Exception('商品绩效比率不能大于1');
  96. }
  97. }
  98. if(in_array($params['type'],[3])){
  99. // ≥1
  100. if($params['rate']<1){
  101. throw new Exception('金额不能小于1');
  102. }
  103. }
  104. $rule = PerformanceRules::where(['goods_id'=>$goods->id])->findOrEmpty();
  105. if(!$rule->isEmpty()){
  106. $rule->type = $params['type'];
  107. $rule->rate = $params['rate'];
  108. }else{
  109. $rule = new PerformanceRules();
  110. $rule->goods_id = $goods->id;
  111. $rule->type = $params['type'];
  112. $rule->rate = $params['rate'];
  113. }
  114. $rule->save();
  115. Db::commit();
  116. return true;
  117. } catch (\Exception $e) {
  118. Db::rollback();
  119. self::setError($e->getMessage());
  120. return false;
  121. }
  122. }
  123. /**
  124. * @notes 编辑
  125. * @param array $params
  126. * @return bool
  127. * @author likeadmin
  128. * @date 2024/07/07 18:37
  129. */
  130. public static function edit(array $params): bool
  131. {
  132. Db::startTrans();
  133. try {
  134. if((!$params['type'] || !$params['rate']) && $params['goods_status']==1){
  135. throw new Exception('商品规格的绩效未配置,不允许上架');
  136. }
  137. if(!in_array($params['type'],[1,3])) throw new Exception('商品绩效应为固定提点或工单结算金额');
  138. $params['goods_category_id'] = end($params['goods_category_ids']);
  139. if ($params['is_type'] == 1) {
  140. $params['is_agent'] = 1;
  141. $params['is_activity'] = 0;
  142. } else if ($params['is_type'] == 2) {
  143. $params['is_agent'] = 0;
  144. $params['is_activity'] = 1;
  145. } else {
  146. $params['is_agent'] = 0;
  147. $params['is_activity'] = 0;
  148. }
  149. Goods::where('id', $params['id'])->update([
  150. 'category_type' => GoodsCategory::where('id',$params['goods_category_id'])->value('category_type'),
  151. 'goods_category_ids' => json_encode($params['goods_category_ids'],true),
  152. 'goods_category_id' => $params['goods_category_id'],
  153. 'goods_name' => $params['goods_name'],
  154. 'goods_image' => $params['goods_image'],
  155. 'goods_video' => $params['goods_video'],
  156. 'goods_banners' => !empty($params['goods_banners']) ? json_encode($params['goods_banners'],true) : null,
  157. 'goods_number' => $params['goods_number'],
  158. 'good_unit' => $params['good_unit'],
  159. 'goods_size' => $params['goods_size'],
  160. 'goods_type' => $params['goods_type'],
  161. 'goods_brand' => $params['goods_brand'],
  162. 'install_guide' => $params['install_guide'],
  163. 'goods_payment_type'=>$params['goods_payment_type'],
  164. 'base_service_fee' => $params['base_service_fee'],
  165. 'service_total' => $params['service_total'],
  166. 'service_fee' => $params['service_fee'],
  167. 'service_image' => $params['service_image'],
  168. 'warranty_period'=>$params['warranty_period'],
  169. 'fee_schedule' => $params['fee_schedule'],
  170. 'goods_status' => $params['goods_status'],
  171. 'is_recommend' => $params['is_recommend'] ?:0,
  172. 'recommend_weight' => $params['recommend_weight'] ?:0,
  173. 'is_top' => $params['is_top'] ?:0,
  174. 'top_weight' => $params['top_weight'] ?:0,
  175. 'is_hot' => $params['is_hot'] ?:0,
  176. 'hot_weight' => $params['hot_weight'] ?:0,
  177. 'is_agent' => $params['is_agent'] ?:0,
  178. 'is_activity' => $params['is_activity'] ??0,
  179. 'property_activity_id' => $params['property_activity_id'] ??0,
  180. 'labels' => (isset($params['labels']) && $params['labels'])?implode(',',$params['labels']):'',
  181. 'activity_service_fee' => $params['activity_service_fee'] ??'',
  182. 'sell_num'=>!empty($params['sell_num'])?$params['sell_num']:0,
  183. 'platform_value'=>$params['platform_value']??0,
  184. ]);
  185. //更新绩效规则
  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. $rule = PerformanceRules::where(['goods_id'=>$params['id']])->findOrEmpty();
  199. if(!$rule->isEmpty()){
  200. $rule->type = $params['type'];
  201. $rule->rate = $params['rate'];
  202. }else{
  203. $rule = new PerformanceRules();
  204. $rule->goods_id = $params['id'];
  205. $rule->type = $params['type'];
  206. $rule->rate = $params['rate'];
  207. }
  208. $rule->save();
  209. Db::commit();
  210. return true;
  211. } catch (\Exception $e) {
  212. Db::rollback();
  213. self::setError($e->getMessage());
  214. return false;
  215. }
  216. }
  217. /**
  218. * @notes 删除
  219. * @param array $params
  220. * @return bool
  221. * @author likeadmin
  222. * @date 2024/07/07 18:37
  223. */
  224. public static function delete(array $params): bool
  225. {
  226. return Goods::destroy($params['id']);
  227. }
  228. /**
  229. * @notes 获取详情
  230. * @param $params
  231. * @return array
  232. */
  233. public static function detail($params): array
  234. {
  235. return Goods::with('performanceRules')->append(['is_type'])->findOrEmpty($params['id'])->toArray();
  236. }
  237. /**
  238. * @notes 批量规则配置
  239. * @param $params
  240. * @return array
  241. */
  242. public static function rulesBatch($params): bool
  243. {
  244. Db::startTrans();
  245. try {
  246. if(!in_array($params['type'],[1,3])) throw new Exception('商品绩效应为固定提点或工单结算金额');
  247. if(in_array($params['type'],[0,1,2])){
  248. // 0-1
  249. if($params['rate']>1){
  250. throw new Exception('商品绩效比率不能大于1');
  251. }
  252. }
  253. if(in_array($params['type'],[3])){
  254. // ≥1
  255. if($params['rate']<1){
  256. throw new Exception('金额不能小于1');
  257. }
  258. }
  259. foreach ($params['ids'] as $id){
  260. $rule = null;
  261. //更新绩效规则
  262. $rule = PerformanceRules::where(['goods_id'=>$id])->findOrEmpty();
  263. if(!$rule->isEmpty()){
  264. $rule->type = $params['type'];
  265. $rule->rate = $params['rate'];
  266. }else{
  267. $rule = new PerformanceRules();
  268. $rule->goods_id = $id;
  269. $rule->type = $params['type'];
  270. $rule->rate = $params['rate'];
  271. }
  272. $rule->save();
  273. }
  274. Db::commit();
  275. return true;
  276. } catch (\Exception $e) {
  277. Db::rollback();
  278. self::setError($e->getMessage());
  279. return false;
  280. }
  281. }
  282. }