GoodsLogic.php 10 KB

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