1
0

GoodsLogic.php 11 KB

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