/**
 * Copyright (C), 2015-2019
 * Author:   HJL
 * Date:     2019/7/19 14:39
 * Description:病案召回
 */
package com.emr.controller;

import com.emr.annotation.OptionalLog;
import com.emr.entity.*;
import com.emr.service.*;
import com.emr.util.ThreadExcelUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@PropertySource(value = "classpath:config/jdbc.properties", encoding = "UTF-8")
@Controller
@RequestMapping("/storage")
public class storageController {
    @Autowired
    private Emr_Fault_DetailService emrFaultDetailService;

    @Autowired
    private Emr_DictionaryService emrDictionaryService;

    @Autowired
    private Archive_MasterService archiveMasterService;
    @Autowired
    private Archive_Master_FollowingService archiveMasterFollowingService;

    @RequestMapping(value = "/MedicalRecordStorage")
    public String faults(Model model) {
        return "medicalRecordStorageDir/MedicalRecordStorage";
    }

    @ResponseBody
    @RequestMapping(value = "/StorageList")
    public OffsetLimitPage recallList(HttpServletRequest request, HttpServletResponse response, Emr_Fault_Vo emrFaultVo, Integer offset, Integer limit) {
        OffsetLimitPage result = emrFaultDetailService.selectStorageByCol(emrFaultVo, offset, limit);
        return result;
    }
    @ResponseBody
    @RequestMapping(value = "/exportExcel")
    public void exportExcel(HttpServletResponse response,Archive_Master_Vo archiveMasterVo) throws Exception {
        response.setContentType("application/vnd.ms-excel;charset=UTF-8");
        String[] header = {"住院号","住院次数","名字","性别","撤回人","撤回时间","归档状态","状态"};
        String[] fileNames = {"inpNo","visitId","name","sex","handleName","handleTime","archivestate","followingContent"};
        //文件名
        String fileName = "病案封存日志" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".xls";
        //构造excel的数据
        List<Archive_Master_Vo> list=archiveMasterService.selectByFollowinglog(archiveMasterVo);
        ThreadExcelUtils utils = new ThreadExcelUtils(fileName, "", header, fileNames);
        String result=utils.exportExcelToFilePath(response,utils.listConvert(list));
    }

    /**
     *病案封存
     * 将已封存数据转换成未封存
     * 新增操作时日志记录
     */
    @Transactional
    @OptionalLog(methods = "病案封存")
    @ResponseBody
    @RequestMapping(value = "/updateState")
    public int updateState(HttpServletRequest request, HttpServletResponse response, Emr_Fault_Detail emrFaultDetail) throws UnsupportedEncodingException {
        response.setCharacterEncoding("utf-8");
        request.setCharacterEncoding("utf-8");
        Subject currentUser = SecurityUtils.getSubject();
        Session session = currentUser.getSession();
        Power_User user = (Power_User) request.getSession().getAttribute("CURRENT_USER");
        String handleName = user.getUserPosition();//姓名
        String username = user.getUserName();//获取前面登录的用户名
        emrFaultDetail.setUpdater(username);
        SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String nowTime = format1.format(new Date());
        emrFaultDetail.setUpdateTime(nowTime);

        Archive_Master archiveMaster = new Archive_Master();
        Archive_Master_Following archiveMasterFollowing = new Archive_Master_Following();
        archiveMaster.setId(emrFaultDetail.getArchiveDetailId());
        archiveMaster.setArchivestate(emrFaultDetail.getArchiveState());
        archiveMaster.setCmtNurse(1);
        archiveMaster.setCmtDoctor(1);
        int bol = archiveMasterService.updateByClo(archiveMaster);
        if (bol == 1) {
            if(archiveMaster.getArchivestate().equals("已封存")){
                archiveMasterFollowing.setMasterId(emrFaultDetail.getArchiveDetailId());
                archiveMasterFollowing.setFollowingContent("病案封存");
                archiveMasterFollowing.setFollowingType("11");
                archiveMasterFollowing.setHandleTime(nowTime);
                archiveMasterFollowing.setHandleId(username);
                archiveMasterFollowing.setHandleName(handleName);
                archiveMasterFollowing.setRemark(emrFaultDetail.getRecallReason());
                bol = archiveMasterFollowingService.insertSelective(archiveMasterFollowing);
            }else if (archiveMaster.getArchivestate().equals("已归档")){
                archiveMasterFollowing.setMasterId(emrFaultDetail.getArchiveDetailId());
                archiveMasterFollowing.setFollowingContent("病案取消封存");
                archiveMasterFollowing.setFollowingType("12");
                archiveMasterFollowing.setHandleTime(nowTime);
                archiveMasterFollowing.setHandleId(username);
                archiveMasterFollowing.setHandleName(handleName);
                archiveMasterFollowing.setRemark(emrFaultDetail.getRecallReason());
                bol = archiveMasterFollowingService.insertSelective(archiveMasterFollowing);
            }
        }
        return bol;
    }


    /*
     * 封存日志
     * */
    @RequestMapping(value = "/MedicalRecordStorageLog")
    public String faultsLog(Model model) {
        return "medicalRecordStorageDir/MedicalRecordStorageLog";
    }

    /*
     * 封存日志
     * */
    @ResponseBody
    @RequestMapping(value = "/storageLogList")
    public OffsetLimitPage storageLogList(HttpServletRequest request, HttpServletResponse response, Archive_Master_Vo archiveMasterVo, Integer offset, Integer limit) {
        OffsetLimitPage result = archiveMasterService.selectByFollowinglog(archiveMasterVo, offset, limit);
        return result;
    }




}
 

