package com.casic.missiles.modular.alarm.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.casic.missiles.core.model.auth.AuthUser;
import com.casic.missiles.model.response.ResponseData;
import com.casic.missiles.modular.alarm.service.IAlarmJobService;
import com.casic.missiles.core.application.context.AbstractPermissionContext;
import com.casic.missiles.core.datascope.DataScope;
import com.casic.missiles.core.util.EhcacheConstant;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.casic.missiles.modular.alarm.service.ISysDictService;
import com.casic.missiles.modular.system.warpper.AlarmJobWarpper;
import com.casic.missiles.core.base.controller.BaseController;
import com.casic.missiles.core.page.PageFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.text.ParseException;
import java.util.*;


/**
 * 控制器
 *
 * @author dev
 * @Date 2019-05-17 10:48:36
 */
@Controller
@RequestMapping("/job")
public class AlarmJobController extends BaseController {


    @Autowired
    private IAlarmJobService alarmJobService;
    @Resource
    private AbstractPermissionContext iCommonPermissionService;
    @Autowired
    private ISysDictService iSysDictService;

    @Value("${smartcity.office.maxRowsExcel}")
    private int maxRowsExcel;
    @Value("${smartcity.leaderRoleName}")
    private String sLeader;
    @Value("${smartcity.memberRoleName}")
    private String sMember;
    @Value("${smartcity.adminRoleName}")
    private String sAdmin;
    @Value("${smartcity.opRoleName}")
    private String sOperation;
    @Value("${smartcity.config.config-path}")
    private String templatePath;


    @RequestMapping(value = "/countByJobStatus")
    @ResponseBody
    public Object countByJobStatus(HttpServletRequest httpServletRequest) throws ParseException {
        String beginTime = httpServletRequest.getParameter("beginTime");
        String endTime = httpServletRequest.getParameter("endTime");
        DataScope dataScope = iCommonPermissionService.getAuthService().getLoginUserDataScope();
        AuthUser currentUser = iCommonPermissionService.getAuthService().getLoginUser();
        Map<String, Object> retObj = new HashMap<>();
        //pc角色
        if (alarmJobService.checkPcRole(currentUser.getRoleTips())) {
            String alarmType = null;
            //operation和admin可以看两种异常，其他只能看一种
            if (!currentUser.getRoleTips().contains(sOperation) && !currentUser.getRoleTips().contains(sAdmin)) {
                alarmType = "1";
            }
            retObj.put("beforeGet", alarmJobService.countByDataScope(dataScope, alarmType, "0", beginTime, endTime));
            retObj.put("beforeConfirm", alarmJobService.countByDataScope(dataScope, alarmType, "1", beginTime, endTime));
            retObj.put("inHandle", alarmJobService.countByDataScope(dataScope, alarmType, "2", beginTime, endTime));
            retObj.put("over", alarmJobService.countByDataScope(dataScope, alarmType, "3", beginTime, endTime));
            retObj.put("cancel", alarmJobService.countByDataScope(dataScope, alarmType, "4", beginTime, endTime));
        } else {
            //app角色
            Integer beforeGet = alarmJobService.countByResponse("1", "0", currentUser.getDeptId(), beginTime, endTime);
            retObj.put("beforeGet", beforeGet);

            Integer myBeforeConfirm = alarmJobService.countByUser("1", "1", currentUser.getId(), beginTime, endTime);
            Integer myInHandle = alarmJobService.countByUser("1", "2", currentUser.getId(), beginTime, endTime);
            Integer myOver = alarmJobService.countByUser("1", "3", currentUser.getId(), beginTime, endTime);
            Integer myCancel = alarmJobService.countByUser("1", "4", currentUser.getId(), beginTime, endTime);

            if (currentUser.getRoleTips().contains(sLeader)) {
                Integer beforeConfirm = alarmJobService.countByResponse("1", "1", currentUser.getDeptId(), beginTime, endTime);
                Integer inHandle = alarmJobService.countByResponse("1", "2", currentUser.getDeptId(), beginTime, endTime);
                Integer over = alarmJobService.countByResponse("1", "3", currentUser.getDeptId(), beginTime, endTime);
                Integer cancel = alarmJobService.countByResponse("1", "4", currentUser.getDeptId(), beginTime, endTime);

                retObj.put("beforeConfirm", myBeforeConfirm + "/" + beforeConfirm);
                retObj.put("inHandle", myInHandle + "/" + inHandle);
                retObj.put("over", myOver + "/" + over);
                retObj.put("cancel", myCancel + "/" + cancel);
            } else {
                retObj.put("beforeConfirm", myBeforeConfirm);
                retObj.put("inHandle", myInHandle);
                retObj.put("over", myOver);
                retObj.put("cancel", myCancel);
            }
        }

        return ResponseData.success(retObj);
    }

