123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475 |
- <?php
- namespace app\admin\controller\coupon;
- use app\admin\service\coupon\CardService;
- use app\admin\service\member\MemberService;
- use app\controller\Curd;
- use app\model\Card;
- use app\model\CardIssue;
- use app\model\CardMain;
- use app\model\MemberRole;
- use app\model\SysCategory;
- use app\model\SysUser;
- use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
- use Illuminate\Database\Query\Builder as QueryBuilder;
- use support\Db;
- use support\exception\BusinessException;
- use support\Log;
- use support\Model;
- use support\Request;
- use support\Response;
- class CardController extends Curd
- {
- public function __construct()
- {
- $this->model = new Card();
- }
- /**
- * 查询
- * @param Request $request
- * @return Response
- * @throws BusinessException
- */
- public function select(Request $request): Response
- {
- $cardMainName = $request->get('card_main_name');
- $cardIssueName = $request->get('card_issue_name');
- [$where, $format, $limit, $field, $order] = $this->selectInput($request);
- $order = $request->get('order', 'desc');
- $field = $field ?? 'card_addtimes';
- if (!empty($where['card_addtimes'])) {
- $where['card_addtimes'][0] = strtotime($where['card_addtimes'][0]);
- $where['card_addtimes'][1] = strtotime($where['card_addtimes'][1]);
- }
- if (!empty($cardMainName)) {
- $cardMainIds = CardMain::where('card_main_name', 'like', '%' . $cardMainName . '%')->pluck('card_main_id')->toArray();
- $where['join_card_main_id'] = ['in', $cardMainIds];
- }
- if (!empty($cardIssueName)) {
- $cardIssueIds = CardIssue::where('card_issue_name', 'like', '%' . $cardIssueName . '%')->pluck('card_issue_id')->toArray();
- $where['join_card_issue_id'] = ['in', $cardIssueIds];
- }
- $query = $this->doSelect($where, $field, $order);
- return $this->doFormat($query, $format, $limit);
- }
- /**
- * 指定查询where条件,并没有真正的查询数据库操作
- * @param array $where
- * @param string|null $field
- * @param string $order
- * @return EloquentBuilder|QueryBuilder|Model
- */
- protected function doSelect(array $where, string $field = null, string $order = 'desc')
- {
- $model = $this->model->with([
- 'member' => function ($query) {
- $query->select('member_id', 'member_mobile');
- },
- 'cert' => function ($query) {
- $query->select('join_cert_member_id', 'member_cert_name');
- },
- 'dept',
- 'user',
- 'main' => function ($query) {
- $query->select('card_main_id', 'card_main_name', 'card_main_amount', 'card_main_valid_mode');
- },
- 'issue' => function ($query) {
- $query->select('card_issue_id', 'card_issue_name');
- }
- ]);
- foreach ($where as $column => $value) {
- if (is_array($value)) {
- if ($value[0] === 'like' || $value[0] === 'not like') {
- $model = $model->where($column, $value[0], "%$value[1]%");
- } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
- $model = $model->where($column, $value[0], $value[1]);
- } elseif ($value[0] == 'in') {
- $valArr = $value[1];
- if (is_string($value[1])) {
- $valArr = explode(",", trim($value[1]));
- }
- $model = $model->whereIn($column, $valArr);
- } elseif ($value[0] == 'not in' && !empty($value[1])) {
- $valArr = $value[1];
- if (is_string($value[1])) {
- $valArr = explode(",", trim($value[1]));
- }
- $model = $model->whereNotIn($column, $valArr);
- } elseif ($value[0] == 'null') {
- $model = $model->whereNull($column);
- } elseif ($value[0] == 'not null') {
- $model = $model->whereNotNull($column);
- } elseif ($value[0] !== '' || $value[1] !== '') {
- $model = $model->whereBetween($column, $value);
- }
- } else {
- $model = $model->where($column, $value);
- }
- }
- if ($field) {
- $model = $model->orderBy($field, $order);
- }
- return $model;
- }
- public function afterQuery($items)
- {
- foreach ($items as &$item) {
- $mobile = $item['member']['member_mobile'] ?? '';
- $certName = $item['cert']['member_cert_name'] ?? '';
- $item['member_name'] = MemberService::getMemberCertName($mobile, $certName, '');
- if (!empty($item['main']['card_main_valid_mode'])) {
- if ($item['main']['card_main_valid_mode'] == 'LONG') {
- $item['valid_datetime'] = '长期有效';
- } else if (!empty($item['card_deadline_datetime'])) {
- $item['valid_datetime'] = date('Y/m/d', strtotime($item['card_assign_datetime'])) . '-' . date('Y/m/d', strtotime($item['card_deadline_datetime']));
- }
- }
- $cardExtendJson = [];
- if (!empty($item['card_extend_json'])) {
- $cardExtendJson = json_decode($item['card_extend_json'], true);
- }
- if (!empty($cardExtendJson['issue_datetime'])) {
- $item['issue_datetime'] = $cardExtendJson['issue_datetime'];
- }
- }
- return $items;
- }
- public function info(Request $request): Response
- {
- $cardId = $request->get('card_id');
- if (!$cardId) {
- return json_fail("参数异常");
- }
- $info = Card::with([
- 'member' => function ($query) {
- $query->select('member_id', 'member_mobile', 'join_member_role_id', 'member_is_owner', 'member_is_vip', 'member_is_partner', 'member_is_referrer', 'member_is_franchisee');
- },
- 'cert',
- 'info',
- 'dept',
- 'user',
- 'main',
- 'issue'
- ])->where('card_id', $cardId)
- ->first();
- $info = $info ? $info->toArray() : [];
- if (!empty($info['member']) && !empty($info['member']['join_member_role_id'])) {
- $info['role_name'] = MemberRole::where('member_role_id', $info['member']['join_member_role_id'])->value('member_role_name');
- }
- // 头像
- $memberAvatar = !empty($info['info']) && !empty($info['info']['member_info_headimg']) ? $info['info']['member_info_headimg'] : '';
- $info['info']['member_info_headimg'] = MemberService::getAvatarUrl($memberAvatar);
- // 昵称
- $memberNickname = !empty($info['info']) && !empty($info['info']['member_info_nickname']) ? $info['info']['member_info_nickname'] : '';
- $memberMobile = !empty($info['member']) && !empty($info['member']['member_mobile']) ? $info['member']['member_mobile'] : '';
- $info['info']['member_info_nickname'] = MemberService::getNickname($memberNickname, $memberMobile);
- if (!empty($info['main'])) {
- if (!empty($info['main']['join_card_main_category_id'])) {
- $info['main_category_name'] = SysCategory::where('category_id', $info['main']['join_card_main_category_id'])->value('category_name');
- }
- if (!empty($info['main']['card_main_valid_mode']) && $info['main']['card_main_valid_mode'] == 'LONG') {
- $info['valid_term'] = '长期有效';
- } elseif (!empty($info['main']['card_main_valid_mode']) && $info['main']['card_main_valid_mode'] == 'DAYS') {
- $info['valid_term'] = $info['main']['card_main_valid_days'] . '天';
- } elseif (!empty($info['main']['card_main_valid_mode']) && $info['main']['card_main_valid_mode'] == 'DATE') {
- $info['valid_term'] = date('Y/m/d', strtotime($info['main']['card_main_valid_begin'])) . '-' . date('Y/m/d', strtotime($info['main']['card_main_valid_end']));
- }
- if (!empty($info['main']['card_main_json'])) {
- $cardMainJson = json_decode($info['main']['card_main_json'], true);
- $info['is_recharge'] = $cardMainJson['is_recharge'];
- $info['issue_mode'] = $cardMainJson['issue'];
- }
- if (!empty($info['main']['card_main_valid_mode'])) {
- if ($info['main']['card_main_valid_mode'] == 'LONG') {
- $info['valid_datetime'] = '长期有效';
- } else if (!empty($info['card_deadline_datetime'])) {
- $info['valid_datetime'] = date('Y/m/d', strtotime($info['card_assign_datetime'])) . '-' . date('Y/m/d', strtotime($info['card_deadline_datetime']));
- }
- }
- }
- if (!empty($info['issue']) && !empty($info['issue']['card_issue_creator_user_id'])) {
- $info['issue_username'] = SysUser::where('user_id', $info['issue']['card_issue_creator_user_id'])->value('user_name');
- }
- return json_success('', $info);
- }
- /**
- * 更新前置方法
- * @param Request $request
- * @return array
- * @throws BusinessException
- */
- protected function updateInput(Request $request): array
- {
- $primary_key = $this->model->getKeyName();
- $id = $request->post($primary_key);
- $data = $this->inputFilter($request->post());
- $model = $this->model->find($id);
- if (!$model) {
- throw new BusinessException('记录不存在', 2);
- }
- $cardExtendJson = [];
- if (!empty($model->card_extend_json)) {
- $cardExtendJson = json_decode($model->card_extend_json, true);
- }
- if ($data['is_issue'] == 'N') {
- unset($cardExtendJson['issue_datetime']);
- } elseif ($data['is_issue'] == 'Y') {
- $cardExtendJson['issue_datetime'] = date('Y-m-d H:i:s');
- }
- $data['card_extend_json'] = json_encode($cardExtendJson);
- if ($model->join_card_dept_id != $data['join_card_dept_id'] && empty($model->join_card_member_id)) {
- $data['card_assign_datetime'] = date('Y-m-d H:i:s');
- }
- _syslog('储值卡', '编辑', $data, $request->all());
- unset($data[$primary_key]);
- return [$id, $data];
- }
- /**
- * @Desc 获取指定状态的 card_id
- * @Author Gorden
- * @Date 2024/11/15 17:21
- *
- * @param Request $request
- * @return Response
- */
- public function getCardIdsByMainId(Request $request)
- {
- $mainId = $request->get('main_id');
- if (!$mainId) {
- return json_fail('参数异常');
- }
- $status = $request->get('status');
- $isIssue = $request->get('is_issue');
- $allocation = $request->get('allocation');
- $cardIds = Card::where('join_card_main_id', $mainId)
- ->when(!empty($status), function ($query) use ($status) {
- $query->where('card_status', $status);
- })->when(!empty($isIssue), function ($query) use ($isIssue) {
- $query->where('is_issue', $isIssue);
- })->when(!empty($allocation), function ($query) use ($allocation) {
- if ($allocation == 'N') { // 未分配
- $query->where('join_card_dept_id', '');
- } elseif ($allocation == 'Y') { // 已分配
- $query->where('join_card_dept_id', '<>', '');
- }
- })->pluck('card_id')
- ->toArray();
- return json_success('success', $cardIds);
- }
- /**
- * @Desc 储值卡发行
- * @Author Gorden
- * @Date 2024/11/18 15:13
- *
- * @param Request $request
- * @return Response
- */
- public function issue(Request $request)
- {
- $params = $request->post();
- Db::beginTransaction();
- try {
- $issueMode = $params['issue'];
- // 1.按卡号
- if ($issueMode == 'CARDID' && !empty($params['card_id'])) {
- foreach ($params['card_id'] as $card_id) {
- $card = Card::where('card_id', $card_id)->select('card_id', 'is_issue', 'card_extend_json')->first();
- if (!empty($card)) {
- $cardExtendJson = json_decode($card->card_extend_json, true);
- $cardExtendJson['issue_datetime'] = date('Y-m-d H:i:s');
- $card->card_extend_json = json_encode($cardExtendJson);
- $card->is_issue = 'Y';
- $card->save();
- } else {
- throw new BusinessException($card_id . "不存在");
- }
- }
- } elseif ($issueMode == 'ISSUE' && !empty($params['card_issue_id'])) {
- // 2.按批次
- $cards = Card::where('join_card_issue_id', $params['card_issue_id'])
- ->where('is_issue', 'N')
- ->select('card_id', 'card_extend_json')
- ->get()
- ->toArray();
- if (!empty($cards)) {
- foreach ($cards as $cardItem) {
- $cardExtendJson = json_decode($cardItem['card_extend_json'], true);
- $cardExtendJson['issue_datetime'] = date('Y-m-d H:i:s');
- Card::where('card_id', $cardItem['card_id'])->update([
- 'is_issue' => 'Y',
- 'card_extend_json' => json_encode($cardExtendJson)
- ]);
- }
- } else {
- throw new BusinessException("批次不存在");
- }
- }
- Db::commit();
- _syslog("储值卡", '发行成功');
- return json_success('success');
- } catch (BusinessException $e) {
- Db::rollBack();
- _syslog("储值卡", "发行失败:" . $e->getMessage());
- Log::error('储值卡发行失败', ['msg' => $e->getMessage()]);
- return json_fail("发行失败:" . $e->getMessage());
- } catch (\Exception $e) {
- Db::rollBack();
- _syslog("储值卡", '发行失败');
- Log::error('储值卡发行失败', ['msg' => $e->getMessage()]);
- return json_fail("发行失败");
- }
- }
- /**
- * @Desc 储值卡分配
- * @Author Gorden
- * @Date 2024/11/18 16:02
- *
- * @param Request $request
- * @return Response
- */
- public function allocation(Request $request)
- {
- $params = $request->post();
- try {
- $allocation = $params['allocation'];
- // 1.按卡号分配
- if ($allocation == 'CARDID' && !empty($params['card_id'])) {
- Card::whereIn('card_id', $params['card_id'])->where('join_card_dept_id', 0)->update([
- 'join_card_dept_id' => $params['join_card_dept_id'],
- 'card_assign_datetime' => date('Y-m-d H:i:s')
- ]);
- } elseif ($allocation == 'ISSUE' && !empty($params['card_issue_id'])) {
- Card::where('join_card_issue_id', $params['card_issue_id'])->where('join_card_dept_id', 0)->update([
- 'join_card_dept_id' => $params['join_card_dept_id'],
- 'card_assign_datetime' => date('Y-m-d H:i:s')
- ]);
- }
- _syslog("储值卡", '分配成功');
- return json_success('success');
- } catch (\Exception $e) {
- _syslog("储值卡", '分配失败');
- Log::error('储值卡分配失败', ['msg' => $e->getMessage()]);
- return json_fail('分配失败');
- }
- }
- /**
- * @Desc 导出储值卡
- * @Author Gorden
- * @Date 2024/11/21 13:03
- *
- * @param Request $request
- * @return Response
- */
- public function exportCard(Request $request)
- {
- $cardStatus = $request->get('card_status');
- $isIssue = $request->get('is_issue');
- $cardMainName = $request->get('card_main_name');
- $cardIssueName = $request->get('card_issue_name');
- $deptId = $request->get('join_card_dept_id');
- $cardAddtimes = $request->get('card_addtimes');
- $cardIds = $request->get('card_ids');
- $cards = Card::with([
- 'member' => function ($query) {
- $query->select('member_id', 'member_mobile');
- },
- 'cert' => function ($query) {
- $query->select('join_cert_member_id', 'member_cert_name');
- },
- 'dept',
- 'user',
- 'main' => function ($query) {
- $query->select('card_main_id', 'card_main_name', 'card_main_amount', 'card_main_valid_mode');
- },
- 'issue' => function ($query) {
- $query->select('card_issue_id', 'card_issue_name');
- }
- ])->when(!empty($cardStatus), function ($query) use ($cardStatus) {
- $query->where('card_status', $cardStatus);
- })->when(!empty($isIssue), function ($query) use ($isIssue) {
- $query->where('is_issue', $isIssue);
- })->when(!empty($deptId), function ($query) use ($deptId) {
- $query->where('join_card_dept_id', $deptId);
- })->when(!empty($cardAddtimes), function ($query) use ($cardAddtimes) {
- $cardAddtimes[0] = strtotime($cardAddtimes[0]);
- $cardAddtimes[1] = strtotime($cardAddtimes[1]);
- $query->whereBetween('card_addtimes', $cardAddtimes);
- })->when(!empty($cardIds), function ($query) use ($cardIds) {
- $query->whereIn('card_id', $cardIds);
- })->when(!empty($cardMainName), function ($query) use ($cardMainName) {
- $cardMainIds = CardMain::where('card_main_name', 'like', '%' . $cardMainName . '%')->pluck('card_main_id')->toArray();
- $query->whereIn('join_card_main_id', $cardMainIds);
- })->when(!empty($cardIssueName), function ($query) use ($cardIssueName) {
- $cardIssueIds = CardIssue::where('card_issue_name', 'like', '%' . $cardIssueName . '%')->pluck('card_issue_id')->toArray();
- $query->whereIn('join_card_issue_id', $cardIssueIds);
- })->orderByDesc('card_addtimes')
- ->get()
- ->toArray();
- $data = [];
- foreach ($cards as $item) {
- $mobile = $item['member']['member_mobile'] ?? '';
- $certName = $item['cert']['member_cert_name'] ?? '';
- $item['member_name'] = MemberService::getMemberCertName($mobile, $certName, '');
- if (!empty($item['main']['card_main_valid_mode'])) {
- if ($item['main']['card_main_valid_mode'] == 'LONG') {
- $item['valid_datetime'] = '长期有效';
- } else if (!empty($item['card_deadline_datetime'])) {
- $item['valid_datetime'] = date('Y/m/d H:i:s', strtotime($item['card_assign_datetime'])) . '-' . date('Y/m/d H:i:s', strtotime($item['card_deadline_datetime']));
- }
- }
- $cardExtendJson = [];
- if (!empty($item['card_extend_json'])) {
- $cardExtendJson = json_decode($item['card_extend_json'], true);
- }
- if (!empty($cardExtendJson['issue_datetime'])) {
- $item['issue_datetime'] = $cardExtendJson['issue_datetime'];
- }
- $data[] = [
- 'card_id' => $item['card_id'],
- 'main_name' => !empty($item['main']['card_main_name']) ? $item['main']['card_main_name'] : '--',
- 'issue_name' => !empty($item['issue']['card_issue_name']) ? $item['issue']['card_issue_name'] : '--',
- 'main_amount' => !empty($item['main']['card_main_amount']) ? $item['main']['card_main_amount'] : '--',
- 'dept_name' => !empty($item['dept']['dept_name']) ? $item['dept']['dept_name'] : '--',
- 'member_name' => $item['member_name'] ?? '--',
- 'valid_datetime' => $item['valid_datetime'] ?? '--',
- 'status' => CardService::$status[$item['card_status']],
- 'is_issue' => $item['is_issue'] == 'Y' ? '已发行' : '未发行',
- 'issue_datetime' => $item['issue_datetime'] ?? '--',
- 'card_addtimes' => $item['card_addtimes']
- ];
- }
- return json_success("success", $data);
- }
- }
|