package com.noahhealth.controller;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.noahhealth.bean.CommonResult;
import com.noahhealth.bean.Constant;
import com.noahhealth.bean.Identity;
import com.noahhealth.bean.PageResult;
import com.noahhealth.bean.input.ResultInputDetailExtend;
import com.noahhealth.bean.input.ResultInputExtend;
import com.noahhealth.bean.rolecheck.RequiredRoles;
import com.noahhealth.bean.user.UserExtend;
import com.noahhealth.pojo.*;
import com.noahhealth.service.*;
import com.noahhealth.util.TimeUtil;
import com.noahhealth.util.Validator;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
/**
* 化验、医技数据管理
* Created by zlren on 2017/6/21.
*/
@RequestMapping("input")
@RestController
@Slf4j
public class ResultInputController {
@Autowired
private UserService userService;
@Autowired
private ResultInputService resultInputService;
@Autowired
private ResultInputDetailService resultInputDetailService;
@Autowired
private CategorySecondService categorySecondService;
@Autowired
private CategoryThirdService categoryThirdService;
@Autowired
private PropertyService propertyService;
/**
* 根据inputId查询单条result的详情
*
* @param inputId
* @return
*/
@RequestMapping(value = "{inputId}", method = RequestMethod.GET)
public CommonResult queryResultInputDetailByInputId(@PathVariable("inputId") Integer inputId) {
ResultInput resultInput = this.resultInputService.queryById(inputId);
ResultInputExtend resultInputExtend = this.resultInputService.extendFromResultInput(resultInput);
ResultInputDetail resultInputDetailRecord = new ResultInputDetail();
resultInputDetailRecord.setResultInputId(resultInputExtend.getId());
List<ResultInputDetail> resultInputDetailList = this.resultInputDetailService.queryListByWhere
(resultInputDetailRecord);
resultInputExtend.data = this.resultInputDetailService.extendFromResultInputDetailList
(resultInputDetailList);
return CommonResult.success("查询成功", resultInputExtend);
}
/**
* 在input表增加一条记录
*
* @param params
* @return
*/
@RequestMapping(method = RequestMethod.POST)
public CommonResult addResultInput(@RequestBody Map<String, Object> params, HttpSession session) {
Integer userId = (Integer) params.get("userId");
{
User record = new User();
record.setId(userId);
if (this.userService.queryOne(record).getRole().equals(Constant.USER_1)) {
// 1级用户没有此权限
return CommonResult.failure("一级用户无此权限");
}
}
Integer secondId = (Integer) params.get("secondId");
Integer inputerId = Integer.valueOf(((Identity) session.getAttribute(Constant.IDENTITY)).getId());
String status = Constant.LU_RU_ZHONG; // 初始状态为录入中
String note = (String) params.get(Constant.NOTE);
String hospital = (String) params.get("hospital");
Date time = TimeUtil.parseTime((String) params.get(Constant.TIME));
ResultInput resultInput = new ResultInput();
resultInput.setUserId(userId);
resultInput.setSecondId(secondId);
resultInput.setInputerId(inputerId);
resultInput.setStatus(status);
resultInput.setNote(note);
resultInput.setHospital(hospital);
resultInput.setTime(time);
resultInput.setUploadTime(TimeUtil.getCurrentTime());
// 级联插入
this.resultInputService.saveInputAndEmptyDetail(resultInput);
return CommonResult.success("添加成功");
}
/**
* 删除input记录,级联删除detail表
*
* @param inputId
* @return
*/
@RequestMapping(value = "{inputId}", method = RequestMethod.DELETE)
@RequiredRoles(roles = {"系统管理员", "档案部员工", "档案部主管"})
public CommonResult deleteResultInputById(@PathVariable("inputId") Integer inputId, HttpSession session) {
Identity identity = (Identity) session.getAttribute(Constant.IDENTITY);
String identityRole = identity.getRole();
String identityId = identity.getId();
ResultInput resultInput = this.resultInputService.queryById(inputId);
// if (this.userService.checkArchiver(identityRole)) {
// // 如果是档案部员工,这条记录必须是他创建的
// if (!resultInput.getInputerId().equals(Integer.valueOf(identityId))) {
// return CommonResult.failure("无此权限");
// }
// } else if (this.userService.checkArchiverManager(identityRole)) {
// // 如果是档案部主管,这条记录必须是手下的人创建的
// Set<Integer> archiverIdSet = this.userService.queryStaffIdSetUnderManager(identity);
//
// if (!archiverIdSet.contains(resultInput.getInputerId())) { // 再加上或是自己创建的
// return CommonResult.failure("无此权限");
// }
// }
boolean result = this.resultInputService.deleteInput(inputId);
if (!result) {
return CommonResult.failure("删除失败");
}
return CommonResult.success("删除成功");
}
/**
* 本质就是条件查询user表的会员
*
* @param params
* @param session
* @return
*/
@RequestMapping(value = "list", method = RequestMethod.POST)
public CommonResult queryResultInputUserList(@RequestBody Map<String, Object> params, HttpSession session) {
Integer pageNow = (Integer) params.get(Constant.PAGE_NOW);
Integer pageSize = (Integer) params.get(Constant.PAGE_SIZE);
String userName = (String) params.get("userName");
String memberNum = (String) params.get("memberNum");
Identity identity = (Identity) session.getAttribute(Constant.IDENTITY);
// 过期的用户看不了
if (!this.userService.checkValid(identity.getId())) {
return CommonResult.failure("过期无效的用户");
}
List<User> userList = this.resultInputService.queryResultInputUserList(identity, userName, memberNum,
pageNow, pageSize);
PageResult pageResult = new PageResult(new PageInfo<>(userList));
List<UserExtend> userExtendList = this.userService.extendFromUser(userList);
pageResult.setData(userExtendList);
return CommonResult.success("查询成功", pageResult);
}
/**
* 档案部查的时候,直接查那些uploaderId是自己的所有记录,不要再嵌套一层user列表
* ADMIN也调用的这个
*
* @return
*/
@RequestMapping(value = "list_by_arc", method = RequestMethod.POST)
public CommonResult queryResultInputListByArc(@RequestBody Map<String, Object> params, HttpSession session) {
Integer pageNow = (Integer) params.get(Constant.PAGE_NOW);
Integer pageSize = (Integer) params.get(Constant.PAGE_SIZE);
String userName = (String) params.get("userName");
String memberNum = (String) params.get("memberNum");
String inputerName = (String) params.get("inputerName");
String checkerName = (String) params.get("checkerName");
String type = (String) params.get("type"); // 化验或者医技
Date beginTime = TimeUtil.parseTime((String) params.g