| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- <?php
- namespace app\api\logic;
- use app\common\logic\BaseLogic;
- use app\common\model\coupon\CouponCategory;
- use app\common\model\coupon\CouponRules;
- use app\common\model\coupon\UserCoupon;
- use think\db\Query;
- use think\db\Where;
- use think\facade\Db;
- /**
- * @author 林海涛
- * @date 2024/7/18 下午4:49
- */
- class UserCouponLogic extends BaseLogic
- {
- public static function add($params)
- {
- Db::startTrans();
- try{
- $errMsgArr = [];
- $rules = CouponRules::whereIn('code',$params['codes'])->where('voucher_status',1)->lock(true)->select();
- $userCoupon = UserCoupon::where('user_id',$params['user_id'])
- ->whereIn('code',$params['codes'])
- ->where('voucher_count','>',0)
- ->select();
- $updateData = [];
- $createData = [];
- foreach($rules as $v) {
- if($v->voucher_status == 2){
- $errMsgArr[] = $v->event_name .'已设置为过期';
- continue;
- }
- if($v->remaining_count <= 0 ){
- $errMsgArr[] = $v->event_name .'数量不足';
- }
- if($userCoupon->where('coupon_id',$v->id)->count()){
- $errMsgArr[] = $v->event_name . '已领取';
- continue;
- }
- $updateData[$v->id] = ['remaining_count' => --$v->remaining_count];
- $createData[] = [
- 'user_id'=>$params['user_id'],
- 'code' => $v->code,
- 'voucher_status' => 0,
- 'voucher_count' =>1,
- 'amount' => $v->mold_type != 1 ? $v->amount:null,
- 'amount_require' => $v->amount_require,
- 'begin_use' => time(),
- 'discount_ratio' => $v->mold_type == 1 ? $v->discount_ratio:null,
- 'event_name' => $v->event_name,
- 'expire_time' => time()+$v->expire_time,
- 'max_deductible_price' => $v->mold_type == 1 ? $v->max_deductible_price:null,
- 'mold_type' => $v->mold_type,
- 'server_category_name' => $v->server_category_name
- ];
- }
- CouponRules::updateWhenCase($updateData);
- if(!empty($createData)){
- (new UserCoupon())->saveAll($createData);
- }
- Db::commit();
- return $errMsgArr;
- }catch(\Exception $e){
- Db::rollback();
- self::setError($e->getMessage());
- return false;
- }
- }
- /**
- * 我的优惠卷列表
- * @param $params
- * @return array|false
- */
- public static function userCouponList($params)
- {
- try{
- $where = [];
- if(isset($params['voucher_status']) && is_array($params['voucher_status'])){
- $where[] = ['voucher_status','IN',$params['voucher_status']];
- }
- $where[] = ['user_id','=',$params['user_id']];
- $data = UserCoupon::with(['couponRules'=>function(Query $query){
- $query->field(['id','code']);
- $query->with(['couponWithCategory'=>function(Query $query){
- $query->field('id,name');
- }]);
- }])
- ->where($where)
- ->where(function(Query $query){
- $query->where(' expire_time >'. (time()-86400*7));
- })
- ->field(['id', 'user_id', 'code','begin_use','expire_time', 'voucher_status','voucher_count'])
- ->append(['voucher_status_text'])
- ->order(['id' => 'desc'])
- ->select()
- ->toArray();
- $couponCodes = [];
- foreach($data as $k => $v){
- if($v['couponRules']){
- $v['couponRules']['goods_category_ids'] = array_column($v['couponRules']['couponWithCategory'],'id');
- $data[$k] = $v;
- }
- if($v['expire_time'] < time()){
- $couponCodes[] = $v['code'];
- $data[$k]['voucher_status_text'] = '已过期';
- }
- $data[$k]['begin_use'] = date("Y-m-d H:i:s",$v['begin_use'] );
- $data[$k]['expire_time'] = date("Y-m-d H:i:s",$v['expire_time'] );
- }
- if(!empty($couponCodes)){
- UserCoupon::whereIn('code',$couponCodes)->update(['voucher_status'=>2]);
- }
- return $data;
- } catch (\Exception $e){
- Db::rollback();
- self::setError($e->getMessage());
- return false;
- }
- }
- /**
- * 商品分类优惠卷领取
- * @param $params
- * @return array|false
- */
- public static function categoryCouponList($params)
- {
- try{
- $data = CouponRules::alias('cr')
- ->leftJoin('coupon_category cc','cc.coupon_id = cr.id')
- ->where('cc.goods_category_id',$params['goods_category_id'])
- ->where('cr.remaining_count','>',0)
- ->where('cr.voucher_status',1)
- ->order(['cr.id' => 'desc'])
- ->field(['cr.id','cr.code','cr.amount','cr.amount_require','cr.discount_ratio','cr.server_category_name',
- 'cr.event_name','cr.expire_time','cr.max_deductible_price','cr.mold_type',])
- ->select();
- $codes = $data->column('code');
- $userCouponCodes = [];
- if(!empty($code)){
- $userCouponCodes = UserCoupon::where('code',$codes)
- ->where('user_id',$params['user_id'])->column('code');
- }
- $data = $data->whereNotIn('code',$userCouponCodes)->toArray();
- return $data;
- }catch(\Exception $e){
- self::setError($e->getMessage());
- return false;
- }
- }
- }
|