package com.yunfeiyun.agmp.fms.service.impl;

import java.util.List;
import java.util.Map;

import com.yunfeiyun.agmp.common.constant.ErrorCode;
import com.yunfeiyun.agmp.common.enums.FmsMessageResVoType;
import com.yunfeiyun.agmp.common.enums.WarnStatus;
import com.yunfeiyun.agmp.common.exception.BizException;
import com.yunfeiyun.agmp.common.utils.DateUtils;
import com.yunfeiyun.agmp.common.utils.SecurityUtils;
import com.yunfeiyun.agmp.fms.domain.FmsTask;
import com.yunfeiyun.agmp.fms.domain.FmsWarn;
import com.yunfeiyun.agmp.fms.mapper.FmsWarnMapper;
import com.yunfeiyun.agmp.fms.util.DataAuthUtil;
import com.yunfeiyun.agmp.fms.domain.reqvo.FmsWarnReqVo;
import com.yunfeiyun.agmp.fms.domain.reqvo.FmsWarnStatusReqVo;
import com.yunfeiyun.agmp.fms.domain.resvo.FmsMessageResVo;
import com.yunfeiyun.agmp.fms.domain.resvo.FmsMessageResVoItem;
import com.yunfeiyun.agmp.fms.domain.resvo.FmsWarnResVo;
import com.yunfeiyun.agmp.fms.service.IFmsTaskService;
import com.yunfeiyun.agmp.fms.service.IFmsWarnService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;


/**
 * 预警Service业务层处理
 * 
 * @author 杨晓辉
 * @date 2023-05-29
 */
@Service
public class FmsWarnServiceImpl implements IFmsWarnService
{
    @Autowired
    private FmsWarnMapper fmsWarnMapper;

    @Autowired
    private IFmsTaskService fmsTaskService;

    @Resource
    private DataAuthUtil dataAuthUtil;

    /**
     * 查询预警
     * 
     * @param warnId 预警主键
     * @return 预警
     */
    @Override
    public FmsWarnResVo selectFmsWarnByWarnId(String warnId)
    {
        FmsWarnResVo fmsWarnResVo = fmsWarnMapper.selectFmsWarnByWarnId(warnId);
        if (ObjectUtils.isEmpty(fmsWarnResVo)){
            // 预警记录不存在
            throw new BizException(ErrorCode.FMS_WARN_RECORD_NO_EXIST);
        }
        return fmsWarnResVo;
    }

    /**
     * 查询预警列表
     * 
     * @param fmsWarnReqVo 预警
     * @return 预警
     */
    @Override
    public List<FmsWarnResVo> selectFmsWarnList(FmsWarnReqVo fmsWarnReqVo)
    {
        return fmsWarnMapper.selectFmsWarnList(fmsWarnReqVo);
    }

    /**
     * 新增预警
     * 
     * @param fmsWarn 预警
     * @return 结果
     */
    @Override
    public int insertFmsWarn(FmsWarn fmsWarn)
    {
        return fmsWarnMapper.insertFmsWarn(fmsWarn);
    }

    /**
     * 修改预警
     * 
     * @param fmsWarn 预警
     * @return 结果
     */
    @Override
    public int updateFmsWarn(FmsWarn fmsWarn)
    {
        return fmsWarnMapper.updateFmsWarn(fmsWarn);
    }

