GroupLogic.php 6.3 KB

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