validate()) { return [ 'code' => 1, 'msg' => $this->getErrorSummary(false)[0] ]; } // 获取可以使用的核销卡 $verifySale = VerifyCardSale::find() ->where(['user_id' => $this->user_id, 'store_id' => $this->store_id, 'status' => 0]) ->andWhere(['is_delete' => 0]) ->asArray() ->all(); // 更新个人核销卡使用状态 foreach ($verifySale as &$val) { // 核销卡详情 $verifycard = VerifyCard::findOne(['id' => $val['verify_card_id'], 'store_id' => $this->store_id, 'is_delete' => VerifyCard::IS_DELETE_NO]); if ($verifycard['date_type'] == 1) { // 有效期 $expire_day = $verifycard['expire_day'] * 24 * 60 * 60; if ($expire_day > 0) { // 永久有效不更新 // expire_day天后的日期 $expire = intval($val['created_at']) + $expire_day; // 过期时间 if (time() > $expire) { if ($verifycard['type'] == 5) { VerifyCardSale::updateAll(['status' => 1, 'video_status' => 1], 'id=' . $val['id']); } else { VerifyCardSale::updateAll(['status' => 1], 'id=' . $val['id']); } } } } if ($verifycard['date_type'] == 2) { if (time() > intval($verifycard['end_time'])) { if ($verifycard['type'] == 5) { VerifyCardSale::updateAll(['status' => 1, 'video_status' => 1], 'id=' . $val['id']); } else { VerifyCardSale::updateAll(['status' => 1], 'id=' . $val['id']); } } } } if ($this->status == 2) { $arr = []; $verifylog = VerifyCardLog::find()->where(['user_id' => $this->user_id, 'type' => VerifyCardLog::WRITE_TYPE_GIVE])->groupBy('sale_id')->asArray()->all(); foreach ($verifylog as &$val) { $arr[] = $val['sale_id']; } $list = VerifyCardSale::find()->alias('vcs') ->leftJoin(['vc' => VerifyCard::tableName()], 'vcs.verify_card_id = vc.id') ->where(['vcs.store_id' => $this->store_id, 'vcs.is_delete' => 0, 'vc.is_delete' => 0]) ->andWhere(['in', 'vcs.id' , $arr]); if($this->type){ $list->andWhere(['vc.type' => $this->type]); } $list = $list->orderBy('vcs.sale_time desc') ->select('vcs.*, vc.type') ->asArray() ->all(); } elseif ($this->status == 1) { $list = VerifyCardSale::find()->alias('vcs') ->leftJoin(['vc' => VerifyCard::tableName()], 'vcs.verify_card_id = vc.id') ->where(['vcs.user_id' => $this->user_id, 'vcs.store_id' => $this->store_id]) ->andWhere(['in', 'vcs.status', [1,2]]) ->andWhere(['vcs.is_delete' => 0, 'vc.is_delete' => 0]); if($this->type){ $list->andWhere(['vc.type' => $this->type]); } $list = $list->orderBy('vcs.sale_time desc') ->select('vcs.*, vc.type') ->asArray() ->all(); foreach ($list as $key => $value) { if ($value['type'] == 5 && $value['video_status'] == 0) { unset($list[$key]); } } $list = array_values($list); } else { // 获取核销卡列表 $list = VerifyCardSale::find()->alias('vcs') ->leftJoin(['vc' => VerifyCard::tableName()], 'vcs.verify_card_id = vc.id') ->where(['vcs.user_id' => $this->user_id, 'vcs.store_id' => $this->store_id, 'vcs.is_delete' => 0, 'vc.is_delete' => 0]) ->andWhere('vcs.status=0 or (vc.type=5 and vcs.status=1 and vcs.video_status=0)'); if($this->type){ $list->andWhere(['vc.type' => $this->type]); } $list = $list->orderBy('vcs.sale_time desc') ->select('vcs.*, vc.type') ->asArray() ->all(); } foreach ($list as &$val) { $cardinfo = VerifyCard::findOne(['id' => $val['verify_card_id']]); $val['name'] = $cardinfo['name']; $val['expire_day'] = $cardinfo['expire_day']; $val['begin_time'] = $cardinfo['begin_time'] == 0 ? '' : $this->dateFormat($cardinfo['begin_time']); $val['date_type'] = $cardinfo['date_type']; $val['type'] = $cardinfo['type']; $val['pic_url'] = $cardinfo['pic_url']; $val['bg_pic_url'] = $cardinfo['bg_pic_url']; $val['created_at'] = $val['created_at'] == 0 ? '' : $this->dateFormat($val['created_at']); $val['updated_at'] = $val['updated_at'] == 0 ? '' : $this->dateFormat($val['updated_at']); $val['sale_time'] = $val['sale_time'] == 0 ? '' : date("Y-m-d H:i:s", $val['sale_time']); $val['end_time'] = $val['end_time'] == 0 ? '' : $this->dateFormat($val['end_time']); } return [ 'code' => 0, 'msg' => 'success', 'data' => $list, 'num' => count($list), ]; } /** * 卡券详情 * @return array */ public function detail() { if (!$this->validate()) { return [ 'code' => 1, 'msg' => $this->getErrorSummary(false)[0] ]; } $sale_info = VerifyCardSale::find()->where([ 'id' => $this->sale_id, ])->asArray()->one(); //查询核销卡信息 $verify_card_info = VerifyCard::find()->where([ 'id' => $sale_info['verify_card_id'], 'store_id' => $this->store_id, ])->asArray()->one(); $account_info = VerifyCardAccount::findOne($sale_info['account_id']); $sale_info['card_info'] = [ 'user' => $account_info->account, 'password' => $account_info->password, 'form' => $account_info->form, ]; // 是否可以转赠 $is_can_give = true; // if ($verify_card_info['type'] == 1) { // if ($sale_info['left_num'] != $verify_card_info['total_num']) { // $is_can_give = false; // } // } // if ($verify_card_info['date_type'] == 1) { // // 有效期 // $expire_day = (int)$verify_card_info['expire_day'] * 24 * 60 * 60; // // 过期时间 // $expire = $verify_card_info['created_at'] + $expire_day; // if (time() >= $expire) { // $is_can_give = false; // } // } // if ($verify_card_info['date_type'] == 2) { // if (time() >= $verify_card_info['end_time']) { // $is_can_give = false; // } // } if (time() >= $sale_info['end_time']) { $is_can_give = false; } if ((int)$sale_info['status'] === 1 || (int)$sale_info['status'] === 2) { $is_can_give = false; } if ((int)$verify_card_info['is_give'] === 0) { $is_can_give = false; } $sale_info['is_can_give'] = intval($is_can_give); $sale_info['sale_time'] = $sale_info['sale_time'] == 0 ? '' : $this->dateFormat($sale_info['sale_time']); $sale_info['end_time'] = $sale_info['end_time'] == 0 ? '' : $this->dateFormat($sale_info['end_time']); $sale_info['created_at'] = $sale_info['created_at'] == 0 ? '' : $this->dateFormat($sale_info['created_at']); $sale_info['updated_at'] = $sale_info['updated_at'] == 0 ? '' : $this->dateFormat($sale_info['updated_at']); $verify_card_info['begin_time'] = $verify_card_info['begin_time'] == 0 ? '' : $this->dateFormat($verify_card_info['begin_time']); $verify_card_info['end_time'] = $verify_card_info['end_time'] == 0 ? '' : $this->dateFormat($verify_card_info['end_time']); $verify_card_info['created_at'] = $verify_card_info['created_at'] == 0 ? '' : $this->dateFormat($verify_card_info['created_at']); $verify_card_info['updated_at'] = $verify_card_info['updated_at'] == 0 ? '' : $this->dateFormat($verify_card_info['updated_at']); $verify_card_info['goods_ids'] = json_decode($verify_card_info['goods_ids']); $verify_card_info['video_ids'] = json_decode($verify_card_info['video_ids']); $sale_info['verify_card_info'] = $verify_card_info; \Yii::warning($sale_info); $arr = []; if (get_md_id() == "-1" || !get_md_id()) { $store_info = Store::findOne(get_store_id()); $arr['coordinate'] = $store_info->coordinate; $arr['address'] = $store_info->address; $arr['name'] = $store_info->name; } else { $md = Md::find()->where(['store_id' => get_store_id(), 'id' => get_md_id()])->one(); $arr['coordinate'] = [ $md->latitude, $md->longitude ]; $arr['address'] = $md->address; $arr['name'] = $md->name; } $form = new WriteOffCodeForm(); $form->sale_id = $this->sale_id; $qrcode_info = $form->getQrcode(); return [ 'code' => 0, 'msg' => 'success', 'data' => [ 'qrcode' => $qrcode_info['data'], 'list' => $sale_info, 'store_info' => $arr ], ]; } public function changePass() { $pass = trim($this->cardpwd); if(strlen($pass) != 8){ return [ 'code' => 1, 'msg' => '密码必须是8位数的英文字母和数字', ]; } $sale_info = VerifyCardSale::find()->where([ 'id' => $this->sale_id, ])->asArray()->one(); $account_info = VerifyCardAccount::findOne($sale_info['account_id']); $account_info->password = $pass; $save = $account_info->save(); return [ 'code'=> $save ? 0 : 1, 'msg' => $save ? '成功' : '失败', ]; } public function order() { if (!$this->validate()) { return [ 'code' => 1, 'msg' => $this->getErrorSummary(false)[0] ]; } $order_info = VerifyCardSale::find()->where([ 'id' => $this->order_no, 'is_delete' => 0 ])->asArray()->one(); // 查询核销卡信息 $verify_card_info = VerifyCard::find()->select('name')->where([ 'id' => $order_info['verify_card_id'], 'is_delete' => 0 ])->asArray()->one(); $order_info['verify_card_name'] = $verify_card_info['name']; // 用户信息 $user_info = User::find()->select('nickname')->where([ 'id' => $order_info['user_id'], 'is_delete' => 0 ])->asArray()->one(); $order_info['username'] = $user_info['nickname']; $order_info['valid_time'] = date("Y-m-d H:i:s", $order_info['end_time']); // 核销员信息 $verify_user_info = User::find()->select('nickname,shop_id')->where([ 'id' => $this->user_id, 'is_clerk' => 1, 'is_delete' => 0 ])->asArray()->one(); // 门店信息 $shop_info = Shop::find()->select('name')->where([ 'id' => $verify_user_info['shop_id'], 'is_delete' => 0 ])->asArray()->one(); $order_info['verify_user_name'] = $verify_user_info['nickname']; $order_info['shop'] = $shop_info['name']; return [ 'code' => 0, 'msg' => 'success', 'data' => [ 'list' => $order_info ], ]; } public function log() { if (!$this->validate()) { return [ 'code' => 1, 'msg' => $this->getErrorSummary(false)[0] ]; } if ($this->id) { // $sale_card_info['coupon_money'] = $sale_info->use_num * $verify_card_info->money; 核销总金额 // 使用记录 $log_info = VerifyCardLog::find()->where([ 'user_id' => $this->user_id, 'is_delete' => 0, 'sale_id' => $this->id, 'store_id' => $this->store_id ])->orderBy('use_time desc')->asArray()->all(); } else { $log_info = VerifyCardLog::find()->where([ 'user_id' => $this->user_id, 'is_delete' => 0, 'store_id' => $this->store_id ])->orderBy('use_time desc')->asArray()->all(); } foreach ($log_info as &$item) { // 门店信息 $shop_info = Md::find()->select('name')->where([ 'id' => $item['shop_id'], 'is_delete' => 0 ])->one(); $item['shop_name'] = $shop_info ? $shop_info['name'] : '未知'; $item['use_time'] = $item['use_time'] == 0 ? '' : date("Y-m-d H:i:s", $item['use_time']); if (!empty($item['shop_id'])) { $user = User::findOne($item['verify_person']); $saasUser = SaasUser::findOne(['mobile' => $user->binding]); $item['verify_person'] = $saasUser->name; } else { $item['shop'] = ""; $item['verify_person'] = ""; } } return [ 'code' => 0, 'msg' => 'success', 'data' => [ 'log_list' => $log_info ], 'num' => count($log_info) ]; } public function add() { if (!$this->validate()) { return [ 'code' => 1, 'msg' => $this->getErrorSummary(false)[0] ]; } $transaction = \Yii::$app->db->beginTransaction(); try { $account = VerifyCardAccount::find()->where([ 'store_id' => $this->store_id, 'account' => $this->account, 'password' => $this->cardpwd, ])->one(); if (!$account) { return [ 'code' => 1, 'msg' => '卡券账号和密码不正确', ]; } if ($account->status == VerifyCardAccount::STATUS_USED) { return [ 'code' => 1, 'msg' => '该账号和密码已被使用', ]; } $verifyCard = VerifyCard::find()->where([ 'id' => $account->card_id, 'is_delete' => 0, ])->one(); if (!$verifyCard) { return [ 'code' => 1, 'msg' => '卡券不存在', ]; } $sale = new VerifyCardSale(); $sale->store_id = $this->store_id; $sale->verify_card_id = $verifyCard->id; $sale->user_id = $this->user_id; $sale->left_num = $verifyCard->type == 1 ? $verifyCard->total_num : 1; $sale->sale_time = time(); if ($verifyCard->date_type == 1) { $sale->end_time = time() + $verifyCard->expire_day * (3600 * 24); } else { $sale->end_time = $verifyCard->end_time; } $sale->account_id = $account->id; if ($verifyCard->type == 5) { $sale->status = 1; } $sale->save(); if ($verifyCard->type == 5) { $logModel = new VerifyCardLog(); $logModel->sale_id = $sale->id; $logModel->store_id = $this->store_id; $logModel->use_time = time(); $logModel->user_id = $this->user_id; $logModel->type = VerifyCardLog::WRITE_TYPE_EXCHANGE; $logModel->describe = '视频卡兑换'; if (!$logModel->save()) { return [ 'code' => 0, 'msg' => '视频卡兑换失败', ]; } } $account->status = VerifyCardAccount::STATUS_USED; $account->save(); $transaction->commit(); return [ 'code' => 0, 'msg' => '绑定成功', 'data' => $sale->id ]; } catch (\Exception $e) { $transaction->rollBack(); return [ 'code' => 0, 'msg' => '绑定失败', ]; } } // /** // * 添加卡券 // * @return boolean // */ // public function add2() // { // if (!$this->validate()) { // return [ // 'code' => 1, // 'msg' => $this->getErrorSummary(false)[0] // ]; // } // $arr = []; // $arr1 = []; // $model = new VerifyCardSale(); // // 查询除核销卡外的所有卡类型卡密信息 // $verifyCardList = VerifyCard::find() // ->where(['is_delete' => VerifyCard::IS_DELETE_NO]) // ->andWhere(['<>', 'status', 2]) // ->andWhere(['<>', 'account_info', '']) // ->select('id, account_info, type, date_type, begin_time, end_time, store_id, total_num') // ->asArray() // ->all(); // if (!empty($verifyCardList)) { // foreach ($verifyCardList as $key => $val) { // if ($val['date_type'] == 2) { // if ((int)$val['end_time'] < time()) { // return [ // 'code' => 1, // 'msg' => '该卡已过期', // ]; // } // } // foreach (json_decode($val['account_info'], true) as $k => $v) { // if ($this->account == $v['user'] && $this->cardpwd == $v['password']) { // $user_info = User::findOne(['id' => $this->user_id]); // if ((int)$val['store_id'] !== (int)$this->store_id || $user_info['store_id'] !== (int)$this->store_id) { // return [ // 'code' => 1, // 'msg' => '您绑定的卡不属于该商城' // ]; // } // if ($v['status'] == "未使用") { // $this->verify_card_id = (int)$val['id']; // if ($val['date_type'] == 2) { // $this->sale_time = $val['begin_time']; // $this->end_time = $val['end_time']; // } // if ($val['type'] > 1) { // $model->left_num = 1; // } else { // $model->left_num = $val['total_num']; // } // break; // } else { // return [ // 'code' => 1, // 'msg' => '您绑定的卡已被使用' // ]; // } // } else { // continue; // } // } // } // } else { // return []; // } // if (empty($this->verify_card_id) || $this->verify_card_id == '' || $this->verify_card_id == null) { // return [ // 'code' => 1, // 'msg' => '账号或者密码错误' // ]; // } // $model->created_at = time(); // $model->card_info = json_encode(['user' => $this->account, 'password' => $this->cardpwd]); // $model->attributes = $this->attributes; // $model->status = 0; // if ($model->save()) { // $verify_card = VerifyCard::find()->where(['id' => $this->verify_card_id])->asArray()->one(); // foreach (json_decode($verify_card['account_info'], true) as $k => $v) { // if ($this->account == $v['user'] && $this->cardpwd == $v['password']) { // $arr[$k]['user'] = $v['user']; // $arr[$k]['password'] = $v['password']; // $arr[$k]['status'] = "已使用"; // } else { // $arr1[$k]['user'] = $v['user']; // $arr1[$k]['password'] = $v['password']; // $arr1[$k]['status'] = $v['status']; // } // } // $account_info = array_merge($arr, $arr1); // $res = VerifyCard::updateAll(['account_info' => json_encode($account_info)], ['id' => $verify_card['id']]); // if ($res > 0) { // return [ // 'code' => 0, // 'msg' => '绑定成功' // ]; // } // } else { // return [ // 'code' => 1, // "msg" => $this->getErrorSummary(false)[0] // ]; // } // } /** * 兑换详情 */ public function exchangeDetail() { if (!$this->validate()) { return [ 'code' => 1, 'msg' => $this->getErrorSummary(false)[0] ]; } if ($this->sale_id) { $verifyCardSale = VerifyCardSale::findOne(['id' => $this->sale_id]); if ($verifyCardSale['user_id'] !== $this->user_id) { return [ 'code' => 1, 'msg' => '无权限查看' ]; } $verifyCard = VerifyCard::findOne(['id' => $verifyCardSale['verify_card_id']]); if ($verifyCard['type'] == 1) { if ($verifyCardSale['left_num'] !== $verifyCard['total_num']) { $status = 2; } } if ($verifyCard['type'] == 2) { $data = Goods::find() ->where(['store_id' => $this->store_id]) ->andWhere(['in', 'id', json_decode($verifyCard['goods_ids'], true)]) ->asArray() ->all(); foreach ($data as $k => $val) { $val['attr'] = json_decode($val['attr'], true); $goods_info = Goods::findOne($val['id']); $data[$k]['attr_group_list'] = $goods_info->getAttrGroupList(); } } if ($verifyCard['type'] == 5) { $data = Video::find() ->where(['store_id' => $this->store_id]) ->andWhere(['in', 'id', json_decode($verifyCard['video_ids']), true]) ->asArray() ->all(); } if ($verifyCard['date_type'] == 1) { // 有效期 $expire_day = $verifyCard['expire_day'] * 24 * 60 * 60; // 过期时间 $expire = $verifyCardSale['created_at'] + $expire_day; if (time() > $expire) { $status = 1; } else { $status = 0; } } if ($verifyCard['date_type'] == 2) { if (time() > $verifyCard['end_time']) { $status = 1; } else { $status = 0; } } return [ 'code' => 0, 'msg' => 'success', 'data' => [ 'list' => empty($data) ? [] : $data, 'verifyCardSale' => $verifyCardSale, 'verifycard' => $verifyCard ], 'num' => empty($data) ? 0 : count($data), 'status' => $status ]; } else { return [ 'code' => 1, 'msg' => '参数错误' ]; } } /** * 更新视频状态 */ public function videoUpdate() { if (!$this->validate()) { return [ 'code' => 1, 'msg' => $this->getErrorSummary(false)[0] ]; } if ($this->sale_id && $this->id) { $user_id = VerifyCardSale::findOne(['id' => $this->sale_id])->user_id; if ($user_id !== $this->user_id) { return [ 'code' => '1', 'msg' => '对不起,您没有权限观看此视频' ]; } $data = Video::findOne(['id' => $this->id]); if (!$data) { return [ 'code' => 1, 'msg' => '该视频不存在' ]; } return [ 'code' => 0, 'msg' => 'success', 'data' => $data ]; } else { return [ 'code' => 1, 'msg' => '参数错误' ]; } } /** * 储值卡兑换 */ public function exchange() { if (!$this->validate()) { return [ 'code' => 1, 'msg' => $this->getErrorSummary(false)[0] ]; } if ($this->sale_id) { $verifyCardSale = VerifyCardSale::findOne([ 'id' => $this->sale_id, 'is_delete' => VerifyCard::IS_DELETE_NO, 'store_id' => $this->store_id ]); $verifycard = VerifyCard::findOne(['id' => $verifyCardSale['verify_card_id']]); if ($verifycard['type'] == 3 && $verifyCardSale['left_num'] > 0) { $user_balance = AccountLog::find() ->where(['user_id' => $this->user_id]) ->orderBy('created_at desc') ->asArray() ->one(); $model = new AccountLog(); $logModel = new VerifyCardLog(); $logModel->sale_id = $this->sale_id; $logModel->store_id = $this->store_id; $logModel->use_time = time(); $logModel->user_id = $this->user_id; $logModel->type = VerifyCardLog::WRITE_TYPE_EXCHANGE; $logModel->describe = '储值卡兑换金额' . $verifycard['total_price'] . '元'; $model->user_id = $this->user_id; $model->store_id = $this->store_id; $model->order_type = AccountLog::TYPE_RECHARGE_ORDER; $model->amount = $verifycard['total_price']; $model->desc = '商城储值卡充值' . $verifycard['total_price'] . '元'; $model->before = $user_balance['after']; $model->after = $user_balance['after'] + $verifycard['total_price']; $model->operator = 'system'; $model->log_type = 1; $model->type = AccountLog::TYPE_BALANCE; $model->operator_type = AccountLog::TYPE_OPERATOR_NORMAL; $model->created_at = time(); if ($user_balance) { // 已充值过的用户 $model->before = $user_balance['after']; $model->after = $user_balance['after'] + $verifycard['total_price']; } else { // 未充值过的用户 $model->before = 0; $model->after = $verifycard['total_price']; } if ($model->save() && $logModel->save()) { $left_num = $verifyCardSale['left_num'] - 1; $send_times = $verifycard['send_times'] + 1; $user_money = User::findOne(['id' => $this->user_id])['money']; $user = User::updateAll(['money' => ($user_money + $verifycard['total_price'])], 'id=' . $this->user_id); $verify_card = VerifyCard::updateAll(['send_times' => $send_times], 'id='.$verifycard['id']); $verify_card_sale = VerifyCardSale::updateAll(['left_num' => $left_num, 'status' => 1], 'id=' . $this->sale_id); if ($user > 0 && $verify_card > 0 && $verify_card_sale > 0) { return [ 'code' => 0, 'msg' => 'success' ]; } else { return [ 'code' => 1, 'msg' => '充值失败' ]; } } else { return [ 'code' => 1, 'msg' => '充值失败' ]; } } if ($verifycard['type'] == 5) { VerifyCardSale::updateAll(['status' => 1], 'id=' . $this->sale_id); $logModel = new VerifyCardLog(); $logModel->sale_id = $this->sale_id; $logModel->store_id = $this->store_id; $logModel->use_time = time(); $logModel->user_id = $this->user_id; $logModel->type = VerifyCardLog::WRITE_TYPE_EXCHANGE; $logModel->describe = '视频卡兑换'; $logModel->save(); return [ 'code' => 0, 'msg' => '兑换成功' ]; } } else { return [ 'code' => 1, 'msg' => '参数错误', ]; } } /** * 卡券领取 */ public function receive() { if (!$this->validate()) { return [ 'code' => 1, 'msg' => $this->getErrorSummary(false)[0] ]; } if ($this->sale_id) { $verifysale = VerifyCardSale::findOne(['id' => $this->sale_id]); // 赠送人信息 $model = new VerifyCardLog(); if (!$verifysale) { return [ 'code' => 1, 'msg' => '未查到此卡信息' ]; } if ($verifysale->user_id == $this->user_id) { return [ 'code' => 1, 'msg' => '不能赠送给自己' ]; } if ($verifysale->user_id == $this->user_id) { return [ 'code' => 1, 'msg' => '您已领取过' ]; } $model->store_id = $this->store_id; $model->sale_id = $this->sale_id; $model->use_time = time(); $model->user_id = $verifysale->user_id; $model->recv_id = $this->user_id; $model->type = VerifyCardLog::WRITE_TYPE_GIVE; $model->describe = '通过分享赠送领取卡券'; $verifysale->user_id = $this->user_id; if ($model->save() && $verifysale->save()) { $verifysale->is_trans_send = 1; $verifysale->save(); return [ 'code' => 0, 'msg' => '领取成功' ]; } return [ 'code' => 1, 'msg' => '领取失败' ]; } else { return [ 'code' => 1, 'msg' => '参数错误' ]; } } /** * 删除核销卡(更新状态) */ public function delVerifyCard() { if ($this->id) { $verifysale = VerifyCardSale::findOne(['id' => $this->id]); if (!$verifysale) { return []; } $verifysale->is_delete = VerifyCardSale::IS_DELETE_YES; $verifysale->status = 2; $verifysale->save(); return [ 'code' => 0, 'msg' => '删除成功' ]; } elseif ($this->ids) { $ids = explode(',', $this->ids); VerifyCardSale::updateAll( ['is_delete' => VerifyCardSale::IS_DELETE_YES, 'status' => 2], ['id' => $ids]); return [ 'code' => 0, 'msg' => '删除成功' ]; } } public function getVerifyLog() { if (!$this->validate()) { return [ 'code' => 1, 'msg' => $this->getErrorSummary(false)[0] ]; } if ($this->sale_id) { $sale = VerifyCardSale::findOne($this->sale_id); if (!$sale) { return [ 'code' => 1, 'msg' => '卡券不存在', ]; } $logs = VerifyCardLog::find()->alias('vcl') ->leftJoin(['md' => Md::tableName()], 'vcl.shop_id = md.id')->where([ 'vcl.store_id' => $this->store_id, 'vcl.sale_id' => $this->sale_id, 'vcl.is_delete' => 0, 'vcl.type' => VerifyCardLog::WRITE_TYPE_WRITE_OFF ])->select('vcl.use_time, md.name as md_name')->asArray()->all(); return [ 'code' => 0, 'data' => $logs, ]; } return [ 'code' => 1, 'msg' => '缺少参数', ]; } /** * 转换日期格式为date */ public function dateFormat($date) { return date('Y-m-d', $date); } public function v_list() { $query = VerifyCard::find()->alias('vc') ->innerJoin(['vsu' => VerifyShareUser::tableName()], 'vsu.verify_card_id=vc.id') ->where(['vc.store_id' => $this->store_id, 'vc.type' => 1, 'vc.is_delete' => 0, 'vsu.user_id' => get_user_id()]); $query->andWhere('(vc.date_type = 1 or (vc.date_type=2 and vc.end_time> unix_timestamp()))'); $list = $query->orderBy('vc.id desc') ->select('vc.*') ->asArray() ->all(); return [ 'code' => 0, 'msg' => 'success', 'data' => $list, 'num' => count($list), ]; } public function add_receive() { if (!$this->validate()) { return [ 'code' => 1, 'msg' => $this->getErrorSummary(false)[0] ]; } $transaction = \Yii::$app->db->beginTransaction(); try { $share_user = VerifyShareUser::find()->where([ 'store_id' => $this->store_id, 'verify_card_id' => $this->verify_card_id, 'user_id' => $this->share_user_id, ])->one(); if (!$share_user) { return [ 'code' => 1, 'msg' => '不是分享人分享,不能领取', ]; } $info = VerifyShareLog::find()->where([ 'verify_card_id' => $this->verify_card_id, 'user_id' => $this->user_id, ])->one(); if ($info) { return [ 'code' => 1, 'msg' => '不可重复领取', ]; } $verifyCard = VerifyCard::find()->where([ 'id' => $this->verify_card_id, 'is_delete' => 0, ])->one(); if (!$verifyCard) { return [ 'code' => 1, 'msg' => '卡券不存在', ]; } $account = VerifyCardAccount::find()->where([ 'store_id' => $this->store_id, 'card_id' => $verifyCard->id, 'status' => 0, ])->one(); if (!$account) { return [ 'code' => 1, 'msg' => '卡卷码不存在', ]; } $account->status = VerifyCardAccount::STATUS_USED; $account->save(); $verifyCard->send_times += 1; $verifyCard->save(); $sale = new VerifyCardSale(); $sale->store_id = $this->store_id; $sale->verify_card_id = $verifyCard->id; $sale->user_id = $this->user_id; $sale->left_num = $verifyCard->type == 1 ? $verifyCard->total_num : 1; $sale->sale_time = time(); if ($verifyCard->date_type == 1) { $sale->end_time = time() + $verifyCard->expire_day * (3600 * 24); } else { $sale->end_time = $verifyCard->end_time; } $sale->account_id = $account->id; if ($verifyCard->type == 5) { $sale->status = 1; } $sale->save(); $logModel = new VerifyShareLog(); $logModel->sale_id = $sale->id; $logModel->store_id = $this->store_id; $logModel->verify_card_id = $this->verify_card_id; $logModel->share_user_id = $this->share_user_id; $logModel->user_id = $this->user_id; $logModel->create_time = time(); if (!$logModel->save()) { return [ 'code' => 1, 'msg' => '兑换失败', ]; } $transaction->commit(); return [ 'code' => 0, 'msg' => '绑定成功', 'data' => $sale->id ]; } catch (\Exception $e) { $transaction->rollBack(); return [ 'code' => 0, 'msg' => '绑定失败', ]; } } }