| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\client\models\v1;
- use app\models\AccountLog;
- use app\models\Goods;
- use app\models\SaasUser;
- use app\models\Store;
- use app\models\VerifyCard;
- use app\models\VerifyCardSale;
- use app\models\Order;
- use app\models\VerifyShareLog;
- use app\models\VerifyShareUser;
- use app\utils\OrderNo;
- use app\models\User;
- use app\models\Recharge;
- use app\models\Shop;
- use app\models\Address;
- use app\models\VerifyCardAccount;
- use app\models\VerifyCardLog;
- use app\models\Video;
- use Think\Verify;
- use yii\base\BaseObject;
- use yii\base\Model;
- use app\models\Md;
- use yii\data\Pagination;
- class VerifyIndexForm extends Model
- {
- public $store_id;
- public $user_id;
- public $status;
- public $id;
- public $ids;
- public $verify_card_id;
- public $account;
- public $cardpwd;
- public $sale_id;
- public $order_no;
- public $card_info;
- public $created_at;
- public $sale_time;
- public $end_time;
- public $type;
- public $share_user_id;
- public function rules()
- {
- return [
- [['status', 'id', 'verify_card_id', 'sale_id', 'order_no','store_id', 'share_user_id'], 'integer'],
- [['account', 'cardpwd', 'card_info', 'ids'], 'string'],
- [['account', 'cardpwd'], 'trim'],
- [['status', 'type'], 'safe']
- ];
- }
- public function search()
- {
- if (!$this->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' => '绑定失败',
- ];
- }
- }
- }
|