GoodsLogic.php 12 KB

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