GoodsLogic.php 9.9 KB

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