VirtualCardModel.class.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  1. <?php
  2. /**
  3. * lionfish 狮子鱼社区团购系统
  4. *
  5. *
  6. * @author fish
  7. *
  8. */
  9. namespace Seller\Model;
  10. /**
  11. * @author yj
  12. * @desc 虚拟卡密
  13. * Class VirtualCardModel
  14. * @package Seller\Model
  15. */
  16. class VirtualCardModel{
  17. /**
  18. * @author yj
  19. * @desc 添加或插入时变更商品关联兑换码信息
  20. * @param $goods_id
  21. */
  22. public function modifyGoodsVirtualCard( $goods_id )
  23. {
  24. $virtual_code_id = I('post.virtual_code_id');
  25. $goods_code_info = $this->getGoodsVirtualCardInfoByGoodsId( $goods_id );
  26. if( empty($goods_code_info) )
  27. {
  28. //插入
  29. $ins_data = [];
  30. $ins_data['goods_id'] = $goods_id;
  31. $ins_data['code_id'] = $virtual_code_id;
  32. M('lionfish_comshop_goods_virturalcard')->add( $ins_data );
  33. }else{
  34. //更新
  35. $up_data = [];
  36. $up_data['code_id'] = $virtual_code_id;
  37. M('lionfish_comshop_goods_virturalcard')->where( ['goods_id' => $goods_id ] )->save( $up_data );
  38. }
  39. }
  40. /**
  41. * @author yj
  42. * @desc 根据商品id获取商品关联的兑换码组
  43. * @param $goods_id
  44. * @return mixed
  45. */
  46. public function getGoodsVirtualCardInfoByGoodsId( $goods_id )
  47. {
  48. $info = M('lionfish_comshop_goods_virturalcard')->where( ['goods_id' => $goods_id ] )->find();
  49. return $info;
  50. }
  51. /**
  52. * @author yj
  53. * @desc 根据code_id获取商品code信息
  54. * @param $code_id
  55. * @return mixed
  56. */
  57. public function getGoodsVirtualCardInfoByCodeId( $code_id )
  58. {
  59. $info = M('lionfish_comshop_goods_virturalcard')->where( ['code_id' => $code_id ] )->find();
  60. return $info;
  61. }
  62. /**
  63. * @author yj
  64. * @desc 根据code_id获取商品数量
  65. * @param $code_id
  66. * @return mixed
  67. */
  68. public function getGoodsVirtualCardCountByCodeId( $code_id )
  69. {
  70. $info = M('lionfish_comshop_goods_virturalcard')->where( ['code_id' => $code_id ] )->count();
  71. return $info;
  72. }
  73. /**
  74. * @author yj
  75. * @desc 获取有效的可用礼品兑换码
  76. * @return mixed
  77. */
  78. public function getCanUseVirtualcardCodes()
  79. {
  80. $list = M('lionfish_comshop_virtualcard_codes')->where(['state' => 1])->select();
  81. return $list;
  82. }
  83. //退款+取消订单时,需要将数量剔除,订单详情不允许部分退款,todo.....
  84. /**
  85. * @param $code_id
  86. * @return mixed
  87. */
  88. public function getCodeInfoByCodeId( $code_id )
  89. {
  90. $info = M('lionfish_comshop_virtualcard_codes')->where(['id' => $code_id ])->find();
  91. return $info;
  92. }
  93. /**
  94. * @author yj
  95. * @desc 获取未使用的code数量
  96. * @param $code_id
  97. * @return mixed
  98. */
  99. public function getCodeUsedCount( $code_id )
  100. {
  101. $count = M('lionfish_comshop_order_virtualcard')->where( ['code_id' => $code_id, 'state' => 2 ] )->count();
  102. return $count;
  103. }
  104. /**
  105. * @author yj
  106. * @desc 获取未使用的code数量
  107. * @param $code_id
  108. * @return mixed
  109. */
  110. public function getCodeUnUseCount( $code_id )
  111. {
  112. $count = M('lionfish_comshop_order_virtualcard')->where( ['code_id' => $code_id, 'state' => 1 ] )->count();
  113. return $count;
  114. }
  115. /**
  116. * @author yj
  117. * @desc 获取已失效的code数量
  118. * @param $code_id
  119. */
  120. public function getCodeinvalidCount( $code_id )
  121. {
  122. $count = M('lionfish_comshop_order_virtualcard')->where( ['code_id' => $code_id, 'state' => 2 ] )->count();
  123. return $count;
  124. }
  125. /**
  126. * @author yj
  127. * @desc 添加修改礼品卡兑换码
  128. * @param $data
  129. */
  130. public function updateCode($data)
  131. {
  132. $ins_data = array();
  133. $ins_data['code_name'] = $data['code_name'];
  134. $ins_data['effect_type'] = $data['effect_type'];
  135. $ins_data['effect_days'] = $data['effect_days'];
  136. $ins_data['code_money'] = $data['code_money'];
  137. $ins_data['addtime'] = time();
  138. $ins_data['state'] = $data['state'];
  139. $id = $data['id'];
  140. if( !empty($id) && $id > 0 )
  141. {
  142. unset($ins_data['addtime']);
  143. M('lionfish_comshop_virtualcard_codes')->where( array('id' => $id) )->save( $ins_data );
  144. $id = $data['id'];
  145. }else{
  146. $id = M('lionfish_comshop_virtualcard_codes')->add( $ins_data );
  147. }
  148. }
  149. /**
  150. * @author yj
  151. * @desc 添加修改礼品卡兑换码
  152. * @param $data
  153. */
  154. public function updateofflineCode($data)
  155. {
  156. $ins_data = array();
  157. $ins_data['code_name'] = $data['code_name'];
  158. $ins_data['effect_type'] = $data['effect_type'];
  159. $ins_data['effect_end_time'] = strtotime( $data['effect_end_time'].':00' );
  160. $ins_data['code_money'] = $data['code_money'];
  161. $ins_data['state'] = $data['state'];
  162. $ins_data['addtime'] = time();
  163. $id = $data['id'];
  164. if( !empty($id) && $id > 0 )
  165. {
  166. unset($ins_data['addtime']);
  167. M('lionfish_comshop_virtualcard_offlinecodes')->where( array('id' => $id) )->save( $ins_data );
  168. $id = $data['id'];
  169. }else{
  170. //开启事务
  171. M()->startTrans();
  172. $id = M('lionfish_comshop_virtualcard_offlinecodes')->add( $ins_data );
  173. //还要开始生成code...todo.
  174. $code_quantity = $data['code_quantity'];
  175. while( true )
  176. {
  177. $code_num = $this->generateCode(1);
  178. $res = M('lionfish_comshop_virtualcard_offlineusercode')->where( "code = '{$code_num}' " )->find();
  179. if( empty($res) )
  180. {
  181. $code_quantity--;
  182. $ins_user_code_data = [];
  183. $ins_user_code_data['offlinecode_id'] = $id;
  184. $ins_user_code_data['code'] = $code_num;
  185. $ins_user_code_data['state'] = 0;
  186. $ins_user_code_data['user_id'] = 0;
  187. $ins_user_code_data['usedtime'] = 0;
  188. $ins_user_code_data['addtime'] = time();
  189. M('lionfish_comshop_virtualcard_offlineusercode')->add( $ins_user_code_data );
  190. }
  191. if( $code_quantity <= 0 )
  192. {
  193. break;
  194. }
  195. }
  196. M()->commit();
  197. }
  198. }
  199. /**
  200. * 生成vip激活码
  201. * @param int $nums 生成多少个优惠码
  202. * @param array $exist_array 排除指定数组中的优惠码
  203. * @param int $code_length 生成优惠码的长度
  204. * @param int $prefix 生成指定前缀
  205. * @return array 返回优惠码数组
  206. */
  207. public function generateCode( $nums,$exist_array='',$code_length = 8,$prefix = '' )
  208. {
  209. $characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnpqrstuvwxyz";
  210. $promotion_codes = array();//这个数组用来接收生成的优惠码
  211. for($j = 0 ; $j < $nums; $j++) {
  212. $code = '';
  213. for ($i = 0; $i < $code_length; $i++) {
  214. $code .= $characters[mt_rand(0, strlen($characters)-1)];
  215. }
  216. //如果生成的4位随机数不再我们定义的$promotion_codes数组里面
  217. if( !in_array($code,$promotion_codes) ) {
  218. if( is_array($exist_array) ) {
  219. if( !in_array($code,$exist_array) ) {//排除已经使用的优惠码
  220. $promotion_codes[$j] = $prefix.$code; //将生成的新优惠码赋值给promotion_codes数组
  221. } else {
  222. $j--;
  223. }
  224. } else {
  225. $promotion_codes[$j] = $prefix.$code;//将优惠码赋值给数组
  226. }
  227. } else {
  228. $j--;
  229. }
  230. }
  231. return $promotion_codes[0];
  232. }
  233. /**
  234. * @author yj
  235. * @desc 取预售首页商品
  236. * @param int $limit
  237. */
  238. public function getIndexVirturalCardGoods($is_index_show_pa =1)
  239. {
  240. $gpc = I('request.');
  241. $head_id = isset($gpc['head_id']) ? $gpc['head_id'] : 0;
  242. if( isset($gpc['communityId']) && $head_id == 0 )
  243. {
  244. $head_id = $gpc['communityId'];
  245. }
  246. $pageNum = isset($gpc['pageNum']) ? $gpc['pageNum'] : 1;
  247. $per_page = isset($gpc['pre_page']) && !empty($gpc['pre_page']) ? $gpc['pre_page'] : 5;
  248. $gid = $gpc['gid'];
  249. $offset = ($pageNum - 1) * $per_page;
  250. $limit = "{$offset},{$per_page}";
  251. if($head_id == 'undefined') $head_id = '';
  252. $is_only_express = $gpc['is_only_express'];
  253. $is_open_only_express = 0;
  254. if($is_only_express==1) {
  255. $is_open_only_express = D('Home/Front')->get_config_by_name('is_open_only_express');
  256. }
  257. if($gid == 'undefined' || $gid =='' || $gid =='null' || $gid ==0)
  258. {
  259. $gid = 0;
  260. }
  261. if( !empty($gid) && $gid > 0)
  262. {
  263. $gids = D('Home/GoodsCategory')->get_index_goods_category($gid,'normal','','',1);
  264. $gidArr = array();
  265. $gidArr[] = $gid;
  266. foreach ($gids as $key => $val) {
  267. $gidArr[] = $val['id'];
  268. }
  269. $gid = implode(',', $gidArr);
  270. }
  271. $token = $gpc['token'];
  272. $weprogram_token = M('lionfish_comshop_weprogram_token')->field('member_id')->where( array('token' => $token) )->find();
  273. if( empty($weprogram_token) || empty($weprogram_token['member_id']) )
  274. {
  275. //echo json_encode( array('code' => 2) );
  276. //die();
  277. }
  278. $member_id = $weprogram_token['member_id'];
  279. $now_time = time();
  280. $where = " g.grounding =1 and g.type ='virtualcard' ";
  281. $is_index_show = isset($gpc['is_index_show']) ? $gpc['is_index_show'] : $is_index_show_pa;
  282. if($is_index_show==1) {
  283. $where .= " and g.is_index_show = 1 ";
  284. }
  285. if($is_open_only_express==1 && $is_only_express==1) {
  286. $where .= " and gc.is_only_express =1 ";
  287. }
  288. $where .= "and gc.begin_time < {$now_time} ";
  289. $community_goods = D('Home/Pingoods')->get_new_community_index_goods($head_id, $gid, 'g.*,gc.begin_time,gc.end_time,gc.big_img,gc.labelname,gc.video,gc.pick_up_type,gc.pick_up_modify,gc.is_take_fullreduction ', $where,$offset,$per_page);
  290. if( !empty($community_goods) )
  291. {
  292. $is_open_fullreduction = D('Home/Front')->get_config_by_name('is_open_fullreduction');
  293. $full_money = D('Home/Front')->get_config_by_name('full_money');
  294. $full_reducemoney = D('Home/Front')->get_config_by_name('full_reducemoney');
  295. if(empty($full_reducemoney) || $full_reducemoney <= 0)
  296. {
  297. $is_open_fullreduction = 0;
  298. }
  299. $list = array();
  300. foreach($community_goods as $val)
  301. {
  302. $tmp_data = array();
  303. $tmp_data['actId'] = $val['id'];
  304. $tmp_data['spuName'] = $val['goodsname'];
  305. $tmp_data['spuCanBuyNum'] = $val['total'];
  306. $tmp_data['spuDescribe'] = $val['subtitle'];
  307. $tmp_data['end_time'] = $val['end_time'];
  308. $tmp_data['soldNum'] = $val['seller_count'] + $val['sales'];
  309. $productprice = $val['productprice'];
  310. $tmp_data['marketPrice'] = explode('.', $productprice);
  311. if( !empty($val['big_img']) )
  312. {
  313. $tmp_data['bigImg'] = tomedia($val['big_img']);
  314. }
  315. $good_image = D('Home/Pingoods')->get_goods_images($val['id']);
  316. if( !empty($good_image) )
  317. {
  318. $tmp_data['skuImage'] = tomedia($good_image['image']);
  319. }
  320. $price_arr = D('Home/Pingoods')->get_goods_price($val['id'],$member_id);
  321. $price = $price_arr['price'];
  322. $tmp_data['actPrice'] = explode('.', $price);
  323. $tmp_data['skuList']= D('Home/Pingoods')->get_goods_options($val['id'],$member_id);
  324. if($is_open_fullreduction == 0)
  325. {
  326. $tmp_data['is_take_fullreduction'] = 0;
  327. }else if($is_open_fullreduction == 1){
  328. $tmp_data['is_take_fullreduction'] = $val['is_take_fullreduction'];
  329. }
  330. // 商品角标
  331. $label_id = unserialize($val['labelname']);
  332. if($label_id){
  333. $label_info = D('Home/Pingoods')->get_goods_tags($label_id);
  334. if($label_info){
  335. if($label_info['type'] == 1){
  336. $label_info['tagcontent'] = tomedia($label_info['tagcontent']);
  337. } else {
  338. $label_info['len'] = mb_strlen($label_info['tagcontent'], 'utf-8');
  339. }
  340. }
  341. $tmp_data['label_info'] = $label_info;
  342. }
  343. $goods_virturalcard = $this->getGoodsVirtualCardInfoByGoodsId( $val['id'] );
  344. $virturalcard_info = $this->getCodeInfoByCodeId( $goods_virturalcard['code_id'] );
  345. $code_money = $virturalcard_info['code_money'];
  346. //增加预售时间:
  347. $tmp_data['code_money'] = round($code_money, 2);
  348. $list[] = $tmp_data;
  349. }
  350. return ['code' =>0 ,'list' => $list ];
  351. }else{
  352. return ['code' => 1];
  353. }
  354. }
  355. /**
  356. * @author yj
  357. * @desc 取预售首页商品
  358. * @param int $limit
  359. */
  360. public function getUserUserecord( $user_id )
  361. {
  362. $gpc = I('request.');
  363. $pageNum = isset($gpc['pageNum']) ? $gpc['pageNum'] : 1;
  364. $per_page = isset($gpc['pre_page']) && !empty($gpc['pre_page']) ? $gpc['pre_page'] : 20;
  365. $offset = ($pageNum - 1) * $per_page;
  366. $limit = "{$offset},{$per_page}";
  367. $list = M('lionfish_comshop_virtualcard_userecord')->where(['use_user_id' => $user_id ])->order('id desc')->limit($limit )->select();
  368. foreach( $list as &$val )
  369. {
  370. $val['adddate'] = date('Y-m-d H:i:s', $val['addtime'] );
  371. $val['money_format'] = round( $val['money'], 2 );
  372. }
  373. if( !empty($list) )
  374. {
  375. return ['code' => 0, 'data' => $list ];
  376. }
  377. else {
  378. return ['code' => 2, 'message' => 'no more'];
  379. }
  380. }
  381. /**
  382. * @author yj
  383. * @desc 插入礼品兑换订单
  384. * @param $order_id
  385. */
  386. public function addVirtualCardOrder( $order_id )
  387. {
  388. //1、找到code_id code_sn动态生成 user_id
  389. $order_info = M('lionfish_comshop_order')->where(['order_id' => $order_id ])->find();
  390. $order_goods_info = M('lionfish_comshop_order_goods')->where( ['order_id' => $order_id ] )->find();
  391. $goods_virtualcard_info = M('lionfish_comshop_goods_virturalcard')->where( ['goods_id' => $order_goods_info['goods_id'] ] )->find();
  392. $code_id = $goods_virtualcard_info['code_id'];
  393. $code_info = M('lionfish_comshop_virtualcard_codes')->where( ['id' => $code_id ] )->find();
  394. $code_money = $code_info['code_money'] * $order_goods_info['quantity'];
  395. //effect_type effect_days
  396. //开始插入数据。。。。。
  397. $ins_data = [];
  398. $ins_data['code_id'] = $code_id;
  399. $ins_data['code_sn'] = md5($order_id.time().$order_info['user_id']);
  400. $ins_data['state'] = 0;
  401. $ins_data['order_id'] = $order_id;
  402. $ins_data['user_user_id'] = 0;
  403. $ins_data['buy_user_id'] = $order_info['member_id'];
  404. $ins_data['code_money'] = $code_money;
  405. $ins_data['effect_type'] = $code_info['effect_type'];
  406. $ins_data['effect_endtime'] = time() + 86400 * $code_info['effect_days'];
  407. $ins_data['addtime'] = time();
  408. M('lionfish_comshop_order_virtualcard')->add( $ins_data );
  409. }
  410. /**
  411. * @author yj
  412. * @desc 礼品卡支付回调处理
  413. * @param $order_id
  414. */
  415. public function payBackOrder( $order_id )
  416. {
  417. $o = array();
  418. $o['order_status_id'] = 4;
  419. $o['pay_time']=time();
  420. $o['express_time']=time();
  421. M('lionfish_comshop_order')->where( array('order_id' => $order_id ) )->save( $o );
  422. M('lionfish_comshop_order_virtualcard')->where(['order_id' => $order_id ])->save( ['state' => 1] );
  423. }
  424. /**
  425. * @author yj
  426. * @desc 根据订单id获取
  427. * @param $order_id
  428. * @return mixed
  429. */
  430. public function getOrderVirtualCardByOrderId( $order_id )
  431. {
  432. $info = M('lionfish_comshop_order_virtualcard')->where(['order_id' => $order_id ])->find();
  433. return $info;
  434. }
  435. /**
  436. * @author yj
  437. * @desc 获取订单详情所需要的信息
  438. * @param $order_id
  439. * @return array
  440. */
  441. public function getVirtualCardOrderInfO( $order_id )
  442. {
  443. $info = $this->getOrderVirtualCardByOrderId( $order_id );
  444. if( empty($info) )
  445. {
  446. return ['code' =>1, 'message' => 'no record'];
  447. }
  448. $now_time = time();
  449. $info['is_effect'] = 1;
  450. $info['effect_enddate'] = date('Y-m-d H:i:s', $info['effect_endtime'] );//已过期日期
  451. if( $info['effect_type'] == 1 && $info['effect_endtime'] < $now_time )
  452. {
  453. $info['is_effect'] = 0;//已过期
  454. $info['state'] = 3;
  455. }
  456. //use_member_name
  457. $info['use_member_name'] = '--';
  458. $info['use_date'] = '';
  459. if( !empty($info['user_user_id']) && $info['user_user_id'] > 0 )
  460. {
  461. $user_info = M('lionfish_comshop_member')->where(['member_id' => $info['user_user_id']])->find();
  462. if( !empty($user_info) )
  463. {
  464. $info['use_member_name'] = $user_info['username'];
  465. }
  466. $virtualcard_userecord = M('lionfish_comshop_virtualcard_userecord')->where(['order_id' => $order_id ])->find();
  467. if( !empty($virtualcard_userecord) )
  468. {
  469. $info['use_date'] = date('Y-m-d H:i:s', $virtualcard_userecord['addtime'] );
  470. }
  471. }
  472. return ['code' =>0 , 'data' => $info ];
  473. }
  474. /**
  475. * @author yj
  476. * @desc 未支付的订单,取消
  477. * @param $order_id
  478. */
  479. public function cancleOrder( $order_id )
  480. {
  481. $info = $this->getOrderVirtualCardByOrderId( $order_id );
  482. if( !empty($info) && $info['state'] == 0 )
  483. {
  484. M('lionfish_comshop_order_virtualcard')->where(['order_id' => $order_id ])->save( ['state' => 3 ] );
  485. }
  486. }
  487. /**
  488. * @author yj
  489. * @desc 退款的订单,取消
  490. * @param $order_id
  491. */
  492. public function refundOrder( $order_id )
  493. {
  494. $info = $this->getOrderVirtualCardByOrderId( $order_id );
  495. if( !empty($info) && $info['state'] == 1 )
  496. {
  497. M('lionfish_comshop_order_virtualcard')->where(['order_id' => $order_id ])->save( ['state' => 3 ] );
  498. }
  499. }
  500. /**
  501. * @author y
  502. * @desc兑换线下核销码
  503. * @param $code_sn
  504. * @param $member_id
  505. * @return array
  506. */
  507. public function subOfflineCodeSn( $code_sn , $member_id )
  508. {
  509. M()->startTrans();
  510. $check_info = M('lionfish_comshop_virtualcard_offlineusercode')->where( ['code' => $code_sn ] )->find();
  511. if( empty($check_info) )
  512. {
  513. M()->rollback();
  514. return ['code' => 2, 'message' => '该兑换码不存在'];
  515. }
  516. else if( $check_info['state'] == 1 )
  517. {
  518. M()->rollback();
  519. return ['code' => 2, 'message' => '该兑换码已被使用'];
  520. }
  521. $unuse_info = M('lionfish_comshop_virtualcard_offlineusercode')->where( ['code' => $code_sn , 'state' => 0 ] )->lock(true)->find();
  522. if( empty($unuse_info) )
  523. {
  524. M()->rollback();
  525. return ['code' => 2, 'message' => '该兑换码已不存在'];
  526. }
  527. //判断这个兑换码是否禁用 code_id
  528. $code_info = M('lionfish_comshop_virtualcard_offlinecodes')->where(['id' => $unuse_info['offlinecode_id']])->find();
  529. if( !empty($code_info) && $code_info['state'] == 0 )
  530. {
  531. M()->rollback();
  532. return ['code' => 2, 'message' => '该兑换码已被禁用'];
  533. }
  534. //开始充钱
  535. D('Admin/Member')->sendMemberMoneyChange($member_id, $code_info['code_money'], 20, '线下礼品卡:'.$code_sn.'兑换余额');
  536. M('lionfish_comshop_virtualcard_offlineusercode')->where( ['code' => $code_sn , 'state' => 0 ] )->save(['state' => 1, 'user_id' => $member_id , 'usedtime' => time() ]);
  537. M()->commit();
  538. return ['code' => 0 , 'money' => round($code_info['code_money'], 2) ];
  539. }
  540. /**
  541. * @author yj
  542. * @desc 使用礼品卡
  543. * @param $code_sn
  544. * @param $member_id
  545. * @return array
  546. */
  547. public function subCodeSn( $code_sn ,$member_id )
  548. {
  549. M()->startTrans();
  550. $check_info = M('lionfish_comshop_order_virtualcard')->where( ['code_sn' => $code_sn ] )->find();
  551. if( empty($check_info) )
  552. {
  553. M()->rollback();
  554. return ['code' => 2, 'message' => '该兑换码不存在'];
  555. }
  556. else if( $check_info['state'] == 2 )
  557. {
  558. M()->rollback();
  559. return ['code' => 2, 'message' => '该兑换码已被使用'];
  560. }
  561. $unuse_info = M('lionfish_comshop_order_virtualcard')->where( ['code_sn' => $code_sn , 'state' => 1 ] )->lock(true)->find();
  562. if( empty($unuse_info) )
  563. {
  564. M()->rollback();
  565. return ['code' => 2, 'message' => '该兑换码已失效'];
  566. }
  567. $now_time = time();
  568. //订单确认收货的问题,
  569. if( $unuse_info['effect_type'] == 1 && $unuse_info['effect_endtime'] < $now_time )
  570. {
  571. M()->rollback();
  572. return ['code' => 2, 'message' => '该兑换码已过期'];
  573. }
  574. //判断这个兑换码是否禁用 code_id
  575. $code_info = M('lionfish_comshop_virtualcard_codes')->where(['id' => $unuse_info['code_id']])->find();
  576. if( !empty($code_info) && $code_info['state'] == 0 )
  577. {
  578. M()->rollback();
  579. return ['code' => 2, 'message' => '该兑换码已被禁用'];
  580. }
  581. //开始充钱
  582. D('Admin/Member')->sendMemberMoneyChange($member_id, $unuse_info['code_money'], 20, '礼品卡:'.$code_sn.'兑换余额');
  583. M('lionfish_comshop_order_virtualcard')->where( ['code_sn' => $code_sn , 'state' => 1 ] )->save(['state' => 2, 'user_user_id' => $member_id ]);
  584. //更改订单为已收货
  585. D('Home/Frontorder')->receive_order($unuse_info['order_id']);
  586. //增加兑换码使用记录
  587. $ins_data = [];
  588. $ins_data['code_sn'] = $code_sn;
  589. $ins_data['code_id'] = $unuse_info['code_id'];
  590. $ins_data['order_id'] = $unuse_info['order_id'];
  591. $ins_data['money'] = $unuse_info['code_money'];
  592. $ins_data['use_user_id'] = $member_id;
  593. $ins_data['addtime'] = time();
  594. M('lionfish_comshop_virtualcard_userecord')->add( $ins_data );
  595. M()->commit();
  596. return ['code' => 0 ];
  597. }
  598. /**
  599. * @author yj
  600. * @desc 检测是否被禁用
  601. * @param $user_id
  602. * @return array
  603. */
  604. public function checkUserIsLock( $user_id )
  605. {
  606. $info = M('lionfish_comshop_virtualcard_limit_user')->where(['user_id' => $user_id ])->find();
  607. $now_time = time();
  608. if( !empty($info) )
  609. {
  610. if( $info['limit_endtime'] > $now_time )
  611. {
  612. $min = ceil( ($info['limit_endtime'] - $now_time) / 60 );
  613. return ['code' => 0 ,'min' => $min ];
  614. }else{
  615. M('lionfish_comshop_virtualcard_limit_user')->where(['user_id' => $user_id ])->delete();
  616. }
  617. }
  618. return ['code' => 1];
  619. }
  620. /**
  621. * @author yj
  622. * @desc 插入错误的兑换码用户
  623. * @param $user_id
  624. */
  625. public function insErrorSubCodeSnUserId( $user_id )
  626. {
  627. $ins_data = [];
  628. $ins_data['user_id'] = $user_id;
  629. $ins_data['addtime'] = time();
  630. M('lionfish_comshop_virtualcard_limituser_error')->add( $ins_data );
  631. }
  632. /**
  633. * 检测是否需要锁定,需要的话,锁定入库
  634. * @param $user_id
  635. */
  636. public function checkIsNeedLockUser( $user_id )
  637. {
  638. //几分钟
  639. $virtcard_flush_limit_miniter = D('Home/Front')->get_config_by_name('virtcard_flush_limit_miniter');
  640. $virtcard_flush_limit_miniter = empty($virtcard_flush_limit_miniter) ? 1 : $virtcard_flush_limit_miniter;
  641. //错误几次
  642. $virtcard_flush_error_timers = D('Home/Front')->get_config_by_name('virtcard_flush_error_timers');
  643. //冻结多久 小时
  644. $virtcard_flush_error_hours = D('Home/Front')->get_config_by_name('virtcard_flush_error_hours');
  645. $now_time = time();
  646. $begin_time = $now_time - 60 * $virtcard_flush_limit_miniter;
  647. $counts = M('lionfish_comshop_virtualcard_limituser_error')->where("user_id={$user_id} and addtime >= {$begin_time} and addtime <= {$now_time} ")->count();
  648. if( $counts >= $virtcard_flush_error_timers )
  649. {
  650. //需要被封
  651. $info = M('lionfish_comshop_virtualcard_limit_user')->where(['user_id' => $user_id ])->find();
  652. $lock_endtime = $now_time + 3600 * $virtcard_flush_error_hours;
  653. if( !empty($info) )
  654. {
  655. M('lionfish_comshop_virtualcard_limit_user')->where(['user_id' => $user_id ])->save(['limit_endtime' => $lock_endtime ]);
  656. }else{
  657. $ins_data = [];
  658. $ins_data['user_id'] = $user_id;
  659. $ins_data['limit_endtime'] = $lock_endtime;
  660. $ins_data['addtime'] = time();
  661. M('lionfish_comshop_virtualcard_limit_user')->add( $ins_data );
  662. }
  663. }
  664. }
  665. }
  666. ?>