CompanyLogic.php 4.6 KB

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