EffectiveRulesLists.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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\lists\effective;
  15. use app\adminapi\lists\BaseAdminDataLists;
  16. use app\common\model\effective\EffectiveRules;
  17. use app\common\lists\ListsSearchInterface;
  18. use think\db\Query;
  19. /**
  20. * EffectiveRules列表
  21. * Class EffectiveRulesLists
  22. * @package app\adminapi\listseffective
  23. */
  24. class EffectiveRulesLists extends BaseAdminDataLists implements ListsSearchInterface
  25. {
  26. /**
  27. * @notes 设置搜索条件
  28. * @return \string[][]
  29. * @author likeadmin
  30. * @date 2024/07/17 11:49
  31. */
  32. public function setSearch(): array
  33. {
  34. return [
  35. '=' => ['effective_num', 'effective_unit', 'remark'],
  36. ];
  37. }
  38. public function queryWhere()
  39. {
  40. $where = [];
  41. if(isset($this->params['goods_category_ids']) && !empty($this->params['goods_category_ids'])){
  42. $categoryIds =[];
  43. foreach($this->params['goods_category_ids'] as $v){
  44. $v = json_decode($v,true)?? (array)$v;
  45. $categoryIds[] = end($v);
  46. }
  47. $ids = EffectiveRules::alias('er')
  48. ->join('effective_category ec','er.id = ec.effective_id')
  49. ->whereIn('ec.goods_category_id',$categoryIds)->distinct(true)->field('ec.effective_id')->column('ec.effective_id');
  50. if(empty($ids)){
  51. $ids = [0];
  52. }
  53. $where[] = ['id','IN',$ids];
  54. }
  55. return $where;
  56. }
  57. /**
  58. * @notes 获取列表
  59. * @return array
  60. * @throws \think\db\exception\DataNotFoundException
  61. * @throws \think\db\exception\DbException
  62. * @throws \think\db\exception\ModelNotFoundException
  63. * @author likeadmin
  64. * @date 2024/07/17 11:49
  65. */
  66. public function lists(): array
  67. {
  68. $data = EffectiveRules::with(['effectiveWithCategory'=>function(Query $query){
  69. $query->field('id,name');
  70. }])
  71. ->where($this->searchWhere)
  72. ->where($this->queryWhere())
  73. ->field(['id', 'effective_num', 'effective_unit', 'remark'])
  74. ->limit($this->limitOffset, $this->limitLength)
  75. ->order(['id' => 'desc'])
  76. ->select()
  77. ->toArray();
  78. foreach($data as $k => $v){
  79. $v['goods_category_ids'] = array_column($v['effectiveWithCategory'],'id');
  80. $data[$k] = $v;
  81. }
  82. return $data;
  83. }
  84. /**
  85. * @notes 获取数量
  86. * @return int
  87. * @author likeadmin
  88. * @date 2024/07/17 11:49
  89. */
  90. public function count(): int
  91. {
  92. return EffectiveRules::where($this->searchWhere)->where($this->queryWhere())->count();
  93. }
  94. }