CouponLists.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop100%开源免费商用商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | gitee下载:https://gitee.com/likeshop_gitee
  10. // | github下载:https://github.com/likeshop-github
  11. // | 访问官网:https://www.likeshop.cn
  12. // | 访问社区:https://home.likeshop.cn
  13. // | 访问手册:http://doc.likeshop.cn
  14. // | 微信公众号:likeshop技术社区
  15. // | likeshop团队 版权所有 拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshopTeam
  18. // +----------------------------------------------------------------------
  19. namespace app\shopapi\lists;
  20. use app\common\enum\CouponEnum;
  21. use app\common\lists\ListsSearchInterface;
  22. use app\common\model\Coupon;
  23. use app\common\model\CouponList;
  24. use app\common\service\TimeService;
  25. class CouponLists extends BaseShopDataLists implements ListsSearchInterface
  26. {
  27. /**
  28. * @notes 优惠券列表搜索条件
  29. * @return array
  30. * @author 张无忌
  31. * @date 2021/7/29 18:13
  32. */
  33. public function setSearch(): array
  34. {
  35. return [];
  36. }
  37. /**
  38. * @notes 获取优惠券列表
  39. * @return array
  40. * @throws @\think\db\exception\DataNotFoundException
  41. * @throws @\think\db\exception\DbException
  42. * @throws @\think\db\exception\ModelNotFoundException
  43. * @author 张无忌
  44. * @date 2021/7/29 18:13
  45. */
  46. public function lists(): array
  47. {
  48. $model = new Coupon();
  49. $hidden = [
  50. 'send_total_type', 'send_total',
  51. 'use_time_type', 'use_time_start', 'use_time_end', 'use_time',
  52. 'get_type', 'get_num_type', 'get_num',
  53. 'use_goods_ids',
  54. 'create_time', 'update_time', 'delete_time',
  55. ];
  56. $lists = $model->field(true)
  57. ->where(['status' => CouponEnum::COUPON_STATUS_CONDUCT])
  58. ->where(['get_type' => CouponEnum::GET_TYPE_USER])
  59. ->append([ 'is_available', 'is_receive', 'condition', 'is_empty', 'use_time_text', 'use_time_text2' ])
  60. ->hidden($hidden)
  61. ->withAttr('is_empty', function ($value, $data) {
  62. // 判断优惠券是否库存已空
  63. unset($value);
  64. if ($data['send_total_type'] == CouponEnum::SEND_TOTAL_TYPE_FIXED) {
  65. if ($data['send_total'] <= 0) {
  66. return 1;
  67. }
  68. $receiveTotal = (new CouponList())->where(['coupon_id'=>intval($data['id'])])->count();
  69. if ($receiveTotal >= $data['send_total']) {
  70. return 1;
  71. }
  72. }
  73. return 0;
  74. })
  75. ->order('id', 'desc')
  76. ->limit($this->limitOffset, $this->limitLength)
  77. ->select()
  78. ->toArray();
  79. foreach ($lists as &$item) {
  80. $item['use_scene'] = CouponEnum::getUseGoodsTypeDesc($item['use_goods_type']);
  81. $item['effective_time'] = $item['use_time_text'];
  82. }
  83. return $lists;
  84. }
  85. /**
  86. * @notes 获取优惠券数量
  87. * @return int
  88. * @author 张无忌
  89. * @date 2021/7/29 18:14
  90. */
  91. public function count(): int
  92. {
  93. $model = new Coupon();
  94. return $model->field(true)
  95. ->where(['status' => CouponEnum::COUPON_STATUS_CONDUCT])
  96. ->count();
  97. }
  98. }