CardLogic.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 Add(array $data = []): Json
  25. {
  26. $rs = CardModel::field('id')
  27. ->where(['is_del' => CommonModel::$del_normal, 'title' => $data['title']])
  28. ->findOrEmpty()
  29. ->isEmpty();
  30. if (!$rs) throw new ValidateException('卡类型已存在,请勿重复添加');
  31. $data = array_merge($data, [
  32. 'status' => CommonModel::$status_normal,
  33. 'is_del' => CommonModel::$del_normal,
  34. 'createrid' => self::$uid,
  35. 'creater' => self::$uname,
  36. 'updaterid' => self::$uid,
  37. 'updater' => self::$uname,
  38. ]);
  39. $res = CardModel::create($data)->save();
  40. return $res ? json_show(CommonModel::$success, '添加卡类型成功') : json_show(CommonModel::$error_param, '添加卡类型失败');
  41. }
  42. //获取卡类型详情
  43. public static function Read(int $id = 0): Json
  44. {
  45. $rs = CardModel::field(true)
  46. ->where(['id' => $id, 'is_del' => CommonModel::$del_normal])
  47. ->findOrEmpty()
  48. ->toArray();
  49. return json_show(CommonModel::$success, '获取卡类型详情成功', $rs);
  50. }
  51. //编辑卡类型
  52. public static function Edit(array $data = []): Json
  53. {
  54. $rs = CardModel::field('id,title')
  55. ->where(['id' => $data['id'], 'is_del' => CommonModel::$del_normal])
  56. ->findOrEmpty();
  57. if ($rs->isEmpty()) throw new ValidateException('该卡类型不存在');
  58. if ($rs->title != $data['title']) {
  59. $temp = CardModel::field('id')
  60. ->where(['is_del' => CommonModel::$del_normal, 'title' => $data['title']])
  61. ->findOrEmpty()
  62. ->isEmpty();
  63. if (!$temp) throw new ValidateException('该卡类型已存在');
  64. }
  65. $data = array_merge($data, [
  66. 'updaterid' => self::$uid,
  67. 'updater' => self::$uname,
  68. 'updatetime' => date('Y-m-d H:i:s'),
  69. ]);
  70. $res = CardModel::where(['id' => $data['id'], 'is_del' => CommonModel::$del_normal])
  71. ->save($data);
  72. return $res ? json_show(CommonModel::$success, '编辑卡类型成功') : json_show(CommonModel::$error_param, '编辑卡类型失败');
  73. }
  74. //卡类型启禁用
  75. public static function Change(array $data = []): Json
  76. {
  77. $where = [
  78. ['id', '=', $data['id']],
  79. ['is_del', '=', CommonModel::$del_normal],
  80. ['status', '<>', $data['status']],
  81. ];
  82. $rs = CardModel::field('id,status')
  83. ->where($where)
  84. ->findOrEmpty()
  85. ->isEmpty();
  86. if ($rs) throw new ValidateException('该卡类型不存在或重复操作');
  87. $res = CardModel::where($where)->save($data);
  88. return $res ? json_show(CommonModel::$success, '操作成功') : json_show(CommonModel::$error_param, '操作失败');
  89. }
  90. //删除卡类型
  91. public static function Delete(int $id = 0): Json
  92. {
  93. $rs = CardModel::where(['id' => $id, 'is_del' => CommonModel::$del_normal])
  94. ->save(['is_del' => CommonModel::$del_deleted, 'updatetime' => date('Y-m-d H:i:s')]);
  95. return $rs ? json_show(CommonModel::$success, '卡类型删除成功') : json_show(CommonModel::$error_param, '卡类型不存在或重复删除');
  96. }
  97. }