GoodsLogic.php 11 KB

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