GoodsLogic.php 8.7 KB

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