VerifyIndexForm.php 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\client\models\v1;
  8. use app\models\AccountLog;
  9. use app\models\Goods;
  10. use app\models\SaasUser;
  11. use app\models\Store;
  12. use app\models\VerifyCard;
  13. use app\models\VerifyCardSale;
  14. use app\models\Order;
  15. use app\models\VerifyShareLog;
  16. use app\models\VerifyShareUser;
  17. use app\utils\OrderNo;
  18. use app\models\User;
  19. use app\models\Recharge;
  20. use app\models\Shop;
  21. use app\models\Address;
  22. use app\models\VerifyCardAccount;
  23. use app\models\VerifyCardLog;
  24. use app\models\Video;
  25. use Think\Verify;
  26. use yii\base\BaseObject;
  27. use yii\base\Model;
  28. use app\models\Md;
  29. use yii\data\Pagination;
  30. class VerifyIndexForm extends Model
  31. {
  32. public $store_id;
  33. public $user_id;
  34. public $status;
  35. public $id;
  36. public $ids;
  37. public $verify_card_id;
  38. public $account;
  39. public $cardpwd;
  40. public $sale_id;
  41. public $order_no;
  42. public $card_info;
  43. public $created_at;
  44. public $sale_time;
  45. public $end_time;
  46. public $type;
  47. public $share_user_id;
  48. public function rules()
  49. {
  50. return [
  51. [['status', 'id', 'verify_card_id', 'sale_id', 'order_no','store_id', 'share_user_id'], 'integer'],
  52. [['account', 'cardpwd', 'card_info', 'ids'], 'string'],
  53. [['account', 'cardpwd'], 'trim'],
  54. [['status', 'type'], 'safe']
  55. ];
  56. }
  57. public function search()
  58. {
  59. if (!$this->validate()) {
  60. return [
  61. 'code' => 1,
  62. 'msg' => $this->getErrorSummary(false)[0]
  63. ];
  64. }
  65. // 获取可以使用的核销卡
  66. $verifySale = VerifyCardSale::find()
  67. ->where(['user_id' => $this->user_id, 'store_id' => $this->store_id, 'status' => 0])
  68. ->andWhere(['is_delete' => 0])
  69. ->asArray()
  70. ->all();
  71. // 更新个人核销卡使用状态
  72. foreach ($verifySale as &$val) {
  73. // 核销卡详情
  74. $verifycard = VerifyCard::findOne(['id' => $val['verify_card_id'], 'store_id' => $this->store_id, 'is_delete' => VerifyCard::IS_DELETE_NO]);
  75. if ($verifycard['date_type'] == 1) {
  76. // 有效期
  77. $expire_day = $verifycard['expire_day'] * 24 * 60 * 60;
  78. if ($expire_day > 0) { // 永久有效不更新
  79. // expire_day天后的日期
  80. $expire = intval($val['created_at']) + $expire_day; // 过期时间
  81. if (time() > $expire) {
  82. if ($verifycard['type'] == 5) {
  83. VerifyCardSale::updateAll(['status' => 1, 'video_status' => 1], 'id=' . $val['id']);
  84. } else {
  85. VerifyCardSale::updateAll(['status' => 1], 'id=' . $val['id']);
  86. }
  87. }
  88. }
  89. }
  90. if ($verifycard['date_type'] == 2) {
  91. if (time() > intval($verifycard['end_time'])) {
  92. if ($verifycard['type'] == 5) {
  93. VerifyCardSale::updateAll(['status' => 1, 'video_status' => 1], 'id=' . $val['id']);
  94. } else {
  95. VerifyCardSale::updateAll(['status' => 1], 'id=' . $val['id']);
  96. }
  97. }
  98. }
  99. }
  100. if ($this->status == 2) {
  101. $arr = [];
  102. $verifylog = VerifyCardLog::find()->where(['user_id' => $this->user_id, 'type' => VerifyCardLog::WRITE_TYPE_GIVE])->groupBy('sale_id')->asArray()->all();
  103. foreach ($verifylog as &$val) {
  104. $arr[] = $val['sale_id'];
  105. }
  106. $list = VerifyCardSale::find()->alias('vcs')
  107. ->leftJoin(['vc' => VerifyCard::tableName()], 'vcs.verify_card_id = vc.id')
  108. ->where(['vcs.store_id' => $this->store_id, 'vcs.is_delete' => 0, 'vc.is_delete' => 0])
  109. ->andWhere(['in', 'vcs.id' , $arr]);
  110. if($this->type){
  111. $list->andWhere(['vc.type' => $this->type]);
  112. }
  113. $list = $list->orderBy('vcs.sale_time desc')
  114. ->select('vcs.*, vc.type')
  115. ->asArray()
  116. ->all();
  117. } elseif ($this->status == 1) {
  118. $list = VerifyCardSale::find()->alias('vcs')
  119. ->leftJoin(['vc' => VerifyCard::tableName()], 'vcs.verify_card_id = vc.id')
  120. ->where(['vcs.user_id' => $this->user_id, 'vcs.store_id' => $this->store_id])
  121. ->andWhere(['in', 'vcs.status', [1,2]])
  122. ->andWhere(['vcs.is_delete' => 0, 'vc.is_delete' => 0]);
  123. if($this->type){
  124. $list->andWhere(['vc.type' => $this->type]);
  125. }
  126. $list = $list->orderBy('vcs.sale_time desc')
  127. ->select('vcs.*, vc.type')
  128. ->asArray()
  129. ->all();
  130. foreach ($list as $key => $value) {
  131. if ($value['type'] == 5 && $value['video_status'] == 0) {
  132. unset($list[$key]);
  133. }
  134. }
  135. $list = array_values($list);
  136. } else {
  137. // 获取核销卡列表
  138. $list = VerifyCardSale::find()->alias('vcs')
  139. ->leftJoin(['vc' => VerifyCard::tableName()], 'vcs.verify_card_id = vc.id')
  140. ->where(['vcs.user_id' => $this->user_id, 'vcs.store_id' => $this->store_id, 'vcs.is_delete' => 0, 'vc.is_delete' => 0])
  141. ->andWhere('vcs.status=0 or (vc.type=5 and vcs.status=1 and vcs.video_status=0)');
  142. if($this->type){
  143. $list->andWhere(['vc.type' => $this->type]);
  144. }
  145. $list = $list->orderBy('vcs.sale_time desc')
  146. ->select('vcs.*, vc.type')
  147. ->asArray()
  148. ->all();
  149. }
  150. foreach ($list as &$val) {
  151. $cardinfo = VerifyCard::findOne(['id' => $val['verify_card_id']]);
  152. $val['name'] = $cardinfo['name'];
  153. $val['expire_day'] = $cardinfo['expire_day'];
  154. $val['begin_time'] = $cardinfo['begin_time'] == 0 ? '' : $this->dateFormat($cardinfo['begin_time']);
  155. $val['date_type'] = $cardinfo['date_type'];
  156. $val['type'] = $cardinfo['type'];
  157. $val['pic_url'] = $cardinfo['pic_url'];
  158. $val['bg_pic_url'] = $cardinfo['bg_pic_url'];
  159. $val['created_at'] = $val['created_at'] == 0 ? '' : $this->dateFormat($val['created_at']);
  160. $val['updated_at'] = $val['updated_at'] == 0 ? '' : $this->dateFormat($val['updated_at']);
  161. $val['sale_time'] = $val['sale_time'] == 0 ? '' : date("Y-m-d H:i:s", $val['sale_time']);
  162. $val['end_time'] = $val['end_time'] == 0 ? '' : $this->dateFormat($val['end_time']);
  163. }
  164. return [
  165. 'code' => 0,
  166. 'msg' => 'success',
  167. 'data' => $list,
  168. 'num' => count($list),
  169. ];
  170. }
  171. /**
  172. * 卡券详情
  173. * @return array
  174. */
  175. public function detail()
  176. {
  177. if (!$this->validate()) {
  178. return [
  179. 'code' => 1,
  180. 'msg' => $this->getErrorSummary(false)[0]
  181. ];
  182. }
  183. $sale_info = VerifyCardSale::find()->where([
  184. 'id' => $this->sale_id,
  185. ])->asArray()->one();
  186. //查询核销卡信息
  187. $verify_card_info = VerifyCard::find()->where([
  188. 'id' => $sale_info['verify_card_id'],
  189. 'store_id' => $this->store_id,
  190. ])->asArray()->one();
  191. $account_info = VerifyCardAccount::findOne($sale_info['account_id']);
  192. $sale_info['card_info'] = [
  193. 'user' => $account_info->account,
  194. 'password' => $account_info->password,
  195. 'form' => $account_info->form,
  196. ];
  197. // 是否可以转赠
  198. $is_can_give = true;
  199. // if ($verify_card_info['type'] == 1) {
  200. // if ($sale_info['left_num'] != $verify_card_info['total_num']) {
  201. // $is_can_give = false;
  202. // }
  203. // }
  204. // if ($verify_card_info['date_type'] == 1) {
  205. // // 有效期
  206. // $expire_day = (int)$verify_card_info['expire_day'] * 24 * 60 * 60;
  207. // // 过期时间
  208. // $expire = $verify_card_info['created_at'] + $expire_day;
  209. // if (time() >= $expire) {
  210. // $is_can_give = false;
  211. // }
  212. // }
  213. // if ($verify_card_info['date_type'] == 2) {
  214. // if (time() >= $verify_card_info['end_time']) {
  215. // $is_can_give = false;
  216. // }
  217. // }
  218. if (time() >= $sale_info['end_time']) {
  219. $is_can_give = false;
  220. }
  221. if ((int)$sale_info['status'] === 1 || (int)$sale_info['status'] === 2) {
  222. $is_can_give = false;
  223. }
  224. if ((int)$verify_card_info['is_give'] === 0) {
  225. $is_can_give = false;
  226. }
  227. $sale_info['is_can_give'] = intval($is_can_give);
  228. $sale_info['sale_time'] = $sale_info['sale_time'] == 0 ? '' : $this->dateFormat($sale_info['sale_time']);
  229. $sale_info['end_time'] = $sale_info['end_time'] == 0 ? '' : $this->dateFormat($sale_info['end_time']);
  230. $sale_info['created_at'] = $sale_info['created_at'] == 0 ? '' : $this->dateFormat($sale_info['created_at']);
  231. $sale_info['updated_at'] = $sale_info['updated_at'] == 0 ? '' : $this->dateFormat($sale_info['updated_at']);
  232. $verify_card_info['begin_time'] = $verify_card_info['begin_time'] == 0 ? '' : $this->dateFormat($verify_card_info['begin_time']);
  233. $verify_card_info['end_time'] = $verify_card_info['end_time'] == 0 ? '' : $this->dateFormat($verify_card_info['end_time']);
  234. $verify_card_info['created_at'] = $verify_card_info['created_at'] == 0 ? '' : $this->dateFormat($verify_card_info['created_at']);
  235. $verify_card_info['updated_at'] = $verify_card_info['updated_at'] == 0 ? '' : $this->dateFormat($verify_card_info['updated_at']);
  236. $verify_card_info['goods_ids'] = json_decode($verify_card_info['goods_ids']);
  237. $verify_card_info['video_ids'] = json_decode($verify_card_info['video_ids']);
  238. $sale_info['verify_card_info'] = $verify_card_info;
  239. \Yii::warning($sale_info);
  240. $arr = [];
  241. if (get_md_id() == "-1" || !get_md_id()) {
  242. $store_info = Store::findOne(get_store_id());
  243. $arr['coordinate'] = $store_info->coordinate;
  244. $arr['address'] = $store_info->address;
  245. $arr['name'] = $store_info->name;
  246. } else {
  247. $md = Md::find()->where(['store_id' => get_store_id(), 'id' => get_md_id()])->one();
  248. $arr['coordinate'] = [
  249. $md->latitude,
  250. $md->longitude
  251. ];
  252. $arr['address'] = $md->address;
  253. $arr['name'] = $md->name;
  254. }
  255. $form = new WriteOffCodeForm();
  256. $form->sale_id = $this->sale_id;
  257. $qrcode_info = $form->getQrcode();
  258. return [
  259. 'code' => 0,
  260. 'msg' => 'success',
  261. 'data' => [
  262. 'qrcode' => $qrcode_info['data'],
  263. 'list' => $sale_info,
  264. 'store_info' => $arr
  265. ],
  266. ];
  267. }
  268. public function changePass() {
  269. $pass = trim($this->cardpwd);
  270. if(strlen($pass) != 8){
  271. return [
  272. 'code' => 1,
  273. 'msg' => '密码必须是8位数的英文字母和数字',
  274. ];
  275. }
  276. $sale_info = VerifyCardSale::find()->where([
  277. 'id' => $this->sale_id,
  278. ])->asArray()->one();
  279. $account_info = VerifyCardAccount::findOne($sale_info['account_id']);
  280. $account_info->password = $pass;
  281. $save = $account_info->save();
  282. return [
  283. 'code'=> $save ? 0 : 1,
  284. 'msg' => $save ? '成功' : '失败',
  285. ];
  286. }
  287. public function order()
  288. {
  289. if (!$this->validate()) {
  290. return [
  291. 'code' => 1,
  292. 'msg' => $this->getErrorSummary(false)[0]
  293. ];
  294. }
  295. $order_info = VerifyCardSale::find()->where([
  296. 'id' => $this->order_no,
  297. 'is_delete' => 0
  298. ])->asArray()->one();
  299. // 查询核销卡信息
  300. $verify_card_info = VerifyCard::find()->select('name')->where([
  301. 'id' => $order_info['verify_card_id'],
  302. 'is_delete' => 0
  303. ])->asArray()->one();
  304. $order_info['verify_card_name'] = $verify_card_info['name'];
  305. // 用户信息
  306. $user_info = User::find()->select('nickname')->where([
  307. 'id' => $order_info['user_id'],
  308. 'is_delete' => 0
  309. ])->asArray()->one();
  310. $order_info['username'] = $user_info['nickname'];
  311. $order_info['valid_time'] = date("Y-m-d H:i:s", $order_info['end_time']);
  312. // 核销员信息
  313. $verify_user_info = User::find()->select('nickname,shop_id')->where([
  314. 'id' => $this->user_id,
  315. 'is_clerk' => 1,
  316. 'is_delete' => 0
  317. ])->asArray()->one();
  318. // 门店信息
  319. $shop_info = Shop::find()->select('name')->where([
  320. 'id' => $verify_user_info['shop_id'],
  321. 'is_delete' => 0
  322. ])->asArray()->one();
  323. $order_info['verify_user_name'] = $verify_user_info['nickname'];
  324. $order_info['shop'] = $shop_info['name'];
  325. return [
  326. 'code' => 0,
  327. 'msg' => 'success',
  328. 'data' => [
  329. 'list' => $order_info
  330. ],
  331. ];
  332. }
  333. public function log()
  334. {
  335. if (!$this->validate()) {
  336. return [
  337. 'code' => 1,
  338. 'msg' => $this->getErrorSummary(false)[0]
  339. ];
  340. }
  341. if ($this->id) {
  342. // $sale_card_info['coupon_money'] = $sale_info->use_num * $verify_card_info->money; 核销总金额
  343. // 使用记录
  344. $log_info = VerifyCardLog::find()->where([
  345. 'user_id' => $this->user_id,
  346. 'is_delete' => 0,
  347. 'sale_id' => $this->id,
  348. 'store_id' => $this->store_id
  349. ])->orderBy('use_time desc')->asArray()->all();
  350. } else {
  351. $log_info = VerifyCardLog::find()->where([
  352. 'user_id' => $this->user_id,
  353. 'is_delete' => 0,
  354. 'store_id' => $this->store_id
  355. ])->orderBy('use_time desc')->asArray()->all();
  356. }
  357. foreach ($log_info as &$item) {
  358. // 门店信息
  359. $shop_info = Md::find()->select('name')->where([
  360. 'id' => $item['shop_id'],
  361. 'is_delete' => 0
  362. ])->one();
  363. $item['shop_name'] = $shop_info ? $shop_info['name'] : '未知';
  364. $item['use_time'] = $item['use_time'] == 0 ? '' : date("Y-m-d H:i:s", $item['use_time']);
  365. if (!empty($item['shop_id'])) {
  366. $user = User::findOne($item['verify_person']);
  367. $saasUser = SaasUser::findOne(['mobile' => $user->binding]);
  368. $item['verify_person'] = $saasUser->name;
  369. } else {
  370. $item['shop'] = "";
  371. $item['verify_person'] = "";
  372. }
  373. }
  374. return [
  375. 'code' => 0,
  376. 'msg' => 'success',
  377. 'data' => [
  378. 'log_list' => $log_info
  379. ],
  380. 'num' => count($log_info)
  381. ];
  382. }
  383. public function add()
  384. {
  385. if (!$this->validate()) {
  386. return [
  387. 'code' => 1,
  388. 'msg' => $this->getErrorSummary(false)[0]
  389. ];
  390. }
  391. $transaction = \Yii::$app->db->beginTransaction();
  392. try {
  393. $account = VerifyCardAccount::find()->where([
  394. 'store_id' => $this->store_id,
  395. 'account' => $this->account,
  396. 'password' => $this->cardpwd,
  397. ])->one();
  398. if (!$account) {
  399. return [
  400. 'code' => 1,
  401. 'msg' => '卡券账号和密码不正确',
  402. ];
  403. }
  404. if ($account->status == VerifyCardAccount::STATUS_USED) {
  405. return [
  406. 'code' => 1,
  407. 'msg' => '该账号和密码已被使用',
  408. ];
  409. }
  410. $verifyCard = VerifyCard::find()->where([
  411. 'id' => $account->card_id,
  412. 'is_delete' => 0,
  413. ])->one();
  414. if (!$verifyCard) {
  415. return [
  416. 'code' => 1,
  417. 'msg' => '卡券不存在',
  418. ];
  419. }
  420. $sale = new VerifyCardSale();
  421. $sale->store_id = $this->store_id;
  422. $sale->verify_card_id = $verifyCard->id;
  423. $sale->user_id = $this->user_id;
  424. $sale->left_num = $verifyCard->type == 1 ? $verifyCard->total_num : 1;
  425. $sale->sale_time = time();
  426. if ($verifyCard->date_type == 1) {
  427. $sale->end_time = time() + $verifyCard->expire_day * (3600 * 24);
  428. } else {
  429. $sale->end_time = $verifyCard->end_time;
  430. }
  431. $sale->account_id = $account->id;
  432. if ($verifyCard->type == 5) {
  433. $sale->status = 1;
  434. }
  435. $sale->save();
  436. if ($verifyCard->type == 5) {
  437. $logModel = new VerifyCardLog();
  438. $logModel->sale_id = $sale->id;
  439. $logModel->store_id = $this->store_id;
  440. $logModel->use_time = time();
  441. $logModel->user_id = $this->user_id;
  442. $logModel->type = VerifyCardLog::WRITE_TYPE_EXCHANGE;
  443. $logModel->describe = '视频卡兑换';
  444. if (!$logModel->save()) {
  445. return [
  446. 'code' => 0,
  447. 'msg' => '视频卡兑换失败',
  448. ];
  449. }
  450. }
  451. $account->status = VerifyCardAccount::STATUS_USED;
  452. $account->save();
  453. $transaction->commit();
  454. return [
  455. 'code' => 0,
  456. 'msg' => '绑定成功',
  457. 'data' => $sale->id
  458. ];
  459. } catch (\Exception $e) {
  460. $transaction->rollBack();
  461. return [
  462. 'code' => 0,
  463. 'msg' => '绑定失败',
  464. ];
  465. }
  466. }
  467. // /**
  468. // * 添加卡券
  469. // * @return boolean
  470. // */
  471. // public function add2()
  472. // {
  473. // if (!$this->validate()) {
  474. // return [
  475. // 'code' => 1,
  476. // 'msg' => $this->getErrorSummary(false)[0]
  477. // ];
  478. // }
  479. // $arr = [];
  480. // $arr1 = [];
  481. // $model = new VerifyCardSale();
  482. // // 查询除核销卡外的所有卡类型卡密信息
  483. // $verifyCardList = VerifyCard::find()
  484. // ->where(['is_delete' => VerifyCard::IS_DELETE_NO])
  485. // ->andWhere(['<>', 'status', 2])
  486. // ->andWhere(['<>', 'account_info', ''])
  487. // ->select('id, account_info, type, date_type, begin_time, end_time, store_id, total_num')
  488. // ->asArray()
  489. // ->all();
  490. // if (!empty($verifyCardList)) {
  491. // foreach ($verifyCardList as $key => $val) {
  492. // if ($val['date_type'] == 2) {
  493. // if ((int)$val['end_time'] < time()) {
  494. // return [
  495. // 'code' => 1,
  496. // 'msg' => '该卡已过期',
  497. // ];
  498. // }
  499. // }
  500. // foreach (json_decode($val['account_info'], true) as $k => $v) {
  501. // if ($this->account == $v['user'] && $this->cardpwd == $v['password']) {
  502. // $user_info = User::findOne(['id' => $this->user_id]);
  503. // if ((int)$val['store_id'] !== (int)$this->store_id || $user_info['store_id'] !== (int)$this->store_id) {
  504. // return [
  505. // 'code' => 1,
  506. // 'msg' => '您绑定的卡不属于该商城'
  507. // ];
  508. // }
  509. // if ($v['status'] == "未使用") {
  510. // $this->verify_card_id = (int)$val['id'];
  511. // if ($val['date_type'] == 2) {
  512. // $this->sale_time = $val['begin_time'];
  513. // $this->end_time = $val['end_time'];
  514. // }
  515. // if ($val['type'] > 1) {
  516. // $model->left_num = 1;
  517. // } else {
  518. // $model->left_num = $val['total_num'];
  519. // }
  520. // break;
  521. // } else {
  522. // return [
  523. // 'code' => 1,
  524. // 'msg' => '您绑定的卡已被使用'
  525. // ];
  526. // }
  527. // } else {
  528. // continue;
  529. // }
  530. // }
  531. // }
  532. // } else {
  533. // return [];
  534. // }
  535. // if (empty($this->verify_card_id) || $this->verify_card_id == '' || $this->verify_card_id == null) {
  536. // return [
  537. // 'code' => 1,
  538. // 'msg' => '账号或者密码错误'
  539. // ];
  540. // }
  541. // $model->created_at = time();
  542. // $model->card_info = json_encode(['user' => $this->account, 'password' => $this->cardpwd]);
  543. // $model->attributes = $this->attributes;
  544. // $model->status = 0;
  545. // if ($model->save()) {
  546. // $verify_card = VerifyCard::find()->where(['id' => $this->verify_card_id])->asArray()->one();
  547. // foreach (json_decode($verify_card['account_info'], true) as $k => $v) {
  548. // if ($this->account == $v['user'] && $this->cardpwd == $v['password']) {
  549. // $arr[$k]['user'] = $v['user'];
  550. // $arr[$k]['password'] = $v['password'];
  551. // $arr[$k]['status'] = "已使用";
  552. // } else {
  553. // $arr1[$k]['user'] = $v['user'];
  554. // $arr1[$k]['password'] = $v['password'];
  555. // $arr1[$k]['status'] = $v['status'];
  556. // }
  557. // }
  558. // $account_info = array_merge($arr, $arr1);
  559. // $res = VerifyCard::updateAll(['account_info' => json_encode($account_info)], ['id' => $verify_card['id']]);
  560. // if ($res > 0) {
  561. // return [
  562. // 'code' => 0,
  563. // 'msg' => '绑定成功'
  564. // ];
  565. // }
  566. // } else {
  567. // return [
  568. // 'code' => 1,
  569. // "msg" => $this->getErrorSummary(false)[0]
  570. // ];
  571. // }
  572. // }
  573. /**
  574. * 兑换详情
  575. */
  576. public function exchangeDetail()
  577. {
  578. if (!$this->validate()) {
  579. return [
  580. 'code' => 1,
  581. 'msg' => $this->getErrorSummary(false)[0]
  582. ];
  583. }
  584. if ($this->sale_id) {
  585. $verifyCardSale = VerifyCardSale::findOne(['id' => $this->sale_id]);
  586. if ($verifyCardSale['user_id'] !== $this->user_id) {
  587. return [
  588. 'code' => 1,
  589. 'msg' => '无权限查看'
  590. ];
  591. }
  592. $verifyCard = VerifyCard::findOne(['id' => $verifyCardSale['verify_card_id']]);
  593. if ($verifyCard['type'] == 1) {
  594. if ($verifyCardSale['left_num'] !== $verifyCard['total_num']) {
  595. $status = 2;
  596. }
  597. }
  598. if ($verifyCard['type'] == 2) {
  599. $data = Goods::find()
  600. ->where(['store_id' => $this->store_id])
  601. ->andWhere(['in', 'id', json_decode($verifyCard['goods_ids'], true)])
  602. ->asArray()
  603. ->all();
  604. foreach ($data as $k => $val) {
  605. $val['attr'] = json_decode($val['attr'], true);
  606. $goods_info = Goods::findOne($val['id']);
  607. $data[$k]['attr_group_list'] = $goods_info->getAttrGroupList();
  608. }
  609. }
  610. if ($verifyCard['type'] == 5) {
  611. $data = Video::find()
  612. ->where(['store_id' => $this->store_id])
  613. ->andWhere(['in', 'id', json_decode($verifyCard['video_ids']), true])
  614. ->asArray()
  615. ->all();
  616. }
  617. if ($verifyCard['date_type'] == 1) {
  618. // 有效期
  619. $expire_day = $verifyCard['expire_day'] * 24 * 60 * 60;
  620. // 过期时间
  621. $expire = $verifyCardSale['created_at'] + $expire_day;
  622. if (time() > $expire) {
  623. $status = 1;
  624. } else {
  625. $status = 0;
  626. }
  627. }
  628. if ($verifyCard['date_type'] == 2) {
  629. if (time() > $verifyCard['end_time']) {
  630. $status = 1;
  631. } else {
  632. $status = 0;
  633. }
  634. }
  635. return [
  636. 'code' => 0,
  637. 'msg' => 'success',
  638. 'data' => [
  639. 'list' => empty($data) ? [] : $data,
  640. 'verifyCardSale' => $verifyCardSale,
  641. 'verifycard' => $verifyCard
  642. ],
  643. 'num' => empty($data) ? 0 : count($data),
  644. 'status' => $status
  645. ];
  646. } else {
  647. return [
  648. 'code' => 1,
  649. 'msg' => '参数错误'
  650. ];
  651. }
  652. }
  653. /**
  654. * 更新视频状态
  655. */
  656. public function videoUpdate()
  657. {
  658. if (!$this->validate()) {
  659. return [
  660. 'code' => 1,
  661. 'msg' => $this->getErrorSummary(false)[0]
  662. ];
  663. }
  664. if ($this->sale_id && $this->id) {
  665. $user_id = VerifyCardSale::findOne(['id' => $this->sale_id])->user_id;
  666. if ($user_id !== $this->user_id) {
  667. return [
  668. 'code' => '1',
  669. 'msg' => '对不起,您没有权限观看此视频'
  670. ];
  671. }
  672. $data = Video::findOne(['id' => $this->id]);
  673. if (!$data) {
  674. return [
  675. 'code' => 1,
  676. 'msg' => '该视频不存在'
  677. ];
  678. }
  679. return [
  680. 'code' => 0,
  681. 'msg' => 'success',
  682. 'data' => $data
  683. ];
  684. } else {
  685. return [
  686. 'code' => 1,
  687. 'msg' => '参数错误'
  688. ];
  689. }
  690. }
  691. /**
  692. * 储值卡兑换
  693. */
  694. public function exchange()
  695. {
  696. if (!$this->validate()) {
  697. return [
  698. 'code' => 1,
  699. 'msg' => $this->getErrorSummary(false)[0]
  700. ];
  701. }
  702. if ($this->sale_id) {
  703. $verifyCardSale = VerifyCardSale::findOne([
  704. 'id' => $this->sale_id,
  705. 'is_delete' => VerifyCard::IS_DELETE_NO,
  706. 'store_id' => $this->store_id
  707. ]);
  708. $verifycard = VerifyCard::findOne(['id' => $verifyCardSale['verify_card_id']]);
  709. if ($verifycard['type'] == 3 && $verifyCardSale['left_num'] > 0) {
  710. $user_balance = AccountLog::find()
  711. ->where(['user_id' => $this->user_id])
  712. ->orderBy('created_at desc')
  713. ->asArray()
  714. ->one();
  715. $model = new AccountLog();
  716. $logModel = new VerifyCardLog();
  717. $logModel->sale_id = $this->sale_id;
  718. $logModel->store_id = $this->store_id;
  719. $logModel->use_time = time();
  720. $logModel->user_id = $this->user_id;
  721. $logModel->type = VerifyCardLog::WRITE_TYPE_EXCHANGE;
  722. $logModel->describe = '储值卡兑换金额' . $verifycard['total_price'] . '元';
  723. $model->user_id = $this->user_id;
  724. $model->store_id = $this->store_id;
  725. $model->order_type = AccountLog::TYPE_RECHARGE_ORDER;
  726. $model->amount = $verifycard['total_price'];
  727. $model->desc = '商城储值卡充值' . $verifycard['total_price'] . '元';
  728. $model->before = $user_balance['after'];
  729. $model->after = $user_balance['after'] + $verifycard['total_price'];
  730. $model->operator = 'system';
  731. $model->log_type = 1;
  732. $model->type = AccountLog::TYPE_BALANCE;
  733. $model->operator_type = AccountLog::TYPE_OPERATOR_NORMAL;
  734. $model->created_at = time();
  735. if ($user_balance) { // 已充值过的用户
  736. $model->before = $user_balance['after'];
  737. $model->after = $user_balance['after'] + $verifycard['total_price'];
  738. } else { // 未充值过的用户
  739. $model->before = 0;
  740. $model->after = $verifycard['total_price'];
  741. }
  742. if ($model->save() && $logModel->save()) {
  743. $left_num = $verifyCardSale['left_num'] - 1;
  744. $send_times = $verifycard['send_times'] + 1;
  745. $user_money = User::findOne(['id' => $this->user_id])['money'];
  746. $user = User::updateAll(['money' => ($user_money + $verifycard['total_price'])], 'id=' . $this->user_id);
  747. $verify_card = VerifyCard::updateAll(['send_times' => $send_times], 'id='.$verifycard['id']);
  748. $verify_card_sale = VerifyCardSale::updateAll(['left_num' => $left_num, 'status' => 1], 'id=' . $this->sale_id);
  749. if ($user > 0 && $verify_card > 0 && $verify_card_sale > 0) {
  750. return [
  751. 'code' => 0,
  752. 'msg' => 'success'
  753. ];
  754. } else {
  755. return [
  756. 'code' => 1,
  757. 'msg' => '充值失败'
  758. ];
  759. }
  760. } else {
  761. return [
  762. 'code' => 1,
  763. 'msg' => '充值失败'
  764. ];
  765. }
  766. }
  767. if ($verifycard['type'] == 5) {
  768. VerifyCardSale::updateAll(['status' => 1], 'id=' . $this->sale_id);
  769. $logModel = new VerifyCardLog();
  770. $logModel->sale_id = $this->sale_id;
  771. $logModel->store_id = $this->store_id;
  772. $logModel->use_time = time();
  773. $logModel->user_id = $this->user_id;
  774. $logModel->type = VerifyCardLog::WRITE_TYPE_EXCHANGE;
  775. $logModel->describe = '视频卡兑换';
  776. $logModel->save();
  777. return [
  778. 'code' => 0,
  779. 'msg' => '兑换成功'
  780. ];
  781. }
  782. } else {
  783. return [
  784. 'code' => 1,
  785. 'msg' => '参数错误',
  786. ];
  787. }
  788. }
  789. /**
  790. * 卡券领取
  791. */
  792. public function receive()
  793. {
  794. if (!$this->validate()) {
  795. return [
  796. 'code' => 1,
  797. 'msg' => $this->getErrorSummary(false)[0]
  798. ];
  799. }
  800. if ($this->sale_id) {
  801. $verifysale = VerifyCardSale::findOne(['id' => $this->sale_id]); // 赠送人信息
  802. $model = new VerifyCardLog();
  803. if (!$verifysale) {
  804. return [
  805. 'code' => 1,
  806. 'msg' => '未查到此卡信息'
  807. ];
  808. }
  809. if ($verifysale->user_id == $this->user_id) {
  810. return [
  811. 'code' => 1,
  812. 'msg' => '不能赠送给自己'
  813. ];
  814. }
  815. if ($verifysale->user_id == $this->user_id) {
  816. return [
  817. 'code' => 1,
  818. 'msg' => '您已领取过'
  819. ];
  820. }
  821. $model->store_id = $this->store_id;
  822. $model->sale_id = $this->sale_id;
  823. $model->use_time = time();
  824. $model->user_id = $verifysale->user_id;
  825. $model->recv_id = $this->user_id;
  826. $model->type = VerifyCardLog::WRITE_TYPE_GIVE;
  827. $model->describe = '通过分享赠送领取卡券';
  828. $verifysale->user_id = $this->user_id;
  829. if ($model->save() && $verifysale->save()) {
  830. $verifysale->is_trans_send = 1;
  831. $verifysale->save();
  832. return [
  833. 'code' => 0,
  834. 'msg' => '领取成功'
  835. ];
  836. }
  837. return [
  838. 'code' => 1,
  839. 'msg' => '领取失败'
  840. ];
  841. } else {
  842. return [
  843. 'code' => 1,
  844. 'msg' => '参数错误'
  845. ];
  846. }
  847. }
  848. /**
  849. * 删除核销卡(更新状态)
  850. */
  851. public function delVerifyCard() {
  852. if ($this->id) {
  853. $verifysale = VerifyCardSale::findOne(['id' => $this->id]);
  854. if (!$verifysale) {
  855. return [];
  856. }
  857. $verifysale->is_delete = VerifyCardSale::IS_DELETE_YES;
  858. $verifysale->status = 2;
  859. $verifysale->save();
  860. return [
  861. 'code' => 0,
  862. 'msg' => '删除成功'
  863. ];
  864. } elseif ($this->ids) {
  865. $ids = explode(',', $this->ids);
  866. VerifyCardSale::updateAll(
  867. ['is_delete' => VerifyCardSale::IS_DELETE_YES, 'status' => 2],
  868. ['id' => $ids]);
  869. return [
  870. 'code' => 0,
  871. 'msg' => '删除成功'
  872. ];
  873. }
  874. }
  875. public function getVerifyLog()
  876. {
  877. if (!$this->validate()) {
  878. return [
  879. 'code' => 1,
  880. 'msg' => $this->getErrorSummary(false)[0]
  881. ];
  882. }
  883. if ($this->sale_id) {
  884. $sale = VerifyCardSale::findOne($this->sale_id);
  885. if (!$sale) {
  886. return [
  887. 'code' => 1,
  888. 'msg' => '卡券不存在',
  889. ];
  890. }
  891. $logs = VerifyCardLog::find()->alias('vcl')
  892. ->leftJoin(['md' => Md::tableName()], 'vcl.shop_id = md.id')->where([
  893. 'vcl.store_id' => $this->store_id,
  894. 'vcl.sale_id' => $this->sale_id,
  895. 'vcl.is_delete' => 0,
  896. 'vcl.type' => VerifyCardLog::WRITE_TYPE_WRITE_OFF
  897. ])->select('vcl.use_time, md.name as md_name')->asArray()->all();
  898. return [
  899. 'code' => 0,
  900. 'data' => $logs,
  901. ];
  902. }
  903. return [
  904. 'code' => 1,
  905. 'msg' => '缺少参数',
  906. ];
  907. }
  908. /**
  909. * 转换日期格式为date
  910. */
  911. public function dateFormat($date)
  912. {
  913. return date('Y-m-d', $date);
  914. }
  915. public function v_list()
  916. {
  917. $query = VerifyCard::find()->alias('vc')
  918. ->innerJoin(['vsu' => VerifyShareUser::tableName()], 'vsu.verify_card_id=vc.id')
  919. ->where(['vc.store_id' => $this->store_id, 'vc.type' => 1, 'vc.is_delete' => 0, 'vsu.user_id' => get_user_id()]);
  920. $query->andWhere('(vc.date_type = 1 or (vc.date_type=2 and vc.end_time> unix_timestamp()))');
  921. $list = $query->orderBy('vc.id desc')
  922. ->select('vc.*')
  923. ->asArray()
  924. ->all();
  925. return [
  926. 'code' => 0,
  927. 'msg' => 'success',
  928. 'data' => $list,
  929. 'num' => count($list),
  930. ];
  931. }
  932. public function add_receive()
  933. {
  934. if (!$this->validate()) {
  935. return [
  936. 'code' => 1,
  937. 'msg' => $this->getErrorSummary(false)[0]
  938. ];
  939. }
  940. $transaction = \Yii::$app->db->beginTransaction();
  941. try {
  942. $share_user = VerifyShareUser::find()->where([
  943. 'store_id' => $this->store_id,
  944. 'verify_card_id' => $this->verify_card_id,
  945. 'user_id' => $this->share_user_id,
  946. ])->one();
  947. if (!$share_user) {
  948. return [
  949. 'code' => 1,
  950. 'msg' => '不是分享人分享,不能领取',
  951. ];
  952. }
  953. $info = VerifyShareLog::find()->where([
  954. 'verify_card_id' => $this->verify_card_id,
  955. 'user_id' => $this->user_id,
  956. ])->one();
  957. if ($info) {
  958. return [
  959. 'code' => 1,
  960. 'msg' => '不可重复领取',
  961. ];
  962. }
  963. $verifyCard = VerifyCard::find()->where([
  964. 'id' => $this->verify_card_id,
  965. 'is_delete' => 0,
  966. ])->one();
  967. if (!$verifyCard) {
  968. return [
  969. 'code' => 1,
  970. 'msg' => '卡券不存在',
  971. ];
  972. }
  973. $account = VerifyCardAccount::find()->where([
  974. 'store_id' => $this->store_id,
  975. 'card_id' => $verifyCard->id,
  976. 'status' => 0,
  977. ])->one();
  978. if (!$account) {
  979. return [
  980. 'code' => 1,
  981. 'msg' => '卡卷码不存在',
  982. ];
  983. }
  984. $account->status = VerifyCardAccount::STATUS_USED;
  985. $account->save();
  986. $verifyCard->send_times += 1;
  987. $verifyCard->save();
  988. $sale = new VerifyCardSale();
  989. $sale->store_id = $this->store_id;
  990. $sale->verify_card_id = $verifyCard->id;
  991. $sale->user_id = $this->user_id;
  992. $sale->left_num = $verifyCard->type == 1 ? $verifyCard->total_num : 1;
  993. $sale->sale_time = time();
  994. if ($verifyCard->date_type == 1) {
  995. $sale->end_time = time() + $verifyCard->expire_day * (3600 * 24);
  996. } else {
  997. $sale->end_time = $verifyCard->end_time;
  998. }
  999. $sale->account_id = $account->id;
  1000. if ($verifyCard->type == 5) {
  1001. $sale->status = 1;
  1002. }
  1003. $sale->save();
  1004. $logModel = new VerifyShareLog();
  1005. $logModel->sale_id = $sale->id;
  1006. $logModel->store_id = $this->store_id;
  1007. $logModel->verify_card_id = $this->verify_card_id;
  1008. $logModel->share_user_id = $this->share_user_id;
  1009. $logModel->user_id = $this->user_id;
  1010. $logModel->create_time = time();
  1011. if (!$logModel->save()) {
  1012. return [
  1013. 'code' => 1,
  1014. 'msg' => '兑换失败',
  1015. ];
  1016. }
  1017. $transaction->commit();
  1018. return [
  1019. 'code' => 0,
  1020. 'msg' => '绑定成功',
  1021. 'data' => $sale->id
  1022. ];
  1023. } catch (\Exception $e) {
  1024. $transaction->rollBack();
  1025. return [
  1026. 'code' => 0,
  1027. 'msg' => '绑定失败',
  1028. ];
  1029. }
  1030. }
  1031. }