package com.emr.controller.exportTask;


import com.alibaba.fastjson.JSON;
import com.emr.annotation.OptionalLog;
import com.emr.dao.ExportTaskDetailsMapper;
import com.emr.dao.ExportTaskMapper;
import com.emr.entity.ExportTask;
import com.emr.entity.ExportTaskDetails;
import com.emr.service.ImportExcel.ImportExcelEntity;
import com.emr.service.ImportExcel.ImportExcelUtil;
import com.emr.util.ExceptionPrintUtil;
import com.emr.vo.ExportTaskDetailsVo;
import com.emr.vo.ExportTaskVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.nio.charset.Charset;
import java.util.List;

/**
 * 任务清单控制层
 */
@Controller
@RequestMapping("exportTask/")
public class ExportTaskController {

    @Autowired
    private ExportTaskMapper exportTaskMapper;

    @Autowired
    private ExportTaskDetailsMapper exportTaskDetailsMapper;

    /*@RequiresPermissions("/exportTask/exportTaskList")*/
    @OptionalLog(module = "任务清单列表", methods = "任务清单列表页面")
    @RequestMapping("exportTaskList")
    public String exportTaskList() {
        return "recordManage/exportTask/exportTaskList";
    }

    @OptionalLog(module = "任务清单列表", methods = "任务清单列表页面")
    @RequestMapping("getExportTaskList")
    @ResponseBody
    public String getExportTaskList(Integer page, Integer limit, ExportTask exportTask,String startTime,String endTime) {
        if (null != page && null != limit) {
            PageHelper.startPage(page, limit);
        }
        try {
            List<ExportTaskVo> list = exportTaskMapper.getExportTaskList(exportTask,startTime,endTime);

            if (list != null && list.size() > 0) {
                for (int i = 0; i < list.size(); i++) {
                    //根据taskid查询所有需要导出病历的住院号出院日期
                    List<ExportTaskDetailsVo> taskList = exportTaskDetailsMapper.selectAllByTaskId(list.get(i).getTaskId());
                    if (taskList != null && taskList.size() > 0) {
                        list.get(i).setState(0);
                    }else{
                        list.get(i).setState(1);
                    }
                }
            }

            PageInfo pageInfo = new PageInfo<>(list);
            return JSON.toJSONString(pageInfo);
        } catch (Exception e) {
            ExceptionPrintUtil.printException(e);
            e.printStackTrace();
            return null;
        }
    }

    @RequiresPermissions("/exportTask/impExcelTask")
    @RequestMapping(value = "impExcelTask", method = {RequestMethod.POST})
    @ResponseBody
    public ResponseEntity<String> impExcelTask(HttpServletRequest request) {
        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.setContentType(new MediaType("text", "html", Charset.forName("UTF-8")));
        try {
            //读取文件
            MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) (request);
            MultipartFile multipartFile = multiRequest.getFile("upfile");
            //属性名
            String[] fieldNames = {"inpno", "dischargedatetime"};
            //判断集中类中的方法名
            String[] judgeMethods = {"judgeInpNo", "judgeDischargeDateTime"};
            ExportTaskDetails exportTaskDetails = new ExportTaskDetails();
            exportTaskDetails.setState(0);

            //实例化
            ImportExcelUtil.newInstance("exportTaskDetailsMapper", exportTaskDetails, ExportTaskDetails.class);
            //导入excel的操作
            ImportExcelEntity excelEntity = ImportExcelUtil.fileImport1(multipartFile, fieldNames, judgeMethods, "");
            if (excelEntity.getSuccessCount() == 0 && excelEntity.getWrongCount() == 0) {
                //无数据
                return new ResponseEntity<String>("无数据!", responseHeaders, HttpStatus.OK);
            }
            if (excelEntity.getWrongCount() == 0) {
                //成功
                return new ResponseEntity<String>(null, responseHeaders, HttpStatus.OK);
            } else {
                //有出错数据
                String msgStr = excelEntity.getWorkBookKey() + "@已成功导入" + excelEntity.getSuccessCount() + "条,失败" + excelEntity.getWrongCount() + "条,随后将导出错误记录!";
                return new ResponseEntity<String>(msgStr, responseHeaders, HttpStatus.OK);
            }

        } catch (Exception e) {
            ExceptionPrintUtil.printException(e);
            e.printStackTrace();
            //抛异常
            return new ResponseEntity<String>(e.getMessage(), responseHeaders, HttpStatus.OK);
        }

    }

}