    /**
     * 下发农事任务并更新预警信息
     *
     * @param fmsTask 任务实体
     * @param warnId 预警记录id
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateFmsWarnByTask(FmsTask fmsTask, String warnId)
    {
        // 检测农事预警记录是否存在，不存在则抛出异常
        FmsWarnResVo fmsWarnResVo = this.selectFmsWarnByWarnId(warnId);
        if (!fmsWarnResVo.getWarnHandlestatus().equals(WarnStatus.PENDING.getCode())){
            throw new BizException(ErrorCode.FMS_WARN_RECORD_NO_PENDING);
        }

        fmsTaskService.insertFmsTask(fmsTask);

        FmsWarn fmsWarn = new FmsWarn();
        fmsWarn.setWarnId(warnId);
        fmsWarn.setTaskId(fmsTask.getTaskId());
        fmsWarn.setWarnHandlestatus(WarnStatus.DISTRIBUTE.getCode());
        fmsWarn.setWarnHandler(SecurityUtils.getLoginUser().getUserId());
        fmsWarn.setWarnHandletime(DateUtils.dateTimeNow("yyyy-MM-dd HH:mm:ss"));

        return fmsWarnMapper.updateFmsWarn(fmsWarn);
    }

    /**
     * 修改预警状态为已读
     *
     * @param fmsWarnStatusReqVo 预警
     * @return 结果
     */
    public int updateFmsWarnByStatus(FmsWarnStatusReqVo fmsWarnStatusReqVo){
        String warnId = fmsWarnStatusReqVo.getWarnId();

        FmsWarnResVo fmsWarnResVo = fmsWarnMapper.selectFmsWarnByWarnId(warnId);
        if (ObjectUtils.isEmpty(fmsWarnResVo)){
            // 预警记录不存在
            throw new BizException(ErrorCode.FMS_WARN_RECORD_NO_EXIST);
        }

        if (!fmsWarnResVo.getWarnHandlestatus().equals(WarnStatus.PENDING.getCode())){
            // 仅允许修改待处理预警
            throw new BizException(ErrorCode.FMS_WARN_RECORD_NO_PENDING);
        }

        FmsWarn fmsWarn = new FmsWarn();
        fmsWarn.setWarnId(warnId);
        fmsWarn.setWarnHandletime(DateUtils.dateTimeNow("yyyy-MM-dd HH:mm:ss"));
        fmsWarn.setWarnHandler(SecurityUtils.getLoginUser().getUserId());
        fmsWarn.setWarnHandlestatus(WarnStatus.READ.getCode());

        return fmsWarnMapper.updateFmsWarn(fmsWarn);
    }

    /**
     * 批量删除预警
     * 
     * @param warnIds 需要删除的预警主键
     * @return 结果
     */
    @Override
    public int deleteFmsWarnByWarnIds(String[] warnIds)
    {
        return fmsWarnMapper.deleteFmsWarnByWarnIds(warnIds);
    }

    /**
     * 删除预警信息
     * 
     * @param warnId 预警主键
     * @return 结果
     */
    @Override
    public int deleteFmsWarnByWarnId(String warnId)
    {
        return fmsWarnMapper.deleteFmsWarnByWarnId(warnId);
    }



    @Override
    public void appendWarnMessage(Map<String, FmsMessageResVo> fmsMessageResVoMap) {
        FmsWarnResVo fmsWarnResVoQuery = new FmsWarnResVo();
        fmsWarnResVoQuery.setBlockIds(dataAuthUtil.blockList());
        fmsWarnResVoQuery.setDataFilter(dataAuthUtil.isValidate());
        if(fmsWarnResVoQuery.getDataFilter()&&fmsWarnResVoQuery.getBlockIds().isEmpty()){
            return;
        }
        List<FmsWarnResVo> fmsWarnResVos = fmsWarnMapper.selectFmsWarnResVoByBlockManager(fmsWarnResVoQuery);
        for(FmsWarnResVo fmsWarnResVo : fmsWarnResVos){
            FmsMessageResVo fmsMessageResVo = fmsMessageResVoMap.get(FmsMessageResVoType.WARN.getType());
            FmsMessageResVoItem fmsMessageResVoItem = new FmsMessageResVoItem();
            fmsMessageResVoItem.setMessageId(fmsWarnResVo.getWarnId());
            fmsMessageResVoItem.setMessageDate(fmsWarnResVo.getWarnCreatedate());
            fmsMessageResVoItem.setMessageBody(fmsWarnResVo);
            fmsMessageResVo.addData(fmsMessageResVoItem);
        }
    }
}
