MemberCard.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\model\user;
  12. use service\PHPExcelService;
  13. use service\UtilService;
  14. use think\Db;
  15. use traits\ModelTrait;
  16. use basic\ModelBasic;
  17. use app\admin\model\user\User;
  18. use app\admin\model\user\UserBill;
  19. use app\admin\model\user\MemberCardBatch;
  20. use app\admin\model\user\Group as GroupModel;
  21. /**
  22. * 会员卡批次 model
  23. * Class User
  24. * @package app\admin\model\user
  25. */
  26. class MemberCard extends ModelBasic
  27. {
  28. use ModelTrait;
  29. public static function getCardOne(array $where)
  30. {
  31. if (empty($where)) {
  32. return false;
  33. }
  34. return Db::name("member_card")->where($where)->find();
  35. }
  36. /**根据批次id和数量生成卡
  37. * @param int $batch_id
  38. * @param int $total_num
  39. * @return bool
  40. */
  41. public static function addCard(int $batch_id, int $total_num)
  42. {
  43. if (!$batch_id || $batch_id == 0 || !$total_num || $total_num == 0) {
  44. return false;
  45. }
  46. try{
  47. $inster_card = array();
  48. for ($i = 0; $i < $total_num; $i++) {
  49. $inster_card['card_number'] = UtilService::makeRandomNumber("CR", $batch_id);
  50. $inster_card['card_password'] = UtilService::makeRandomNumber();
  51. $inster_card['card_batch_id'] = $batch_id;
  52. $inster_card['create_time'] = time();
  53. $res[] = $inster_card;
  54. }
  55. //数据切片批量插入,提高性能
  56. $chunk_inster_card = array_chunk($res, 100 ,true);
  57. foreach ($chunk_inster_card as $v) {
  58. Db::table('eb_member_card')->insertAll($v);
  59. }
  60. return true;
  61. }catch (\Exception $e){
  62. echo $e->getMessage();
  63. }
  64. }
  65. public function getCreateTimeAttr($time)
  66. {
  67. return $time;//返回create_time原始数据,不进行时间戳转换。
  68. }
  69. public static function getCardList(array $where){
  70. if (!is_array($where)) {
  71. return false;
  72. }
  73. $batch_where = array();
  74. if (isset($where['card_batch_id']) && $where['card_batch_id']){
  75. $batch_where['card_batch_id'] = $where['card_batch_id'];
  76. }
  77. if (isset($where['card_number']) && $where['card_number']) {
  78. $batch_where['card_number'] = array('like', '%'.$where['card_number']);
  79. }
  80. if (isset($where['is_use']) && $where['is_use'] != "") {
  81. if ($where['is_use'] == 1) {
  82. $batch_where['use_uid'] = array('>', 0);
  83. }else{
  84. $batch_where['use_uid'] = 0;
  85. }
  86. }
  87. if (isset($where['is_status']) && $where['is_status'] != "") {
  88. $batch_where['status'] = $where['is_status'];
  89. }
  90. if (isset($where['phone']) && $where['phone']) {
  91. $user_phone = User::where(['phone' => $where['phone']])->field("uid")->find();
  92. $data = $count = [];
  93. if (!$user_phone) return compact('data','count');
  94. $batch_where['use_uid'] = $user_phone['uid'];
  95. }
  96. $model=new self();
  97. $model=$model->where($batch_where)->order('use_uid desc');
  98. if (isset($where['excel']) && $where['excel'] == 1) {
  99. $data = ($data = $model->select()) && count($data) ? $data->toArray() : [];
  100. self::SaveExcel($data);
  101. } else {
  102. $data = ($data = $model->page((int)$where['page'], (int)$where['limit'])->select()) && count($data) ? $data->toArray() : [];
  103. if (!empty($data)) {
  104. foreach ($data as $k => $v) {
  105. $data[$k]['use_time'] = ($v['use_time'] != 0 || $v['use_time']) ? date('Y-m-d H:i:s', $v['use_time']) : "";
  106. if ($v['use_uid'] && $v['use_uid'] > 0) {
  107. $user_info = User::where(['uid' => $v['use_uid']])->field("account, nickname, phone")->find();
  108. if($user_info){
  109. $data[$k]['username'] = (isset($user_info['nickname']) && $user_info['nickname']) ? $user_info['nickname'] : $user_info['account'];
  110. $data[$k]['user_phone'] = (isset($user_info['phone']) && $user_info['phone']) ? $user_info['phone'] : "";
  111. }else{
  112. $data[$k]['username'] = "用户已被删除";
  113. $data[$k]['user_phone'] = "无";
  114. }
  115. }else{
  116. $data[$k]['username'] = "";
  117. $data[$k]['user_phone'] = "";
  118. }
  119. }
  120. }
  121. $count = self::where($batch_where)->count();
  122. return compact('data','count');
  123. }
  124. }
  125. /*
  126. * 保存并下载excel
  127. * $list array
  128. * return
  129. */
  130. public static function SaveExcel($list)
  131. {
  132. $export = [];
  133. foreach ($list as $index => $item) {
  134. $batch=MemberCardBatch::where('id',$item['card_batch_id'])->value('title');
  135. $export[] = [
  136. $item['id'], $item['card_number'],
  137. $item['card_password'],
  138. [$item['status'] == 1 ? '激活' : '冻结'],
  139. [$item['use_uid'] >0 ? '使用' : '未使用'],
  140. $item['card_batch_id'],
  141. $batch,
  142. ];
  143. }
  144. PHPExcelService::setExcelHeader(['编号', '卡号', '密码','激活','使用','批次编号','批次名称'])
  145. ->setExcelTile('会员卡导出', '会员卡信息' . time(), ' 生成时间:' . date('Y-m-d H:i:s', time()))
  146. ->setExcelContent($export)
  147. ->ExcelSave();
  148. }
  149. public static function cateDays($code){
  150. $cate=self::where('card_number',$code)->find();
  151. $use_day=MemberCardBatch::where('id',$cate['card_batch_id'])->value('use_day');
  152. return $use_day;
  153. }
  154. }