package com.emr.controller;

import com.emr.dao.CommomMapper;
import com.emr.entity.Power_User;
import com.emr.vo.commomSearch.CommomVo;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

@Controller
@RequestMapping("font/")
public class FontShowRecordController {
    @Autowired
    private CommomMapper commomMapper;
    /**
     * @MethodName recordList
     * @Description:
     * @Param
     * @Returnt
     * @Author: 曾文和
     * @CreateDate: 2020-03-27
     * @UpdateUser: 曾文和
     * @UpdateDate: 2020-03-27
     * @Version: 1.0
     */
    @RequestMapping(value = "recordList")
    public String showRecord(String inpatientNo, Model model, HttpServletRequest request) {
        //根据病案号查看病案是否存在
        if(StringUtils.isNotBlank(inpatientNo)){
            int commom = commomMapper.isExistInpatientOrAdmissId(inpatientNo, null, null);
            if(commom == 0){
                model.addAttribute("msg","病案不存在!");
                return "font/msg";
            }
        }else{
            model.addAttribute("msg","参数病案号不能为空!");
            return "font/msg";
        }
        //虚假登陆
        //验证shiro(有shiro才此操作)
        try{
            Power_User user = new Power_User();
            user.setUserName("第三方");
            user.setUserId(-999);
            user.setRoleId(-999);
            Set<String> menu = new TreeSet<>();
            menu.add("/commom/printPdf");
            user.setMenus(menu);
            UsernamePasswordToken userToken = new UsernamePasswordToken(user.getUserName(), "123456");
            Subject subject = SecurityUtils.getSubject();
            subject.login(userToken);
            request.getSession().setAttribute("CURRENT_USER", user);
            model.addAttribute("inpatientNo",inpatientNo);
        }catch (Exception e){
            e.printStackTrace();
        }
        return "font/recordList";
    }

    @RequestMapping("getRecordListByInpatientNo")
    @ResponseBody
    public String getRecordListByInpatientNo(Integer page, Integer limit,String inpatientNo){
        try{
            if(null != page && null != limit){
                PageHelper.startPage(page, limit);
            }
            List<CommomVo> list = commomMapper.selectCommomByInpatientNo(inpatientNo);
            PageInfo pageInfo = new PageInfo<>(list);
            ObjectMapper mapper = new ObjectMapper();
            String json = mapper.writeValueAsString(pageInfo);
            return json;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
}