    /**
     * 获取分页列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object listPage(HttpServletRequest httpServletRequest) {
        String keywords = httpServletRequest.getParameter("keywords");
        String beginTime = httpServletRequest.getParameter("beginTime");
        String endTime = httpServletRequest.getParameter("endTime");
        String jobStatusStr = httpServletRequest.getParameter("jobStatus");
        String alarmTypeStr = httpServletRequest.getParameter("alarmType");
        String alarmContentStr = httpServletRequest.getParameter("alarmContentType");
        Page<Map<String, Object>> page = PageFactory.defaultPage();
        DataScope dataScope = iCommonPermissionService.getAuthService().getLoginUserDataScope();
        AuthUser currentUser = iCommonPermissionService.getAuthService().getLoginUser();
        List<Map<String, Object>> retList = new ArrayList<>();
        if (alarmJobService.checkPcRole(currentUser.getRoleTips())) {
            // pc角色
            //operation和admin能查设备异常，其他只能查数据异常
            if (!currentUser.getRoleTips().contains(sOperation) && !currentUser.getRoleTips().contains(sAdmin)) {
                alarmTypeStr = "1";
            }
            retList = alarmJobService.jobList(page, keywords, beginTime, endTime, jobStatusStr, alarmTypeStr, alarmContentStr, dataScope, currentUser.getId());
        } else {
            //app角色
            long leaderId = 0L;
            if (currentUser.getRoleTips().contains(sLeader)) {
                // 组长，查组内全部
                leaderId = currentUser.getId();
                if (StrUtil.isNotEmpty(jobStatusStr)) {
                    //learder&&jobStatus=1,2,3时，关闭传入的sort，按sql排序(自己的排在前)
                    page.setOpenSort(!alarmJobService.checkJobStatus(jobStatusStr));
                }
            }
            retList = alarmJobService.jobListApp(page, keywords, beginTime, endTime, jobStatusStr, alarmTypeStr, alarmContentStr, currentUser.getDeptId(), currentUser.getId(), leaderId);
        }
        new AlarmJobWarpper(retList).warp();
        page.setRecords(retList);
        return ResponseData.success(super.packForBT(page));
    }

    /**
     * 工单查询接口 APP用
     */
    @RequestMapping(value = "/searchList")
    @ResponseBody
    public Object jobListSearch(String keywords, String alarmLevel, String alarmContent, String jobStatus, String beginTime, String endTime,
                                String getJobBeginTime, String getJobEndTime, String shouldGetBeginTime, String shouldGetEndTime,
                                String handleJobBeginTime, String handleJobEndTime, String shouldHandleBeginTime, String shouldHandleEndTime) {

        Page<Map<String, Object>> page = PageFactory.defaultPage();
        DataScope dataScope = iCommonPermissionService.getAuthService().getLoginUserDataScope();
        AuthUser currentUser = iCommonPermissionService.getAuthService().getLoginUser();
        List<Map<String, Object>> retList = new ArrayList<>();
        if (alarmJobService.checkPcRole(currentUser.getRoleTips())) {
            // pc角色
            retList = alarmJobService.jobListSearch(page, keywords, alarmLevel, alarmContent, jobStatus, beginTime, endTime, getJobBeginTime, getJobEndTime, shouldGetBeginTime, shouldGetEndTime,
                    handleJobBeginTime, handleJobEndTime, shouldHandleBeginTime, shouldHandleEndTime, dataScope, currentUser.getId());
        } else {
            //app角色
            long leaderId = 0L;
            if (currentUser.getRoleTips().contains(sLeader)) {
                // 组长，查组内全部
                leaderId = currentUser.getId();
                if (StrUtil.isNotEmpty(jobStatus)) {
                    //learder&&jobStatus=1,2,3时，关闭传入的sort，按sql排序(自己的排在前)
                    page.setOpenSort(!alarmJobService.checkJobStatus(jobStatus));
                }
            }
            retList = alarmJobService.jobListSearchApp(page, keywords, alarmLevel, alarmContent, jobStatus, beginTime, endTime, getJobBeginTime, getJobEndTime, shouldGetBeginTime, shouldGetEndTime,
                    handleJobBeginTime, handleJobEndTime, shouldHandleBeginTime, shouldHandleEndTime, currentUser.getDeptId(), currentUser.getId(), leaderId);
        }

        new AlarmJobWarpper(retList).warp();
        page.setRecords(retList);
        Map<String, Object> retMap = new HashMap<>();

        return ResponseData.success(super.packForBT(page));
    }

