package cn.yw.repairmanage.action;


import cn.base.aspect.OperateType;
import cn.base.bean.BaseAction;
import cn.base.excel.ExcelCol;
import cn.base.excel.ExportEngine;
import cn.base.pdf.ExportPDF;
import cn.base.util.*;
import cn.bsystem.ptuserrole.model.PtyhBean;
import cn.yw.alarm.service.YwalarmeventinfoService;
import cn.yw.detector.service.DetectorService;
import cn.yw.machinemanage.service.YwmachinecurrentstateService;
import cn.yw.nhyw.nhywjtledydpgl.service.NhywjtledydpglService;
import cn.yw.nhyw.nhywjtxhdgl.service.NhywjtxhdglService;
import cn.yw.repairmanage.service.YwrepairjobinfoService;
import cn.yw.repairmanage.service.YwrepairjoboplogService;
import cn.yw.video.videostate.service.YwvideocurrentstateService;
import cn.yw.zxld.service.ZxldService;
import jxl.write.DateTime;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.SQLException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

//import cn.base.excel.ExcelReader;

@SuppressWarnings({"all"})
@Component("ywrepairjobinfoAction")
public class YwrepairjobinfoAction extends BaseAction {
    private static final long serialVersionUID = 1L;
    private static Logger iLog = Logger.getLogger(YwrepairjobinfoAction.class);

    @Autowired
    private YwrepairjobinfoService service;
    @Autowired
    private YwrepairjoboplogService ywrepairjoboplogService;
    @Autowired
    private YwvideocurrentstateService ywvideocurrentstateService;
    @Autowired
    private YwmachinecurrentstateService ywmachinecurrentstateService;
    @Autowired
    private YwalarmeventinfoService ywalarmeventinfoService;

    @Autowired
    private NhywjtledydpglService nhywjtledydpglService;
    @Autowired
    private DetectorService detectorService;

    @Autowired
    private NhywjtxhdglService nhywjtxhdglService;

    @Autowired
    private ZxldService zxldService;

    /**
     * 分页
     */
    @OperateType(value = LogConstants.LOG_QUERY, info = "listYwrepairjobinfoByPage")
    public void listYwrepairjobinfoByPage() {
        Map map = Struts2Utils.getRequestParameterMap();
        try {
            PtyhBean ptyh = (PtyhBean) Struts2Utils.getSession().getAttribute("userInfo");
            String yhbh = ptyh.getYhbh();
            String bmbh = ptyh.getBmbh();
            String yhzh = ptyh.getYhzh();
            Integer sfgly = ptyh.getSfgly();
            String flag = map.get("flag") + "";
            if ("null".equals(flag) || StringUtils.isEmpty(flag) || "my".equals(flag)) {  //待办工单--我的工单
                //map.put("jobreportcompany", bmbh);
                map.put("jobreportpeople", yhzh);
                map.put("jobprocessstate", 901);
            } else if ("accept".equals(flag)) {  //待办工单--待受理
                if (sfgly != 1) { //管理员查询所有待办工单，非管理员查看本人的工单
                    //map.put("jobacceptcompany", bmbh);
                    map.put("jobacceptpeople", yhzh);
                }
                map.put("jobprocessstates", " jobprocessstate in ('901','904')");
            } else if ("feedback".equals(flag)) {    //待办工单--待反馈工单
                if (sfgly != 1) { //管理员查询所有待办工单，非管理员查看本人的工单
                    //map.put("feedbackcompany", bmbh);
                    map.put("feedbackpeople", yhzh);
                }
                map.put("jobprocessstates", " jobprocessstate in ('902','913')"); // TODO jobprocessstates
            } else if ("confirm".equals(flag)) {    //待办工单--反馈待确认工单
                if (sfgly != 1) { //管理员查询所有待办工单，非管理员查看本人的工单
                    //map.put("confirmcompany", bmbh);
                    map.put("confirmpeople", yhzh);
                }
                map.put("jobprocessstates", " jobprocessstate in ('906','916')");
            } else if ("check".equals(flag)) {    //待办工单--待驗收工單
                if (sfgly != 1) { //管理员查询所有待办工单，非管理员查看本人的工单
                    //map.put("confirmcompany", bmbh);
                    map.put("receiverpeople", yhzh);
                }
                map.put("jobprocessstate", 911);
            } else if ("delay".equals(flag)) {        //待办工单--延期待审批工单
                if (sfgly != 1) { //管理员查询所有待办工单，非管理员查看本人的工单
                    //map.put("delayforcompany", bmbh);
                    map.put("delayforpeople", yhzh);
                }
                map.put("jobprocessstate", 908);
            } else if ("todo".equals(flag)) { //首页待办事项中显示
                if (sfgly == 1) { //管理员查询所有待办工单
                    //901：工单上报-待受理；902：维修中-已受理-待反馈；904：挂起；906：已反馈待确认；908：延期申请待审核；913：维修确认不通过
                    String comprehensive = " jobprocessstate in ('901','902','904','906','908','913')";
                    map.put("comprehensive", comprehensive);
                } else { //查询当前用户的待办工单
                    String comprehensive = " (extend1='" + yhzh + "') "
                            + " and jobprocessstate in ('901','902','904','906','908','913')";
                    map.put("comprehensive", comprehensive);
                }
				/*
				String comprehensive = " ((jobreportcompany='"+bmbh+"' and jobreportpeople='"+yhzh+"') "
						+" or (jobacceptcompany='"+bmbh+"' and jobacceptpeople='"+yhzh+"') "
						+" or (feedbackcompany='"+bmbh+"' and feedbackpeople='"+yhzh+"') "
						+" or (delayforcompany='"+bmbh+"' and delayforpeople='"+yhzh+"') "
						+" or (confirmcompany='"+bmbh+"' and confirmpeople='"+yhzh+"') ) "
						+ " and jobprocessstate in ('901','902','904','906',908)";
				*/

            } else {//综合查询
                //管理员查询所有
                if(ptyh.getSfgly()!=1) {

                    String comprehensive = " ((jobreportcompany='" + bmbh + "' and jobreportpeople='" + yhzh + "') "
                            + " or (jobacceptcompany='" + bmbh + "' and jobacceptpeople='" + yhzh + "') "
                            + " or (feedbackcompany='" + bmbh + "' and feedbackpeople='" + yhzh + "') "
                            + " or (delayforcompany='" + bmbh + "' and delayforpeople='" + yhzh + "') "
                            + " or (confirmcompany='" + bmbh + "' and confirmpeople='" + yhzh + "')"
                            + " or (receivercompany='" + bmbh + "' and receiverpeople='" + yhzh + "') )";
                    map.put("comprehensive", comprehensive);
                }
            }

            int page = Integer.parseInt(Struts2Utils.getParameter("page"));
            int rows = Integer.parseInt(Struts2Utils.getParameter("rows"));
            int start = ((page == 0 ? 1 : page) - 1) * rows;
            map.put("start", start);
            map.put("limit", rows);
            Struts2Utils.renderJson(this.service.listYwrepairjobinfoByPage(map));
        } catch (Exception e) {
            e.printStackTrace();
            iLog.error("ywrepairjobinfoAction-listYwrepairjobinfoByPage-error:" + e);
            Struts2Utils.renderJson(null);
        }
    }

