GiftCard.php 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116
  1. <?php
  2. namespace addon\giftcard\model;
  3. use addon\supermember\model\MemberCard;
  4. use app\model\BaseModel;
  5. use app\model\member\Member;
  6. use app\model\member\MemberAccount;
  7. use app\model\message\Message;
  8. use app\model\order\Config;
  9. use addon\giftcard\model\GiftCard as GiftCardModel;
  10. use app\model\system\Cron;
  11. use app\model\system\Pay;
  12. use think\facade\Cache;
  13. use app\model\order\StoreOrder;
  14. use app\model\order\LocalOrder;
  15. use app\model\order\VirtualOrder;
  16. use app\model\order\Order;
  17. use app\model\upload\Upload;
  18. class GiftCard extends BaseModel
  19. {
  20. private $account_content = [
  21. 'point' => [ 'relate_tag' => "兑换积分", 'remark' => "礼品卡兑换" ],
  22. 'growth' => [ 'relate_tag' => "兑换成长值", 'remark' => "礼品卡兑换" ],
  23. 'balance' => [ 'relate_tag' => "兑换储值余额", 'remark' => "礼品卡兑换" ],
  24. 'balance_meney' => [ 'relate_tag' => "兑换现金余额", 'remark' => "礼品卡兑换" ]
  25. ];
  26. private $goods_money = 0;//商品金额
  27. private $balance_money = 0;//余额
  28. private $delivery_money = 0;//配送费用
  29. private $coupon_money = 0;//优惠券金额
  30. private $adjust_money = 0;//调整金额
  31. private $invoice_money = 0;//发票费用
  32. private $promotion_money = 0;//优惠金额
  33. private $order_money = 0;//订单金额
  34. private $pay_money = 0;//支付总价
  35. private $is_virtual = 0; //是否是虚拟类订单
  36. private $order_name = ''; //订单详情
  37. private $goods_num = 0; //商品种数
  38. private $member_balance_money = 0;//会员账户余额(计算过程中会逐次减少)
  39. private $pay_type = 'ONLINE_PAY';//支付方式
  40. private $invoice_delivery_money = 0;
  41. private $error = 0; //是否有错误
  42. private $error_msg = ''; //错误描述
  43. /**
  44. * 兑换卡列表
  45. * @param array $condition
  46. * @param int $page
  47. * @param int $page_size
  48. * @param string $order
  49. * @param string $field
  50. * @return array
  51. */
  52. public function getGiftCardPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '', $field = '*', $alias = 'a', $join = [], $group = null, $limit = null)
  53. {
  54. $bargain_list = model("giftcard")->pageList($condition, $field, $order, $page, $page_size, $alias, $join, $group, $limit);
  55. if ($bargain_list[ 'list' ]) {
  56. $bargain_info[ 'goods_list' ] = [];
  57. foreach ($bargain_list[ 'list' ] as $key => $val) {
  58. $giftcard_list = model("goods_giftcard")->getList([ [ 'giftcard_id', '=', $val[ 'id' ] ], [ 'site_id', '=', $val[ 'site_id' ] ] ]);
  59. $bargain_list[ 'list' ][ $key ][ 'goods_num' ] = count($giftcard_list) ?? 0;
  60. if ($giftcard_list) {
  61. $bargain_list[ 'list' ][ $key ][ 'is_electronic' ] = 1;
  62. foreach ($giftcard_list as $k => $v) {
  63. $goods_list = model('goods_sku')->getInfo(
  64. [ [ 'sku_id', '=', $v[ 'sku_id' ] ] ],
  65. 'goods_id,sku_id,sku_name,price,sku_image,stock'
  66. );
  67. $goods_list[ 'num' ] = $v[ 'num' ];
  68. $bargain_list[ 'list' ][ $key ][ 'goods_list' ][] = $goods_list;
  69. }
  70. } else {
  71. $bargain_list[ 'list' ][ $key ][ 'is_electronic' ] = 0;
  72. }
  73. }
  74. }
  75. return $this->success($bargain_list);
  76. }
  77. public function giftcardSort($id, $sort)
  78. {
  79. $res = model('giftcard')->update([ 'sort' => $sort ], [ [ 'id', '=', $id ] ]);
  80. return $this->success($res);
  81. }
  82. /**
  83. * api兑换卡列表
  84. * @param array $condition
  85. * @param int $page
  86. * @param int $page_size
  87. * @param string $order
  88. * @param string $field
  89. * @return array
  90. */
  91. public function getGiftCardApiPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '', $field = '*', $alias = 'a', $join = [], $group = null, $limit = null)
  92. {
  93. if ($order == 2) {
  94. $order_by = 'selling_price desc';
  95. } else if ($order == 3) {
  96. $order_by = 'selling_price asc';
  97. } else {
  98. $order_by = 'sort asc,create_time desc';
  99. }
  100. $bargain_list = model("giftcard")->pageList($condition, $field, $order_by, $page, $page_size, $alias, $join, $group, $limit);
  101. if ($bargain_list[ 'list' ]) {
  102. $time = time();
  103. $bargain_info[ 'goods_list' ] = [];
  104. foreach ($bargain_list[ 'list' ] as $key => $val) {
  105. $giftcard_list = model("goods_giftcard")->getList([ [ 'giftcard_id', '=', $val[ 'id' ] ], [ 'site_id', '=', $val[ 'site_id' ] ] ]);
  106. $bargain_list[ 'list' ][ $key ][ 'goods_num' ] = count($giftcard_list) ?? 0;
  107. if ($giftcard_list) {
  108. $bargain_list[ 'list' ][ $key ][ 'is_electronic' ] = 1;
  109. foreach ($giftcard_list as $k => $v) {
  110. $goods_list = model('goods_sku')->getInfo(
  111. [ [ 'sku_id', '=', $v[ 'sku_id' ] ] ],
  112. 'goods_id,sku_id,sku_name,price,sku_image,stock'
  113. );
  114. $goods_list[ 'num' ] = $v[ 'num' ];
  115. $bargain_list[ 'list' ][ $key ][ 'goods_list' ][] = $goods_list;
  116. }
  117. } else {
  118. $bargain_list[ 'list' ][ $key ][ 'is_electronic' ] = 0;
  119. }
  120. if ($val[ 'time_type' ] == 2 && $val[ 'youxiao_time' ] < $time) {
  121. unset($bargain_list[ 'list' ][ $key ]);
  122. }
  123. }
  124. }
  125. $money = [];
  126. foreach ($bargain_list[ 'list' ] as $key => $row) {
  127. $money[ $key ] = $row[ 'selling_price' ];
  128. }
  129. if ($order == 2) {
  130. array_multisort($money, SORT_DESC, $bargain_list[ 'list' ]);
  131. } else if ($order == 3) {
  132. array_multisort($money, SORT_ASC, $bargain_list[ 'list' ]);
  133. } else {
  134. array_multisort($bargain_list[ 'list' ]);
  135. }
  136. return $this->success($bargain_list);
  137. }
  138. public function getGiftCardList($condition = [], $order = '', $field = '*', $alias = 'a', $join = [], $group = null, $limit = null)
  139. {
  140. $list = model("giftcard")->getList($condition, $field, $order, $alias, $join, $group, $limit);
  141. return $this->success($list);
  142. }
  143. public function getGiftCardInfo($condition = [], $field = '*', $alias = "a", $join = [])
  144. {
  145. $data = model("giftcard")->getInfo($condition, $field, $alias, $join);
  146. return $this->success($data);
  147. }
  148. public function getGiftCardGoodsInfo($condition = [], $field = '*', $alias = "a", $join = [])
  149. {
  150. $bargain_info = model("giftcard")->getInfo($condition, $field, 'a', $join);
  151. $bargain_info[ 'goods_list' ] = [];
  152. if ($bargain_info) {
  153. $giftcard_info = model("goods_giftcard")->getList([ [ 'giftcard_id', '=', $bargain_info[ 'id' ] ], [ 'site_id', '=', $bargain_info[ 'site_id' ] ] ]);
  154. $bargain_info[ 'goods_num' ] = count($giftcard_info) ?? 0;
  155. if ($giftcard_info) {
  156. $bargain_info[ 'is_electronic' ] = 1;
  157. foreach ($giftcard_info as $k => $v) {
  158. $goods_list = model('goods_sku')->getInfo(
  159. [ [ 'sku_id', '=', $v[ 'sku_id' ] ] ],
  160. 'goods_id,sku_id,sku_name,price,sku_image,stock'
  161. );
  162. if($goods_list){
  163. $goods_list[ 'num' ] = $v[ 'num' ];
  164. $bargain_info[ 'goods_list' ][] = $goods_list;
  165. }
  166. }
  167. } else {
  168. $bargain_info[ 'is_electronic' ] = 0;
  169. }
  170. $bargain_info[ 'goods_list_count' ] = count($bargain_info[ 'goods_list' ]);
  171. }
  172. return $this->success($bargain_info);
  173. }
  174. public function getGiftCardRecordInfo($condition = [], $field = '*', $alias = 'a', $join = [])
  175. {
  176. $bargain_info = model("giftcard_record")->getInfo($condition, $field, $alias, $join);
  177. if ($bargain_info) {
  178. $giftcard_info = model("goods_giftcard")->getList([ [ 'giftcard_id', '=', $bargain_info[ 'giftcard_id' ] ], [ 'site_id', '=', $bargain_info[ 'site_id' ] ] ]);
  179. $bargain_info[ 'goods_list' ] = [];
  180. $bargain_info[ 'goods_num' ] = count($giftcard_info) ?? 0;
  181. if ($giftcard_info) {
  182. foreach ($giftcard_info as $k => $v) {
  183. $goods_list = model('goods_sku')->getInfo(
  184. [ [ 'sku_id', '=', $v[ 'sku_id' ] ] ],
  185. 'goods_id,sku_id,sku_name,price,sku_image,stock'
  186. );
  187. if($goods_list) {
  188. $goods_list['num'] = $v['num'];
  189. $bargain_info['goods_list'][] = $goods_list;
  190. }
  191. }
  192. }
  193. }
  194. return $this->success($bargain_info);
  195. }
  196. /**
  197. * 获取卡密详细列表
  198. * @param array $condition
  199. * @param int $page
  200. * @param int $page_size
  201. * @param string $order
  202. * @param string $field
  203. * @param string $alias
  204. * @param array $join
  205. * @param null $group
  206. * @param null $limit
  207. * @return array
  208. */
  209. public function recordPagelist($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '', $field = '*', $alias = 'a', $join = [], $group = null, $limit = null)
  210. {
  211. $bargain_list = model("giftcard_record")->pageList($condition, $field, $order, $page, $page_size, $alias, $join, $group, $limit);
  212. if ($bargain_list[ 'list' ]) {
  213. $bargain_info[ 'goods_list' ] = [];
  214. foreach ($bargain_list[ 'list' ] as $key => $val) {
  215. $giftcard_list = model("goods_giftcard")->getList([ [ 'giftcard_id', '=', $val[ 'giftcard_id' ] ], [ 'site_id', '=', $val[ 'site_id' ] ] ]);
  216. $bargain_list[ 'list' ][ $key ][ 'goods_num' ] = count($giftcard_list) ?? 0;
  217. if ($giftcard_list) {
  218. $bargain_list[ 'list' ][ $key ][ 'is_electronic' ] = 1;
  219. foreach ($giftcard_list as $k => $v) {
  220. $goods_list = model('goods_sku')->getInfo(
  221. [ [ 'sku_id', '=', $v[ 'sku_id' ] ] ],
  222. 'goods_id,sku_id,sku_name,price,sku_image,stock'
  223. );
  224. $goods_list[ 'num' ] = $v[ 'num' ];
  225. $bargain_list[ 'list' ][ $key ][ 'goods_list' ][] = $goods_list;
  226. }
  227. } else {
  228. $bargain_list[ 'list' ][ $key ][ 'is_electronic' ] = 0;
  229. }
  230. }
  231. }
  232. return $this->success($bargain_list);
  233. }
  234. /**
  235. * 批量激活/批量禁用
  236. * @param $id
  237. * @param $status
  238. * @return array
  239. */
  240. public function editStatus($id, $status)
  241. {
  242. $data = [ 'status' => $status ];
  243. $condition = [
  244. [ 'id', '=', $id ],
  245. ];
  246. $condition_record = [
  247. [ 'giftcard_id', '=', $id ],
  248. ];
  249. model('giftcard')->startTrans();
  250. try {
  251. model('giftcard')->update($data, $condition);
  252. if ($status == 1) {
  253. $record_data[ 'activation_time' ] = time();
  254. } else {
  255. $record_data[ 'activation_time' ] = 0;
  256. }
  257. if ($status == 1) {
  258. $record_data[ 'entity_status' ] = 1;
  259. $condition_record[] = [ 'entity_status', '=', 0 ];
  260. }
  261. if ($status == 2) {
  262. $condition_record[] = [ 'entity_status', 'in', [ 0, 1 ] ];
  263. $record_data[ 'entity_status' ] = 2;
  264. }
  265. model('giftcard_record')->update($record_data, $condition_record);
  266. model('giftcard')->commit();
  267. return $this->success();
  268. } catch (\Exception $e) {
  269. model('giftcard')->rollback();
  270. return $this->error($e->getMessage() . $e->getLine());
  271. }
  272. }
  273. public function editeCordStatus($id, $status)
  274. {
  275. $data = [ 'entity_status' => $status ];
  276. if ($status) {
  277. $data[ 'activation_time' ] = time();
  278. } else {
  279. $data[ 'activation_time' ] = 0;
  280. }
  281. $condition = [ [ 'id', '=', $id ] ];
  282. model('giftcard_record')->update($data, $condition);
  283. return $this->success();
  284. }
  285. public function updateGiftCard($id, $data)
  286. {
  287. $condition = [ [ 'id', '=', $id ] ];
  288. $gift_info = model('giftcard')->getInfo($condition);
  289. if (!empty($gift_info[ 'card_cover' ]) && !empty($data[ 'card_cover' ]) && $gift_info[ 'card_cover' ] != $data[ 'card_cover' ]) {
  290. $upload_model = new Upload();
  291. $upload_model->deletePic($gift_info[ 'card_cover' ], $gift_info[ 'site_id' ]);
  292. }
  293. model('giftcard')->update($data, $condition);
  294. return $this->success();
  295. }
  296. public function updateGoodsGiftCard($id, $data, $site_id)
  297. {
  298. model('goods_giftcard')->delete([ [ 'giftcard_id', '=', $id ] ]);
  299. if ($data) {
  300. foreach ($data as $k => $v) {
  301. $data_goods = [
  302. 'site_id' => $site_id,
  303. 'sku_id' => $v[ 'sku_id' ] ?? 0,
  304. 'num' => $v[ 'num' ] ?? 1,
  305. 'giftcard_id' => $id,
  306. ];
  307. $condition = [ [ 'id', '=', $id ] ];
  308. model('goods_giftcard')->add($data_goods, $condition);
  309. }
  310. }
  311. return $this->success();
  312. }
  313. /**
  314. * 添加电子卡
  315. * @param $data
  316. * @return array
  317. */
  318. public function addElectGiftCard($data)
  319. {
  320. $data[ 'code' ] = $this->createCode(8, 'G');
  321. $gift_id = model('giftcard')->add($data);
  322. return $this->success($gift_id);
  323. }
  324. /**
  325. * 添加实体卡
  326. * @param
  327. * @return array
  328. */
  329. public function addGiftCard($data)
  330. {
  331. $card_count = $data[ 'card_count' ];
  332. $carmichael_length = $data[ 'carmichael_length' ] ?? 6;
  333. $carmichael_prefix = $data[ 'carmichael_prefix' ] ?? '';
  334. $carmichael_suffix = $data[ 'carmichael_suffix' ] ?? '';
  335. $carmichael_type = $data[ 'carmichael_type' ] ?? [];
  336. $time = time();
  337. $data[ 'status' ] = 0;
  338. $data[ 'create_time' ] = $time;
  339. $data[ 'carmichael_type' ] = implode(',', $carmichael_type);
  340. $arecord_list = [];
  341. $data[ 'code' ] = $this->createCode(8, 'G');
  342. model("giftcard")->startTrans();
  343. try {
  344. $giftcard_id = model('giftcard')->add($data);
  345. if ($data[ 'time_type' ] == 2) {
  346. $cron = new Cron();
  347. //增加定时更改活动状态自动事件
  348. if (!empty($data[ 'youxiao_time' ])) {
  349. $cron->addCron(1, 0, "实体卡作废", "GiftCardChangeStatus", $data[ 'youxiao_time' ], $giftcard_id);
  350. }
  351. }
  352. $carmichael_lists = model('giftcard_record')->getColumn([], 'carmichael_text');
  353. for ($i = 0; $i < $card_count; $i++) {
  354. while (true) {
  355. $carmichael_text = $this->createCarmichael($carmichael_length, $carmichael_prefix, $carmichael_suffix, $carmichael_type);
  356. if (!in_array($carmichael_text, $carmichael_lists)) {
  357. break;
  358. }
  359. }
  360. $arecord_list[ $i ][ 'entity_bianhao' ] = $this->createEntityBianhao(9, 'F', $i + 1);
  361. $arecord_list[ $i ][ 'giftcard_id' ] = $giftcard_id;
  362. $arecord_list[ $i ][ 'status' ] = $data[ 'status' ];
  363. $arecord_list[ $i ][ 'create_time' ] = $time;
  364. $arecord_list[ $i ][ 'carmichael_text' ] = $carmichael_text;
  365. $arecord_list[ $i ][ 'site_id' ] = $data[ 'site_id' ];
  366. $arecord_list[ $i ][ 'goods_ids' ] = $data[ 'goods_ids' ];
  367. }
  368. model('giftcard_record')->addList($arecord_list);
  369. model("giftcard")->commit();
  370. return $this->success($giftcard_id);
  371. } catch (\Exception $e) {
  372. model("giftcard")->rollback();
  373. return $this->error($e->getMessage() . $e->getLine());
  374. }
  375. }
  376. /**
  377. * 兑换码兑换奖励
  378. * @param $carmichael_text
  379. * @param $member_id
  380. * @param $site_id
  381. * @return array
  382. */
  383. public function exchange($carmichael_text, $member_id, $site_id, $data)
  384. {
  385. $message = "领取成功";
  386. $condition = [
  387. [ 'carmichael_text', '=', $carmichael_text ]
  388. ];
  389. $alias = "er";
  390. $field = "er.*,e.point,e.growth,e.balance,e.balance_money,e.coupon";
  391. $join = [
  392. [ 'giftcard e', 'er.giftcard_id = e.id', 'inner' ]
  393. ];
  394. $carmichael_record_info = model('giftcard_record')->getInfo($condition, $field, $alias, $join);
  395. if ($carmichael_record_info[ 'entity_bianhao' ] == "" && $member_id != $carmichael_record_info[ 'use_member_id' ]) {
  396. return $this->error('', '未查到该礼品卡');
  397. }
  398. if ($carmichael_record_info[ 'entity_bianhao' ]) {
  399. switch ( $carmichael_record_info[ 'entity_status' ] ) {
  400. case 0:
  401. return $this->error('', '该礼品卡未激活');
  402. break;
  403. case 2:
  404. return $this->error('', '该礼品卡已作废');
  405. break;
  406. case 3:
  407. return $this->error('', '该礼品卡已使用');
  408. break;
  409. case 4:
  410. return $this->error('', '该礼品卡已过期');
  411. break;
  412. }
  413. } else {
  414. switch ( $carmichael_record_info[ 'elect_status' ] ) {
  415. case 1:
  416. return $this->error('', '该礼品卡已使用');
  417. break;
  418. case 2:
  419. return $this->error('', '该礼品卡已过期');
  420. break;
  421. case 3:
  422. return $this->error('', '该卡密已使用');
  423. break;
  424. }
  425. }
  426. //领取优惠卷
  427. // if (!empty($carmichael_record_info['coupon'])) {
  428. // $coupon_type_id = $carmichael_record_info['coupon'];
  429. // $coupon_model = new CouponModel();
  430. // $res = $coupon_model->receiveCoupon($coupon_type_id, $site_id, $member_id, 2);
  431. // if($res['code']<0){
  432. // $message = '优惠卷已领完';
  433. // }
  434. // }
  435. //领取账户有关奖励 积分/成长值/储值余额/现金余额
  436. $member_account = [
  437. 'point' => $carmichael_record_info[ 'point' ],
  438. 'balance' => $carmichael_record_info[ 'balance' ],
  439. 'growth' => $carmichael_record_info[ 'growth' ],
  440. 'balance_money' => $carmichael_record_info[ 'balance_money' ]
  441. ];
  442. $member_account_model = new MemberAccount();
  443. foreach ($member_account as $k => $v) {
  444. if ($v > 0) {
  445. $relate_tag = $this->account_content[ $k ][ 'relate_tag' ] . $v;
  446. $remark = $this->account_content[ $k ][ 'remark' ] . $v;
  447. $member_account_model->addMemberAccount($site_id, $member_id, $k, $v, 'giftcard', $relate_tag, $remark);
  448. }
  449. }
  450. if ($carmichael_record_info[ 'entity_bianhao' ]) {
  451. model('giftcard_record')->update([ 'entity_status' => 3, 'use_member_id' => $member_id, 'use_time' => time() ], [ [ 'carmichael_text', '=', $carmichael_text ] ]);
  452. } else {
  453. model('giftcard_record')->update([ 'elect_status' => 1, 'use_member_id' => $member_id, 'use_time' => time() ], [ [ 'carmichael_text', '=', $carmichael_text ] ]);
  454. }
  455. model('giftcard_record')->startTrans();
  456. try {
  457. model('giftcard_record')->commit();
  458. $giftcar_create = new GiftcardOrderCreate();
  459. if (strlen($data[ 'sku_id' ]) > 1) {
  460. $giftcar_create->create($data);
  461. }
  462. // 组合返回信息的信息文字
  463. $msg = '';
  464. if (!empty($carmichael_record_info[ 'balance' ]) && $carmichael_record_info[ 'balance' ] != '0.00') $msg .= '余额' . $carmichael_record_info[ 'balance' ];
  465. if (!empty($carmichael_record_info[ 'point' ])) $msg .= ( empty($msg) ? '' : '+' ) . '积分' . $carmichael_record_info[ 'point' ];
  466. $message = '获得' . $msg;
  467. return $this->success($message);
  468. } catch (\Exception $e) {
  469. model('giftcard_record')->rollback();
  470. return $this->error($e->getMessage());
  471. }
  472. }
  473. /**
  474. * 兑换码兑换奖励
  475. * @param $carmichael_text
  476. * @param $member_id
  477. * @param $site_id
  478. * @return array
  479. */
  480. public function useGiftCard($record_id, $member_id, $site_id, $data)
  481. {
  482. $message = "使用成功";
  483. $condition = [
  484. [ 'er.id', '=', $record_id ],
  485. [ 'er.site_id', '=', $site_id ],
  486. [ 'er.use_member_id', '=', $member_id ]
  487. ];
  488. $alias = "er";
  489. $field = "er.*,e.point,e.growth,e.balance,e.balance_money,e.coupon";
  490. $join = [
  491. [ 'giftcard e', 'er.giftcard_id = e.id', 'inner' ]
  492. ];
  493. $carmichael_record_info = model('giftcard_record')->getInfo($condition, $field, $alias, $join);
  494. if ($carmichael_record_info[ 'entity_bianhao' ] == "" && $member_id != $carmichael_record_info[ 'use_member_id' ]) {
  495. return $this->error('', '未查到该礼品卡');
  496. }
  497. if ($carmichael_record_info[ 'entity_bianhao' ]) {
  498. switch ( $carmichael_record_info[ 'entity_status' ] ) {
  499. case 0:
  500. return $this->error('', '该卡密未激活');
  501. break;
  502. case 2:
  503. return $this->error('', '该卡密已作废');
  504. break;
  505. case 3:
  506. return $this->error('', '该卡密已使用');
  507. break;
  508. case 4:
  509. return $this->error('', '该卡密已过期');
  510. break;
  511. }
  512. } else {
  513. switch ( $carmichael_record_info[ 'elect_status' ] ) {
  514. case 1:
  515. return $this->error('', '该卡密已使用');
  516. break;
  517. case 2:
  518. return $this->error('', '该卡密已过期');
  519. break;
  520. case 3:
  521. return $this->error('', '该卡密已使用');
  522. break;
  523. }
  524. }
  525. //领取优惠卷
  526. // if (!empty($carmichael_record_info['coupon'])) {
  527. // $coupon_type_id = $carmichael_record_info['coupon'];
  528. // $coupon_model = new CouponModel();
  529. // $res = $coupon_model->receiveCoupon($coupon_type_id, $site_id, $member_id, 2);
  530. // if($res['code']<0){
  531. // $message = '优惠卷已领完';
  532. // }
  533. // }
  534. //领取账户有关奖励 积分/成长值/储值余额/现金余额
  535. $member_account = [
  536. 'point' => $carmichael_record_info[ 'point' ],
  537. 'balance' => $carmichael_record_info[ 'balance' ],
  538. 'growth' => $carmichael_record_info[ 'growth' ],
  539. 'balance_money' => $carmichael_record_info[ 'balance_money' ]
  540. ];
  541. $member_account_model = new MemberAccount();
  542. foreach ($member_account as $k => $v) {
  543. if ($v > 0) {
  544. $relate_tag = $this->account_content[ $k ][ 'relate_tag' ] . $v;
  545. $remark = $this->account_content[ $k ][ 'remark' ] . $v;
  546. $member_account_model->addMemberAccount($site_id, $member_id, $k, $v, 'giftcard', $relate_tag, $remark);
  547. }
  548. }
  549. if ($carmichael_record_info[ 'entity_bianhao' ]) {
  550. model('giftcard_record')->update([ 'entity_status' => 3, 'use_member_id' => $member_id, 'use_time' => time() ], [ [ 'id', '=', $record_id ], [ 'site_id', '=', $site_id ], ]);
  551. } else {
  552. model('giftcard_record')->update([ 'elect_status' => 1, 'use_member_id' => $member_id, 'use_time' => time() ], [ [ 'id', '=', $record_id ], [ 'site_id', '=', $site_id ], ]);
  553. }
  554. model('giftcard_record')->startTrans();
  555. try {
  556. if ($data[ 'sku_id' ]) {
  557. $giftcar_create = new GiftcardOrderCreate();
  558. $giftcar_create->create($data);
  559. }
  560. model('giftcard_record')->commit();
  561. return $this->success($message);
  562. } catch (\Exception $e) {
  563. model('giftcard_record')->rollback();
  564. return $this->error($e->getMessage());
  565. }
  566. }
  567. /**
  568. * 生成卡密
  569. * @param $length
  570. * @param string $prefix
  571. * @param string $suffix
  572. * @param array $type
  573. * @return string
  574. */
  575. private function createCarmichael($length, $prefix = '', $suffix = '', $type = [ '0-9' ])
  576. {
  577. $chars = [];
  578. $number = [ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' ];
  579. $small = [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' ];
  580. $large = [ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' ];
  581. if (in_array('0-9', $type)) {
  582. $chars = array_merge($chars, $number);
  583. }
  584. if (in_array('a-z', $type)) {
  585. $chars = array_merge($chars, $small);
  586. }
  587. if (in_array('A-Z', $type)) {
  588. $chars = array_merge($chars, $large);
  589. }
  590. $charsLen = count($chars) - 1;
  591. shuffle($chars);
  592. $output = $prefix;
  593. $length = $length - strlen($prefix) + strlen($suffix);
  594. for ($i = 0; $i < $length; $i++) {
  595. $output .= $chars[ mt_rand(0, $charsLen) ];
  596. }
  597. $output .= $suffix;
  598. return $output;
  599. }
  600. /**
  601. * 创建卡编号
  602. * @param int $length
  603. * @param string $prefix
  604. * @return string
  605. */
  606. private function createCode($length = 8, $prefix = '')
  607. {
  608. $last_data = model('giftcard')->getList([], 'id', 'id DESC', '', '', '', 1);
  609. if (!empty($last_data)) {
  610. $number = $last_data[ 0 ][ 'id' ] + 1;
  611. } else {
  612. $number = 1;
  613. }
  614. $length = $length - (int) strlen($prefix);
  615. $number = sprintf("%0" . $length . "d", $number);
  616. return $prefix . $number;
  617. }
  618. /**
  619. * 创建实体卡卡密编号
  620. * @param int $length
  621. * @param string $prefix
  622. * @return string
  623. */
  624. private function createEntityBianhao($length = 8, $prefix = '', $i = 1)
  625. {
  626. $last_data = model('giftcard_record')->getList([], 'id', 'id DESC', '', '', '', 1);
  627. if (!empty($last_data)) {
  628. $number = $last_data[ 0 ][ 'id' ] + $i;
  629. } else {
  630. $number = $i++;
  631. }
  632. $length = $length - (int) strlen($prefix);
  633. $number = sprintf("%0" . $length . "d", $number);
  634. return $prefix . $number;
  635. }
  636. /**
  637. * 订单创建
  638. * @param $data
  639. * @return unknown|array|mixed
  640. */
  641. public function create($data)
  642. {
  643. //查询出会员相关信息
  644. $calculate_data = $this->calculate($data);
  645. if (isset($calculate_data[ 'code' ]) && $calculate_data[ 'code' ] < 0)
  646. return $calculate_data;
  647. if ($this->error > 0) {
  648. return $this->error([ 'error_code' => $this->error ], $this->error_msg);
  649. }
  650. if (!empty($calculate_data[ 'invoice_type' ])) {
  651. if ($calculate_data[ 'invoice_type' ] == 1 && $calculate_data[ 'invoice_full_address' ] == "") {
  652. //物流,同城
  653. if ($calculate_data[ 'shop_goods_list' ][ 'delivery' ][ 'delivery_type' ] == "express" || $calculate_data[ 'shop_goods_list' ][ 'delivery' ][ 'delivery_type' ] == "local") {
  654. $calculate_data[ 'invoice_full_address' ] = $calculate_data[ 'member_address' ][ 'full_address' ] . $calculate_data[ 'member_address' ][ 'address' ];
  655. $calculate_data[ 'shop_goods_list' ][ 'invoice_full_address' ] = $calculate_data[ 'member_address' ][ 'full_address' ] . $calculate_data[ 'member_address' ][ 'address' ];
  656. }
  657. //门店
  658. if ($calculate_data[ 'shop_goods_list' ][ 'delivery' ][ 'delivery_type' ] == "store") {
  659. $delivery_store_info = json_decode($calculate_data[ 'shop_goods_list' ][ 'delivery_store_info' ], true);
  660. $calculate_data[ 'invoice_full_address' ] = $delivery_store_info[ 'full_address' ];
  661. $calculate_data[ 'shop_goods_list' ][ 'invoice_full_address' ] = $delivery_store_info[ 'full_address' ];
  662. }
  663. }
  664. }
  665. $pay = new Pay();
  666. $out_trade_no = $pay->createOutTradeNo($data[ 'member_id' ]);
  667. model("giftcard_order")->startTrans();
  668. model("giftcard_record")->startTrans();
  669. //循环生成多个订单
  670. try {
  671. //订单主表
  672. $order_no = $this->createOrderNo($data[ 'site_id' ], $data[ 'member_id' ]);
  673. $data_order = [
  674. 'order_number' => $order_no,
  675. 'site_id' => $data[ 'site_id' ],
  676. 'out_trade_no' => $out_trade_no,
  677. 'member_id' => $data[ 'member_id' ],
  678. 'buyer_ip' => request()->ip(),
  679. 'card_price' => $data[ 'card_price' ],
  680. 'card_id' => $data[ 'card_id' ],
  681. 'num' => $data[ 'num' ],
  682. 'order_total_price' => $data[ 'order_total_price' ],
  683. 'create_time' => time(),
  684. 'order_from' => $data[ 'order_from' ],
  685. 'order_from_name' => $data[ 'order_from_name' ],
  686. 'is_invoice' => $data[ 'is_invoice' ],
  687. 'invoice_type' => $data[ 'invoice_type' ],
  688. 'invoice_title' => $data[ 'invoice_title' ],
  689. 'taxpayer_number' => $data[ 'taxpayer_number' ],
  690. 'invoice_content' => $data[ 'invoice_content' ],
  691. 'invoice_full_address' => $data[ 'invoice_full_address' ],
  692. 'is_tax_invoice' => $data[ 'is_tax_invoice' ],
  693. 'invoice_email' => $data[ 'invoice_email' ],
  694. 'invoice_title_type' => $data[ 'invoice_title_type' ],
  695. ];
  696. $order_id = model("giftcard_order")->add($data_order);
  697. //验证库存
  698. $card_stock_model = new CardStock();
  699. $check_stock = $card_stock_model->checkStock($data);
  700. if ($check_stock[ 'code' ] < 0) {
  701. model("giftcard_order")->rollback();
  702. return $check_stock;
  703. }
  704. $config_model = new Config();
  705. $balance_config = $config_model->getBalanceConfig($data[ 'site_id' ]);
  706. //扣除余额(统一扣除)
  707. if ($calculate_data[ "balance_money" ] > 0 && $balance_config[ 'data' ][ 'value' ][ 'balance_show' ] == 1) {
  708. $this->pay_type = "BALANCE";
  709. $calculate_data[ 'order_id' ] = $order_id;
  710. $balance_result = $this->useBalance($calculate_data, $data[ 'site_id' ]);
  711. if ($balance_result[ "code" ] < 0) {
  712. model("giftcard_order")->rollback();
  713. return $balance_result;
  714. }
  715. } else {
  716. $this->pay_money = $data[ 'order_total_price' ];
  717. }
  718. //生成支付单据
  719. $pay_body = '购买礼品卡';
  720. $pay->addPay($data[ 'site_id' ], $out_trade_no, $this->pay_type, $pay_body, $pay_body, $this->pay_money, '', 'GiftCardOrderPayNotify', '');
  721. $this->addOrderCronClose($order_id, $data[ 'site_id' ]);//增加关闭订单自动事件
  722. //订单生成的消息
  723. $message_model = new Message();
  724. $message_model->sendMessage([ 'keywords' => "ORDER_CREATE", 'order_id' => $order_id, 'site_id' => $data[ 'site_id' ] ]);
  725. model("giftcard_record")->commit();
  726. model("giftcard_order")->commit();
  727. return $this->success($out_trade_no);
  728. } catch (\Exception $e) {
  729. model("giftcard_order")->rollback();
  730. model("giftcard_record")->rollback();
  731. return $this->error('', $e->getMessage() . $e->getFile() . $e->getLine());
  732. }
  733. }
  734. /**
  735. * 增加订单自动关闭事件
  736. * @param $order_id
  737. */
  738. public function addOrderCronClose($order_id, $site_id)
  739. {
  740. //计算订单自动关闭时间
  741. $config_model = new Config();
  742. $order_config_result = $config_model->getOrderEventTimeConfig($site_id);
  743. $order_config = $order_config_result[ "data" ];
  744. $now_time = time();
  745. if ($order_config[ "value" ][ "auto_close" ] > 0) {
  746. $execute_time = $now_time + $order_config[ "value" ][ "auto_close" ] * 60; //自动关闭时间
  747. $cron_model = new Cron();
  748. $cron_model->addCron(1, 0, "订单自动关闭", "GiftCardOrderClose", $execute_time, $order_id);
  749. }
  750. }
  751. /**
  752. * 生成订单编号
  753. *
  754. * @param array $site_id
  755. */
  756. public function createOrderNo($site_id, $member_id = 0)
  757. {
  758. $time_str = date('YmdHi');
  759. $max_no = Cache::get($site_id . "_" . $member_id . "_" . $time_str);
  760. if (!isset($max_no) || empty($max_no)) {
  761. $max_no = 1;
  762. } else {
  763. $max_no = $max_no + 1;
  764. }
  765. $order_no = $time_str . $member_id . sprintf("%03d", $max_no);
  766. Cache::set($site_id . "_" . $member_id . "_" . $time_str, $max_no);
  767. return $order_no;
  768. }
  769. /**
  770. * 获取购卡订单列表
  771. * @param array $condition
  772. * @param int $page
  773. * @param int $page_size
  774. * @param string $order
  775. * @param string $field
  776. * @param string $alias
  777. * @param array $join
  778. * @param null $group
  779. * @param null $limit
  780. * @return array
  781. */
  782. public function orderPagelist($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '', $field = '*', $alias = 'a', $join = [], $group = null, $limit = null)
  783. {
  784. $list = model("giftcard_order")->pageList($condition, $field, $order, $page, $page_size, $alias, $join, $group, $limit);
  785. return $this->success($list);
  786. }
  787. /**
  788. * 订单计算
  789. * @param unknown $data
  790. */
  791. public function calculate($data)
  792. {
  793. $giftcard_model = new GiftCardModel();
  794. $data = $this->initMemberAccount($data);//初始化会员账户
  795. $giftcard_data = $giftcard_model->getGiftCardInfo([ [ 'site_id', '=', $data[ 'site_id' ] ], [ 'id', '=', $data[ 'card_id' ] ], [ 'elect_is_use', '=', 1 ] ])[ 'data' ] ?? [];
  796. if (empty($giftcard_data)) {
  797. return $this->error([], "礼品卡已下架");
  798. }
  799. $data[ "giftcard_info" ] = $giftcard_data;
  800. //余额付款
  801. if ($data[ 'is_balance' ] > 0 && $giftcard_data[ 'is_balance' ] == 1) {
  802. $this->member_balance_money = $data[ "member_account" ][ "balance_total" ] ?? 0;
  803. $balance_money = $data[ 'member_account' ][ 'balance' ] - $data[ 'order_total_price' ];
  804. if ($balance_money > 0) {
  805. $this->balance_money = $data[ 'order_total_price' ];
  806. } else {
  807. $this->balance_money = $data[ 'member_account' ][ 'balance' ];
  808. }
  809. }
  810. // 限购
  811. if ($giftcard_data[ 'max_buy' ] > 0) {
  812. if (( $data[ 'num' ] ) > $giftcard_data[ 'max_buy' ]) {
  813. $this->error = 1;
  814. $this->error_msg = "该商品每人限购{$giftcard_data['max_buy']}张";
  815. }
  816. }
  817. //总结计算
  818. $data[ 'delivery_money' ] = $this->delivery_money;
  819. $data[ 'coupon_money' ] = $this->coupon_money;
  820. $data[ 'adjust_money' ] = $this->adjust_money;
  821. $data[ 'invoice_money' ] = $this->invoice_money;
  822. $data[ 'invoice_delivery_money' ] = $this->invoice_delivery_money;
  823. $data[ 'promotion_money' ] = $this->promotion_money;
  824. $data[ 'order_money' ] = $this->order_money;
  825. $data[ 'balance_money' ] = $this->balance_money;
  826. $data[ 'pay_money' ] = $this->pay_money;
  827. $data[ 'goods_money' ] = $this->goods_money;
  828. $data[ 'goods_num' ] = $this->goods_num;
  829. return $data;
  830. }
  831. /**
  832. * 初始化会员账户
  833. * @param $data
  834. * @return mixed
  835. */
  836. public function initMemberAccount($data)
  837. {
  838. $member_model = new Member();
  839. $member_info_result = $member_model->getMemberDetail($data[ "member_id" ], $data[ "site_id" ]);
  840. $member_info = $member_info_result[ "data" ];
  841. if (!empty($member_info)) {
  842. if (!empty($member_info[ "pay_password" ])) {
  843. $is_pay_password = 1;
  844. } else {
  845. $is_pay_password = 0;
  846. }
  847. unset($member_info[ "pay_password" ]);
  848. $member_info[ "is_pay_password" ] = $is_pay_password;
  849. $data[ 'member_account' ] = $member_info;
  850. // 查询推荐会员卡
  851. if ($member_info[ 'member_level_type' ] == 0 && addon_is_exit('supermember', $data[ 'site_id' ])) {
  852. $member_card_model = new MemberCard();
  853. $recommend_member_card = $member_card_model->getRecommendMemberCard($data[ 'site_id' ]);
  854. if (!empty($recommend_member_card[ 'data' ])) {
  855. $recommend_member_card[ 'data' ][ 'discount_money' ] = 0;
  856. $recommend_member_card[ 'data' ][ 'charge_rule' ] = json_decode($recommend_member_card[ 'data' ][ 'charge_rule' ], true);
  857. $this->recommend_member_card = $recommend_member_card[ 'data' ];
  858. }
  859. }
  860. }
  861. return $data;
  862. }
  863. /**
  864. * 使用余额
  865. * @param $data
  866. * @param $site_id
  867. * @param string $from_type
  868. * @return array
  869. */
  870. public function useBalance($data, $site_id, $from_type = 'order')
  871. {
  872. $this->pay_type = "BALANCE";
  873. $member_model = new Member();
  874. // $result = $member_model->checkPayPassword($data["member_id"], $data["pay_password"]);
  875. // if ($result["code"] >= 0) {
  876. $balance_money = $data[ "member_account" ][ "balance_money" ]; //现金余额
  877. $balance = $data[ "member_account" ][ "balance" ]; //储值余额
  878. $member_account_model = new MemberAccount();
  879. $surplus_banance = $data[ "balance_money" ];
  880. //优先扣除储值余额
  881. if ($balance > 0) {
  882. if ($balance >= $surplus_banance) {
  883. $real_balance = $surplus_banance;
  884. } else {
  885. $real_balance = $balance;
  886. }
  887. $result = $member_account_model->addMemberAccount($site_id, $data[ "member_id" ], "balance", -$real_balance, $from_type, $data[ 'order_id' ], "订单消费扣除");
  888. $surplus_banance -= $real_balance;
  889. }
  890. // if($balance_money > 0){
  891. // if($balance_money > $surplus_banance){
  892. // $real_balance_money = $surplus_banance;
  893. // }else{
  894. // $real_balance_money = $balance_money;
  895. // }
  896. // $result = $member_account_model->addMemberAccount($data["member_id"], "balance", -$real_balance, "order", "余额抵扣","订单余额抵扣,扣除储值余额:".$real_balance);
  897. // }
  898. if ($surplus_banance > 0) {
  899. $result = $member_account_model->addMemberAccount($site_id, $data[ "member_id" ], "balance_money", -$surplus_banance, $from_type, $data[ 'order_id' ], "订单消费扣除");
  900. }
  901. $this->pay_money = $data[ "balance_money" ] - ( $real_balance + $surplus_banance ); //计算出实际支付金额
  902. return $result;
  903. // } else {
  904. // return $result;
  905. // }
  906. }
  907. /**
  908. * 关闭礼品卡
  909. * @param $order_id
  910. * @return array|\multitype
  911. */
  912. public function cronCloseGiftCard($record_id)
  913. {
  914. $giftcard_info = model('giftcard_record')->getInfo([ [ 'id', '=', $record_id ] ], 'elect_status');
  915. if (!empty($giftcard_info)) {
  916. if ($giftcard_info[ 'elect_status' ] == 0) {
  917. $res = model('giftcard_record')->update([ 'elect_status' => 2 ], [ [ 'id', '=', $record_id ] ]);
  918. return $this->success($res);
  919. } else {
  920. return $this->error("", "该礼品卡已使用或关闭");
  921. }
  922. } else {
  923. return $this->error("", "该礼品卡不存在");
  924. }
  925. }
  926. /**
  927. * 订单线上支付
  928. * @param unknown $out_trade_no
  929. */
  930. public function orderOnlinePay($data)
  931. {
  932. model('order')->startTrans();
  933. try {
  934. $out_trade_no = $data[ "out_trade_no" ];
  935. $order_list = model("order")->getList([ [ 'out_trade_no', '=', $out_trade_no ] ], '*');
  936. $message_list = [];
  937. //订单支付消息
  938. foreach ($order_list as $k => $order) {
  939. if ($order[ 'order_status' ] == -1) {
  940. continue;
  941. }
  942. switch ( $order[ 'order_type' ] ) {
  943. case 1:
  944. $order_model = new Order();
  945. break;
  946. case 2:
  947. $order_model = new StoreOrder();
  948. break;
  949. case 3:
  950. $order_model = new LocalOrder();
  951. break;
  952. case 4:
  953. $order_model = new VirtualOrder();
  954. break;
  955. }
  956. if (isset($data[ 'log_data' ])) {
  957. $order_model->orderPay($order, $data[ "pay_type" ], $data[ "log_data" ]);
  958. } else {
  959. $order_model->orderPay($order, $data[ "pay_type" ]);
  960. }
  961. $is_enable_refund = array (
  962. "is_enable_refund" => 0,
  963. );
  964. //赠送商品禁止退货
  965. model("order")->update($is_enable_refund, [ [ "order_id", "=", $order[ "order_id" ] ] ]);
  966. //同时将用户表的order_money和order_num更新
  967. model('member')->setInc([ [ 'member_id', '=', $order[ 'member_id' ] ] ], 'order_money', $order[ 'order_money' ]);
  968. model('member')->setInc([ [ 'member_id', '=', $order[ 'member_id' ] ] ], 'order_num');
  969. //支付后商品增加销量
  970. $order_goods_list = model("order_goods")->getList([ [ "order_id", "=", $order[ "order_id" ] ] ], "sku_id,num");
  971. $message_list[] = $order;
  972. $order = model("order")->getInfo([ [ 'order_id', '=', $order[ "order_id" ] ] ], '*');
  973. $message_model = new Message();
  974. // 发送消息
  975. $param = [ "keywords" => "ORDER_PAY" ];
  976. $param = array_merge($param, $order);
  977. $message_model->sendMessage($param);
  978. //商家消息
  979. $param = [ "keywords" => "BUYER_PAY" ];
  980. $param = array_merge($param, $order);
  981. $message_model->sendMessage($param);
  982. }
  983. model('order')->commit();
  984. return $this->success();
  985. } catch (\Exception $e) {
  986. model('order')->rollback();
  987. return $this->error('', $e->getMessage());
  988. }
  989. }
  990. /**
  991. * @param $id
  992. * @return int
  993. * 实体卡作废
  994. */
  995. public function cronGiftCardChangeStatus($id)
  996. {
  997. $res = model('giftcard')->update([ 'status' => 2 ], [ [ 'id', '=', $id ] ]);
  998. model('giftcard_record')->update([ 'elect_status' => 2, 'entity_status' => 2 ], [ [ 'giftcard_id', '=', $id ] ]);
  999. return $res;
  1000. }
  1001. /**
  1002. * 限制错误次数
  1003. */
  1004. public function cardErrorNum($carmichael_text, $entity_bianhao, $member_id)
  1005. {
  1006. $session_card_num = Cache::get("giftcard_num_" . $member_id . "_" . $entity_bianhao);
  1007. $card_info = model("giftcard_record")->getInfo([ [ 'entity_bianhao', '=', $entity_bianhao ] ], "*", "", "");
  1008. $card_time_d = 0;//最后一次错误时间
  1009. $time_d = 0;//当前错误时间
  1010. if (!empty($session_card_num)) {
  1011. $card_time_d = date("d", $session_card_num[ 'time' ]);//最后一次错误时间
  1012. $time_d = date('d', time());//当前错误时间
  1013. if ($time_d <= $card_time_d && $session_card_num[ 'num' ] == 5 && $card_info) {
  1014. return $this->error("", "您多次输入卡号卡密错误,该卡今日不再支持兑换");
  1015. }
  1016. }
  1017. $info = $this->getGiftCardRecordInfo([ [ 'carmichael_text', '=', $carmichael_text ], [ 'entity_bianhao', '=', $entity_bianhao ] ]);
  1018. if ($info[ 'code' ] >= 0 && $info[ 'data' ]) {
  1019. model('giftcard_record')->update([ 'use_member_id' => $member_id ], [ [ 'id', '=', $info[ 'data' ][ 'id' ] ], [ 'site_id', '=', $info[ 'data' ][ 'site_id' ] ], [ 'entity_status', '=', 1 ] ]);
  1020. return $info;
  1021. }
  1022. if ($card_info) {
  1023. if (empty($session_card_num)) {
  1024. Cache::set("giftcard_num_" . $member_id . "_" . $entity_bianhao, [ 'time' => time(), 'num' => 1 ], 86400);
  1025. return $this->error("", "当前兑换码卡密错误,还可尝试4次");
  1026. } else {
  1027. if ($time_d > $card_time_d) {
  1028. Cache::set("giftcard_num_" . $member_id . "_" . $entity_bianhao, [ 'time' => time(), 'num' => 1 ], 86400);
  1029. return $this->error("", "当前兑换码卡密错误,还可尝试4次");
  1030. } else if ($session_card_num[ 'num' ] < 5) {
  1031. Cache::set("giftcard_num_" . $member_id . "_" . $entity_bianhao, [ 'time' => time(), 'num' => $session_card_num[ 'num' ] + 1 ], 86400);
  1032. $num = 5 - $session_card_num[ 'num' ] - 1;
  1033. return $this->error("", "当前兑换码卡密错误,还可尝试" . $num . "次");
  1034. }
  1035. }
  1036. }
  1037. return $info;
  1038. }
  1039. }