You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MemberCardBatchServices.php 5.1KB

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