package com.emr.controller.emrPrintOrDownLoadInfo;

import com.emr.annotation.OptionalLog;
import com.emr.entity.ResultUtil;
import com.emr.service.emrPrintOrDownLoadInfo.PrintOrDownLoadInfoService;
import com.emr.util.ExceptionPrintUtil;
import com.emr.util.ExportExcelUtil;
import com.emr.vo.EmrPrintOrDownLoadInfo.EmrPrintOrDownLoadInfoVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Controller
public class PrintOrDownLoadInfoController {
    @Autowired
    private PrintOrDownLoadInfoService printOrDownLoadInfoService;
    /**
     * @ProjectName:printInfoList
     * @Description:跳转到打印记录页面
     * @Param
     * @Return
     * @Author: 曾文和
     * @CreateDate: 2020-03-04
     * @UpdateUser: 曾文和
     * @UpdateDate: 2020-03-04
     * @UpdateRemark: 更新说明
     * @Version: 1.0
     */
    //174
    @RequiresPermissions("/printInfoList/select")
    @RequestMapping("printInfoList/pageUI174")
    public String printInfoList174(){
        return "otherManage/printInfoList174";
    }

    /**
     * @ProjectName:getPrintInfoList
     * @Description:查询打印记录
     * @Param
     * @Return
     * @Author: 曾文和
     * @CreateDate: 2020-03-04
     * @UpdateUser: 曾文和
     * @UpdateDate: 2020-03-04
     * @UpdateRemark: 更新说明
     * @Version: 1.0
     */
    @RequiresPermissions("/printInfoList/select")
    @RequestMapping("printInfoList/getPrintInfoList")
    @ResponseBody
    public PageInfo getPrintInfoList(Integer page, Integer limit, EmrPrintOrDownLoadInfoVo emrPrintOrDownLoadInfoVo){
        if(null != page && null != limit){
            PageHelper.startPage(page, limit);
        }
        try{
            //查询
            List<EmrPrintOrDownLoadInfoVo> list = printOrDownLoadInfoService.selectAll(emrPrintOrDownLoadInfoVo);
            return new PageInfo<>(list);
        }catch (Exception e){
            ExceptionPrintUtil.printException(e);
            e.printStackTrace();
            return null;
        }
    }

    /**
     * @ProjectName:getPrintCount
     * @Description:查询最近打印记录
     * @Param
     * @Return
     * @Author: 曾文和
     * @CreateDate: 2020-03-05
     * @UpdateUser: 曾文和
     * @UpdateDate: 2020-03-05
     * @UpdateRemark: 更新说明
     * @Version: 1.0
     */
    @RequestMapping("/printInfoList/getPrintCount")
    @ResponseBody
    public ResultUtil getPrintCount(String patientId) throws Exception{
        //查询
        List<EmrPrintOrDownLoadInfoVo> list = printOrDownLoadInfoService.getPrintCount(patientId);
        return ResultUtil.ok(list);
    }

    /**
     * @MethodName: exportExcel
     * @Description: 根据搜索条件导出excel
     * @Param
     * @Return
     * @Author: 曾文和
     * @CreateDate: 2019-06-27
     * @UpdateUser: 曾文和
     * @UpdateDate: 2019-06-27
     * @UpdateRemark: 更新说明
     * @Version: 1.0
     */
    @RequiresPermissions("/printInfoList/export")
    @OptionalLog(module = "导出excel",methods = "打印记录列表")
    @RequestMapping(value="/printInfoList/export174",produces = {"text/json;charset=UTF-8"})
    @ResponseBody
    public void export174(HttpServletResponse response,EmrPrintOrDownLoadInfoVo printOrDownLoadInfoVo,String checks,String flag,Integer operType){
        Export174(response, printOrDownLoadInfoVo,checks,flag,operType);
    }

    /**
     * @MethodName: exportExcel
     * @Description: 根据搜索条件导出excel
     * @Param
     * @Return
     * @Author: 曾文和
     * @CreateDate: 2019-06-27
     * @UpdateUser: 曾文和
     * @UpdateDate: 2019-06-27
     * @UpdateRemark: 更新说明
     * @Version: 1.0
     */
    @RequiresPermissions("/downloadInfoList/export")
    @OptionalLog(module = "导出excel",methods = "下载记录列表")
    @RequestMapping(value="/downloadInfoList/export174",produces = {"text/json;charset=UTF-8"})
    @ResponseBody
    public void export1741(HttpServletResponse response,EmrPrintOrDownLoadInfoVo printOrDownLoadInfoVo, String checks,String flag,Integer operType){
        Export174(response, printOrDownLoadInfoVo,checks,flag,operType);
    }

    private void Export174(HttpServletResponse response,EmrPrintOrDownLoadInfoVo printOrDownLoadInfoVo,String checks,String flag,Integer operType){
        String tableThNames = "操作人,姓名,病案号,住院次数,类别,文件名,页数,费用,用途,操作时间,ip地址";
        String fieldCns = "creater,name,inpatientNo,admissTimes,assortName,scanPage,printCount,printFee,typeName,createTime,ip";
        //构造excel的数据
        try{
            List<EmrPrintOrDownLoadInfoVo> list = new ArrayList<>();
            if(StringUtils.isNotBlank(checks)){
                list = printOrDownLoadInfoService.selectAllByIds(checks,flag);
            }else{
                //查询
                list = printOrDownLoadInfoService.selectAll(printOrDownLoadInfoVo);
            }
            String fileName = "";
            if(operType == 1){
                fileName = "打印";
            }else{
                fileName = "下载";
            }
            //文件名
            fileName += "记录列表导出数据(" + new SimpleDateFormat("yyyy-MM-dd HH_mm_ss").format(new Date()) + ").xls";
            //ExportExcelUtil
            ExportExcelUtil exportExcelUtil = new ExportExcelUtil();
            //导出excel的操作
            exportExcelUtil.expordExcel(tableThNames,fieldCns,list,fileName,response);
        }catch (Exception e){
            ExceptionPrintUtil.printException(e);
            e.printStackTrace();
        }
    }

    /**
     * @ProjectName:downloadInfoList
     * @Description:跳转到下载记录页面
     * @Param
     * @Return
     * @Author: 曾文和
     * @CreateDate: 2020-03-04
     * @UpdateUser: 曾文和
     * @UpdateDate: 2020-03-04
     * @UpdateRemark: 更新说明
     * @Version: 1.0
     */
    //174
    @RequiresPermissions("/downloadInfoList/select")
    @RequestMapping("downloadInfoList/pageUI174")
    public String downloadInfoList174(){
        return "otherManage/downloadInfoList174";
    }
}
