validate()) { return [ 'code' => 1, 'msg' => $this->getErrorSummary(false)[0] ]; } $saas_id = get_saas_user_id(); $verifyCard = VerifyCard::find(['id'=>$this->verify_card_id,'is_business'=>1])->asArray()->one(); if(!$verifyCard) { return [ 'code' => 1, 'msg' => '卡券不存在', ]; } $verifyInfo = VerifyCardSale::find()->where(['saas_id'=>$saas_id,'verify_card_id'=>$this->verify_card_id])->one(); if ($verifyInfo && $verifyInfo->id > 0) { return [ 'code' => 1, 'msg' => '您已领取过' ]; } $info = $this->generateAccount($verifyCard); if($info && $info['account_id'] > 0){ $verify_form = new VerifyCardSale(); $verify_form->store_id = $verifyCard['store_id']; $verify_form->user_id = 0; $verify_form->saas_id = $saas_id; $verify_form->verify_card_id = $this->verify_card_id; $verify_form->left_num = 1; $verify_form->account_id = $info['account_id']; $verify_form->sale_time = time(); if ($verifyCard['date_type'] == 1) { $verify_form->end_time = time() + $verifyCard['expire_day'] * (3600*24); } else { $verify_form->end_time = $verifyCard['end_time']; } if($verify_form->save() ) { return [ 'code' => 0, 'msg' => '领取成功' ]; }else{ return [ 'code' => 1, 'msg' => '领取失败' ]; } } } /** * 转换日期格式为date */ public function dateFormat($date) { return date('Y-m-d', $date); } // 生成卡密 public function generateAccount($verifyInfo) { $account = new VerifyCardAccount(); $account->store_id = $verifyInfo['store_id']; $account->card_id = $verifyInfo['id']; $account->account = 'TX'. mt_rand(10000000, 99999999); $account->password = $this->makeCardPassword(); $account->status = VerifyCardAccount::STATUS_UNUSED; $account->save(); if($account->id > 0){ $count = VerifyCardAccount::find()->where([ 'store_id' => $verifyInfo['store_id'], 'card_id' => $verifyInfo['id'], ])->count(); $verifyCard = VerifyCard::findOne($verifyInfo['id']); $verifyCard->num = $count; $verifyCard->save(); return [ 'code' => 0, 'account_id' => $account->id ]; }else{ return [ 'code' => 1, 'account_id' => 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 search() { if (!$this->validate()) { return [ 'code' => 1, 'msg' => $this->getErrorSummary(false)[0] ]; } // 获取可以使用的核销卡 $verifySale = VerifyCardSale::find() ->where(['saas_id' => $this->saas_id,'status' => 0]) ->andWhere(['is_delete' => 0]) ->asArray() ->all(); // 更新个人核销卡使用状态 foreach ($verifySale as &$val) { // 核销卡详情 $verifycard = VerifyCard::findOne(['id' => $val['verify_card_id'], 'is_delete' => VerifyCard::IS_DELETE_NO ,'is_business'=>1]); 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()->where(['store_id' => $this->store_id]) // ->andWhere(['in', 'id' , $arr]) // ->orderBy('sale_time desc') // ->asArray() // ->all(); // } if ($this->status == 1) { $list = VerifyCardSale::find()->alias('vcs') ->leftJoin(['vc' => VerifyCard::tableName()], 'vcs.verify_card_id = vc.id') ->where(['vcs.saas_id' => $this->saas_id]) ->andWhere(['in', 'vcs.status', [1,2]]) ->andWhere(['<>', 'vcs.is_delete', 1]) ->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.saas_id' => $this->saas_id]) ->andWhere('vcs.status=0 or (vc.type=5 and vcs.status=1 and vcs.video_status=0)') ->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), ]; } 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([ 'saas_verify_person' => $this->saas_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([ 'saas_verify_person' => $this->saas_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($item['saas_verify_person']); // $item['verify_person'] = $SaasUser->name; // } else { $item['shop'] = ""; $item['verify_person'] = $SaasUser->name; //} } return [ 'code' => 0, 'msg' => 'success', 'data' => [ 'log_list' => $log_info ], 'num' => count($log_info) ]; } /** * 卡券详情 * @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'], ])->asArray()->one(); $account_info = VerifyCardAccount::findOne($sale_info['account_id']); $sale_info['card_info'] = [ 'user' => $account_info->account, 'password' => $account_info->password ]; // // 是否可以转赠 // $is_can_give = true; // if (time() >= $sale_info['end_time']) { // $is_can_give = false; // } // if ($sale_info['status'] == 1 || $sale_info['status'] == 2) { // $is_can_give = false; // } // if ($verify_card_info['is_give'] == 0) { // $is_can_give = false; // } //商盟核销卡不能转赠 $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($sale_info['store_id']); $arr['coordinate'] = $store_info->coordinate; $arr['address'] = $store_info->address; $arr['name'] = $store_info->name; $arr['mobile'] = $store_info->contact_tel; // } else { // $md = Md::find()->where(['store_id' => get_store_id(), 'md' => get_md_id()])->one(); // $arr['coordinate'] = [ // $md->latitude, // $md->longitude // ]; // $arr['address'] = $md->address; // $arr['name'] = $md->name; // } return [ 'code' => 0, 'msg' => 'success', 'data' => [ 'list' => $sale_info, 'store_info' => $arr ], ]; } /** * 删除核销卡(更新状态) */ 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' => '删除成功' ]; } } 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(); $logs = VerifyCardLog::find()->alias('vcl') ->leftJoin(['s' => Store::tableName()], 'vcl.store_id = s.id')->where([ 'vcl.store_id' => $sale->store_id, 'vcl.sale_id' => $this->sale_id, 'vcl.is_delete' => 0, 'vcl.type' => VerifyCardLog::WRITE_TYPE_WRITE_OFF ])->select('vcl.use_time, s.name as store_name')->asArray()->all(); return [ 'code' => 0, 'data' => $logs, ]; } return [ 'code' => 1, 'msg' => '缺少参数', ]; } }