| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- <?php
- /**
- * @package merchant
- *
- * @author xaboy
- * @day 2020-05-14
- *
- *
- */
- namespace app\common\dao\store\coupon;
- use app\common\dao\BaseDao;
- use app\common\enum\store\StoreCouponUserEnum;
- use app\common\model\BaseModel;
- use app\common\model\store\coupon\StoreCouponUser;
- use app\entity\data\store\StoreCouponUserEntity;
- /**
- * Class StoreCouponUserDao
- * @package app\common\dao\store\coupon
- * @author xaboy
- * @day 2020-05-14
- */
- class StoreCouponUserDao extends BaseDao
- {
- /**
- * @return BaseModel
- * @author xaboy
- * @day 2020-03-30
- */
- protected function getModel(): string
- {
- return StoreCouponUser::class;
- }
- public function search(array $where)
- {
- return StoreCouponUser::when(isset($where['username']) && $where['username'] != '', function ($query) use ($where) {
- $query->hasWhere('user', [['nickname', 'LIKE', "%{$where['username']}%"]]);
- })->when(true, function ($query) use ($where) {
- $query->hasWhere('user', [['uid', '>', 0]]);
- })->alias('StoreCouponUser')->when(isset($where['coupon_title']) && $where['coupon_title'] !== '', function ($query) use ($where) {
- $query->whereLike('StoreCouponUser.coupon_title', "%{$where['coupon_title']}%");
- })->when(isset($where['status']) && $where['status'] != '', function ($query) use ($where) {
- $query->where('StoreCouponUser.status', $where['status']);
- })->when(isset($where['uid']) && $where['uid'] != '', function ($query) use ($where) {
- $query->where('StoreCouponUser.uid', $where['uid']);
- })->when(isset($where['mer_id']) && $where['mer_id'] != '', function ($query) use ($where) {
- $query->where('StoreCouponUser.mer_id', $where['mer_id']);
- })->when(isset($where['coupon_id']) && $where['coupon_id'] != '', function ($query) use ($where) {
- $query->where('StoreCouponUser.coupon_id', $where['coupon_id']);
- })->order('StoreCouponUser.create_time desc');
- }
- public function validIntersection($merId, $uid, array $ids): array
- {
- $time = date('Y-m-d H:i:s');
- return StoreCouponUser::getDB()->whereIn('coupon_user_id', $ids)->where('start_time', '<', $time)->where('end_time', '>', $time)
- ->where('is_fail', 0)->where('status', 0)->where('mer_id', $merId)->where('uid', $uid)->column('coupon_user_id');
- }
- public function validQuery()
- {
- $time = date('Y-m-d H:i:s');
- return StoreCouponUser::getDB()->where('start_time', '<', $time)->where('end_time', '>', $time)->where('is_fail', 0)->where('status', 0);
- }
- public function failCoupon()
- {
- $time = date('Y-m-d H:i:s');
- return StoreCouponUser::getDB()->where('end_time', '<', $time)->where('is_fail', 0)->where('status', 0)->update(['status' => 2]);
- }
- public function userTotal($uid)
- {
- return $this->validQuery()->where('uid', $uid)->count();
- }
- public function usedNum($couponId)
- {
- return StoreCouponUser::getDB()->where('coupon_id', $couponId)->where('status', 1)->count();
- }
- public function sendNum($couponId)
- {
- return StoreCouponUser::getDB()->where('coupon_id', $couponId)->count();
- }
- /**
- * @param int $merId
- * @param int $uid
- * @return StoreCouponUserEntity[]
- */
- public function getValidStoreCouponUserEntityListByMerIdAndUid(int $merId, int $uid): array
- {
- $entityList = [];
- try {
- $dateTime = date('Y-m-d H:i:s');
- /** @var StoreCouponUser $model */
- $model = $this->getModel();
- $dataListRes = $model::getDB()
- ->where('mer_id', '=', $merId)
- ->where('uid', '=', $uid)
- ->where('status', '=', StoreCouponUserEnum::STATUS['Unused']['code'])
- ->where('is_fail', '=', StoreCouponUserEnum::IS_FAIL['No']['code'])
- ->where('start_time', '<=', $dateTime)
- ->where('end_time', '>=', $dateTime)
- ->select()
- ->toArray();
- if (!empty($dataListRes)) {
- foreach ($dataListRes as $data) {
- $entityList[] = StoreCouponUserEntity::newInstance($data);
- }
- }
- } catch (\Exception $e) {
- }
- return $entityList;
- }
- }
|