@Fecshop #2楼
第一次 addCoupon是 $cu_model->times_used = 1;为1的
/**
* @property $type | String add or cancel
* @return bool
* 增加或者减少优惠券使用的次数
*/
protected function updateCouponUse($type)
{
if ($this->_customer_id) {
$c_model = $this->getCouponModel();
if ($c_model) {
if ($type == 'add') {
$cu_model = $this->getCouponUsageModel();
if (!$cu_model) {
$cu_model = new $this->_couponUsageModelName;
$cu_model->times_used = 1;
$cu_model->customer_id = $this->_customer_id;
$cu_model->coupon_id = $c_model['coupon_id'];
$cu_model->save();
} else {
Yii::info('是否新增优惠券使用次数!1','fecshop_debug');
// 通过update函数 将times_used +1
$sql = 'update '.$this->_couponUsageModel->tableName().' set times_used = times_used + 1 where id = :id';
$data = [
'id' => $cu_model['id'],
];
Yii::info('sql:'.$sql,'fecshop_debug');
Yii::info('$cu_model[\'id\']:'.$cu_model['id'],'fecshop_debug');
$result = $this->_couponUsageModel->getDb()->createCommand($sql,$data)->execute();
Yii::info('是否新增优惠券使用次数!2','fecshop_debug');
}
// coupon的总使用次数+1
$sql = 'update '.$this->_couponModel->tableName().' set times_used = times_used + 1 where coupon_id = :coupon_id ';
$data = [
'coupon_id' => $c_model['coupon_id'],
];
$result = $this->_couponModel->getDb()->createCommand($sql,$data)->execute();
return true;
} elseif ($type == 'cancel') {
$couponModel = $this->getCouponModel();
$cu_model = $this->getCouponUsageModel();
if ($cu_model) {
// $this->_couponUsageModel 使用次数-1
$sql = 'update '.$this->_couponUsageModel->tableName().' set times_used = times_used - 1 where id = :id';
$data = [
'id' => $cu_model['id'],
];
$result = $this->_couponUsageModel->getDb()->createCommand($sql,$data)->execute();
// $this->_couponModel 使用次数-1
$sql = 'update '.$this->_couponModel->tableName().' set times_used = times_used - 1 where coupon_id = :coupon_id ';
$data = [
'coupon_id' => $c_model['coupon_id'],
];
$result = $this->_couponModel->getDb()->createCommand($sql,$data)->execute();
return true;
}
}
}
}
}