Card.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * @author: xaboy<365615158@qq.com>
  4. * @day: 2017/11/11
  5. */
  6. namespace app\models\store;
  7. use app\admin\model\system\SystemStoreBill;
  8. use app\models\system\SystemStore;
  9. use crmeb\services\PHPExcelService;
  10. use crmeb\traits\ModelTrait;
  11. use crmeb\basic\BaseModel;
  12. /**
  13. * 商品浏览分析
  14. * Class StoreVisit
  15. * @package app\admin\model\store
  16. */
  17. class Card extends BaseModel
  18. {
  19. /**
  20. * 数据表主键
  21. * @var string
  22. */
  23. protected $pk = 'id';
  24. /**
  25. * 模型名称
  26. * @var string
  27. */
  28. protected $name = 'card';
  29. use ModelTrait;
  30. /**
  31. * @param $where
  32. * @return array
  33. */
  34. public static function systemPage($where)
  35. {
  36. $model = new self;
  37. $model = $model->alias('s');
  38. if ($where['status'] != '') $model = $model->where('s.status', $where['status']);
  39. if (isset($where['store_id']) && $where['store_id'] != '') $model = $model->where('s.store_id', $where['store_id']);
  40. if (isset($where['name']) && $where['name'] != '') $model = $model->where('s.name|s.code', 'LIKE', "%$where[name]%");
  41. $model = $model->page(bcmul($where['page'], $where['limit'], 0), $where['limit']);
  42. $model = $model->order('s.id desc');
  43. return self::page($model, function ($item) {
  44. if ($item['status'] == 0) {
  45. $item['start_name'] = '待赠送';
  46. } else if ($item['status'] == 1) {
  47. $item['start_name'] = '待核销';
  48. } else $item['start_name'] = '已使用';
  49. $item['store'] = $item['store_id'] ? SystemStore::where('id', $item['store_id'])->value('name') . '/' . $item['store_id'] : '--';
  50. $item['check_store'] = $item['check_store_id'] ? SystemStore::where('id', $item['check_store_id'])->value('name') . '/' . $item['check_store_id'] : '--';
  51. $item['add_time_text'] = $item['add_time'] ? date('Y-m-d H:i:s', $item['add_time']) : '--';
  52. $item['send_time_text'] = $item['send_time'] ? date('Y-m-d H:i:s', $item['send_time']) : '--';
  53. $item['check_time_text'] = $item['check_time'] ? date('Y-m-d H:i:s', $item['check_time']) : '--';
  54. }, $where, $where['limit']);
  55. }
  56. public static function SaveExcel($where)
  57. {
  58. $model = new self;
  59. if ($where['status'] != '') $model = $model->where('s.status', $where['status']);
  60. if ($where['name'] != '') $model = $model->where('s.name|s.code', 'LIKE', "%$where[name]%");
  61. $list = $model->order('id desc')->select();
  62. count($list) && $list = $list->toArray();
  63. $excel = [];
  64. foreach ($list as $item) {
  65. if ($item['status'] == 0) {
  66. $item['start_name'] = '待赠送';
  67. } else if ($item['status'] == 1) {
  68. $item['start_name'] = '待核销';
  69. } else $item['start_name'] = '已使用';
  70. $item['store'] = $item['store_id'] ? SystemStore::where('id', $item['store_id'])->value('name') . '/' . $item['store_id'] : '--';
  71. $item['check_store'] = $item['check_store_id'] ? SystemStore::where('id', $item['check_store_id'])->value('name') . '/' . $item['check_store_id'] : '--';
  72. $item['add_time_text'] = $item['add_time'] ? date('Y-m-d H:i:s', $item['add_time']) : '--';
  73. $item['send_time_text'] = $item['send_time'] ? date('Y-m-d H:i:s', $item['send_time']) : '--';
  74. $item['check_time_text'] = $item['check_time'] ? date('Y-m-d H:i:s', $item['check_time']) : '--';
  75. $excel[] = [
  76. $item['id'],
  77. $item['name'],
  78. $item['code'],
  79. $item['password'],
  80. $item['price'],
  81. $item['store_award'],
  82. $item['start_name'],
  83. $item['store'],
  84. $item['check_store'],
  85. $item['add_time_text'],
  86. $item['send_time_text'],
  87. $item['check_time_text'],
  88. ];
  89. }
  90. PHPExcelService::setExcelHeader(['编号', '标题', '卡号', '密码', '价格', '奖金', '状态'
  91. , '发放门店', '核销门店', '添加时间', '发放时间', '核销时间',])
  92. ->setExcelTile('礼品卡导出', ' ', ' 生成时间:' . date('Y-m-d H:i:s', time()))
  93. ->setExcelContent($excel)
  94. ->ExcelSave();
  95. }
  96. public static function createCode()
  97. {
  98. do {
  99. $str = date('Y') . '-' . substr(time(), 6, 4) . '-' . rand(1000, 9999) . '-' . rand(1000, 9999);
  100. } while (self::be(['code' => $str]));
  101. return $str;
  102. }
  103. public static function createPassword()
  104. {
  105. return substr(md5(md5('lpk.zccy.rand' . rand(100000, 999999)) . rand(0, 100000)), 0, 16);
  106. }
  107. public static function sendCard($store_id, $card_code)
  108. {
  109. $card = self::where('code', $card_code)->find();
  110. if (!$card) return self::setErrorInfo('礼品卡不存在');
  111. if ($card['status'] != 0) return self::setErrorInfo('礼品卡不可赠送');
  112. $card['store_id'] = $store_id;
  113. $card['send_time'] = time();
  114. $card['status'] = 1;
  115. $res = $card->save();
  116. if ($res) return true;
  117. else return self::setErrorInfo('礼品卡送出失败');
  118. }
  119. public static function checkCard($store_id, $card_code, $password)
  120. {
  121. $card = self::where('code', $card_code)->find();
  122. if (!$card) return self::setErrorInfo('礼品卡不存在');
  123. if ($card['status'] != 1) return self::setErrorInfo('礼品卡不可核销');
  124. if ($card['password'] !== $password) return self::setErrorInfo('卡密错误');
  125. $card['check_store_id'] = $store_id;
  126. $card['check_time'] = time();
  127. $card['status'] = 2;
  128. $res = $card->save() && self::sendAward($card);
  129. if ($res) return true;
  130. else return self::setErrorInfo(self::getErrorInfo('核销失败'));
  131. }
  132. public static function sendAward($card)
  133. {
  134. if ($card['store_id'] && $card['store_award'] && SystemStore::be(['id' => $card['store_id']])) {
  135. $store = SystemStore::get($card['store_id']);
  136. $mark = '赠送的礼品卡' . $card['name'] . '(' . $card['id'] . ')核销成功,获得佣金。';
  137. SystemStoreBill::income('礼品卡佣金', $card['store_id'], 'brokerage_price', 'card_award', $card['store_award'], $card['id'], bcadd($store['brokerage_price'], $card['store_award'], 2), $mark);
  138. SystemStore::bcInc($card['store_id'], 'brokerage_price', $card['store_award'], 'id');
  139. }
  140. return true;
  141. }
  142. }