MemberCardBatchServices.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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\services\user\member;
  12. use app\dao\user\member\MemberCardBatchDao;
  13. use app\services\BaseServices;
  14. use crmeb\exceptions\AdminException;
  15. /**
  16. * Class MemberCardBatchServices
  17. * @package app\services\user\member
  18. * @mixin MemberCardBatchDao
  19. */
  20. class MemberCardBatchServices extends BaseServices
  21. {
  22. /**
  23. * 初始化,获得dao层句柄
  24. * MemberCardServices constructor.
  25. * @param MemberCardBatchDao $memberCardBatchDao
  26. */
  27. public function __construct(MemberCardBatchDao $memberCardBatchDao)
  28. {
  29. $this->dao = $memberCardBatchDao;
  30. }
  31. /**
  32. * 获取会员卡批次列表
  33. * @param array $where
  34. */
  35. public function getList(array $where = [])
  36. {
  37. [$page, $limit] = $this->getPageValue();
  38. $list = $this->dao->getList($where, $page, $limit);
  39. if ($list) {
  40. foreach ($list as &$v) {
  41. $v['add_time'] = date('Y-m-d H:i:s', $v['add_time']);
  42. //$v['qrcode'] = json_decode($v['qrcode'], true);
  43. }
  44. }
  45. $count = $this->dao->count($where);
  46. return compact('list', 'count');
  47. }
  48. /**
  49. * @param int $id
  50. * @param array $data
  51. */
  52. public function save(int $id, array $data)
  53. {
  54. if (!$data['title']) throw new AdminException("请填写批次名称");
  55. if (!$data['total_num']) throw new AdminException("请填写要生成卡的数量");
  56. if (!is_numeric($data['total_num']) || $data['total_num'] < 0) throw new AdminException("卡片数量只能为正整数");
  57. if ($data['total_num'] > 6000) throw new AdminException("单次制卡数量最高不得超过6000张");
  58. if (!$data['use_day'] || !is_numeric($data['use_day'])) throw new AdminException("请填写免费使用天数");
  59. if ($data['use_day'] < 0) throw new AdminException("免费使用天数只能为正整数");
  60. /**
  61. * 具体时间段试用,业务需要打开即可
  62. */
  63. /* $use_start_time = strtotime($data['use_start_time']);
  64. $use_end_time = strtotime($data['use_end_time']);
  65. if (!$use_start_time) {
  66. $use_start_time = strtotime(date('Y-m-d 00:00:00', strtotime('+1 day')));
  67. }else{
  68. $use_start_time = strtotime($data['use_start_time']);
  69. }
  70. if (!$use_end_time) {
  71. $use_end_time = strtotime(date('Y-m-d 23:59:59', strtotime('+1 day')));
  72. }else{
  73. $use_end_time = strtotime($data['use_end_time']);
  74. }
  75. if ($use_end_time < time()) throw new AdminException("体验结束时间不能小于当天");
  76. if ($use_end_time < $use_start_time) throw new AdminException("体验结束时间不能小于体验开始时间");
  77. $data['use_start_time'] = $use_start_time;
  78. $data['use_end_time'] = $use_end_time;*/
  79. $data['use_day'] = abs(ceil($data['use_day']));
  80. $data['total_num'] = abs(ceil($data['total_num']));
  81. $data['add_time'] = time();
  82. $this->transaction(function () use ($id, $data) {
  83. if ($id) {
  84. unset($data['total_num']);
  85. $data['update_time'] = time();
  86. return $this->dao->update($id, $data);
  87. //return ['status' => 1, "msg" => "编辑批次卡成功"];
  88. } else {
  89. /** @var MemberCardServices $memberCardService */
  90. $memberCardService = app()->make(MemberCardServices::class);
  91. $res = $this->dao->save($data);
  92. $add_card['card_batch_id'] = $res->id;
  93. $add_card['total_num'] = $data['total_num'];
  94. return $memberCardService->addCard($add_card);
  95. // return ['status' => 2, "msg" => "生成批次卡成功"];
  96. }
  97. });
  98. }
  99. /**
  100. * 列表操作
  101. * @param int $id
  102. * @param array $data
  103. */
  104. public function setValue(int $id, array $data)
  105. {
  106. if (!is_numeric($id) || !$id) throw new AdminException("参数缺失");
  107. if (!isset($data['field']) || !isset($data['value']) || !$data['field']) throw new AdminException("参数错误");
  108. $res = true;
  109. if ($data['field'] == 'status') {
  110. /** @var MemberCardServices $memberCardService */
  111. $memberCardService = app()->make(MemberCardServices::class);
  112. $res = $memberCardService->update(['card_batch_id' => $id], ['status' => $data['value']]);
  113. }
  114. if ($res) {
  115. $this->dao->update($id, [$data['field'] => $data['value']]);
  116. } else {
  117. throw new AdminException("操作错误");
  118. }
  119. }
  120. /**
  121. * 获取单条卡批次资源
  122. * @param array $uid
  123. * @param string $field
  124. * @return array|\think\Model|null
  125. * @throws \think\db\exception\DataNotFoundException
  126. * @throws \think\db\exception\DbException
  127. * @throws \think\db\exception\ModelNotFoundException
  128. */
  129. public function getOne(int $bid, $field = '*')
  130. {
  131. if (is_string($field)) $field = explode(',', $field);
  132. return $this->dao->get($bid, $field);
  133. }
  134. /**
  135. * 批次卡数量统计
  136. * @param int $id
  137. * @param string $field
  138. * @param int $inc
  139. * @return bool
  140. * @throws \think\db\exception\DataNotFoundException
  141. * @throws \think\db\exception\DbException
  142. * @throws \think\db\exception\ModelNotFoundException
  143. */
  144. public function useCardSetInc(int $id, string $field, int $inc = 1)
  145. {
  146. return $this->dao->bcInc($id, $field, $inc);
  147. }
  148. }