    /**
     * 获取超时未接收工单
     */
    @RequestMapping(value = "/overtimeGetList")
    @ResponseBody
    public Object overtimeGetList(HttpServletRequest httpServletRequest) {
        String keywords = httpServletRequest.getParameter("keywords");
        String beginTime = httpServletRequest.getParameter("beginTime");
        String endTime = httpServletRequest.getParameter("endTime");
        String alarmTypeStr = httpServletRequest.getParameter("alarmType");
        String alarmContentStr = httpServletRequest.getParameter("alarmContentType");

        Page<Map<String, Object>> page = PageFactory.defaultPage();
        DataScope dataScope = iCommonPermissionService.getAuthService().getLoginUserDataScope();
        AuthUser currentUser = iCommonPermissionService.getAuthService().getLoginUser();
        List<Map<String, Object>> retList = new ArrayList<>();
        if (alarmJobService.checkPcRole(currentUser.getRoleTips())) {
            // pc角色
            //operation和admin能查设备异常，其他只能查数据异常
            if (!currentUser.getRoleTips().contains(sOperation) && !currentUser.getRoleTips().contains(sAdmin)) {
                alarmTypeStr = "1";
            }
            retList = alarmJobService.jobListDelayRe(page, keywords, beginTime, endTime, alarmTypeStr, alarmContentStr, dataScope, currentUser.getId());
        } else {
            // app角色
            Long leaderId = 0L;
            if (currentUser.getRoleTips().contains(sLeader)) {
                // 组长，查组内全部
                leaderId = currentUser.getId();
            }
            retList = alarmJobService.jobListDelayReApp(page, keywords, beginTime, endTime, alarmTypeStr, alarmContentStr, currentUser.getDeptId(), currentUser.getId(), leaderId);
        }
        new AlarmJobWarpper(retList).warp();
        page.setRecords(retList);

        return ResponseData.success(super.packForBT(page));
    }

