GroupLogic.php 5.8 KB

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