CardLogic.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. namespace app\admin\logic;
  3. use app\model\CardModel;
  4. use app\model\CommonModel;
  5. use think\exception\ValidateException;
  6. use think\response\Json;
  7. class CardLogic extends BaseLogic
  8. {
  9. //获取卡类型列表
  10. public static function list(array $data = []): Json
  11. {
  12. $db = CardModel::where('is_del', CommonModel::$del_normal);
  13. if ($data['title'] != '') $db->whereLike('title', '%' . $data['title'] . '%');
  14. if ($data['status'] != '') $db->where('status', $data['status']);
  15. $count = $db->count('id');
  16. $list = $db->field('id,title,status,creater,addtime')
  17. ->page($data['page'], $data['size'])
  18. ->order(['id' => 'desc'])
  19. ->select()
  20. ->toArray();
  21. return json_show(CommonModel::$success, '获取卡类型列表成功', ['count' => $count, 'list' => $list]);
  22. }
  23. //获取全部卡类型
  24. public static function all(string $keyword = ''): Json
  25. {
  26. $db = CardModel::where('is_del', CommonModel::$del_normal);
  27. if ($keyword != '') $db->whereLike('title', '%' . $keyword . '%');
  28. $list = $db->field('id,title,status')
  29. ->order(['id' => 'desc'])
  30. ->select()
  31. ->toArray();
  32. return json_show(CommonModel::$success, '获取卡类型列表成功', $list);
  33. }
  34. //添加卡类型
  35. public static function add(array $data = []): Json
  36. {
  37. $rs = CardModel::field('id')
  38. ->where(['is_del' => CommonModel::$del_normal, 'title' => $data['title']])
  39. ->findOrEmpty()
  40. ->isEmpty();
  41. if (!$rs) throw new ValidateException('卡类型已存在,请勿重复添加');
  42. $data = array_merge($data, [
  43. 'status' => CommonModel::$status_normal,
  44. 'is_del' => CommonModel::$del_normal,
  45. 'createrid' => self::$uid,
  46. 'creater' => self::$uname,
  47. 'updaterid' => self::$uid,
  48. 'updater' => self::$uname,
  49. ]);
  50. $res = CardModel::create($data)->save();
  51. return $res ? json_show(CommonModel::$success, '添加卡类型成功') : json_show(CommonModel::$error_param, '添加卡类型失败');
  52. }
  53. //获取卡类型详情
  54. public static function read(int $id = 0): Json
  55. {
  56. $rs = CardModel::field(true)
  57. ->where(['id' => $id, 'is_del' => CommonModel::$del_normal])
  58. ->findOrEmpty()
  59. ->toArray();
  60. return json_show(CommonModel::$success, '获取卡类型详情成功', $rs);
  61. }
  62. //编辑卡类型
  63. public static function edit(array $data = []): Json
  64. {
  65. $rs = CardModel::field('id,title')
  66. ->where(['id' => $data['id'], 'is_del' => CommonModel::$del_normal])
  67. ->findOrEmpty();
  68. if ($rs->isEmpty()) throw new ValidateException('该卡类型不存在');
  69. if ($rs->title != $data['title']) {
  70. $temp = CardModel::field('id')
  71. ->where(['is_del' => CommonModel::$del_normal, 'title' => $data['title']])
  72. ->findOrEmpty()
  73. ->isEmpty();
  74. if (!$temp) throw new ValidateException('该卡类型已存在');
  75. }
  76. $data = array_merge($data, [
  77. 'updaterid' => self::$uid,
  78. 'updater' => self::$uname,
  79. 'updatetime' => date('Y-m-d H:i:s'),
  80. ]);
  81. $res = CardModel::where(['id' => $data['id'], 'is_del' => CommonModel::$del_normal])
  82. ->save($data);
  83. return $res ? json_show(CommonModel::$success, '编辑卡类型成功') : json_show(CommonModel::$error_param, '编辑卡类型失败');
  84. }
  85. //卡类型启禁用
  86. public static function change(array $data = []): Json
  87. {
  88. $where = [
  89. ['id', '=', $data['id']],
  90. ['is_del', '=', CommonModel::$del_normal],
  91. ['status', '<>', $data['status']],
  92. ];
  93. $rs = CardModel::field('id,status')
  94. ->where($where)
  95. ->findOrEmpty()
  96. ->isEmpty();
  97. if ($rs) throw new ValidateException('该卡类型不存在或重复操作');
  98. $res = CardModel::where($where)->save($data);
  99. return $res ? json_show(CommonModel::$success, '操作成功') : json_show(CommonModel::$error_param, '操作失败');
  100. }
  101. //删除卡类型
  102. public static function delete(int $id = 0): Json
  103. {
  104. $rs = CardModel::where(['id' => $id, 'is_del' => CommonModel::$del_normal])
  105. ->save(['is_del' => CommonModel::$del_deleted, 'updatetime' => date('Y-m-d H:i:s')]);
  106. return $rs ? json_show(CommonModel::$success, '卡类型删除成功') : json_show(CommonModel::$error_param, '卡类型不存在或重复删除');
  107. }
  108. }