    /**
     * 获取超时未处理工单
     */
    @RequestMapping(value = "/overtimeHandleList")
    @ResponseBody
    public Object overtimeHandleList(HttpServletRequest httpServletRequest) {
        String keywords = httpServletRequest.getParameter("keywords");
        String beginTime = httpServletRequest.getParameter("beginTime");
        String endTime = httpServletRequest.getParameter("endTime");
        String alarmTypeStr = httpServletRequest.getParameter("alarmType");
        String alarmContentStr = httpServletRequest.getParameter("alarmContentType");

        Page<Map<String, Object>> page = PageFactory.defaultPage();
        DataScope dataScope = iCommonPermissionService.getAuthService().getLoginUserDataScope();
        AuthUser currentUser = iCommonPermissionService.getAuthService().getLoginUser();
        List<Map<String, Object>> retList = new ArrayList<>();
        if (alarmJobService.checkPcRole(currentUser.getRoleTips())) {
            // pc角色
            //operation和admin能查设备异常，其他只能查数据异常
            if (!currentUser.getRoleTips().contains(sOperation) && !currentUser.getRoleTips().contains(sAdmin)) {
                alarmTypeStr = "1";
            }
            retList = alarmJobService.jobListDelayPro(page, keywords, beginTime, endTime, alarmTypeStr, alarmContentStr, dataScope, currentUser.getId());
        } else {
            // app角色
            Long leaderId = 0L;
            if (currentUser.getRoleTips().contains(sLeader)) {
                // 组长，查组内全部
                leaderId = currentUser.getId();
            }
            retList = alarmJobService.jobListDelayProApp(page, keywords, beginTime, endTime, alarmTypeStr, alarmContentStr, currentUser.getDeptId(), currentUser.getId(), leaderId);
        }
        new AlarmJobWarpper(retList).warp();
        page.setRecords(retList);

        return ResponseData.success(super.packForBT(page));
    }


    @RequestMapping(value = "/export")
    public void exportJob(HttpServletRequest httpServletRequest
            , HttpServletResponse httpServletResponse) throws IOException {

        Page<Map<String, Object>> page = PageFactory.defaultPage();

        page.setMaxLimit((long) maxRowsExcel);
        page.setSize(maxRowsExcel);
        page.setSearchCount(false);
        page.offset();

        String keywords = httpServletRequest.getParameter("keywords");
        String beginTime = httpServletRequest.getParameter("beginTime");
        String endTime = httpServletRequest.getParameter("endTime");
        String jobStatusStr = httpServletRequest.getParameter("jobStatus");
        String alarmTypeStr = httpServletRequest.getParameter("alarmType");
        String alarmContentStr = httpServletRequest.getParameter("alarmContentType");

        List<Map<String, Object>> jobExpList = new ArrayList<>();
        DataScope dataScope = iCommonPermissionService.getAuthService().getLoginUserDataScope();
        AuthUser currentUser = iCommonPermissionService.getAuthService().getLoginUser();
        jobExpList = alarmJobService.jobList(page, keywords, beginTime, endTime, jobStatusStr, alarmTypeStr, alarmContentStr, dataScope, currentUser.getId());
        new AlarmJobWarpper(jobExpList).warp();
        FileInputStream fileInputStream = null;
        if (CollectionUtil.isEmpty(jobExpList)) {
            fileInputStream = new FileInputStream(templatePath + "/jobRecEmpty.xlsx");
        } else {
            fileInputStream = new FileInputStream(templatePath + "/jobRecTemplate.xlsx");
        }
        try {
            httpServletResponse.setContentType("application/octet-stream");
            httpServletResponse.addHeader("Content-Disposition", " attachment;filename=" + "jobOverview.xlsx");

            Map<String, Object> var = new HashMap<>();
            var.put("标题", "工单一览表");
            var.put("list", jobExpList);
            ExcelIO.writeTemplate(fileInputStream, httpServletResponse.getOutputStream(), var);

        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            httpServletResponse.getOutputStream().flush();
            httpServletResponse.getOutputStream().close();
            fileInputStream.close();
        }
        ;
    }

    /**
     * 接收工单
     */
    @RequestMapping(value = "/getJob")
    @ResponseBody
    public Object getJob(int id) {

        AuthUser currentUser = iCommonPermissionService.getAuthService().getLoginUser();

        if (alarmJobService.getJob(id, currentUser.getId())) {
            return ResponseData.success();
        } else {
            return ResponseData.error("接单失败");
        }
    }

