package com.casic.missiles.modular.alarm.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.casic.missiles.core.base.response.ResponseData;
import com.casic.missiles.core.util.HttpClientUtils;
import com.casic.missiles.modular.alarm.service.IAlarmJobService;
import com.casic.missiles.core.datascope.DataScope;
import com.casic.missiles.core.util.EhcacheConstant;
import com.casic.missiles.core.util.ToolUtil;
import com.casic.missiles.modular.system.dao.AlarmJobMapper;
import com.casic.missiles.modular.system.model.AlarmJob;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 * 报警工单表 服务实现类
 * </p>
 *
 * @author casic123
 * @since 2019-05-17
 */
@Service
public class AlarmJobServiceImpl extends ServiceImpl<AlarmJobMapper, AlarmJob> implements IAlarmJobService {
    private static final Logger logger = LoggerFactory.getLogger(AlarmJobServiceImpl.class);

    @Override
    public List<AlarmJob> jobListApp(Page<AlarmJob> page, String keywords, String beginTime, String endTime, String jobStatus, String alarmType, String alarmContent,long deptId,long userId,long leaderId,DataScope dataScope)
    {
        String sContent = null;
        if (ToolUtil.isNotEmpty(alarmContent))
            sContent = EhcacheConstant.retBean().getAlarmContentName(Integer.valueOf(alarmContent));
        return this.baseMapper.jobListApp(page, keywords, beginTime, endTime, jobStatus, alarmType, sContent,deptId,userId,leaderId,dataScope);
    }
    @Override
    public List<AlarmJob> jobList(Page<AlarmJob> page, String keywords, String beginTime, String endTime, String jobStatus, String alarmType, String alarmContent,DataScope dataScope)
    {
        String sContent = null;
        if (ToolUtil.isNotEmpty(alarmContent))
            sContent = EhcacheConstant.retBean().getAlarmContentName(Integer.valueOf(alarmContent));
        return this.baseMapper.jobList(page, keywords, beginTime, endTime, jobStatus, alarmType, sContent,dataScope);
    }
    @Override
    public List<AlarmJob> jobListExport(Page<AlarmJob> page, String keywords, String beginTime, String endTime, String jobStatus, String alarmType, String alarmContent,DataScope dataScope)
    {
        return this.baseMapper.jobListExport(page, keywords, beginTime, endTime, jobStatus, alarmType, alarmContent,dataScope);
    }

    @Override
    public List<AlarmJob> jobListScope(Page<AlarmJob> page, String keywords, String beginTime, String endTime, String jobStatus, String alarmType, String alarmContent, DataScope dataScope) {
        String sContent = null;
        if (ToolUtil.isNotEmpty(alarmContent)){
            sContent = EhcacheConstant.retBean().getAlarmContentName(Integer.valueOf(alarmContent));
        }
        return this.baseMapper.jobListScope(page, keywords, beginTime, endTime, jobStatus, alarmType, sContent,dataScope);

    }

    @Override
    public List<AlarmJob> jobListGroup(Page<AlarmJob> page, String keywords, String beginTime, String endTime, String jobStatus, String alarmType, String alarmContent, Long userId) {
        String sContent = null;
        if (ToolUtil.isNotEmpty(alarmContent)){
            sContent = EhcacheConstant.retBean().getAlarmContentName(Integer.valueOf(alarmContent));
        }
        if(ToolUtil.isNotEmpty(jobStatus)){
            if("0".equals(jobStatus)){
                return this.baseMapper.jobListGroup(page, keywords, beginTime, endTime, jobStatus, alarmType, sContent, userId);
            }else if("3".equals(jobStatus)){
                return this.baseMapper.jobListUser(page, keywords, beginTime, endTime, jobStatus, alarmType, sContent, userId);
            }else {
                return new ArrayList<>();
            }
        }else {
            return this.baseMapper.jobListAll(page, keywords, beginTime, endTime, jobStatus, alarmType, sContent, userId);
        }
    }

    @Override
    public List<AlarmJob> jobInfo(int id, DataScope dataScope)
    {
        return this.baseMapper.jobInfo(id,dataScope);
    }

