123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418 |
- <?php
- namespace app\admin\controller\coupon;
- use app\controller\Curd;
- use app\model\Card;
- use app\model\CardIssue;
- use app\model\CardMain;
- 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;
- use Tinywan\Jwt\JwtToken;
- class CardMainController extends Curd
- {
- public function __construct()
- {
- $this->model = new CardMain();
- }
- public function select(Request $request): Response
- {
- [$where, $format, $limit, $field, $order] = $this->selectInput($request);
- $order = $request->get('order', 'desc');
- $field = $field ?? 'card_main_addtimes';
- if (!empty($where['card_main_name'])) {
- $where['card_main_name'] = ['like', $where['card_main_name']];
- }
- if (!empty($where['card_main_addtimes'])) {
- $where['card_main_addtimes'][0] = strtotime($where['card_main_addtimes'][0]);
- $where['card_main_addtimes'][1] = strtotime($where['card_main_addtimes'][1]);
- }
- $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('category');
- 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' && !empty($value[1])) {
- $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;
- }
- /**
- * @Desc 查询后处理
- * @Author Gorden
- * @Date 2024/11/15 8:35
- *
- * @param $items
- * @return mixed
- */
- public function afterQuery($items)
- {
- foreach ($items as &$item) {
- if ($item['card_main_valid_mode'] == 'DAYS') {
- $item['valid_term'] = $item['card_main_valid_days'] ? $item['card_main_valid_days'] . '天' : 0;
- } elseif ($item['card_main_valid_mode'] == 'LONG') {
- $item['valid_term'] = '长期有效';
- } elseif ($item['card_main_valid_mode'] == 'DATE') {
- $item['valid_term'] = date('Y/m/d', strtotime($item['card_main_valid_begin'])) . '-' . date('Y/m/d', strtotime($item['card_main_valid_end']));
- }
- }
- return $items;
- }
- /**
- * @Desc 卡的下拉搜索
- * @Author Gorden
- * @Date 2024/11/18 9:51
- *
- * @return Response
- */
- public function selectList()
- {
- $cardMain = CardMain::where('card_main_status', 'ACTIVED')
- ->orderBy('card_main_addtimes', 'DESC')
- ->select('card_main_id', 'card_main_name')
- ->get()
- ->toArray();
- return json_success('success', $cardMain);
- }
- /**
- * @Desc 储值卡详情
- * @Author Gorden
- * @Date 2024/11/15 11:33
- *
- * @param Request $request
- * @return Response
- */
- public function info(Request $request): Response
- {
- $cardMainId = $request->get('card_main_id');
- if (!$cardMainId) {
- return json_fail('参数异常');
- }
- // 发行记录
- $cardIssue = CardIssue::where('join_issue_card_main_id', $cardMainId)->orderBy('card_issue_addtimes', 'DESC')->get()->toArray();
- // 发行统计
- $cardTotal = Card::where('join_card_main_id', $cardMainId)->count();
- $isIssueTotal = Card::where('join_card_main_id', $cardMainId)->where('is_issue', 'Y')->count();
- $cardInitTotal = Card::where('join_card_main_id', $cardMainId)->where('card_status', 'INIT')->count();
- $cardWaitingTotal = Card::where('join_card_main_id', $cardMainId)->where('card_status', 'WAITING')->count();
- $cardPendingTotal = Card::where('join_card_main_id', $cardMainId)->where('card_status', 'PENDING')->count();
- $cardUsedTotal = Card::where('join_card_main_id', $cardMainId)->where('card_status', 'USED')->count();
- $cardDoneTotal = Card::where('join_card_main_id', $cardMainId)->where('card_status', 'DONE')->count();
- $cardExpiredTotal = Card::where('join_card_main_id', $cardMainId)->where('card_status', 'EXPIRED')->count();
- $cardPausedTotal = Card::where('join_card_main_id', $cardMainId)->where('card_status', 'PAUSED')->count();
- $statistics = [
- [
- 'total' => $cardTotal,
- 'isIssueTotal' => $isIssueTotal,
- 'initTotal' => $cardInitTotal,
- 'waitingTotal' => $cardWaitingTotal,
- 'pendingTotal' => $cardPendingTotal,
- 'usedTotal' => $cardUsedTotal,
- 'doneTotal' => $cardDoneTotal,
- 'expiredTotal' => $cardExpiredTotal,
- 'pausedTotal' => $cardPausedTotal
- ]
- ];
- $data = [
- 'issue' => $cardIssue,
- 'statistics' => $statistics
- ];
- return json_success('', $data);
- }
- /**
- * @Desc 插入前置方法
- * @Author Gorden
- * @Date 2024/11/14 14:29
- *
- * @param Request $request
- * @return array
- * @throws BusinessException
- */
- public function insertInput(Request $request): array
- {
- $params = $request->post();
- $data = $this->inputFilter($request->post());
- if ($data['card_main_valid_mode'] == 'DATE') {
- $data['card_main_valid_begin'] = date('Y-m-d 00:00:00', strtotime($data['card_main_valid_begin']));
- $data['card_main_valid_end'] = date('Y-m-d 23:59:59', strtotime($data['card_main_valid_end']));
- $data['card_main_valid_days'] = 0;
- } elseif ($data['card_main_valid_mode'] == 'LONG' || $data['card_main_valid_mode'] == 'DAYS') {
- $data['card_main_valid_begin'] = '';
- $data['card_main_valid_end'] = '';
- }
- $cardMainJson = [
- 'desc' => $params['card_main_desc'] ?? [],
- 'is_recharge' => $params['is_recharge'] ?? 'N',
- 'issue' => $params['issue'] ?? 'BATCH',
- 'demand_config' => []
- ];
- if ($params['issue'] == 'DEMAND') {
- $cardMainJson['demand_config'] = [
- 'length' => $params['card_length'],
- 'begin' => $params['card_begin'],
- 'end' => $params['card_end'],
- 'prefix' => $params['card_prefix'],
- 'suffix' => $params['card_suffix'],
- 'rand' => $params['card_rand'],
- 'rand_varchar' => is_numeric($params['card_rand']) ? 'NUMBER' : 'STRING',
- ];
- }
- $data['card_main_json'] = json_encode($cardMainJson);
- return $data;
- }
- /**
- * 更新前置方法
- * @param Request $request
- * @return array
- * @throws BusinessException
- */
- protected function updateInput(Request $request): array
- {
- $params = $request->post();
- $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);
- }
- if ($data['card_main_valid_mode'] == 'DATE') {
- $data['card_main_valid_begin'] = date('Y-m-d 00:00:00', strtotime($data['card_main_valid_begin']));
- $data['card_main_valid_end'] = date('Y-m-d 23:59:59', strtotime($data['card_main_valid_end']));
- $data['card_main_valid_days'] = 0;
- } elseif ($data['card_main_valid_mode'] == 'LONG' || $data['card_main_valid_mode'] == 'DAYS') {
- $data['card_main_valid_begin'] = '';
- $data['card_main_valid_end'] = '';
- }
- $cardMainJson = [];
- if (!empty($model->card_main_json)) {
- $cardMainJson = json_decode($model->card_main_json, true);
- }
- $cardMainJson['desc'] = $params['card_main_desc'] ?? [];
- if (!empty($params['is_recharge'])) {
- $cardMainJson['is_recharge'] = $params['is_recharge'];
- }
- if (!empty($params['issue'])) {
- $cardMainJson['issue'] = $params['issue'];
- }
- if ($cardMainJson['issue'] == 'BATCH') {
- $cardMainJson['demand_config'] = [];
- } elseif ($cardMainJson['issue'] == 'DEMAND') {
- $cardMainJson['demand_config'] = [
- 'length' => $params['card_length'],
- 'begin' => $params['card_begin'],
- 'end' => $params['card_end'],
- 'prefix' => $params['card_prefix'],
- 'suffix' => $params['card_suffix'],
- 'rand' => $params['card_rand'],
- 'rand_varchar' => is_numeric($params['card_rand']) ? 'NUMBER' : 'STRING',
- ];
- }
- $data['card_main_json'] = json_encode($cardMainJson);
- // 卡状态设置为禁用,则未绑定的卡冻结,恢复未发行状态
- if ($data['card_main_status'] == 'DISABLED' && $model->card_main_status != 'DISABLED') {
- Card::where('join_card_main_id', $id)->where('join_card_member_id', '')->update(['card_status' => 'PAUSED', 'is_issue' => 'N']);
- // 记录日志
- _syslog("储值卡", "禁用");
- } elseif ($data['card_main_status'] == 'ACTIVED' && $model->card_main_status == 'DISABLED') {
- Card::where('join_card_main_id', $id)->where('join_card_member_id', '')
- ->where('card_status', 'PAUSED')
- ->update(['card_status' => 'INIT']);
- // 记录日志
- _syslog("储值卡", "启用");
- }
- unset($data[$primary_key]);
- return [$id, $data];
- }
- /**
- * @Desc 删除储值卡
- * @Author Gorden
- * @Date 2024/11/18 16:33
- *
- * @param Request $request
- * @return Response
- * @throws BusinessException
- */
- public function delete(Request $request): Response
- {
- $ids = $this->deleteInput($request);
- Db::beginTransaction();
- try {
- if (Card::whereIn('join_card_main_id', $ids)->where(function ($query) {
- $query->where('card_status', '<>', 'INIT')
- ->orWhere('join_card_dept_id', '<>', '')
- ->orWhere('join_card_member_id', '<>', '');
- })->exists()) {
- return json_fail("存在已发行、已分配的卡,无法删除!");
- }
- $this->doDelete($ids);
- // 同时删除批次记录
- CardIssue::whereIn('join_issue_card_main_id', $ids)->delete();
- // 同时删除卡记录
- Card::whereIn('join_card_main_id', $ids)->delete();
- Db::commit();
- _syslog("储值卡", '删除成功');
- return json_success('success');
- } catch (\Exception $e) {
- Db::rollBack();
- _syslog("储值次", '删除失败');
- Log::error("储值卡删除失败", ['msg' => $e->getMessage()]);
- return json_fail("删除失败");
- }
- }
- /**
- * @Desc 储值卡统计
- * @Author Gorden
- * @Date 2024/11/22 13:06
- *
- * @param Request $request
- * @return Response
- */
- public function statistics(Request $request)
- {
- $classify = $request->get('card_main_classify');
- $categoryId = $request->get('join_card_main_category_id');
- $name = $request->get('card_main_name');
- $addtimes = $request->get('card_main_addtimes');
- $cardMain = CardMain::when(!empty($classify), function ($query) use ($classify) {
- $query->where('card_main_classify', $classify);
- })->when(!empty($categoryId), function ($query) use ($categoryId) {
- $query->where('join_card_main_category_id', $categoryId);
- })->when(!empty($name), function ($query) use ($name) {
- $query->where('card_main_name', 'like', '%' . $name . '%');
- })->when(!empty($addtimes), function ($query) use ($addtimes) {
- $addtimes[0] = strtotime($addtimes[0]);
- $addtimes[1] = strtotime($addtimes[1]);
- $query->whereBetween('card_main_addtimes', $addtimes);
- });
- $statistics = [];
- $totalModel = clone $cardMain;
- $totalList = $totalModel->select('card_main_id', 'card_main_amount')->get()->toArray();
- $statistics = [
- 'total' => ['total' => 0, 'amount' => 0],
- 'is_issue' => ['total' => 0, 'amount' => 0],
- 'init' => ['total' => 0, 'amount' => 0],
- 'waiting' => ['total' => 0, 'amount' => 0],
- 'pending' => ['total' => 0, 'amount' => 0],
- 'used' => ['total' => 0, 'amount' => 0],
- 'done' => ['total' => 0, 'amount' => 0],
- 'paused' => ['total' => 0, 'amount' => 0]
- ];
- foreach ($totalList as $total) {
- // 发行统计
- $isIssueTotal = Card::where('is_issue', 'Y')->where('join_card_main_id', $total['card_main_id'])->count();
- $statistics['is_issue']['total'] += $isIssueTotal;
- $statistics['is_issue']['amount'] = sprintf('%.2f', $isIssueTotal * $total['card_main_amount'] + $statistics['is_issue']['amount']);
- // 待分配统计
- $initTotal = Card::where('card_status', 'INIT')->where('join_card_main_id', $total['card_main_id'])->count();
- $statistics['init']['total'] += $initTotal;
- $statistics['init']['amount'] = sprintf('%.2f', $initTotal * $total['card_main_amount'] + $statistics['init']['amount']);
- // 已分配,待售统计
- $waitingTotal = Card::where('card_status', 'WAITING')->where('join_card_main_id', $total['card_main_id'])->count();
- $statistics['waiting']['total'] += $waitingTotal;
- $statistics['waiting']['amount'] = sprintf('%.2f', $waitingTotal * $total['card_main_amount'] + $statistics['waiting']['amount']);
- // 已售,待激活统计
- $pendingTotal = Card::where('card_status', 'PENDING')->where('join_card_main_id', $total['card_main_id'])->count();
- $statistics['pending']['total'] += $pendingTotal;
- $statistics['pending']['amount'] = sprintf('%.2f', $pendingTotal * $total['card_main_amount'] + $statistics['pending']['amount']);
- // 已激活统计
- $usedTotal = Card::where('card_status', 'USED')->where('join_card_main_id', $total['card_main_id'])->count();
- $statistics['used']['total'] += $usedTotal;
- $statistics['used']['amount'] = sprintf('%.2f', $usedTotal * $total['card_main_amount'] + $statistics['used']['amount']);
- // 使用完成统计
- $doneTotal = Card::where('card_status', 'DONE')->where('join_card_main_id', $total['card_main_id'])->count();
- $statistics['done']['total'] += $doneTotal;
- $statistics['done']['amount'] = sprintf('%.2f', $doneTotal * $total['card_main_amount'] + $statistics['done']['amount']);
- // 冻结统计
- $pausedTotal = Card::where('card_status', 'PAUSED')->where('join_card_main_id', $total['card_main_id'])->count();
- $statistics['paused']['total'] += $pausedTotal;
- $statistics['paused']['amount'] = sprintf('%.2f', $pausedTotal * $total['card_main_amount'] + $statistics['paused']['total']);
- }
- $statistics['total']['total'] = $statistics['init']['total'] + $statistics['waiting']['total']
- + $statistics['pending']['total'] + $statistics['used']['total'] + $statistics['done']['total']
- + $statistics['paused']['total'];
- $statistics['total']['amount'] = sprintf('%.2f', $statistics['init']['amount']
- + $statistics['waiting']['amount'] + $statistics['pending']['amount'] + $statistics['used']['amount']
- + $statistics['done']['amount'] + $statistics['paused']['amount']);
- return json_success('success', $statistics);
- }
- }
|