GoodsLogic.php 9.7 KB

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