SpecialExchange.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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\ump;
  12. use service\SystemConfigService;
  13. use think\Db;
  14. use traits\ModelTrait;
  15. use basic\ModelBasic;
  16. use service\UtilService;
  17. use app\admin\model\user\User;
  18. use app\admin\model\special\Special;
  19. use service\PhpSpreadsheetService;
  20. /**
  21. * 活动批次 model
  22. * Class SpecialExchange
  23. * @package app\admin\model\ump
  24. */
  25. class SpecialExchange extends ModelBasic
  26. {
  27. use ModelTrait;
  28. const fileLocation = 'public/qrcode/';
  29. /**
  30. * 生成会员卡批次二维码
  31. */
  32. public static function qrcodes_url($special_id, $size = 5)
  33. {
  34. vendor('phpqrcode.phpqrcode');
  35. $urls = SystemConfigService::get('site_url') . '/';
  36. $url = $urls . 'wap/special/exchange/special_id/' . $special_id;
  37. $value = $url; //二维码内容
  38. $errorCorrectionLevel = 'H'; //容错级别
  39. $matrixPointSize = $size; //生成图片大小
  40. //生成二维码图片
  41. $filename = self::fileLocation . rand(10000000, 99999999) . '.png';
  42. \QRcode::png($value, $filename, $errorCorrectionLevel, $matrixPointSize, 2);
  43. return $urls . $filename;
  44. }
  45. /**获取单条批次信息
  46. * @param $id
  47. * @return array|bool|false|\PDOStatement|string|\think\Model
  48. * @throws \think\db\exception\DataNotFoundException
  49. * @throws \think\db\exception\ModelNotFoundException
  50. * @throws \think\exception\DbException
  51. */
  52. public static function getBatchOne($id)
  53. {
  54. if (!$id) {
  55. return false;
  56. }
  57. return self::where(['id' => $id])->find();
  58. }
  59. public function getCreateTimeAttr($time)
  60. {
  61. return $time;//返回create_time原始数据,不进行时间戳转换。
  62. }
  63. /**根据批次id和数量生成卡
  64. * @param int $batch_id
  65. * @param int $total_num
  66. * @return bool
  67. */
  68. public static function addCard($batch_id, $total_num, $special_id)
  69. {
  70. if (!$batch_id || $batch_id == 0 || !$special_id || $special_id == 0 || !$total_num || $total_num == 0) {
  71. return false;
  72. }
  73. try {
  74. $inster_card = array();
  75. for ($i = 0; $i < $total_num; $i++) {
  76. $inster_card['special_id'] = $special_id;
  77. $inster_card['exchange_code'] = UtilService::makeRandomNumber('', 6);
  78. $inster_card['card_batch_id'] = $batch_id;
  79. $inster_card['add_time'] = time();
  80. $res[] = $inster_card;
  81. }
  82. //数据切片批量插入,提高性能
  83. $chunk_inster_card = array_chunk($res, 100, true);
  84. foreach ($chunk_inster_card as $v) {
  85. SpecialExchange::insertAll($v);
  86. }
  87. return true;
  88. } catch (\Exception $e) {
  89. echo $e->getMessage();
  90. }
  91. }
  92. public static function setCardWhere($where)
  93. {
  94. $model = new self();
  95. if (isset($where['card_batch_id']) && $where['card_batch_id']) {
  96. $model = $model->where('card_batch_id', $where['card_batch_id']);
  97. }
  98. if (isset($where['exchange_code']) && $where['exchange_code']) {
  99. $model = $model->where('exchange_code', 'like', "%$where[exchange_code]%");
  100. }
  101. if (isset($where['is_status']) && $where['is_status'] != "") {
  102. $model = $model->where('status', $where['is_status']);
  103. }
  104. if (isset($where['is_use']) && $where['is_use'] != "") {
  105. if ($where['is_use'] == 1) {
  106. $model = $model->where('use_uid', '>=', 1);
  107. } else {
  108. $model = $model->where('use_uid', 0);
  109. }
  110. }
  111. if (isset($where['phone']) && $where['phone']) {
  112. $uid = User::where(['phone' => $where['phone']])->value('uid');
  113. $model = $model->where('use_uid', $uid);
  114. }
  115. $time['data'] = '';
  116. if ($where['start_time'] != '' && $where['end_time'] != '') {
  117. $time['data'] = $where['start_time'] . ' - ' . $where['end_time'];
  118. }
  119. $model = self::getModelTime($time, $model, 'use_time');
  120. return $model->order('use_time desc,use_uid desc,id desc');
  121. }
  122. public static function getCardList($where = [])
  123. {
  124. if (!is_array($where)) {
  125. return false;
  126. }
  127. $model = self::setCardWhere($where);
  128. if (isset($where['excel']) && $where['excel'] == 1) {
  129. $data = ($data = $model->select()) && count($data) ? $data->toArray() : [];
  130. self::SaveExcel($data);
  131. } else {
  132. $data = ($data = $model->page((int)$where['page'], (int)$where['limit'])->select()) && count($data) ? $data->toArray() : [];
  133. if (!empty($data)) {
  134. foreach ($data as $k => $v) {
  135. $data[$k]['use_time'] = ($v['use_time'] != 0 || $v['use_time']) ? date('Y-m-d H:i:s', $v['use_time']) : "";
  136. if ($v['use_uid'] && $v['use_uid'] > 0) {
  137. $user_info = User::where(['uid' => $v['use_uid']])->field("account, nickname, phone")->find();
  138. if ($user_info) {
  139. $data[$k]['username'] = (isset($user_info['nickname']) && $user_info['nickname']) ? $user_info['nickname'] : $user_info['account'];
  140. $data[$k]['user_phone'] = (isset($user_info['phone']) && $user_info['phone']) ? $user_info['phone'] : "";
  141. } else {
  142. $data[$k]['username'] = "用户已被删除";
  143. $data[$k]['user_phone'] = "无";
  144. }
  145. } else {
  146. $data[$k]['username'] = "";
  147. $data[$k]['user_phone'] = "";
  148. }
  149. }
  150. }
  151. $count = self::setCardWhere($where)->count();
  152. return compact('data', 'count');
  153. }
  154. }
  155. /**
  156. * 保存并下载excel
  157. * $list array
  158. * return
  159. */
  160. public static function SaveExcel($list)
  161. {
  162. $export = [];
  163. foreach ($list as $index => $item) {
  164. $batch = SpecialBatch::where('id', $item['card_batch_id'])->value('title');
  165. $special_title = Special::where('id', $item['special_id'])->value('title');
  166. $use_time = ($item['use_time'] != 0 || $item['use_time']) ? date('Y-m-d H:i:s', $item['use_time']) : "";
  167. if ($item['use_uid'] && $item['use_uid'] > 0) {
  168. $user_info = User::where(['uid' => $item['use_uid']])->field("account, nickname, phone")->find();
  169. if ($user_info) {
  170. $username = (isset($user_info['nickname']) && $user_info['nickname']) ? $user_info['nickname'] : $user_info['account'];
  171. $user_phone = (isset($user_info['phone']) && $user_info['phone']) ? $user_info['phone'] : "";
  172. } else {
  173. $username = "用户已被删除";
  174. $user_phone = "无";
  175. }
  176. } else {
  177. $username = "";
  178. $user_phone = "";
  179. }
  180. $export[] = [
  181. $item['card_batch_id'],
  182. $batch,
  183. $special_title,
  184. $item['exchange_code'],
  185. $item['status'] == 1 ? '激活' : '冻结',
  186. $item['use_uid'] > 0 ? '使用' : '未使用',
  187. $username,
  188. $user_phone,
  189. $use_time
  190. ];
  191. }
  192. $filename = '兑换码导出' . time() . '.xlsx';
  193. $head = ['活动编号', '活动名称', '专题名称', '兑换码', '是否激活', '是否使用', '兑换人', '兑换人电话', '兑换时间'];
  194. PhpSpreadsheetService::outdata($filename, $export, $head);
  195. }
  196. }