listCoupon([ ['id', 'in', $coupon_ids] ], 'id,type,name,price,integral,brief,limit_price,total_number,receive_number,per_people_number,validity_type,start_time,end_time,validity_days'); // 获取我领取的优惠券以及张数 $my_coupon_number = $coupon_my_model->countMyCouponByCoupon(); // 我可以兑换的优惠券列表 $coupon_my_list = []; $integral_log = []; //积分兑换优惠券数据明细 // 转为数组 $coupon_list = $coupon_list->toArray(); foreach ($coupon_list as $k => $v) { // 个人领取数量到达上限 有领取该优惠券 && 限制数量 && 领取张数>=限制张数 if (isset($my_coupon_number[$v['id']]) && $v['per_people_number'] != 0 && $my_coupon_number[$v['id']] >= $v['per_people_number']) { continue; } // 优惠券已抢光 if ($v['total_number'] - $v['receive_number'] <= 0) { continue; } // 我的优惠券数据 $my_coupon = [ // 雪花ID 'id' => $snow_flake_class->id(), 'uid' => $this->mid, 'user_agent' => $this->userAgent, 'user_id' => $this->userId, 'coupon_id' => $v['id'], // 优惠券类型 1--领取优惠券 2--首关优惠券 3--下单给自己送优惠券 4--分享给自己送优惠券 5--分享给别人送优惠券 6--生日优惠券 7--积分商城兑换优惠券 8--兑换码兑换优惠券 'coupon_type' => $v['type'], 'name' => $v['name'], 'limit_price' => $v['limit_price'], 'price' => $v['price'], 'brief' => $v['brief'], 'status' => 1, 'remark' => serialize([ 'coupon_id' => $v['id'] ]), 'create_time' => time(), 'update_time' => time() ]; // 优惠券有效时间类型 1--固定日期 2--X天后 3--永久有效 if ($v['validity_type'] == 1) { $my_coupon['is_forever'] = 0; $my_coupon['start_time'] = $v['start_time']; $my_coupon['end_time'] = $v['end_time']; } else if ($v['validity_type'] == 2) { $my_coupon['is_forever'] = 0; $my_coupon['start_time'] = strtotime(date("Y-m-d")); $my_coupon['end_time'] = strtotime(date("Y-m-d")) + ($v['validity_days'] + 1) * 86400; } else if ($v['validity_type'] == 3) { $my_coupon['is_forever'] = 1; $my_coupon['start_time'] = strtotime(date("Y-m-d")); $my_coupon['end_time'] = 0; } // 追加到待添加我的优惠券列表 $coupon_my_list[] = $my_coupon; // 优惠券表数据 $coupon_update_list[] = [ 'id' => $v['id'], // 已领取人数增加 'receive_number' => Db::raw('receive_number + 1'), 'update_time' => time() ]; // type=7时,积分兑换优惠券 if ($v['type'] == 7) { // 积分大于0时需要记录 if ($v['integral'] >= 0) { $integral_log[] = [ // 兑换优惠券的积分数量 'integral' => $v['integral'], 'coupon_my_id' => $my_coupon['id'], 'coupon_id' => $v['id'] ]; } } } // 没有可以领取的优惠券 if (empty($coupon_my_list)) { return sendErrorArray(3001, '没有可以领取的优惠券,有可能是优惠券可领取张数已达上限'); } // 批量插入我的优惠券 $res = $coupon_my_model->insertAll($coupon_my_list); if (!$res) { return sendErrorArray(3002, '领取失败'); } // 批量更新优惠券库存 $res = $coupon_model->saveAll($coupon_update_list); if (!$res) { return sendErrorArray(3003, '领取失败'); } // 如果有积分兑换优惠券,则扣除积分 if (!empty($integral_log)) { foreach ($integral_log as $k => $v) { $res = $integral_service->change($this->userId, 6, -$v['integral'], '积分兑换优惠券', '', [ 'coupon_id' => $v['coupon_id'], 'coupon_my_id' => $v['coupon_my_id'] ]); if ($res['code'] != 0) { return $res; } } } return sendSuccessArray(); } /** * 选择我的优惠券列表 * @param float $price 商品总价钱 * @date 2022-10-17 */ public function listChooseCoupon($price) { $coupon_my_model = new DiscountCouponMy(); // 可使用优惠券 $able_list = []; // 不可使用优惠券 $unable_list = []; // 获取所有有效优惠券 $where = [ ['uid', '=', $this->mid], ['user_id', '=', $this->userId], ['status', '=', 1] ]; $field = 'id,name,price,brief,limit_price,start_time,end_time,is_forever,status'; $order = 'price desc'; $my_coupon_list = $coupon_my_model->listMyCoupon($where, $field, $order); foreach ($my_coupon_list as $k => $v) { if ($v['limit_price'] <= $price) { // 是否可以使用 1--可以使用 2--不可以使用 $v['is_able'] = 1; $v['unable_reason'] = ''; $able_list[] = $v; } else { $v['is_able'] = 0; $v['unable_reason'] = '还差' . ($v['limit_price'] - $price) . '元可使用优惠券'; $unable_list[] = $v; } } return sendSuccessArray([ 'able_list' => $able_list, 'unable_list' => $unable_list ]); } }