    /**
     * 查询总数
     */
    //@OperateType(value = LogConstants.LOG_QUERY ,info ="count_Ywrepairjobinfo")
    public void count_Ywrepairjobinfo() {
        Map map = Struts2Utils.getRequestParameterMap();
        try {
            Struts2Utils.renderJson(this.service.count_Ywrepairjobinfo(map));
        } catch (Exception e) {
            e.printStackTrace();
            iLog.error("YwrepairjobinfoAction-count_Ywrepairjobinfo-error:" + e);
            Struts2Utils.renderJson(null);
        }
    }

    /**
     * 查询 Ywrepairjobinfo
     */
    @OperateType(value = LogConstants.LOG_GET, info = "query_Ywrepairjobinfo")
    public void query_Ywrepairjobinfo() {
        Map map = Struts2Utils.getRequestParameterMap();
        try {
            Struts2Utils.renderJson(this.service.query_Ywrepairjobinfo(map));
        } catch (Exception e) {
            e.printStackTrace();
            iLog.error("ywrepairjobinfoAction-query_Ywrepairjobinfo-error:" + e);
            Struts2Utils.renderJson(null);
        }
    }

    /**
     * 查询 一条Ywrepairjobinfo
     */
    @OperateType(value = LogConstants.LOG_GET, info = "get_Ywrepairjobinfo")
    public void get_Ywrepairjobinfo() {
        Map map = Struts2Utils.getRequestParameterMap();
        try {
            // 获取用户
            PtyhBean ptyh = (PtyhBean) Struts2Utils.getSession().getAttribute("userInfo");
            map.put("yhbh", ptyh.getYhbh());
            Map ywrepairjobinfoMap = this.service.get_Ywrepairjobinfo(map);
            if (ywrepairjobinfoMap != null) {
                ywrepairjobinfoMap.put("yhbh", ptyh.getYhbh());
                ywrepairjobinfoMap.put("yhzh", ptyh.getYhzh());
            }
            Struts2Utils.renderJson(ywrepairjobinfoMap);
        } catch (Exception e) {
            e.printStackTrace();
            iLog.error("YwrepairjobinfoActionquery_Ywrepairjobinfo-error:" + e);
            Struts2Utils.renderJson(null);
        }
    }

