VirtualCardModel.class.php 26 KB

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