123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- <?php
- namespace app\services\user\member;
- use app\dao\user\member\MemberCardBatchDao;
- use app\services\BaseServices;
- use crmeb\exceptions\AdminException;
- class MemberCardBatchServices extends BaseServices
- {
-
- public function __construct(MemberCardBatchDao $memberCardBatchDao)
- {
- $this->dao = $memberCardBatchDao;
- }
-
- public function getList(array $where = [])
- {
- [$page, $limit] = $this->getPageValue();
- $list = $this->dao->getList($where, $page, $limit);
- if ($list) {
- foreach ($list as &$v) {
- $v['add_time'] = date('Y-m-d H:i:s', $v['add_time']);
-
- }
- }
- $count = $this->dao->count($where);
- return compact('list', 'count');
- }
-
- public function save(int $id, array $data)
- {
- if (!$data['title']) throw new AdminException("请填写批次名称");
- if (!$data['total_num']) throw new AdminException("请填写要生成卡的数量");
- if (!is_numeric($data['total_num']) || $data['total_num'] < 0) throw new AdminException("卡片数量只能为正整数");
- if ($data['total_num'] > 6000) throw new AdminException("单次制卡数量最高不得超过6000张");
- if (!$data['use_day'] || !is_numeric($data['use_day'])) throw new AdminException("请填写免费使用天数");
- if ($data['use_day'] < 0) throw new AdminException("免费使用天数只能为正整数");
-
-
- $data['use_day'] = abs(ceil($data['use_day']));
- $data['total_num'] = abs(ceil($data['total_num']));
- $data['add_time'] = time();
- $this->transaction(function () use ($id, $data) {
- if ($id) {
- unset($data['total_num']);
- $data['update_time'] = time();
- return $this->dao->update($id, $data);
-
- } else {
-
- $memberCardService = app()->make(MemberCardServices::class);
- $res = $this->dao->save($data);
- $add_card['card_batch_id'] = $res->id;
- $add_card['total_num'] = $data['total_num'];
- return $memberCardService->addCard($add_card);
-
- }
- });
- }
-
- public function setValue(int $id, array $data)
- {
- if (!is_numeric($id) || !$id) throw new AdminException("参数缺失");
- if (!isset($data['field']) || !isset($data['value']) || !$data['field']) throw new AdminException("参数错误");
- $res = true;
- if ($data['field'] == 'status') {
-
- $memberCardService = app()->make(MemberCardServices::class);
- $res = $memberCardService->update(['card_batch_id' => $id], ['status' => $data['value']]);
- }
- if ($res) {
- $this->dao->update($id, [$data['field'] => $data['value']]);
- } else {
- throw new AdminException("操作错误");
- }
- }
-
- public function getOne(int $bid, $field = '*')
- {
- if (is_string($field)) $field = explode(',', $field);
- return $this->dao->get($bid, $field);
- }
-
- public function useCardSetInc(int $id, string $field, int $inc = 1)
- {
- return $this->dao->bcInc($id, $field, $inc);
- }
- }
|