CompanyLogic.php 5.1 KB

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