package com.sm.graduation.root.controller;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sm.graduation.accident.pojo.AccidentRecord;
import com.sm.graduation.accident.service.AccidentRecordService;
import com.sm.graduation.admin.pojo.AdminInfo;
import com.sm.graduation.admin.service.AdminInfoService;
import com.sm.graduation.checkin.pojo.CheckIn;
import com.sm.graduation.checkin.service.CheckInService;
import com.sm.graduation.common.loginpojo.LoginPojo;
import com.sm.graduation.common.result.AjaxResult;
import com.sm.graduation.dormitory.pojo.DormitoryAllocation;
import com.sm.graduation.dormitory.service.DormitoryAllocationService;
import com.sm.graduation.food.pojo.MonthlyCatering;
import com.sm.graduation.food.service.MonthlyCateringService;
import com.sm.graduation.health.pojo.HealthRecords;
import com.sm.graduation.health.service.HealthRecordsService;
import com.sm.graduation.high.pojo.HighRisk;
import com.sm.graduation.high.service.HighRiskService;
import com.sm.graduation.medication.pojo.Medication;
import com.sm.graduation.medication.service.MedicationService;
import com.sm.graduation.nurse.pojo.Nursing;
import com.sm.graduation.nurse.service.NursingService;
import com.sm.graduation.older.pojo.OlderInfo;
import com.sm.graduation.older.service.OlderInfoService;
import com.sm.graduation.out.pojo.GoOut;
import com.sm.graduation.out.service.GoOutService;
import com.sm.graduation.usr.pojo.UsrInfo;
import com.sm.graduation.usr.service.UsrInfoService;
import com.sm.graduation.visitor.pojo.Visitor;
import com.sm.graduation.visitor.service.VisitorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.sm.graduation.root.pojo.RootInfo;
import com.sm.graduation.root.service.RootInfoService;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static com.sm.graduation.common.result.AjaxResult.*;
@SuppressWarnings("all")
@RestController
@RequestMapping("/root")
public class RootInfoController {
@Autowired
private RootInfoService rootInfoService;
@Autowired
private AdminInfoService adminInfoService;
@Autowired
private UsrInfoService usrInfoService;
@Autowired
private HealthRecordsService healthRecordsService;
@Autowired
private HighRiskService highRiskService;
@Autowired
private MedicationService medicationService;
@Autowired
private MonthlyCateringService monthlyCateringService;
@Autowired
private GoOutService goOutService;
@Autowired
private OlderInfoService olderInfoService;
@Autowired
private DormitoryAllocationService dormitoryAllocationService;
@Autowired
private AccidentRecordService accidentRecordService;
@Autowired
private VisitorService visitorService;
@Autowired
private CheckInService checkInService;
@Autowired
private NursingService nursingService;
private String olderName;
private boolean login;
/** 登录验证 */
@RequestMapping("/loginIn")
public AjaxResult loginIn(HttpServletRequest request, HttpSession session, String username,
LoginPojo loginPojo, RootInfo rootInfo,AdminInfo adminInfo, UsrInfo usrInfo){
if (loginPojo.getCaptcha().equalsIgnoreCase(String.valueOf(request.getSession().getAttribute("captCode")))) {
//超管登录
if (0 == loginPojo.getPower()){
rootInfo.setName(loginPojo.getUsername());
rootInfo.setPwd(loginPojo.getPassword());
RootInfo root = rootInfoService.sltName(rootInfo);
if (root == null){
return AjaxResult.error("登录名不存在");
}
login = rootInfoService.loginIn(rootInfo);
if (login) {
session.setAttribute("username",root.getNickName());
session.setAttribute("power",root.getPower());
session.setAttribute("root",root);
return AjaxResult.success(0,"登录成功");
} else return AjaxResult.error("登录名或密码错误");
}
//管理员登录
if (1 == loginPojo.getPower()){
adminInfo.setAdminLogin(loginPojo.getUsername());
adminInfo.setAdminPwd(loginPojo.getPassword());
AdminInfo admin = adminInfoService.sltName(adminInfo);
if (admin == null){
return AjaxResult.error("登录名不存在");
}
login = adminInfoService.loginIn(adminInfo);
if (login) {
session.setAttribute("username",admin.getAdminName());
session.setAttribute("power",admin.getPower());
session.setAttribute("admin",admin);
return AjaxResult.success(1,"登录成功");
} else return AjaxResult.error("登录名或密码错误");
}
//用户登录
if (2 == loginPojo.getPower()){
usrInfo.setUsrLogin(loginPojo.getUsername());
usrInfo.setUsrPwd(loginPojo.getPassword());
UsrInfo usr = usrInfoService.sltName(usrInfo);
if (usr == null){
return AjaxResult.error("登录名不存在");
}
login = usrInfoService.loginIn(usrInfo);
if (login) {
String olderName = usr.getOlderName();
HealthRecords older = healthRecordsService.older_dorm(olderName);
this.olderName = older.getName();
session.setAttribute("username",usr.getUsrName());
session.setAttribute("power",usr.getUsrPwd());
session.setAttribute("usr",usr);
session.setAttribute("older",older);
return AjaxResult.success(2,"登录成功");
} else return AjaxResult.error("登录名或密码错误");
}
}
return AjaxResult.error("验证码错误");
}
/** admin --- List */
@RequestMapping("/adminList")
public AjaxResult adminList(@RequestParam(defaultValue = "1" , value = "page") Integer pageNum,
@RequestParam(defaultValue = "10", value = "limit") Integer pageSize,
@RequestParam(defaultValue = "" , value = "adminName") String user
){
HashMap<String, Object> map = new HashMap<>();
PageHelper.startPage(pageNum,pageSize);
if (user == null || user.equals("")) {
map.put("user","");
List<AdminInfo> admins = adminInfoService.listAll(map);
PageInfo<AdminInfo> page = new PageInfo<>(admins);
return successData(page.getTotal(), admins);
}
map.put("user",user);
List<AdminInfo> admins = adminInfoService.listAll(map);
PageInfo<AdminInfo> page = new PageInfo<>(admins);
return successData(page.getTotal(), admins);
}
/** usr --- List */
@RequestMapping("/userList")
public AjaxResult userList(@RequestParam(defaultValue = "1", value = "page") Integer pageNum,
@RequestParam(defaultValue = "10", value = "limit") Integer pageSize,
@RequestParam(defaultValue = "" , value = "usrName") String usr
){
HashMap<String, Object> map = new HashMap<>();
PageHelper.startPage(pageNum,pageSize);
if (usr == null || usr.equals("")) {
map.put("usr","");
List<UsrInfo> usrInfos = usrInfoService.listAll(map);
PageInfo<UsrInfo> page = new PageInfo<