| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- <?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\Exception;
- 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();
- if($rules->isEmpty()){
- $errMsgArr[] = '优惠券不存在';
- }
- $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 .'数量不足';
- continue;
- }
- 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'],
- 'coupon_id' => $v->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
- ];
- }
- if(!empty($updateData)){
- 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::where($where)
- ->where(function(Query $query){
- $query->where(' expire_time >'. (time()-86400*7));
- })
- ->field(['id','code','amount','amount_require','begin_use','voucher_status','discount_ratio','event_name','expire_time','max_deductible_price','server_category_name','mold_type'])
- ->append(['voucher_status_text','discount_ratio_text'])
- ->order(['id' => 'desc'])
- ->select()
- ->toArray();
- $couponCodes = [];
- foreach($data as $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'] );
- $data[$k]['amount_require'] = '满'.$v['amount_require'].'可用';
- }
- 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 categoryCouponLists($params)
- {
- try{
- $coupon_ids = CouponCategory::where('goods_category_id',$params['goods_category_id'])->column('coupon_id');
- $data = CouponRules::where('remaining_count','>',0)
- ->whereIn('id',$coupon_ids)
- ->where('voucher_status',1)
- ->append(['discount_ratio_text'])
- ->order(['id' => 'desc'])
- ->field(['code','amount','amount_require','discount_ratio','server_category_name',
- 'event_name','expire_time','max_deductible_price','mold_type',])
- ->select();
- foreach($data as $k => $v){
- $data[$k]['amount_require'] = '满'.$v['amount_require'].'可用';
- }
- $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;
- }
- }
- public static function categoryWithAmountLists($params)
- {
- try{
- $data = UserCoupon::with(['couponCategory'=>function ($query) use ($params) {
- $query->where('goods_category_id',$params['goods_category_id']);
- }])->where('user_id',$params['user_id'])
- ->where('voucher_count','>',0)
- ->where('voucher_status',0)
- ->append(['discount_ratio_text'])
- ->where('amount_require','<=',$params['amount'])
- ->visible(['id','amount','amount_require','begin_use','discount_ratio','event_name','expire_time','max_deductible_price','server_category_name','mold_type'])
- ->select()->toArray();
- foreach($data as $k => $v){
- $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'] );
- $data[$k]['amount_require'] = '满'.$v['amount_require'].'可用';
- }
- return $data;
- } catch(\Exception $e){
- self::setError($e->getMessage());
- return false;
- }
- }
- }
|