    @Override
    public Map<String, Object> countByJobStatus(long deptId,int spanDays) {
        return this.baseMapper.countByJobStatus(deptId,spanDays);
    }
    @Override
    public Map<String, Object> countMyJob(long userId,long deptId,int spanDays,String deptIds) {
//        return this.baseMapper.countMyJob(userId,deptId,spanDays,deptIds);
        int countGroup = this.baseMapper.countJobGroup(userId);
        int countUser = this.baseMapper.countJobUser(userId);
        Map<String, Object> count = new HashMap<>();
        count.put("beforeGet",countGroup);
        count.put("over",countUser);
        count.put("total",countGroup+countUser);
        count.put("inHandle",0);
        count.put("beforeConfirm",0);
        return count;
    }

    @Override
    public boolean getJob(int id,long personId) {
        return this.baseMapper.getJob(id,personId);
    }

    @Override
    public boolean confirmJob(int id,long personId, String firstState, String firstStatePhotos, String needHandle) {
        if ("1".equals(needHandle))
            return this.baseMapper.confirmJob(id,personId, firstState, firstStatePhotos, needHandle);
        else
            return this.baseMapper.confirmOverJobAndAlarm(id,personId);
    }

    @Override
    public boolean transferJob(int id, long transferPerson,String flowRec) {
        return this.baseMapper.transferJob(id, transferPerson,flowRec);
    }

    @Override
    public boolean saveJob(int id, String handleMessage, String handlePhotos) {
        return this.baseMapper.saveJob(id, handleMessage, handlePhotos);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean overJob(int id,long personId, String handleMessage, String handlePhotos) {
        boolean bTrue = true;
        bTrue = bTrue && this.baseMapper.overJobAndAlarm(id, personId,handleMessage, handlePhotos);
        bTrue = bTrue && this.baseMapper.insWellCover(id);
        return bTrue;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void overJobByDevCode(String devCdoe, String handleMessage, String handlePhotos) {
        boolean updateFlag = this.baseMapper.overJobAndAlarmByDevCode(devCdoe,handleMessage, handlePhotos);
        if(updateFlag){
            this.baseMapper.insWellCoverByDevCode(devCdoe);
        }
    }

    @Override
    public boolean overAlarm(int jobId) {
        return this.baseMapper.overAlarm(jobId);
    }

    @Override
    public boolean handleJob(int id,long personId) {
        return this.baseMapper.handleJob(id,personId);
    }

    @Override
    public List<String> batchCancel(String deptIds,long userId,String handleMessage){
        List<String> stringList = this.baseMapper.getDevCodesByDeptIds(deptIds);
        boolean bTrue = this.baseMapper.batchCancel(deptIds,userId,handleMessage);
        if (bTrue)
            return stringList;
        else
            return null;
    }

    @Override
    @Async
    public void postUri(String sUri, Object devCodes, String auth) {

        JSONObject jsonBody = new JSONObject();
        JSONObject jsonBody1 = new JSONObject();
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        long ts = date.getTime();
        try {
            jsonBody.put("mType","event");
            jsonBody.put("bType","Well");
            jsonBody.put("devCode",devCodes);
            jsonBody1.put("bType","WellEvent");
            jsonBody1.put("eventType",new String[]{"WellAlarmClear"});
            jsonBody1.put("logTime",simpleDateFormat.format(date));
            jsonBody.put("mBody",jsonBody1);
            jsonBody.put("ts",ts);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        String sRet = HttpClientUtils.post(sUri,jsonBody.toString(),auth);
        System.out.println("postRet:"+sRet);
//        return true;
    }

    @Override
    @Async
    public void httptUri(String sUri,String devCode,String handleMessage, String handlePhotos){
        sUri = sUri + "/job/overJobSync";
        sUri = sUri + "?devCode=" + devCode + "&handleMessage=" + handleMessage + "&handlePhotos=" + handlePhotos;
        logger.info(sUri);
        String sRet = HttpClientUtils.get(sUri,null);
        logger.info("httpRet:"+sRet);
    }

}
