GoodsLogic.php 11 KB

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