GoodsLogic.php 12 KB

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