| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\models;
- use app\models\Goods;
- use app\models\Md;
- use app\models\Shop;
- use app\models\VerifyCardLog;
- use app\models\VerifyShareLog;
- use app\models\VerifyShareUser;
- use app\models\Video;
- use app\models\User;
- use app\models\Store;
- use app\models\SaasUser;
- use app\models\VerifyCard;
- use app\utils\Export;
- use app\utils\ExportList;
- use app\models\VerifyCardSale;
- use app\models\VerifyCardAccount;
- use app\models\Order;
- use yii\base\Model;
- class VerifyCardForm extends Model
- {
- public $id;
- public $store_id;
- public $name;
- public $total_num;
- public $use_num;
- public $expire_day;
- public $money;
- public $price;
- public $pic_url;
- public $begin_time;
- public $end_time;
- public $sort;
- public $is_delete;
- // 12.8新增字段
- public $status;
- public $account_info;
- public $type;
- public $date;
- public $bg_pic_url;
- public $date_type;
- public $is_give;
- public $content;
- public $num;
- public $total_price;
- public $freight_id;
- public $savingsType;
- public $goods_ids;
- public $video_ids;
- public $send_times;
- //搜索
- public $search_key;
- public $search_is_show;
- public $search_shop_name;
- //导出
- public $fields;
- public $flag;
- public $is_business;
- public $business_type;
- public $share_user_id;
-
- public $form;
-
- public $user_nickname;
- public $user_mobile;
- public $verify_person_nickname;
- public $md_name;
- public $start_time;
- public $order_no;
- public $for_cash;
- public $use_type;
- public $export;
- public $account;
- public $is_unlimited;
- const SCENARIO_ADD = 'add';
- const SCENARIO_EDIT = 'edit';
- const SCENARIO_DEL = 'del';
- const SCENARIO_LIST = 'list';
- const USER_PLATFORM_TYPE = [
- User::USER_FROM_WECHAT => '微信小程序',
- User::USER_FROM_ALIPAY => '支付宝',
- User::USER_FROM_OFFICIAL_ACCOUNTS => '公众号',
- User::USER_FROM_APP => 'app微信',
- User::USER_FROM_PHONE => '手机号注册',
- User::USER_FROM_BYTEDANCE => '抖音',
- ];
- const SEDN_TIMES_DEFAULT = 0; // 默认发放次数
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['id', 'store_id', 'total_num', 'expire_day', 'is_delete', 'status', 'type', 'date_type', 'is_give', 'freight_id', 'num', 'savingsType', 'send_times','is_business','business_type','export', 'is_unlimited'], 'integer'],
- [['money', 'price', 'total_price', 'for_cash'], 'double'],
- [['name','search_shop_name'], 'string', 'max' => 255],
- [['goods_ids', 'video_ids', 'account_info', 'flag'], 'string'],
- [['pic_url', 'bg_pic_url','content'], 'string', 'max' => 2048],
- [['flag'], 'trim'],
- [['use_num', 'begin_time', 'end_time', 'date'], 'safe'],
- ['is_delete', 'default', 'value'=> VerifyCard::IS_DELETE_NO],
- [['name', 'pic_url', 'price', 'expire_day'],'required', 'on'=> [self::SCENARIO_ADD, self::SCENARIO_EDIT]],
- [['id'],'required', 'on'=> [self::SCENARIO_DEL, self::SCENARIO_EDIT]],
- ['id', 'string', 'on' => self::SCENARIO_DEL],
- ['search_key', 'string', 'on' => self::SCENARIO_LIST],
- [['fields', 'form'], 'safe'],
- [['user_nickname', 'user_mobile', 'verify_person_nickname', 'md_name', 'start_time', 'order_no'], 'safe'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'store_id' => 'Store ID',
- 'name' => '核销卡名称',
- 'total_num' => '总次数',
- 'use_num' => '核销次数',
- 'expire_day' => '有效期',
- 'money' => '核销卡单次核销优惠金额',
- 'price' => '核销卡价值',
- 'pic_url' => '核销卡Logo',
- 'begin_time' => '核销卡开始时间',
- 'end_time' => '核销卡结束时间',
- 'date' => '核销卡起止时间',
- 'sort' => '排序',
- 'is_delete' => '状态',
- 'status' => '核销卡类型:1=生效中,2=已失效',
- 'account_info' => '卡密账号信息',
- 'type' => '卡券类型:1=核销卡,2=礼品卡,3=储值卡,4=虚拟卡,5=视频卡',
- 'bg_pic_url' => '背景图',
- 'date_type' => '日期类型',
- 'is_give' => '是否转赠:0=否,1=是',
- 'content' => '卡券说明',
- 'num' => '卡券数量',
- 'total_price' => '储值卡金额',
- 'freight_id' => '运费模板',
- 'savingsType' => '储值卡类型',
- 'goods_ids' => '商品id',
- 'video_ids' => '视频id',
- 'send_times' => '发放次数',
- 'is_business' => '是否是商盟',
- 'business_type' => '展示位置',
- 'for_cash' => '礼品卡折现',
- 'is_unlimited' => '核销次数是否无限:0=否;1=是;',
- ];
- }
- public function scenarios()
- {
- $scenarios = parent::scenarios();
- return $scenarios;
- }
- //获取店铺信息
- private function getStoreInfoById($store_id){
- $storeInfo = Store::find()->where(['id'=>$store_id,'is_delete'=>0])->asArray()->one();
- if($storeInfo && $storeInfo['name']){
- return $storeInfo['name'];
- }else{
- return "店铺暂无名称";
- }
- }
- public function saveVerifyCard()
- {
- if ($this->validate()) {
- if ($this->scenario == self::SCENARIO_EDIT) {
- $model = VerifyCard::findOne(['id' => $this->id]);
- } else {
- $model = new VerifyCard();
- }
- $model->attributes = $this->attributes;
- if($this->is_unlimited == 1) {
- $model->total_num = 0;
- }
- if (!$model->save()) {
- return [
- 'code' => 1,
- 'msg' => $model->getErrorSummary(false)[0]
- ];
- }
- $wayShop = VerifyShareUser::findOne(['verify_card_id' => $model->id]);
- if ($wayShop) {
- VerifyShareUser::deleteAll(['verify_card_id'=> $model->id]);
- }
- foreach ($this->share_user_id as $value) {
- $way_form = new VerifyShareUser();
- $way_form->store_id = $this->store_id;
- $way_form->user_id = $value;
- $way_form->verify_card_id = $model->id;
- $way_form->save();
- }
- return [
- 'code' => 0,
- 'msg' => '保存成功'
- ];
- } else {
- // 验证失败:$errors 是一个包含错误信息的数组
- return [
- 'code' => 1,
- "msg" => $this->getErrorSummary(false)[0]
- ];
- }
- }
- public function searchVerifyCard()
- {
- $query = VerifyCard::find()->alias('v');
- $query->where(['v.is_delete' => VerifyCard::IS_DELETE_NO])->orderBy("v.sort desc,v.id desc");
-
- if ($this->id) {
- $query->andWhere(['id' => $this->id]);
- }
-
- if ($this->type > 0) {
- $query->andWhere(['type' => $this->type]);
- }
- if($this->search_shop_name){
- $query->leftJoin(['s' => Store::tableName()], 's.id=c.store_id')->andWhere(['like', 's.name', $this->search_shop_name]);
- }
- // 搜索
- if ($this->store_id) {
- $query->andWhere(['=','v.store_id', $this->store_id]);
- }
- if($this->is_business){
- $query->andWhere(['v.is_business'=>$this->is_business]);
- }else{
- $query->andWhere(['v.is_business'=>0]);
- }
- if ($this->search_key) {
- $query->andWhere(['like', 'v.name', $this->search_key]);
- }
- if ($this->date) {
- $query->andWhere(['>=', 'v.created_at', strtotime($this->date[0])]);
- $query->andWhere(['<=', 'v.created_at', strtotime($this->date[1])]);
- }
- // 根据核销卡类型查找数据 未实现
- // if ($this->type) {
- // $query->andWhere(['=','type', $this->type]);
- // }
- $list = pagination_make($query);
- $storeName = [];
- foreach ($list['list'] as &$val) {
- if($this->is_business){
- //获取店铺名称
- if(isset($storeName[$val['store_id']]) && $storeName[$val['store_id']] ){
- $val['store_name'] = $storeName[$val['store_id']];
- }else{
- $store_name = $this->getStoreInfoById($val['store_id']);
- $storeName[$val['store_id']] = $store_name;
- $val['store_name'] = $store_name;
- }
- }
- $val['created_at'] = date('Y-m-d H:i:s', $val['created_at']);
- $val['updated_at'] = date('Y-m-d H:i:s', $val['updated_at']);
- $val['begin_time'] = date('Y-m-d H:i:s', $val['begin_time']);
- $val['end_time'] = date('Y-m-d H:i:s', $val['end_time']);
- $val['goods_ids'] = json_decode($val['goods_ids']);
- $val['video_ids'] = json_decode($val['video_ids']);
- if (!empty($val['goods_ids'])) {
- $val['goodlist'] = Goods::find()->where(['store_id' => $this->store_id, 'is_delete' => 0])
- ->andWhere(['in', 'id', $val['goods_ids']])->asArray()->all();
- }
- if (!empty($val['video_ids'])) {
- $val['videolist'] = Video::find()->where(['store_id' => $this->store_id, 'is_delete' => Video::IS_DELETE_NO])
- ->andWhere(['in', 'id', $val['video_ids']])->asArray()->all();
- }
- $share_user = VerifyShareUser::find()->alias('vsu')
- ->leftJoin(['u' => User::tableName()], 'u.id=vsu.user_id')
- ->where(['verify_card_id' => $val['id']])->select('u.id,u.nickname')->asArray()->all();
- $val['share_user'] = $share_user;
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'data' => $list['list'],
- 'pageNo' => $list['pageNo'],
- 'totalCount' => $list['totalCount']
- ]
- ];
- }
- public function searchVerifyShareList($id)
- {
- $query = VerifyShareLog::find()->alias('vsl')
- ->leftJoin(['vcs' => VerifyCardSale::tableName()], 'vsl.sale_id=vcs.id')
- ->leftJoin(['vc' => VerifyCard::tableName()], 'vsl.verify_card_id=vc.id')
- ->leftJoin(['u' => User::tableName()], 'vsl.share_user_id=u.id')
- ->leftJoin(['u2' => User::tableName()], 'vcs.user_id=u2.id')
- ->where(['vsl.store_id' => get_store_id(), 'vsl.verify_card_id' => $id])
- ->select('vc.name as verify_card_name,u.nickname as share_nickname,u2.nickname as lq_nickname,vsl.create_time');
- $list = pagination_make($query);
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'data' => $list['list'],
- 'pageNo' => $list['pageNo'],
- 'totalCount' => $list['totalCount']
- ]
- ];
- }
- public function searchVerifyCardOrderGetList()
- {
- $query = Order::find()->alias('o')
- ->innerJoin(['vcs' => VerifyCardSale::tableName()], 'FIND_IN_SET(vcs.id, o.get_verify_id)')
- ->innerJoin(['vc' => VerifyCard::tableName()], 'vc.id = vcs.verify_card_id')
- ->leftJoin(['u' => User::tableName()], 'u.id = o.user_id')
- ->leftJoin(['su' => SaasUser::tableName()], 'u.binding = su.mobile');
-
- $query->andWhere(['o.store_id' => $this->store_id]);
- if ($this->id > 0) {
- $query->andWhere(['vc.id' => $this->id]);
- }
-
- if($this->user_nickname){
- $query->andWhere(['like', 'su.name', $this->user_nickname]);
- }
-
- if($this->user_mobile){
- $query->andWhere(['like', 'su.mobile', $this->user_mobile]);
- }
-
- if($this->name){
- $query->andWhere(['like', 'vc.name', $this->name]);
- }
-
- if($this->order_no){
- $query->andWhere(['like', 'o.order_no', $this->order_no]);
- }
- if (!empty($this->start_time)) {
- $query->andWhere(['>' , 'vcs.sale_time', strtotime($this->start_time)]);
- }
- if (!empty($this->end_time)) {
- $query->andWhere(['<' , 'vcs.sale_time', strtotime($this->end_time)]);
- }
- $query->groupBy('order_id, vc_id');
- $query->orderBy('o.id DESC');
- $query->select('vcs.sale_time, o.id order_id, o.order_no, o.user_id, su.avatar, su.name, su.mobile, vc.id vc_id, vc.name vc_name, vcs.id vcs_id, count(1) count, GROUP_CONCAT(vcs.id) vcs_id_list');
- $list = pagination_make($query);
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $list,
- 'q' => $query->createCommand()->getRawSql(),
- ];
- }
- public function searchVerifyCardLogList()
- {
- $query = VerifyCardLog::find()->alias('vcl')
- ->leftJoin(['vcs' => VerifyCardSale::tableName()], 'vcl.sale_id=vcs.id')
- ->leftJoin(['vc' => VerifyCard::tableName()], 'vcs.verify_card_id=vc.id')
- ->leftJoin(['u' => User::tableName()], 'vcl.user_id=u.id')
- ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile=u.binding')
- ->where(['vcl.store_id' => get_store_id()])
- ->select('vc.price, vcl.id vcl_id, vcs.id vcs_id, vc.name name, vcl.user_id, su.name user_nickname, su.avatar user_avatar, su.mobile user_mobile, vcl.use_time');
-
- $query->orderBy('vcl.id DESC');
- if ($this->type > 0) {
- $query->andWhere(['vc.type' => $this->type]);
- }
-
- if ($this->id > 0) {
- $query->andWhere(['vc.id' => $this->id]);
- }
-
- if($this->user_nickname){
- $query->andWhere(['like', 'su.name', $this->user_nickname]);
- }
-
- if($this->user_mobile){
- $query->andWhere(['like', 'su.mobile', $this->user_mobile]);
- }
-
- if($this->name){
- $query->andWhere(['like', 'vc.name', $this->name]);
- }
- if (!empty($this->start_time)) {
- $query->andWhere(['>' , 'vcl.use_time', strtotime($this->start_time)]);
- }
- if (!empty($this->end_time)) {
- $query->andWhere(['<' , 'vcl.use_time', strtotime($this->end_time)]);
- }
-
- if($this->type == 1){
- $query->leftJoin(['u2' => User::tableName()], 'vcl.verify_person=u2.id')
- ->leftJoin(['su2' => SaasUser::tableName()], 'su2.mobile=u2.binding')
- ->leftJoin(['md' => Md::tableName()], 'vcl.shop_id=md.id')
- ->addSelect('su2.name verify_person_nickname, su2.avatar verify_person_avatar, su2.mobile verify_person_mobile, md.name md_name');
-
- if($this->md_name){
- $query->andWhere(['like', 'md.name', $this->md_name]);
- }
-
- if($this->verify_person_nickname){
- $query->andWhere(['like', 'su2.name', $this->verify_person_nickname]);
- }
- }
- if($this->type == 2){
- $query->leftJoin(['o' => Order::tableName()], 'vcs.id = o.verify_card_id')
- ->addSelect('o.id order_id, o.order_no');
-
- if($this->order_no){
- $query->andWhere(['like', 'o.order_no', $this->order_no]);
- }
- }
-
-
- $query->andWhere(['vcl.type' => [VerifyCardLog::WRITE_TYPE_WRITE_OFF, VerifyCardLog::WRITE_TYPE_EXCHANGE]]);
- if($this->export = input_params('export', 0)){
- $export_list = $query->asArray()->all();
- return $this->export($export_list);
- }
- $list = pagination_make($query);
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $list,
- 'q' => $query->createCommand()->getRawSql(),
- ];
- }
- private function export($list)
- {
- $rows = [[
- 'ID',
- '用户名称',
- '手机号',
- '核销卡名称',
- '核销门店',
- '核销店员名称',
- '核销店员手机号',
- '核销时间',
- ]];
- foreach($list as $item){
- $r = [
- $item['vcl_id'],
- $item['user_nickname'],
- $item['user_mobile'],
- $item['name'],
- $item['md_name'],
- $item['verify_person_nickname'],
- $item['verify_person_mobile'],
- $item['use_time']? date('Y-m-d H:i:s', $item['use_time']):'',
- ];
- $rows[] = $r;
- }
- $writer = \Spatie\SimpleExcel\SimpleExcelWriter::streamDownload(time() . '.xlsx')->noHeaderRow()
- ->addRows($rows)->toBrowser();
- }
- public function verifyTmp()
- {
- $row = [
- // '账号',
- // '密码',
- ];
- $id = $this->id;
- $vc = VerifyCard::findOne($id);
- if($vc['form']){
- $form = json_decode($vc['form'], true);
- foreach($form['list'] as $item){
- $row[] = $item['name'];
- }
- }
-
-
- $rows = [$row];
- $writer = \Spatie\SimpleExcel\SimpleExcelWriter::streamDownload(time() . '.xlsx')->noHeaderRow()
- ->addRows($rows)->toBrowser();
- }
- public function delVerifyCard()
- {
- if ($this->validate()) {
- if ($this->scenario !== self::SCENARIO_DEL) {
- return [
- 'code' => 1,
- 'msg' => '删除失败'
- ];
- }
- $model = VerifyCard::findOne(['id' => $this->id]);
- $model->is_delete = VerifyCard::IS_DELETE_YES;
- if (!$model || !$model->save()) {
- return [
- 'code' => 1,
- 'msg' => $model->getErrorSummary(false)[0]
- ];
- }
- return [
- 'code' => 0,
- 'msg' => '删除成功'
- ];
- // 所有输入数据都有效 all inputs are valid
- } else {
- // 验证失败:$errors 是一个包含错误信息的数组
- return [
- 'code' => 1,
- "msg" => $this->getErrorSummary(false)[0]
- ];
- }
- }
- public function getAccountInfo()
- {
- try {
- if (empty($this->id)) {
- throw new \Exception("ID不能为空");
- }
- $query = VerifyCardAccount::find()->where([
- 'store_id' => get_store_id(),
- 'card_id' => $this->id,
- ])
- ->orderBy('id DESC');//->asArray()->all();
- $page = pagination_make($query);
- $data['list'] = $page['list'];
- $data['pageNo'] = $page['pageNo'];
- $data['totalCount'] = $page['totalCount'];
- foreach ($data['list'] as $k => $item) {
- $sale_card = VerifyCardSale::find()->where(['account_id' => $item['id']])->select('id,status as sale_status')->orderBy('id DESC')->asArray()->one();
- // status 是核销卡领取状态 sale_status是领取后的使用状态
- $data['list'][$k]['sale_status'] = empty($sale_card) ? 0 :$sale_card['sale_status'];
- if (!empty($sale_card)) {
- $VerifyCardLog = VerifyCardLog::find()->where(['sale_id' => $sale_card['id']])->select('id, shop_id, verify_person, use_time,saas_verify_person')->asArray()->all();
- if (!empty($VerifyCardLog)) {
- foreach ($VerifyCardLog as &$log_item) {
- $log_item['use_time'] = date("Y-m-d H:i:s", $log_item['use_time']);
- if (!empty($log_item['shop_id'])) {
- $shop = Md::findOne($log_item['shop_id']);
- $log_item['shop_id'] = $shop->name ?: "";
- }
- if(empty($log_item['verify_person'])){
- $saasUser = SaasUser::find()->where(['id' => $log_item['saas_verify_person']])->select('id, name')->one();
- if (!empty($saasUser)) {
- $log_item['verify_person'] = $saasUser->name;
- }
- }else{
- $user = User::find()->where(['id' => $log_item['verify_person']])->select('id, nickname')->one();
- if (!empty($user)) {
- $log_item['verify_person'] = $user->nickname;
- }
- }
- }
- $data['list'][$k]['card_log'][] = $log_item;
- }
- }
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $data,
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- "msg" => $e->getMessage()
- ];
- }
- }
- /**
- * 获取卡券详情
- * @return array
- * @throws \yii\base\Exception
- */
- public function verifyDetail() {
- if ($this->id) {
- // 卡券详情
- $data = VerifyCard::find()->where(['id' => $this->id, 'store_id' => $this->store_id])->asArray()->one();
- $data['begin_time'] = date('Y-m-d H:i:s', $data['begin_time']);
- $data['end_time'] = date('Y-m-d H:i:s', $data['end_time']);
- $data['created_at'] = date('Y-m-d H:i:s', $data['created_at']);
- $data['updated_at'] = date('Y-m-d H:i:s', $data['updated_at']);
- $query = VerifyCardAccount::find()->where([
- 'store_id' => $this->store_id,
- 'card_id' => $this->id,
- ])->select('account, password, status, id');//->asArray()->all();
- $page = pagination_make($query);
- $data['account_info']['list'] = $page['list'];
- $data['account_info']['pageNo'] = $page['pageNo'];
- $data['account_info']['totalCount'] = $page['totalCount'];
- $data['goods_ids'] = json_decode($data['goods_ids'], true);
- $data['video_ids'] = json_decode($data['video_ids'], true);
- $data['use_num'] = $data['use_num'] == '' ? 0 : $data['use_num'];
- //系统后台 核销卡详情,显示具体核销 门店信息、员工信息、核销时间(精确到秒)的信息
- foreach ($data['account_info']['list'] as &$item) {
- $sale_card = VerifyCardSale::find()->where(['account_id' => $item['id']])->select('id')->asArray()->one();
- // status 是核销卡领取状态 sale_status是领取后的使用状态
- $item['sale_status'] = empty($sale_card) ? 0 :$sale_card['sale_status'];
- if (!empty($sale_card)) {
- $VerifyCardLog = VerifyCardLog::find()->where(['sale_id' => $sale_card['id']])->select('id, shop_id, verify_person, use_time,saas_verify_person')->asArray()->all();
- if (!empty($VerifyCardLog)) {
- foreach ($VerifyCardLog as &$log_item) {
- $log_item['use_time'] = date("Y-m-d H:i:s", $log_item['use_time']);
- if (!empty($log_item['shop_id'])) {
- $shop = Md::findOne($log_item['shop_id']);
- $log_item['shop'] = $shop->name ?: "";
- }
- if(empty($log_item['verify_person'])){
- $saasUser = SaasUser::find()->where(['id' => $log_item['saas_verify_person']])->select('id, name')->one();
- $log_item['verify_person'] = $saasUser->name;
- }else{
- $User = User::find()->where(['id' => $log_item['verify_person']])->select('id, nickname')->asArray()->one();
- $log_item['verify_person'] = $User->nickname;
- }
- }
- $item['card_log'] = $VerifyCardLog;
- }
- }
- }
- // 卡券类型
- if (!empty($data['goods_ids'])) {
- $data['goodlist'] = Goods::find()->where(['store_id' => $this->store_id, 'is_delete' => 0])
- ->andWhere(['in', 'id', $data['goods_ids']])->asArray()->all();
- }
- if (!empty($data['video_ids'])) {
- $data['videolist'] = Video::find()->where(['store_id' => $this->store_id, 'is_delete' => Video::IS_DELETE_NO])
- ->andWhere(['in', 'id', $data['video_ids']])->asArray()->all();
- }
- // 卡券领取记录
- $query = VerifyCardSale::find()->alias('vc')
- ->leftJoin(['u' => User::tableName()], 'vc.user_id = u.id')
- ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile = u.binding')
- ->where(['vc.store_id' => $this->store_id, 'vc.is_delete' => VerifyCardSale::IS_DELETE_NO, 'vc.verify_card_id' => $this->id]);
- $query->select('vc.*');
-
- if($this->user_nickname){
- $query->andWhere(['like', 'su.name', $this->user_nickname]);
- }
- if($this->user_mobile){
- $query->andWhere(['like', 'u.binding', $this->user_mobile]);
- }
- if (!empty($this->start_time)) {
- $query->andWhere(['>' , 'vc.sale_time', strtotime($this->start_time)]);
- }
- if (!empty($this->end_time)) {
- $query->andWhere(['<' , 'vc.sale_time', strtotime($this->end_time)]);
- }
- $query->orderBy('vc.id DESC');
- $list = pagination_make($query);
- foreach ($list['list'] as &$val) {
- $user = User::find()->where(['id' => $val['user_id']])->select('binding, avatar_url, nickname, platform')->asArray()->one();
- if(!$user){ continue; }
- $sassuser = SaasUser::find()->where(['mobile' => $user['binding']])->select('name, avatar')->asArray()->one();
- $val['avatar_url'] = !empty($sassuser['avatar']) ? $sassuser['avatar'] : $user['avatar_url'];
- $val['nickname'] = !empty($sassuser['name']) ? $sassuser['name'] : $user['nickname'];
- $val['mobile'] = $user['binding'];
- $val['platform'] = self::USER_PLATFORM_TYPE[$user['platform']];
- $val['card_info'] = [];
- if ($val['account_id'] > 0) {
- $val['card_info'] = VerifyCardAccount::findOne($val['account_id']);
- }
- $val['created_at']= date('Y-m-d H:i:s', $val['created_at']);
- }
- } else {
- return [
- 'code' => 1,
- 'msg' => '参数错误',
- ];
- }
- $list = [
- 'data' => $list['list'],
- 'pageNo' => $list['pageNo'],
- 'totalCount' => $list['totalCount'],
- ];
- if($data['send_times'] != $list['totalCount']){
- $c = VerifyCard::find()->where(['id' => $this->id, 'store_id' => $this->store_id])->one();
- $c->send_times = $list['totalCount'];
- $c->save();
- $data['send_times'] = $list['totalCount'];
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $data,
- 'list' => $list,
- ];
- }
- public static function getVerifyCardList(){
- return VerifyCard::find()->where(['is_delete' => VerifyCard::IS_DELETE_NO])->orderBy(['sort desc, id desc'])->select('*')->asArray()->all();
- }
- // 生成卡密
- public function generateAccount()
- {
- $verifyCard = VerifyCard::findOne($this->id);
- if (!$verifyCard) {
- return [
- 'code' => 1,
- 'msg' => '卡券不存在',
- ];
- }
- if ($this->num > 0) {
- for ($i = 0; $i < $this->num; $i++) {
- try {
- $account = new VerifyCardAccount();
- $account->store_id = $this->store_id;
- $account->card_id = $this->id;
- $account->account = 'TX'. mt_rand(10000000, 99999999);
- $account->password = $this->makeCardPassword();
- $account->status = VerifyCardAccount::STATUS_UNUSED;
- $account->save();
- } catch (\Throwable $throwable) {}
- }
- $count = VerifyCardAccount::find()->where([
- 'store_id' => $this->store_id,
- 'card_id' => $this->id,
- ])->count();
- $verifyCard->num = $count;
- $verifyCard->save();
- return [
- 'code' => 0,
- 'msg' => '生成成功',
- ];
- } else {
- return [
- 'code' => 1,
- 'msg' => '数量必须大于0',
- ];
- }
- }
- // 生成卡密
- public function makeCardPassword()
- {
- $code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
- $rand = $code[rand(0, 25)]
- . strtoupper(dechex(date('m')))
- . date('d') . substr(time(), -5)
- . substr(microtime(), 2, 5)
- . sprintf('%02d', rand(0, 99));
- for (
- $a = md5($rand, true),
- $s = '0123456789ABCDEFGHIJKLMNOPQRSTUV',
- $d = '',
- $f = 0;
- $f < 8;
- $g = ord($a[$f]),
- $d .= $s[($g ^ ord($a[$f + 8])) - $g & 0x1F],
- $f++
- ) ;
- return $d;
- }
- public function changePass($id, $password) {
- $pass = trim($password);
- if(strlen($pass) <= 2){
- return [
- 'code' => 1,
- 'msg' => '密码格式不正确',
- ];
- }
- $account_info = VerifyCardAccount::findOne($id);
- $account_info->password = $pass;
- $save = $account_info->save();
- return [
- 'code'=> $save ? 0 : 1,
- 'msg' => $save ? '成功' : '失败',
- ];
- }
- public function vcAccountSave($list) {
- try{
- foreach($list as $i => $item){
- if($item['id']){
- $account_info = VerifyCardAccount::findOne($item['id']);
- $account_info->account = $item['account'];
- $pass = trim($item['password']);
- if(strlen($pass) <= 2){
- throw new \Exception('密码格式不正确');
- }
- $account_info->password = $pass;
- }else{
- $account_info = new VerifyCardAccount();
- $account_info->store_id = $this->store_id;
- $account_info->card_id = $item['card_id'];
- $account_info->account = 'TX'. mt_rand(10000000, 99999999);
- $account_info->password = $this->makeCardPassword();
- }
- $item['form'] && $account_info->form = json_encode($item['form'], true);
- $save = $account_info->save();
- if(!$save){
- throw new \Exception(array_shift($account_info->getFirstErrors()));
- }
- $verifyCard = VerifyCard::findOne($item['card_id']);
- $dbcount = VerifyCardAccount::find()->where(['card_id' => $verifyCard->id])->count();
- $verifyCard->num = $dbcount;
- $verifyCard->save();
- }
- } catch (\Exception $e) {
- return [
- 'code'=> 1,
- 'msg' => '出现错误,成功' . $i . '条数据,错误位置:卡号,' . $item['account'] . ',错误原因,' . $e->getMessage(),
- ];
- }
- return [
- 'code'=> 0,
- 'msg' => '操作成功',
- ];
- }
- //导入卡密
- public function upload() {
- set_time_limit(0);
- $verifyCard = VerifyCard::findOne($this->id);
- if (!$verifyCard) {
- return [
- 'code' => 1,
- 'msg' => '卡券不存在',
- ];
- }
- $filename = $_FILES['excel']['name'];
- $tmpname = $_FILES['excel']['tmp_name'];
- $path = \Yii::$app->basePath . '/web/temp/';
- if(!is_dir($path)){
- mkdir($path);
- }
- $ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
- if (($ext != 'xlsx') && ($ext != 'xls')) {
- return [
- 'code' => 1,
- 'msg' => '请上传excel文件'
- ];
- }
- $file = time() . $this->store_id . '.' . $ext;
- $uploadfile = $path . $file;
- move_uploaded_file($tmpname, $uploadfile);
- $rows = \Spatie\SimpleExcel\SimpleExcelReader::create($uploadfile)
- ->getRows();
- $arr = [];
- $rows->each(function (array $item) use ($verifyCard, &$arr) {
- try{
- if (isset($item['账号']) && isset($item['密码']) && !empty($item['账号']) && !empty($item['账号'])) {
- $account = new VerifyCardAccount();
- $account->store_id = $verifyCard->store_id;
- $account->card_id = $verifyCard->id;
- $account->account = (string)$item['账号'];
- $account->password = (string)$item['密码'];
-
- $res = $account->save();
- if($res){
- $arr[] = $res;
- }
- }
- if($verifyCard['form']){
- $form = json_decode($verifyCard['form'], true);
- if($form){
- $account = new VerifyCardAccount();
- $account->store_id = $verifyCard->store_id;
- $account->card_id = $verifyCard->id;
- $account->account = 'TX'. mt_rand(10000000, 99999999);
- $account->password = $this->makeCardPassword();
- foreach($form['list'] as &$k){
- $k['default'] = $item[$k['name']];
- }
- $account->form = json_encode($form);
-
- $res = $account->save();
- if($res){
- $arr[] = $res;
- }
- }
- }
- } catch (\Exception $e){
- debug_log($e->getMessage());
- }
- });
- $count = count($arr);
- $dbcount = VerifyCardAccount::find()->where(['card_id' => $verifyCard->id])->count();
- $verifyCard->num = $dbcount;
- $verifyCard->save();
- @unlink($uploadfile);
- return [
- 'code' => $count ? 0 : 1,
- 'msg' => "共导入{$count}条数据"
- ];
- }
- /* begin 2024/11/16 14:48:52 id1126开发需求:新增储值卡消费记录 WPing丶 */
- // 2025年7月12日14:13:25调整,解决补丁git冲突
- public function searchCardPayLogList()
- {
- $query = VerifyCardAccount::find()->alias('vca')
- ->leftJoin(['vcs' => VerifyCardSale::tableName()], 'vca.id=vcs.account_id OR vcs.account_id IS NULL')
- ->leftJoin(['vc' => VerifyCard::tableName()], 'vca.card_id=vc.id')
- ->leftJoin(['vcl' => VerifyCardLog::tableName()], 'vcl.sale_id=vcs.id')
- ->leftJoin(['u' => User::tableName()], 'vcl.user_id=u.id')
- ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile=u.binding')
- ->select([
- 'vc.total_price as price',
- 'vcl.id AS vcl_id',
- 'IFNULL(vcs.id, 0) AS vcs_id', // 默认值为0
- 'vc.name AS name',
- 'vcl.user_id',
- 'su.name AS user_nickname',
- 'su.avatar AS user_avatar',
- 'su.mobile AS user_mobile',
- 'vcl.use_time',
- 'vcl.use_price',
- 'vcl.use_order_ids',
- 'vca.account'
- ]);
-
- $query->orderBy('vcl.id DESC');
-
- // 搜索条件
- if ($this->user_nickname) {
- $query->andWhere(['like', 'su.name', $this->user_nickname]);
- }
- if ($this->account) {
- $query->andWhere(['like', 'vca.account', $this->account]);
- }
- if ($this->user_mobile) {
- $query->andWhere(['like', 'su.mobile', $this->user_mobile]);
- }
- if ($this->name) {
- $query->andWhere(['like', 'vc.name', $this->name]);
- }
- if (!empty($this->start_time)) {
- $query->andWhere(['>', 'vcl.use_time', strtotime($this->start_time)]);
- }
- if (!empty($this->end_time)) {
- $query->andWhere(['<', 'vcl.use_time', strtotime($this->end_time)]);
- }
- if ($this->use_type == 1) {
- $query->andWhere(['vcl.use_price' => 0]);
- }
- if ($this->use_type == 2) {
- $query->andWhere(['>', 'vcl.use_price', 0]);
- $query->andWhere(['vcl.is_complete' => 0]);
- }
- if ($this->use_type == 3) {
- $query->andWhere(['vcl.is_complete' => 1]);
- }
- if ($this->use_type == 4) {
- $query->andWhere(['IS', 'vcs.id', null]);
- }
-
- $query->andWhere(['or', ['vcl.type' => [VerifyCardLog::WRITE_TYPE_WRITE_OFF, VerifyCardLog::WRITE_TYPE_EXCHANGE]], ['vcl.type' => null]]);
- $list = pagination_make($query);
-
- foreach ($list['list'] as $k => &$item) {
- $item['order_count'] = !$item['use_order_ids'] ? 0 :count(explode(',', $item['use_order_ids']));
- $item['card_balance'] = bcsub($item['price'], $item['use_price'], 2);
- }
-
- // 获取可导出数据
- $f = new ExportList();
- $exportList = $f->getList();
- if($this->export){
- return $this->LogExport($list['list']);
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $list,
- ];
- }
- private function LogExport($list) {
- $rows = [[
- 'ID',
- '会员信息',
- '手机号',
- '储值卡卡号',
- '储值卡名称',
- '储值卡金额',
- '卡内余额',
- '已用余额',
- '使用订单数量',
- '关联订单ID',
- '使用状态',
- '激活时间',
- ]];
- foreach($list as $item) {
- if($item['use_order_ids']) {
- $order_no = implode(',', Order::find()->where(['id' => explode(',', $item['use_order_ids'])])->select('order_no')->column());
- }
- if($item['user_id']) {
- if($item['use_price'] == 0) {
- $use_type = '未使用';
- } elseif($item['use_price'] == $item['price']) {
- $use_type = '已用完';
- } else {
- $use_type = '使用中';
- }
- } else {
- $use_type = '未激活';
- }
- $r = [
- $item['vcl_id'],
- $item['user_nickname'],
- $item['user_mobile'],
- $item['account'],
- $item['name'],
- $item['price'],
- $item['card_balance'],
- $item['use_price'],
- $item['order_count'],
- $order_no,
- $use_type,
- $item['use_time'] ? date('Y-m-d H:i:s',$item['use_time']) : '/',
- ];
- $rows[] = $r;
- }
- // array_unshift($list, $rows);
- $writer = \Spatie\SimpleExcel\SimpleExcelWriter::streamDownload(time() . '.xlsx')->noHeaderRow()
- ->addRows($rows)->toBrowser();
- }
- /* end */
- }
|