    /**
     * 确认工单,并保存图片
     */
    @RequestMapping(value = "/confirmJob")
    @ResponseBody
    public Object confirmJob(@RequestParam(value = "id", required = true) int id,
                             @RequestParam(value = "firstState", required = true) String firstState,
                             @RequestParam(value = "firstStatePhotos", required = true) String firstStatePhotos,
                             @RequestParam(value = "needHandle", required = true) String needHandle) {
        AuthUser currentUser = iCommonPermissionService.getAuthService().getLoginUser();
        if (alarmJobService.confirmJob(id, currentUser.getId(), firstState, firstStatePhotos, needHandle)) {
            return ResponseData.success();
        } else {
            return ResponseData.error("确认失败");
        }
    }

    /**
     * 转单
     */
    @RequestMapping(value = "/transferJob")
    @ResponseBody
    public Object transferJob(@RequestParam(value = "id", required = true) int id,
                              @RequestParam(value = "transferPerson", required = true) String transferPerson) throws JSONException {

        AuthUser currentUser = iCommonPermissionService.getAuthService().getLoginUser();

        String dbTime = iSysDictService.getDBtime();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("from", currentUser.getName());
        jsonObject.put("to", EhcacheConstant.retBean().getUsernameById(Long.valueOf(transferPerson)));
        jsonObject.put("time", dbTime);

        if (alarmJobService.transferJob(id, Long.valueOf(transferPerson), jsonObject.toString())) {
            return ResponseData.success();
        } else {
            return ResponseData.error("转单失败");
        }
    }

    /**
     * 保存工单信息及图片
     */
    @RequestMapping(value = "/saveJob")
    @ResponseBody
    public Object saveJob(@RequestParam(value = "id", required = true) int id,
                          @RequestParam(value = "handleMessage", required = true) String handleMessage,
                          @RequestParam(value = "handlePhotos", required = true) String handlePhotos) {

        if (alarmJobService.saveJob(id, handleMessage, handlePhotos)) {
            return ResponseData.success();
        } else {
            return ResponseData.error("保存失败");
        }
    }

    /**
     * 结束工单并消警
     */
    @RequestMapping(value = "/overJob")
    @ResponseBody
    public Object overJob(@RequestParam(value = "id", required = true) int id,
                          @RequestParam(value = "handleMessage", required = true) String handleMessage,
                          @RequestParam(value = "handlePhotos", required = true) String handlePhotos) {

        AuthUser currentUser = iCommonPermissionService.getAuthService().getLoginUser();

        if (alarmJobService.overJob(id, currentUser.getId(), handleMessage, handlePhotos)) {
            return ResponseData.success();
        } else {
            return ResponseData.error("保存失败");
        }
    }


    /**
     * 确认工单
     */
    @RequestMapping(value = "/handleJob")
    @ResponseBody
    public Object handleJob(@RequestParam(value = "id", required = true) int id) {
        AuthUser currentUser = iCommonPermissionService.getAuthService().getLoginUser();
        if (alarmJobService.handleJob(id, currentUser.getId())) {
            return ResponseData.success();
        } else {
            return ResponseData.error("保存失败");
        }
    }

    /**
     * 工单详情
     */
    @RequestMapping(value = "/info")
    @ResponseBody
    public Object jobInfo(@RequestParam(value = "id", required = true) int id) {

        DataScope dataScope = iCommonPermissionService.getAuthService().getLoginUserDataScope();
        AuthUser currentUser = iCommonPermissionService.getAuthService().getLoginUser();
        List<Map<String, Object>> retList = alarmJobService.jobInfo(id, dataScope, currentUser.getId());

        new AlarmJobWarpper(retList).warp();
        return ResponseData.success(retList);
    }


    /***
     * 更新sink生成工单的接单时间，及建立超期未接工单的处理任务
     * @param id
     * @return
     */
    @RequestMapping(value = "/updateSinkJob")
    @ResponseBody
    public Map<String, Object> updateSinkJob(@RequestParam(value = "id", required = true) String id,
                                             @RequestParam(value = "msg", required = true) String msg,
                                             @RequestParam(value = "wellCode", required = true) String wellCode) {
        Map<String, Object> retMap = new HashMap<>();
        try {
            alarmJobService.updateSinkJob(id, URLDecoder.decode(msg, "UTF-8"), wellCode);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return retMap;
    }


}