    /**
     * 删除
     */
    @OperateType(value = LogConstants.LOG_DELETE, info = "deleteYwrepairjobinfo")
    public void delete_Ywrepairjobinfo() {
        Map map = Struts2Utils.getRequestParameterMap();
        try {
            if (!CommUtils.isEmpty(map)) {
                int result = service.delete_Ywrepairjobinfo(map);
                Struts2Utils.renderJson(Constants.SUCCESS);
            } else {
                throw new Exception("请正确的选择删除记录!!!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            iLog.error("ywrepairjobinfoAction-delete_Ywrepairjobinfo-error:" + e);
            Struts2Utils.renderJson(null);
        }
    }

    /**
     * 插入
     */
    @OperateType(value = LogConstants.LOG_ADD, info = "insert_Ywrepairjobinfo")
    public void insert_Ywrepairjobinfo() {
        Map map = Struts2Utils.getRequestParameterMap();
        try {
            PtyhBean ptyh = (PtyhBean) Struts2Utils.getSession().getAttribute("userInfo");
            String username = ptyh.getYhzh();
            map.put("createaccount", username);
            map.put("modifyaccount", username);
            map.put("createtime", DateUtil.currentDateTime());
            map.put("modifytime", DateUtil.currentDateTime());
            String jobid = null;
            if (StringUtil.checkEmpty(map.get("jobid"))) {
                jobid = StringUtil.getUUID();
                map.put("jobid", jobid);
            } else {
                jobid = map.get("jobid").toString();
            }
            service.insert_Ywrepairjobinfo(map);
            // 向 日志表添加记录  ywrepairjoboplog
            String faultdescription = map.get("faultdescription") == null ? "" : String.valueOf(map.get("faultdescription"));
            insert_repairjoboplog(jobid, "901", faultdescription, ptyh);
			/*Map logMap = new HashMap();
			logMap.put("logId", StringUtil.getUUID());
			logMap.put("jobid", jobid);
			logMap.put("processstate", "901");
			logMap.put("processtime", DateUtil.currentDateTime());
			logMap.put("processcompany", ptyh.getBmbh());
			logMap.put("processpeople", username);
			logMap.put("processphone", ptyh.getSjhm());
			logMap.put("processdescription", map.get("faultdescription"));
			logMap.put("createaccount", username);
			logMap.put("modifyaccount", username);
			logMap.put("createtime", DateUtil.currentDateTime());
			logMap.put("modifytime", DateUtil.currentDateTime());
			ywrepairjoboplogService.insert_Ywrepairjoboplog(logMap);*/

            // 更新 ywvideocurrentstate  或    ywmachinecurrentstate + ywalarmeventinfo 表
            String alarmdevicetype = map.get("faultdevicetype") == null ? "" : String.valueOf(map.get("faultdevicetype"));
            String alarmdeviceid = map.get("faultdeviceid") == null ? "" : String.valueOf(map.get("faultdeviceid"));
            String channelnum = map.get("channelnum") == null ? "" : String.valueOf(map.get("channelnum"));
            String repairstate = "901";
            String alarmeventid = map.get("alarmeventid") == null ? "" : String.valueOf(map.get("alarmeventid"));
            updateState(alarmdevicetype, repairstate, alarmdeviceid, channelnum, alarmeventid);

            Struts2Utils.renderJson(Constants.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            iLog.error("ywrepairjobinfoAction-insert_Ywrepairjobinfo()-error:" + e);
            Struts2Utils.renderJson(null);
        }
    }


    /**
     * 修改
     */
    @OperateType(value = LogConstants.LOG_UPDATE, info = "update_Ywrepairjobinfo")
    public void update_Ywrepairjobinfo() {
        Map map = Struts2Utils.getRequestParameterMap();
        try {
            if (!StringUtil.checkEmpty(map.get("checkparam"))) {
                Map sMap = new HashMap();
                sMap.put("tableparam", map.get("checkparam"));
                List ls = service.query_Ywrepairjobinfo(sMap);
                if (!StringUtil.checkEmpty(ls) || ls.size() > 0) {
                    Struts2Utils.renderJson(Constants.SAME);
                } else {
                    int result = service.update_Ywrepairjobinfo(map);
                    Struts2Utils.renderJson(Constants.SUCCESS);
                }
            } else {
                int result = service.update_Ywrepairjobinfo(map);
                Struts2Utils.renderJson(Constants.SUCCESS);
            }
        } catch (Exception e) {
            e.printStackTrace();
            iLog.error("YwrepairjobinfoAction-update_Ywrepairjobinfo-error: " + e);
            Struts2Utils.renderJson(null);
        }
    }

    /**
     * 批量删除
     */
    @OperateType(value = LogConstants.LOG_DELETE, info = "batch_delete_Ywrepairjobinfo")
    public void batch_delete_Ywrepairjobinfo() {
        Map map = Struts2Utils.getRequestParameterMap();
        try {
            Map m = new HashMap();
            m.put("keyword", map.get("keyword").toString());
            int result = service.batch_delete_Ywrepairjobinfo(m);
            Struts2Utils.renderJson(Constants.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            iLog.error("YwrepairjobinfoAction-batch_delete_Ywrepairjobinfo-error:" + e);
            Struts2Utils.renderJson(null);
        }
    }


    // 查询用户相关信息
    public void getUserInfo() {
        Map map = Struts2Utils.getRequestParameterMap();
        try {
            PtyhBean userInfo = (PtyhBean) Struts2Utils.getSession().getAttribute("userInfo");
            String yhbh = userInfo.getYhbh();
            map.put("yhbh", yhbh);
            Map user = service.getUserInfo(map);
            Struts2Utils.renderJson(user);
        } catch (Exception e) {
            e.printStackTrace();
            iLog.error("YwrepairjobinfoAction-getUserInfo-error: " + e);
            Struts2Utils.renderJson(null);
        }
    }

    // 有权限审批工单的人所在的部门
    public void getReportBm() {
        Map map = Struts2Utils.getRequestParameterMap();
        try {
            List list = service.getReportBm(map);
            Struts2Utils.renderJson(list);
        } catch (Exception e) {
            e.printStackTrace();
            iLog.error("ywrepairjobinfoAction-getUserInfo-error: " + e);
            Struts2Utils.renderJson(null);
        }
    }

    // 有权限审批工单的人
    public void getReportUser() {
        Map map = Struts2Utils.getRequestParameterMap();
        try {
            List list = service.getReportUser(map);
            Struts2Utils.renderJson(list);
        } catch (Exception e) {
            e.printStackTrace();
            iLog.error("YwrepairjobinfoAction-getReportUser-error: " + e);
            Struts2Utils.renderJson(null);
        }
    }


    /**
     * 更新状态     监控设备 或 服务器设备 + ywalarmeventinfo 表状态
     *
     * @param alarmdevicetype 设备类型  1：监控    2：服务器
     * @param jobacceptstate  处理状态
     * @param deviceId        设备编号
     * @param channelnum      通道编号
     * @param eventId         告警事件id
     * @throws SQLException
     */
    private void updateState(String alarmdevicetype, String jobacceptstate, String deviceId,
                             String channelnum, String eventId) throws SQLException {

        PtyhBean ptyh = (PtyhBean) Struts2Utils.getSession().getAttribute("userInfo");
        if ("1".equals(alarmdevicetype)) {                // 监控设备
            Map videoMap = new HashMap();
            videoMap.put("XGTJvideonum", deviceId);
            videoMap.put("XGTJchannelnum", channelnum);
            videoMap.put("repairstate", jobacceptstate);
            videoMap.put("modifyaccount", ptyh.getYhzh());
            videoMap.put("modifytime", DateUtil.currentDateTime());
            ywvideocurrentstateService.update_Ywvideocurrentstate(videoMap);
        } else if ("2".equals(alarmdevicetype)) {            // 服务器设备
            Map machineMap = new HashMap();
            machineMap.put("XGTJmachineid", deviceId);
            machineMap.put("repairstate", jobacceptstate);
            machineMap.put("modifyaccount", ptyh.getYhzh());
            machineMap.put("modifytime", DateUtil.currentDateTime());
            ywmachinecurrentstateService.update_Ywmachinecurrentstate(machineMap);
        } else if ("3".equals(alarmdevicetype)) {            // 信号灯
            Map machineMap = new HashMap();
            machineMap.put("XGTJxh", deviceId);
            machineMap.put("repairstate", jobacceptstate);
            nhywjtxhdglService.update_NhyxhdCurrentState(machineMap);
        } else if ("4".equals(alarmdevicetype)) {            // 诱导屏
            Map machineMap = new HashMap();
            machineMap.put("XGTJxh", deviceId);
            machineMap.put("repairstate", jobacceptstate);
            nhywjtledydpglService.update_NhywledydpCurrentState(machineMap);
        } else if ("10".equals(alarmdevicetype) || "11".equals(alarmdevicetype)) {            // "10"://地磁,"11"://微波
            Map machineMap = new HashMap();
            if ("10".equals(alarmdevicetype)) {
                machineMap.put("detectortype", "GVD");
            } else if ("11".equals(alarmdevicetype)) {
                machineMap.put("detectortype", "SMARTSENSOR");
            }
            machineMap.put("XGTJid", deviceId);
            machineMap.put("repairstate", jobacceptstate);
            detectorService.update_DetectorCurrentState(machineMap);
        } else if ("12".equals(alarmdevicetype)) {            // 雷达
            Map machineMap = new HashMap();
            machineMap.put("XGTJxh", deviceId);
            machineMap.put("repairstate", jobacceptstate);
            zxldService.update_ZxldCurrentState(machineMap);
        }
        // 更新 ywalarmeventinfo 表状态
        Map eventMap = new HashMap();
        eventMap.put("XGTJalarmeventid", eventId);
        eventMap.put("processstatus", jobacceptstate);
        eventMap.put("modifyaccount", ptyh.getYhzh());
        eventMap.put("modifytime", DateUtil.currentDateTime());
        ywalarmeventinfoService.update_Ywalarmeventinfo(eventMap);

    }


    /**
     * 加入数据到 日志表   ywrepairjoboplog
     *
     * @param jobid              工单id
     * @param processstate       处理状态
     * @param processdescription 处理描述
     * @param ptyh               获取用户信息
     * @throws SQLException
     */
    private void insert_repairjoboplog(String jobid, String processstate,
                                       String processdescription, PtyhBean ptyh) throws SQLException {
        Map logMap = new HashMap();
        logMap.put("logId", StringUtil.getUUID());
        logMap.put("jobid", jobid);
        logMap.put("processstate", processstate);
        logMap.put("processtime", DateUtil.currentDateTime());
        logMap.put("processcompany", ptyh.getBmbh());
        logMap.put("processpeople", ptyh.getYhzh());
        logMap.put("processphone", ptyh.getSjhm());
        logMap.put("processdescription", processdescription);
        logMap.put("createaccount", ptyh.getYhzh());
        logMap.put("modifyaccount", ptyh.getYhzh());
        logMap.put("createtime", DateUtil.currentDateTime());
        logMap.put("modifytime", DateUtil.currentDateTime());
        ywrepairjoboplogService.insert_Ywrepairjoboplog(logMap);
    }

    /**
     * 工单处理-更新
     */
    @OperateType(value = LogConstants.LOG_UPDATE, info = "updateWorkOrderInfo")
    public void updateWorkOrderInfo() {
        Map map = Struts2Utils.getRequestParameterMap();
        try {
            PtyhBean userInfo = (PtyhBean) Struts2Utils.getSession().getAttribute("userInfo");
            String userName = userInfo.getYhzh();
            String bmbh = userInfo.getBmbh();
            String now = DateUtil.currentDateTime();
            //当前表
            String deviceType = map.get("faultdevicetype") + "";
            if (!("null".equals(deviceType) || StringUtils.isEmpty(deviceType))) {
				/*if("1".equals(deviceType)){//监控
					ywvideocurrentstateService.update_Ywvideocurrentstate_WorkOrder(map);
				}else if("2".equals(deviceType)){//服务器
					ywmachinecurrentstateService.update_Ywmachinecurrentstate_WorkOrder(map);
				}*/
                updateState(deviceType, StringUtil.transEmpty(map.get("processstatus") + ""),
                        StringUtil.transEmpty(map.get("XGTJfaultdeviceid") + ""),
                        StringUtil.transEmpty(map.get("XGTJchannelnum") + ""),
                        StringUtil.transEmpty(map.get("XGTJalarmeventid") + ""));
            }
//			//告警表
//			ywalarmeventinfoService.update_Ywalarmeventinfo(map);
            //运维维修工单信息
            String jobprocessstate = map.get("jobprocessstate") + "";
            if (!("null".equals(jobprocessstate) || StringUtils.isEmpty(jobprocessstate))) {
                if ("911".equals(jobprocessstate)) {
                    map.put("actualfinishtime", now);
                    String jobreporttime = map.get("jobreporttime") + "";
                    if (!("null".equals(jobreporttime) || StringUtils.isEmpty(jobreporttime))) {
                        map.put("repairdays", DateUtil.subtractDay(jobreporttime, now));
                    }
                } else if ("904".equals(jobprocessstate)) {
                    // 挂起
                    // 挂起人=当前用户
                    map.put("hanguppeople", userInfo.getYhbh());
                    // 挂起时间=当前时间
//					map.put("hanguptime", now);
                    // 挂起人单位=当前用户单位
                    map.put("hangupcompany", userInfo.getBmbh());
                } else if ("905".equals(jobprocessstate)) {
                    // 取消挂起
                    // 取消挂起人=当前用户
                    map.put("unhanguppeople", userInfo.getYhbh());
                    // 取消挂起时间=当前时间
//					map.put("unhanguptime", now);
                    // 取消挂起人单位=当前用户单位
                    map.put("unhangupcompany", userInfo.getBmbh());
                }
            }
            map.put("modifyaccount", userName);
            map.put("modifytime", now);
            map.put("XGTJchannelnum", StringUtil.transEmpty(map.get("XGTJchannelnum") + ""));
            service.update_Ywrepairjobinfo(map);
            //工单日志
            Map<String, Object> insertLog = new HashMap<String, Object>();
            insertLog.put("logId", StringUtil.getUUID());
            insertLog.put("jobid", map.get("XGTJjobid"));
            insertLog.put("processstate", map.get("processstatus"));
            insertLog.put("processtime", now);
            insertLog.put("createaccount", userName);
            insertLog.put("createtime", now);
            insertLog.put("processcompany", bmbh);
            insertLog.put("processpeople", userName);
            insertLog.put("processdescription", map.get("processdescription"));
            ywrepairjoboplogService.insert_Ywrepairjoboplog(insertLog);
            Struts2Utils.renderJson(Constants.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            iLog.error("YwrepairjobinfoAction-updateWorkOrderInfo-error: " + e);
            Struts2Utils.renderJson(null);
        }
    }

    @OperateType(value = LogConstants.LOG_UPDATE, info = "edit_confirmOrder")
    public void edit_confirmOrder() {
        Map map = Struts2Utils.getRequestParameterMap();
        try {

            // 更新 Ywrepairjobinfo 表
            PtyhBean ptyh = (PtyhBean) Struts2Utils.getSession().getAttribute("userInfo");
            map.put("modifyaccount", ptyh.getYhzh());
            map.put("modifytime", DateUtil.currentDateTime());
            int result = service.update_Ywrepairjobinfo(map);

            String repairstate = null;
            String processdescription = null;
            String flag = String.valueOf(map.get("flag"));
            if ("feedback".equals(flag)) {
                repairstate = map.get("jobprocessstate") == null ? "" : String.valueOf(map.get("jobprocessstate"));
                processdescription = map.get("delaycause") == null ? "" : String.valueOf(map.get("delaycause"));
            } else if ("delay".equals(flag)) {
                repairstate = map.get("delayforstate") == null ? "" : String.valueOf(map.get("delayforstate"));
                processdescription = map.get("delayfordescription") == null ? "" : String.valueOf(map.get("delayfordescription"));
            }
            // 更新当前状态
            String alarmdevicetype = map.get("faultdevicetype") == null ? "" : String.valueOf(map.get("faultdevicetype"));
            String alarmdeviceid = map.get("faultdeviceid") == null ? "" : String.valueOf(map.get("faultdeviceid"));
            String channelnum = map.get("channelnum") == null ? "" : String.valueOf(map.get("channelnum"));
            String alarmeventid = map.get("alarmeventid") == null ? "" : String.valueOf(map.get("alarmeventid"));
            updateState(alarmdevicetype, repairstate, alarmdeviceid, channelnum, alarmeventid);

            // 向日志表增加记录
//			PtyhBean ptyh = (PtyhBean)Struts2Utils.getSession().getAttribute("userInfo");
            String jobid = String.valueOf(map.get("XGTJjobid"));
            insert_repairjoboplog(jobid, repairstate, processdescription, ptyh);

            Struts2Utils.renderJson(Constants.SUCCESS);

        } catch (Exception e) {
            e.printStackTrace();
            iLog.error("YwrepairjobinfoAction-edit_confirmOrder-error: " + e);
            Struts2Utils.renderJson(null);
        }
    }

    /**
     * 导出Excel
     */
    @OperateType(value = LogConstants.LOG_EXPORT, info = "exp_Ywrepairjobinfo")
    public void exp_Ywrepairjobinfo() {
        try {
            Map map = Struts2Utils.getRequestParameterMap();
            Map queryMap = JSONObject.fromObject(map.get("downParam"));
            PtyhBean ptyh = (PtyhBean) Struts2Utils.getSession().getAttribute("userInfo");
            String yhbh = ptyh.getYhbh();
            String bmbh = ptyh.getBmbh();
            String yhzh = ptyh.getYhzh();
            Integer sfgly = ptyh.getSfgly();
            String flag = queryMap.get("flag") + "";
            if ("null".equals(flag) || StringUtils.isEmpty(flag) || "my".equals(flag)) {  //待办工单--我的工单
                //map.put("jobreportcompany", bmbh);
                queryMap.put("jobreportpeople", yhzh);
                queryMap.put("jobprocessstate", 901);
            } else if ("accept".equals(flag)) {  //待办工单--待受理
                if (sfgly != 1) { //管理员查询所有待办工单，非管理员查看本人的工单
                    //map.put("jobacceptcompany", bmbh);
                    queryMap.put("jobacceptpeople", yhzh);
                }
                queryMap.put("jobprocessstates", " jobprocessstate in ('901','904')");
            } else if ("feedback".equals(flag)) {    //待办工单--待反馈工单
                if (sfgly != 1) { //管理员查询所有待办工单，非管理员查看本人的工单
                    //map.put("feedbackcompany", bmbh);
                    queryMap.put("feedbackpeople", yhzh);
                }
                queryMap.put("jobprocessstates", " jobprocessstate in ('902','913')"); // TODO jobprocessstates
            } else if ("confirm".equals(flag)) {    //待办工单--反馈待确认工单
                if (sfgly != 1) { //管理员查询所有待办工单，非管理员查看本人的工单
                    //map.put("confirmcompany", bmbh);
                    queryMap.put("confirmpeople", yhzh);
                }
                queryMap.put("jobprocessstate", 906);
            } else if ("delay".equals(flag)) {        //待办工单--延期待审批工单
                if (sfgly != 1) { //管理员查询所有待办工单，非管理员查看本人的工单
                    //map.put("delayforcompany", bmbh);
                    queryMap.put("delayforpeople", yhzh);
                }
                queryMap.put("jobprocessstate", 908);
            } else if ("todo".equals(flag)) { //首页待办事项中显示
                if (sfgly == 1) { //管理员查询所有待办工单
                    //901：工单上报-待受理；902：维修中-已受理-待反馈；904：挂起；906：已反馈待确认；908：延期申请待审核；913：维修确认不通过
                    String comprehensive = " jobprocessstate in ('901','902','904','906','908','913')";
                    queryMap.put("comprehensive", comprehensive);
                } else { //查询当前用户的待办工单
                    String comprehensive = " (extend1='" + yhzh + "') "
                            + " and jobprocessstate in ('901','902','904','906','908','913')";
                    queryMap.put("comprehensive", comprehensive);
                }
            } else {//综合查询
                String comprehensive = " ((jobreportcompany='" + bmbh + "' and jobreportpeople='" + yhzh + "') "
                        + " or (jobacceptcompany='" + bmbh + "' and jobacceptpeople='" + yhzh + "') "
                        + " or (feedbackcompany='" + bmbh + "' and feedbackpeople='" + yhzh + "') "
                        + " or (delayforcompany='" + bmbh + "' and delayforpeople='" + yhzh + "') "
                        + " or (receivercompany='" + bmbh + "' and receiverpeople='" + yhzh + "') "
                        + " or (confirmcompany='" + bmbh + "' and confirmpeople='" + yhzh + "') )";
                queryMap.put("comprehensive", comprehensive);
            }

            queryMap.put("start", 0);
            queryMap.put("limit", 5000);
            Map resMap = service.listYwrepairjobinfoByPage(queryMap);
            List<Map> sList = (List<Map>) resMap.get("rows");

            String[] zdbm = {"jobnum", "jobprocessstatename", "faultdevicetypename", "faultdevicename", "faultdeviceid", "channelnum", "faulttypename", "faultdescription", "jobreporttime"};
            String[] zdmc = {"工单编号", "工单状态", "故障设备类型", "故障设备名称", "故障设备编号", "通道编号", "故障类型", "故障描述", "上报时间"};
            int excelNum = zdbm.length;
            ExcelCol[] cells = new ExcelCol[excelNum];
            for (int i = 0; i < excelNum; i++) {
                cells[i] = new ExcelCol();
                cells[i].setColKey(zdbm[i]);
                cells[i].setColName(zdmc[i]);
            }
            new ExportEngine().exportCommExcel("工单查询", cells, sList, Struts2Utils.getResponse());
        } catch (Exception e) {
            e.printStackTrace();
            iLog.error("YwrepairjoboplogAction-exoprtExcel-error:" + e);

        }
    }

    //查询所有设备维修情况
    @OperateType(value = LogConstants.LOG_QUERY, info = "listYwrepairDevicejobinfoByPage")
    public void listYwrepairDevicejobinfoByPage() {
        Map map = Struts2Utils.getRequestParameterMap();
        try {
            int page = Integer.parseInt(Struts2Utils.getParameter("page"));
            int rows = Integer.parseInt(Struts2Utils.getParameter("rows"));
            int start = ((page == 0 ? 1 : page) - 1) * rows;

            map.put("start", start);
            map.put("limit", rows);
            Struts2Utils.renderJson(this.service.listYwrepairDevicejobinfoByPage(map));
        } catch (Exception e) {
            e.printStackTrace();
            iLog.error("YwrepairjobinfoAction-listYwrepairDevicejobinfoByPage-error:" + e);
            Struts2Utils.renderJson(null);
        }
    }

    @OperateType(value = LogConstants.LOG_EXPORT, info = "exp_YwrepairDevicejobinfo")
    public void exp_YwrepairDevicejobinfo() {
        try {
            Map map = Struts2Utils.getRequestParameterMap();
            map.put("start", 0);
            map.put("limit", 5000);
            Map resMap = service.listYwrepairjobinfoByPage(map);

            List<Map> sList = (List<Map>) resMap.get("rows");

            String[] zdbm = {"jobnum", "jobprocessstatename", "faultdevicetypename", "faultdevicename", "faultdeviceid", "channelnum", "faulttypename", "faultdescription", "jobreporttime"};
            String[] zdmc = {"工单编号", "工单状态", "故障设备类型", "故障设备名称", "故障设备编号", "通道编号", "故障类型", "故障描述", "上报时间"};
            int excelNum = zdbm.length;
            ExcelCol[] cells = new ExcelCol[excelNum];
            for (int i = 0; i < excelNum; i++) {
                cells[i] = new ExcelCol();
                cells[i].setColKey(zdbm[i]);
                cells[i].setColName(zdmc[i]);
            }
            new ExportEngine().exportCommExcel("工单查询", cells, sList, Struts2Utils.getResponse());
        } catch (Exception e) {
            e.printStackTrace();
            iLog.error("YwrepairjoboplogAction-exp_YwrepairDevicejobinfo-error:" + e);

        }
    }

    //统计
    @OperateType(value = LogConstants.LOG_QUERY, info = "countYwrepairjobinstent")
    public void countYwrepairjobinstent() {
        Map map = Struts2Utils.getRequestParameterMap();
        try {
            int page = Integer.parseInt(Struts2Utils.getParameter("page"));
            int rows = Integer.parseInt(Struts2Utils.getParameter("rows"));
            int start = ((page == 0 ? 1 : page) - 1) * rows;
            map.put("start", start);
            map.put("limit", rows);
            if (null != map.get("xzbh")) {
                String xzbh = map.get("xzbh").toString();
                map.put("xzbh", xzbh.split(","));
            }
            Struts2Utils.renderJson(this.service.countYwrepairjobinstent(map));
        } catch (Exception e) {
            e.printStackTrace();
            iLog.error("ywrepairjobinfoAction-listYwrepairDevicejobinfoByPage-error:" + e);
            Struts2Utils.renderJson(null);
        }
    }

    /**
     * 导出pdf
     */
    @OperateType(value = LogConstants.LOG_EXPORT, info = "expPDF_countYwrepairjobInstent")
    public void expPDF_countYwrepairjobInstent() {
        try {
            Map map = Struts2Utils.getRequestParameterMap();
            Map queryMap = JSONObject.fromObject(map.get("downParam"));
            PtyhBean ptyh = (PtyhBean) Struts2Utils.getSession().getAttribute("userInfo");
            String bmbh = ptyh.getBmbh();
            if (ptyh.getSfgly() != 1) {
                queryMap.put("bmbh", bmbh);
            }
            if (null != queryMap.get("xzbh")) {
                String xzbh = queryMap.get("xzbh").toString();
                queryMap.put("xzbh", xzbh.split(","));
            }
            queryMap.put("start", 0);
            queryMap.put("limit", 5000);
            List<Map> sList = service.query_Ywrepairjobinstent(queryMap);
            //表头
            String[] header = {"行政区划", "行政区划名称", "设备编号", "设备名称", "及时维护数", "未及时维护数", "及时维护率"};
            //内容
            String[][] expArr = null;
            if (RHUtil.isNotEmpty(sList)) {
                expArr = new String[sList.size() + 1][header.length];
                Integer zs = 0;
                Integer zhf = 0;
                String zhfl = "0";
                int index = 0;
                for (Map smap : sList) {
                    String zcount = smap.get("cnt") == null ? "0" : String.valueOf(smap.get("cnt"));
                    String hfcount = smap.get("uncnt") == null ? "0" : String.valueOf(smap.get("uncnt"));
                    String hfl = "0";
                    if ("0".equals(hfcount)) {
                        smap.put("hfl", hfl);
                    } else {
                        hfl = division(Integer.parseInt(hfcount), Integer.parseInt(zcount));
                        smap.put("hfl", hfl);
                    }
                    expArr[index][0] = StringUtil.transEmpty(smap.get("xzmc") + "");
                    expArr[index][1] = zcount;
                    expArr[index][2] = hfcount;
                    expArr[index][3] = hfl;
                    index++;
                    zs = zs + Integer.parseInt(zcount);
                    zhf = zhf + Integer.parseInt(hfcount);
                }
                if (zhf != 0) {
                    zhfl = division(zhf, zs);
                }
                expArr[index][0] = "总计";
                expArr[index][1] = zs + "";
                expArr[index][2] = zhf + "";
                expArr[index][3] = zhfl;
            }
            SimpleDateFormat formart = new SimpleDateFormat("yyyy-MM-dd");
            String dateTime = formart.format(new Date());
            Map params = new HashMap();
            params.put("fileName", "维护及时率统计-" + dateTime);
            params.put("sheetName", "维护及时率统计信息");
            params.put("header", "维护及时率统计(制表时间：" + DateUtil.currentDateTime() + ")");
            new ExportPDF().exportCommPDF(params, header, sList, 0, expArr, Struts2Utils.getResponse());
        } catch (Exception e) {
            e.printStackTrace();
            iLog.error("YwalarmcountAction-expPDF_Ywalarmcount-error:" + e);

        }
    }

    // 导出excel 点位录像
    @OperateType(value = LogConstants.LOG_EXPORT, info = "exp_countYwrepairjobInstent")
    public void exp_countYwrepairjobInstent() {
        try {
            Map map = Struts2Utils.getRequestParameterMap();
            Map queryMap = JSONObject.fromObject(map.get("downParam"));
            PtyhBean ptyh = (PtyhBean) Struts2Utils.getSession().getAttribute("userInfo");
            String bmbh = ptyh.getBmbh();
            if (ptyh.getSfgly() != 1) {
                queryMap.put("bmbh", bmbh);
            }
            if (null != queryMap.get("xzbh")) {
                String xzbh = queryMap.get("xzbh").toString();
                queryMap.put("xzbh", xzbh.split(","));
            }
            queryMap.put("start", 0);
            queryMap.put("limit", 5000);
            List<Map> sList = service.query_Ywrepairjobinstent(queryMap);
            if (null != sList && sList.size() > 0) {
                Map footMap = new HashMap();
                if (RHUtil.isNotEmpty(sList)) {
                    Integer zs = 0;
                    Integer zzcs = 0;
                    Integer zycs = 0;
                    String zwhl = "0";
                    for (Map smap : sList) {
                        String zcs = String.valueOf(smap.get("cnt")) == "null" ? "0" : String.valueOf(smap.get("cnt"));
                        String ycs = String.valueOf(smap.get("uncnt")) == "null" ? "0" : String.valueOf(smap.get("uncnt"));
                        String whl = "0";
                        Integer mcount = Integer.parseInt(zcs) + Integer.parseInt(ycs);
                        if (mcount != 0) {
                            whl = division(Integer.parseInt(zcs), mcount);
                        }
                        smap.put("whl", whl);
                        zs = zs + mcount;
                        zzcs = zzcs + Integer.parseInt(zcs);
                        zycs = zycs + Integer.parseInt(ycs);
                    }
                    // 总计
                    if (zs != 0) {
                        zwhl = division(zzcs, zs);
                    }
                    footMap.put("0", "总计");
                    footMap.put("1", "总计");
                    footMap.put("2", "总计");
                    footMap.put("3", "总计");
                    footMap.put("4", String.valueOf(zzcs));
                    footMap.put("5", String.valueOf(zycs));
                    footMap.put("6", String.valueOf(zwhl));
                }
                String[] zdbm = {"xzbh", "xzmc", "sbbh", "tdmc", "cnt", "uncnt", "whl"};
                String[] zdmc = {"行政区划", "行政区划名称", "设备编号", "设备名称", "及时维护数", "未及时维护数", "及时维护率"};
                int excelNum = zdmc.length;
                ExcelCol[] cells = new ExcelCol[excelNum];
                for (int i = 0; i < excelNum; i++) {
                    cells[i] = new ExcelCol();
                    cells[i].setColKey(zdbm[i]);
                    cells[i].setColName(zdmc[i]);
                }
                SimpleDateFormat formart = new SimpleDateFormat("yyyy-MM-dd");
                String dateTime = formart.format(new Date());
                Map params = new HashMap();
                params.put("fileName", "维护及时率统计情况-" + dateTime);
                params.put("sheetName", "维护及时率统计情况");
                params.put("header", "维护及时率统计情况");
                new ExportEngine().exportCommDataWithCellRange(params, cells, sList, footMap, Struts2Utils.getResponse(), 3);
            }
        } catch (Exception e) {
            e.printStackTrace();
            iLog.error("ywrepairjobinfoAction-exp_AreaOnline-error:" + e);

        }
    }

    public static String division(int num1, int num2) {
        String rate = "0.00%";
        //定义格式化起始位数
        String format = "0.00";
        if (num2 != 0 && num1 != 0) {
            DecimalFormat dec = new DecimalFormat(format);
            rate = dec.format((double) num1 / num2 * 100) + "%";
            while (true) {
                if (rate.equals(format + "%")) {
                    format = format + "0";
                    DecimalFormat dec1 = new DecimalFormat(format);
                    rate = dec1.format((double) num1 / num2 * 100) + "%";
                } else {
                    break;
                }
            }
        } else if (num1 != 0 && num2 == 0) {
            rate = "100%";
        }
        return rate;
    }

    /***
     *查询代办任务
     */
    @OperateType(value = LogConstants.LOG_EXPORT, info = "queryAgentTaskByPage")
    public void queryAgentTaskByPage() {
        Map map = Struts2Utils.getRequestParameterMap();
        String yhbh = "";
        //查询该用户下是否有未处理单号
        if (RHUtil.isNotEmpty(map.get("yhbh"))) {
            yhbh = String.valueOf(map.get("yhbh"));
        } else {
            PtyhBean ptyh = (PtyhBean) Struts2Utils.getSession().getAttribute("userInfo");
            if (null != ptyh) {
                yhbh = ptyh.getYhbh();
            }
        }
        //查询该用户名下单子，有就推送，没有不推送
        String comprehensive = " (extend1='" + yhbh + "') "
                + " and jobprocessstate in ('901','902','904','906','908','913')";
        map.put("comprehensive", comprehensive);
        map.put("tableparam", " and  jobreporttime <='" + DateUtil.currentDateTime() + "' and jobreporttime>='" + DateUtil.getDateTimeStr(DateUtil.day(-7)) + "'");
        try {
            Map resultmap = new HashMap();
            resultmap.put("total", service.count_Ywrepairjobinfo(map));
            resultmap.put("rows", service.query_Ywrepairjobinfo(map));
            Struts2Utils.renderJson(resultmap);
        } catch (Exception e) {
            e.printStackTrace();
            iLog.error("ywrepairjobinfoAction-queryAgentTaskByPage-error:" + e);
        }
    }

    /***
     * 新增到ksh_picture
     */

    @OperateType(value = LogConstants.LOG_QUERY, info = "insert_kshPicture")
    public void insert_kshPicture() {
        Map map = Struts2Utils.getRequestParameterMap();
        try {
            PtyhBean ptyh = (PtyhBean) Struts2Utils.getSession().getAttribute("userInfo");
            if (RHUtil.isNotEmpty(map.get("data"))) {
                List<Map> picturelist = JSONArray.toList(JSONArray.fromObject(map.get("data")),Map.class);
                // 遍历集合添加创建时间，创建人，objecttype
                List<Map> list=new ArrayList<Map>();
                for (Map pictureMap : picturelist) {
                    pictureMap.put("create_time",new Date() );
                    pictureMap.put("id", pictureMap.get("name"));
                    pictureMap.put("name", pictureMap.get("originalname"));
                    pictureMap.put("create_user ", ptyh.getYhzh());
                    pictureMap.put("object_type",map.get("type"));
                    pictureMap.put("object_id", map.get("jobid"));
                    list.add(pictureMap);
                }
                this.service.batch_insert_KSHPicture(list);
                //返回上传的结果
                Struts2Utils.renderJson(list);
            } else Struts2Utils.renderJson("内容为空!");
        }catch (Exception e) {
            e.printStackTrace();
            iLog.error("ywrepairjobinfoAction-insert_kshPicture-error:" + e);
        }
    }

    /***
     * 根据object_id 查询所有图片信息返回
     */

    @OperateType(value = LogConstants.LOG_QUERY, info = "listYwrepairjobinfoByPage")
    public void query_kshPictureByObjectId() {
        Map map = Struts2Utils.getRequestParameterMap();
        try {
            Struts2Utils.renderJson(this.service.query_kshPictureByObjectId(map));
        }catch (Exception e) {
            e.printStackTrace();
            iLog.error("ywrepairjobinfoAction-listYwrepairjobinfoByPage-error:" + e);
        }
    }


    /***
     * 根据id 删除图片信息
     */

    @OperateType(value = LogConstants.LOG_DELETE, info = "delete_kshPictureByID")
    public void delete_kshPictureByID() {
        Map map = Struts2Utils.getRequestParameterMap();
        try {
           this.service.delete_kshPictureByID(map);
            Struts2Utils.renderJson(Constants.SUCCESS);
        }catch (Exception e) {
            e.printStackTrace();
            iLog.error("ywrepairjobinfoAction-delete_kshPictureByID-error:" + e);
        }
    }


    //统计故障类型
    @OperateType(value = LogConstants.LOG_QUERY, info = "originStatistic")
    public void originStatistic() {
        Map map = Struts2Utils.getRequestParameterMap();
        try {
            Struts2Utils.renderJson(this.service.originStatistic(map));
        } catch (Exception e) {
            e.printStackTrace();
            iLog.error("ywrepairjobinfoAction-originStatistic-error:" + e);
            Struts2Utils.renderJson(null);
        }
    }

    /**
     * 统计每种设备最多故障类型
     */
    @OperateType(value = LogConstants.LOG_QUERY, info = "query_maxDevicetypeorigin")
    public void query_maxDevicetypeorigin() {
        Map map = Struts2Utils.getRequestParameterMap();
        try {
            Struts2Utils.renderJson(this.service.query_maxDevicetypeorigin(map));
        } catch (Exception e) {
            e.printStackTrace();
            iLog.error("ywrepairjobinfoAction-query_maxDevicetypeorigin-error:" + e);
            Struts2Utils.renderJson(null);
        }
    }

    /***
     * 查询接收到的文件
     */
    @OperateType(value = LogConstants.LOG_QUERY, info = "query_YwrepairjobinfoReceiver")
    public void query_YwrepairjobinfoReceiver() {
        Map map = Struts2Utils.getRequestParameterMap();
        try {
            Struts2Utils.renderJson(this.service.query_YwrepairjobinfoReceiver(map));
        } catch (Exception e) {
            e.printStackTrace();
            iLog.error("ywrepairjobinfoAction-query_YwrepairjobinfoReceiver-error:" + e);
            Struts2Utils.renderJson(null);
        }
    }
    /***
     * 查询所有处理状态
     */
    @OperateType(value = LogConstants.LOG_QUERY, info = "listjobprocessstate")
    public void listjobprocessstate() {
        try {
            Struts2Utils.renderJson(this.service.listjobprocessstate());
        } catch (Exception e) {
            e.printStackTrace();
            iLog.error("ywrepairjobinfoAction-listjobprocessstate-error:" + e);
            Struts2Utils.renderJson(null);
        }
    }


}
