EffectiveRulesLogic.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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\effective;
  15. use app\common\model\effective\EffectiveCategory;
  16. use app\common\model\effective\EffectiveRules;
  17. use app\common\logic\BaseLogic;
  18. use think\facade\Db;
  19. /**
  20. * EffectiveRules逻辑
  21. * Class EffectiveRulesLogic
  22. * @package app\adminapi\logic\effective
  23. */
  24. class EffectiveRulesLogic extends BaseLogic
  25. {
  26. /**
  27. * @notes 添加
  28. * @param array $params
  29. * @return bool
  30. * @author likeadmin
  31. * @date 2024/07/17 11:49
  32. */
  33. public static function add(array $params): bool
  34. {
  35. Db::startTrans();
  36. try {
  37. $effectiveRules = EffectiveRules::create([
  38. 'effective_num' => $params['effective_num'],
  39. 'effective_unit' => $params['effective_unit'],
  40. 'remark' => $params['remark'],
  41. ]);
  42. if(!empty($params['goods_category_ids'])){
  43. $effectiveCategoryArr = [];
  44. $ruleId = $effectiveRules->id;
  45. foreach($params['goods_category_ids'] as $v){
  46. $effectiveCategoryArr[] = end($v);
  47. }
  48. $categoryIds = EffectiveCategory::where([['goods_category_id','in',$effectiveCategoryArr],['effective_id','<>',$ruleId]])->column('goods_category_id');
  49. if(!empty($categoryIds)){
  50. throw new \Exception('存在已分配的分类:'.implode(',',$categoryIds));
  51. }
  52. $effectiveRules->effectiveWithCategory()->saveAll($effectiveCategoryArr);
  53. }
  54. Db::commit();
  55. return true;
  56. } catch (\Exception $e) {
  57. Db::rollback();
  58. self::setError($e->getMessage());
  59. return false;
  60. }
  61. }
  62. /**
  63. * @notes 编辑
  64. * @param array $params
  65. * @return bool
  66. * @author likeadmin
  67. * @date 2024/07/17 11:49
  68. */
  69. public static function edit(array $params): bool
  70. {
  71. Db::startTrans();
  72. try {
  73. EffectiveRules::where('id', $params['id'])->update([
  74. 'effective_num' => $params['effective_num'],
  75. 'effective_unit' => $params['effective_unit'],
  76. 'remark' => $params['remark'],
  77. ]);
  78. EffectiveCategory::where('effective_id',$params['id'])->delete();
  79. $effectiveCategoryArr = [];
  80. if(!empty($params['goods_category_ids'])){
  81. foreach($params['goods_category_ids'] as $v){
  82. if(!is_array($v)){
  83. $effectiveCategoryArr[] = $v;
  84. continue;
  85. }
  86. $effectiveCategoryArr[] = end($v);
  87. }
  88. $categoryIds = EffectiveCategory::where([['goods_category_id','in',$effectiveCategoryArr],['effective_id','<>', $params['id']]])->column('goods_category_id');
  89. if(!empty($categoryIds)){
  90. throw new \Exception('存在已分配的分类:'.implode(',',$categoryIds));
  91. }
  92. $effectiveRules = EffectiveRules::find($params['id']);
  93. $effectiveRules->effectiveWithCategory()->saveAll($effectiveCategoryArr);
  94. }
  95. Db::commit();
  96. return true;
  97. } catch (\Exception $e) {
  98. Db::rollback();
  99. self::setError($e->getMessage());
  100. return false;
  101. }
  102. }
  103. /**
  104. * @notes 删除
  105. * @param array $params
  106. * @return bool
  107. * @author likeadmin
  108. * @date 2024/07/17 11:49
  109. */
  110. public static function delete(array $params): bool
  111. {
  112. EffectiveCategory::where('effective_id',$params['id'])->delete();
  113. return EffectiveRules::destroy($params['id']);
  114. }
  115. /**
  116. * @notes 获取详情
  117. * @param $params
  118. * @return array
  119. * @author likeadmin
  120. * @date 2024/07/17 11:49
  121. */
  122. public static function detail($params): array
  123. {
  124. $rules = EffectiveRules::findOrEmpty($params['id'])->toArray();
  125. if(!empty($rules)){
  126. $rules['goods_category_ids'] = EffectiveCategory::where('effective_id',$params['id'])->column('goods_category_id');
  127. }
  128. return $rules;
  129. }
  130. }