MemberCard.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 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\UtilService;
  13. use think\Db;
  14. use traits\ModelTrait;
  15. use basic\ModelBasic;
  16. use app\admin\model\user\User;
  17. use app\admin\model\user\UserBill;
  18. use app\admin\model\user\MemberCardBatch;
  19. use service\PhpSpreadsheetService;
  20. /**
  21. * 会员卡批次 model
  22. * Class MemberCard
  23. * @package app\admin\model\user
  24. */
  25. class MemberCard extends ModelBasic
  26. {
  27. use ModelTrait;
  28. public static function getCardOne(array $where)
  29. {
  30. if (empty($where)) {
  31. return false;
  32. }
  33. return self::where($where)->find();
  34. }
  35. /**根据批次id和数量生成卡
  36. * @param int $batch_id
  37. * @param int $total_num
  38. * @return bool
  39. */
  40. public static function addCard(int $batch_id, int $total_num)
  41. {
  42. if (!$batch_id || $batch_id == 0 || !$total_num || $total_num == 0) {
  43. return false;
  44. }
  45. try {
  46. $inster_card = array();
  47. for ($i = 0; $i < $total_num; $i++) {
  48. $inster_card['card_number'] = UtilService::makeRandomNumber("CR", 5, $batch_id);
  49. $inster_card['card_password'] = UtilService::makeRandomNumber('', 5);
  50. $inster_card['card_batch_id'] = $batch_id;
  51. $inster_card['create_time'] = time();
  52. $res[] = $inster_card;
  53. }
  54. //数据切片批量插入,提高性能
  55. $chunk_inster_card = array_chunk($res, 100, true);
  56. foreach ($chunk_inster_card as $v) {
  57. self::insertAll($v);
  58. }
  59. return true;
  60. } catch (\Exception $e) {
  61. echo $e->getMessage();
  62. }
  63. }
  64. public function getCreateTimeAttr($time)
  65. {
  66. return $time;//返回create_time原始数据,不进行时间戳转换。
  67. }
  68. public static function setCardWhere($where)
  69. {
  70. $model = new self();
  71. if (isset($where['card_batch_id']) && $where['card_batch_id']) {
  72. $model = $model->where('card_batch_id', $where['card_batch_id']);
  73. }
  74. if (isset($where['card_number']) && $where['card_number']) {
  75. $model = $model->where('card_number', 'like', "%$where[card_number]%");
  76. }
  77. if (isset($where['is_status']) && $where['is_status'] != "") {
  78. $model = $model->where('status', $where['is_status']);
  79. }
  80. if (isset($where['is_use']) && $where['is_use'] != "") {
  81. if ($where['is_use'] == 1) {
  82. $model = $model->where('use_uid', '>=', 1);
  83. } else {
  84. $model = $model->where('use_uid', 0);
  85. }
  86. }
  87. if (isset($where['phone']) && $where['phone']) {
  88. $uid = User::where(['phone' => $where['phone']])->value('uid');
  89. $model = $model->where('use_uid', $uid);
  90. }
  91. return $model->order('use_uid desc,id desc');
  92. }
  93. public static function getCardList(array $where)
  94. {
  95. if (!is_array($where)) {
  96. return false;
  97. }
  98. $model = self::setCardWhere($where);
  99. if (isset($where['excel']) && $where['excel'] == 1) {
  100. $data = ($data = $model->select()) && count($data) ? $data->toArray() : [];
  101. self::SaveExcel($data);
  102. } else {
  103. $data = ($data = $model->page((int)$where['page'], (int)$where['limit'])->select()) && count($data) ? $data->toArray() : [];
  104. if (!empty($data)) {
  105. foreach ($data as $k => $v) {
  106. $data[$k]['use_time'] = ($v['use_time'] != 0 || $v['use_time']) ? date('Y-m-d H:i:s', $v['use_time']) : "";
  107. if ($v['use_uid'] && $v['use_uid'] > 0) {
  108. $user_info = User::where(['uid' => $v['use_uid']])->field("account, nickname, phone")->find();
  109. if ($user_info) {
  110. $data[$k]['username'] = (isset($user_info['nickname']) && $user_info['nickname']) ? $user_info['nickname'] : $user_info['account'];
  111. $data[$k]['user_phone'] = (isset($user_info['phone']) && $user_info['phone']) ? $user_info['phone'] : "";
  112. } else {
  113. $data[$k]['username'] = "用户已被删除";
  114. $data[$k]['user_phone'] = "无";
  115. }
  116. } else {
  117. $data[$k]['username'] = "";
  118. $data[$k]['user_phone'] = "";
  119. }
  120. }
  121. }
  122. $count = self::setCardWhere($where)->count();
  123. return compact('data', 'count');
  124. }
  125. }
  126. /*
  127. * 保存并下载excel
  128. * $list array
  129. * return
  130. */
  131. public static function SaveExcel($list)
  132. {
  133. $export = [];
  134. foreach ($list as $index => $item) {
  135. $batch = MemberCardBatch::where('id', $item['card_batch_id'])->value('title');
  136. $use_time = ($item['use_time'] != 0 || $item['use_time']) ? date('Y-m-d H:i:s', $item['use_time']) : "";
  137. if ($item['use_uid'] && $item['use_uid'] > 0) {
  138. $user_info = User::where(['uid' => $item['use_uid']])->field("account, nickname, phone")->find();
  139. if ($user_info) {
  140. $username = (isset($user_info['nickname']) && $user_info['nickname']) ? $user_info['nickname'] : $user_info['account'];
  141. $user_phone = (isset($user_info['phone']) && $user_info['phone']) ? $user_info['phone'] : "";
  142. } else {
  143. $username = "用户已被删除";
  144. $user_phone = "无";
  145. }
  146. } else {
  147. $username = "";
  148. $user_phone = "";
  149. }
  150. $export[] = [
  151. $item['card_batch_id'],
  152. $batch,
  153. $item['card_number'],
  154. $item['card_password'],
  155. $item['status'] == 1 ? '激活' : '冻结',
  156. $item['use_uid'] > 0 ? '使用' : '未使用',
  157. $username,
  158. $user_phone,
  159. $use_time
  160. ];
  161. }
  162. $filename = '会员卡导出' . time() . '.xlsx';
  163. $head = ['批次编号', '批次名称', '卡号', '密码', '是否激活', '是否使用', '领取人', '领取人电话', '领取时间'];
  164. PhpSpreadsheetService::outdata($filename, $export, $head);
  165. }
  166. /**
  167. * @param $code
  168. * @return mixed
  169. * @throws \think\db\exception\DataNotFoundException
  170. * @throws \think\db\exception\ModelNotFoundException
  171. * @throws \think\exception\DbException
  172. */
  173. public static function cateDays($code)
  174. {
  175. $cate = self::where('card_number', $code)->find();
  176. $use_day = MemberCardBatch::where('id', $cate['card_batch_id'])->value('use_day');
  177. return $use_day;
  178. }
  179. }