hasMany('CouponReceive', 'coupon_id'); } /** * 发放打折券 * @param $params * @throws \Exception */ public function grantDiscountsCoupon($params) { $CouponReceive = model('CouponReceive'); if ($params['discounts'] && $CouponReceive->where(array( 'uid' => $params['uid'], 'coupon_id' => 1, 'coupon_type' => 1, ))->find()) { //发放半价券并锁定 $CouponReceive->save(array( 'uid' => $params['uid'], 'coupon_id' => 1, 'coupon_type' => 1, 'status' => 2, )); } } /** * 发放缴费券 * @param $params * @throws \Exception */ public function grantDeductionCoupon($params) { $coupon = $this->get($params['cid']); $couponReceive = model('CouponReceive'); $data = []; switch ($params['service_type']) { case 'monthly': $num = 1; break; case 'seasonal': $num = 3; break; case 'halfYearly': $num = 6; break; case 'yearly': $num = 12; break; default: $num = 1; break; } for ($i = 0; $i < $num; $i++) { array_push($data, array( 'uid' => $params['uid'], 'oid' => $params['oid'], 'cid' => $params['cid'], 'type' => $coupon['type'], 'name' => $coupon['name'], 'value' => $coupon['value'], 'end_timestamp' => date('Y-m-d H:i:s', strtotime("+$i month 1 day")), 'create_timestamp' => date('Y-m-d H:i:s',time()), 'status' => 1, )); } // 扣除一张缴费券 $data[0]['status'] = 3; $res = Db::name('coupon_receive')->insertAll($data); $first_id = (int)Db::name('coupon_receive')->getLastInsID() + 1; return $first_id; } /** * 获取并锁定缴费券 * @param $uid * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function getDeductionCoupon($uid) { $id = model('CouponReceive')->where([['uid', '=', $uid], ['status', '=', 1]]) ->order('end_timestamp') ->value('id'); return $id; } }