123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- <?php
- namespace app\admin\logic;
- use app\model\CardModel;
- use app\model\CommonModel;
- use think\exception\ValidateException;
- use think\response\Json;
- class CardLogic extends BaseLogic
- {
- //获取卡类型列表
- public static function list(array $data = []): Json
- {
- $db = CardModel::where('is_del', CommonModel::$del_normal);
- if ($data['title'] != '') $db->whereLike('title', '%' . $data['title'] . '%');
- if ($data['status'] != '') $db->where('status', $data['status']);
- $count = $db->count('id');
- $list = $db->field('id,title,status,creater,addtime')
- ->page($data['page'], $data['size'])
- ->order(['id' => 'desc'])
- ->select()
- ->toArray();
- return json_show(CommonModel::$success, '获取卡类型列表成功', ['count' => $count, 'list' => $list]);
- }
- //获取全部卡类型
- public static function all(string $keyword = ''): Json
- {
- $db = CardModel::where('is_del', CommonModel::$del_normal);
- if ($keyword != '') $db->whereLike('title', '%' . $keyword . '%');
- $list = $db->field('id,title,status')
- ->order(['id' => 'desc'])
- ->select()
- ->toArray();
- return json_show(CommonModel::$success, '获取卡类型列表成功', $list);
- }
- //添加卡类型
- public static function add(array $data = []): Json
- {
- $rs = CardModel::field('id')
- ->where(['is_del' => CommonModel::$del_normal, 'title' => $data['title']])
- ->findOrEmpty()
- ->isEmpty();
- if (!$rs) throw new ValidateException('卡类型已存在,请勿重复添加');
- $data = array_merge($data, [
- 'status' => CommonModel::$status_normal,
- 'is_del' => CommonModel::$del_normal,
- 'createrid' => self::$uid,
- 'creater' => self::$uname,
- 'updaterid' => self::$uid,
- 'updater' => self::$uname,
- ]);
- $res = CardModel::create($data)->save();
- return $res ? json_show(CommonModel::$success, '添加卡类型成功') : json_show(CommonModel::$error_param, '添加卡类型失败');
- }
- //获取卡类型详情
- public static function read(int $id = 0): Json
- {
- $rs = CardModel::field(true)
- ->where(['id' => $id, 'is_del' => CommonModel::$del_normal])
- ->findOrEmpty()
- ->toArray();
- return json_show(CommonModel::$success, '获取卡类型详情成功', $rs);
- }
- //编辑卡类型
- public static function edit(array $data = []): Json
- {
- $rs = CardModel::field('id,title')
- ->where(['id' => $data['id'], 'is_del' => CommonModel::$del_normal])
- ->findOrEmpty();
- if ($rs->isEmpty()) throw new ValidateException('该卡类型不存在');
- if ($rs->title != $data['title']) {
- $temp = CardModel::field('id')
- ->where(['is_del' => CommonModel::$del_normal, 'title' => $data['title']])
- ->findOrEmpty()
- ->isEmpty();
- if (!$temp) throw new ValidateException('该卡类型已存在');
- }
- $data = array_merge($data, [
- 'updaterid' => self::$uid,
- 'updater' => self::$uname,
- 'updatetime' => date('Y-m-d H:i:s'),
- ]);
- $res = CardModel::where(['id' => $data['id'], 'is_del' => CommonModel::$del_normal])
- ->save($data);
- return $res ? json_show(CommonModel::$success, '编辑卡类型成功') : json_show(CommonModel::$error_param, '编辑卡类型失败');
- }
- //卡类型启禁用
- public static function change(array $data = []): Json
- {
- $where = [
- ['id', '=', $data['id']],
- ['is_del', '=', CommonModel::$del_normal],
- ['status', '<>', $data['status']],
- ];
- $rs = CardModel::field('id,status')
- ->where($where)
- ->findOrEmpty()
- ->isEmpty();
- if ($rs) throw new ValidateException('该卡类型不存在或重复操作');
- $res = CardModel::where($where)->save($data);
- return $res ? json_show(CommonModel::$success, '操作成功') : json_show(CommonModel::$error_param, '操作失败');
- }
- //删除卡类型
- public static function delete(int $id = 0): Json
- {
- $rs = CardModel::where(['id' => $id, 'is_del' => CommonModel::$del_normal])
- ->save(['is_del' => CommonModel::$del_deleted, 'updatetime' => date('Y-m-d H:i:s')]);
- return $rs ? json_show(CommonModel::$success, '卡类型删除成功') : json_show(CommonModel::$error_param, '卡类型不存在或重复删除');
- }
- }
|