123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- <?php
- namespace app\common\model;
- use think\Model;
- use think\Db;
- class Coupon extends Model
- {
- public function couponReceive()
- {
- return $this->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;
- }
- }
|