MemberCardBatchServices.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 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\MemberCardBatchDao;
  13. use app\services\BaseServices;
  14. use crmeb\exceptions\AdminException;
  15. use think\App;
  16. class MemberCardBatchServices extends BaseServices
  17. {
  18. /**
  19. * 初始化,获得dao层句柄
  20. * MemberCardServices constructor.
  21. * @param MemberCardBatchDao $memberCardDao
  22. */
  23. public function __construct(MemberCardBatchDao $memberCardBatchDao)
  24. {
  25. $this->dao = $memberCardBatchDao;
  26. }
  27. /**
  28. * 获取会员卡批次列表
  29. * @param array $where
  30. * @return array
  31. * @throws \think\db\exception\DataNotFoundException
  32. * @throws \think\db\exception\DbException
  33. * @throws \think\db\exception\ModelNotFoundException
  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(400614);
  55. if (!$data['total_num']) throw new AdminException(400615);
  56. if (!is_numeric($data['total_num']) || $data['total_num'] < 0) throw new AdminException(400616);
  57. if ($data['total_num'] > 6000) throw new AdminException(400617);
  58. if (!$data['use_day'] || !is_numeric($data['use_day'])) throw new AdminException(400618);
  59. if ($data['use_day'] < 0) throw new AdminException(400619);
  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(100100);
  107. if (!isset($data['field']) || !isset($data['value']) || !$data['field']) throw new AdminException(100100);
  108. $this->dao->update($id, [$data['field'] => $data['value']]);
  109. app()->make(MemberCardServices::class)->update(['card_batch_id' => $id], ['status' => $data['value']]);
  110. }
  111. /**
  112. * 获取单条卡批次资源
  113. * @param array $uid
  114. * @param string $field
  115. * @return array|\think\Model|null
  116. * @throws \think\db\exception\DataNotFoundException
  117. * @throws \think\db\exception\DbException
  118. * @throws \think\db\exception\ModelNotFoundException
  119. */
  120. public function getOne(int $bid, $field = '*')
  121. {
  122. if (is_string($field)) $field = explode(',', $field);
  123. return $this->dao->get($bid, $field);
  124. }
  125. /**
  126. * 批次卡数量统计
  127. * @param int $id
  128. * @param string $field
  129. * @param int $inc
  130. * @return bool
  131. * @throws \think\db\exception\DataNotFoundException
  132. * @throws \think\db\exception\DbException
  133. * @throws \think\db\exception\ModelNotFoundException
  134. */
  135. public function useCardSetInc(int $id, string $field, int $inc = 1)
  136. {
  137. return $this->dao->bcInc($id, $field, $inc);
  138. }
  139. }