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

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import com.yunfeiyun.agmp.common.constant.ErrorCode;
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.common.web.system.domain.SysRes;
import com.yunfeiyun.agmp.common.web.system.mapper.SysResMapper;
import com.yunfeiyun.agmp.fms.domain.FmsPolicy;
import com.yunfeiyun.agmp.fms.mapper.FmsPolicyMapper;
import com.yunfeiyun.agmp.fms.domain.reqvo.FmsPolicyReqVo;
import com.yunfeiyun.agmp.fms.domain.resvo.FmsPolicyListResVo;
import com.yunfeiyun.agmp.fms.domain.vo.FmsPolicyVo;
import com.yunfeiyun.agmp.fms.service.IFmsPolicyService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;


/**
 * 决策Service业务层处理
 * 
 * @author 杨晓辉
 * @date 2023-06-05
 */
@Service
public class FmsPolicyServiceImpl implements IFmsPolicyService
{
    @Autowired
    private FmsPolicyMapper fmsPolicyMapper;
    @Autowired
    private SysResMapper sysResMapper;

    /**
     * 查询决策
     * 
     * @param decisionId 决策主键
     * @return 决策
     */
    @Override
    public FmsPolicyVo selectFmsPolicyByDecisionId(String decisionId)
    {
        FmsPolicyVo fmsPolicyVo = fmsPolicyMapper.selectFmsPolicyByDecisionId(decisionId);
        if (ObjectUtils.isEmpty(fmsPolicyVo)){
            throw new BizException(ErrorCode.FMS_POLICY_NOT_EXIST);
        }
        return fmsPolicyVo;
    }

    /**
     * 查询决策列表
     * 
     * @param fmsPolicyReqVo 决策
     * @return 决策
     */
    @Override
    public List<FmsPolicyListResVo> selectFmsPolicyList(FmsPolicyReqVo fmsPolicyReqVo)
    {
        String decisionCreateddate = fmsPolicyReqVo.getDecisionCreateddate();
        try{
            SimpleDateFormat sdf = new SimpleDateFormat(DateUtils.YYYY_MM_DD);
            Date startDt = sdf.parse(decisionCreateddate);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(startDt);
            calendar.add(Calendar.DAY_OF_YEAR, 1);
            Date endDt = calendar.getTime();
            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
            String startDate = sdf2.format(startDt);
            String endDate = sdf2.format(endDt);

            fmsPolicyReqVo.setStartDate(startDate);
            fmsPolicyReqVo.setEndDate(endDate);
        }catch (Exception ignored){

        }
        return fmsPolicyMapper.selectFmsPolicyList(fmsPolicyReqVo);
    }


    private void updateRes(String resId, String busId){
        if (resId != null && !resId.equals("")){
            SysRes sysRes = this.sysResMapper.selectSysResByResId(resId);

            System.out.println("dddddddddddddddddddd");
            System.out.println(resId);
            System.out.println(busId);
            System.out.println(sysRes);

            if (!ObjectUtils.isEmpty(sysRes)){
                SysRes newSysRes = new SysRes();
                newSysRes.setResId(sysRes.getResId());
                newSysRes.setResBusId(busId);
                System.out.println("888888888888888888888");
                System.out.println(newSysRes);
                this.sysResMapper.updateSysRes(newSysRes);
            }
        }
    }

    /**
     * 新增决策
     * 
     * @param fmsPolicyVo 决策
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertFmsPolicy(FmsPolicyVo fmsPolicyVo)
    {
        FmsPolicy fmsPolicy = new FmsPolicy();
        BeanUtils.copyProperties(fmsPolicyVo, fmsPolicy);
        fmsPolicy.setDecisionId(fmsPolicy.getId());
        fmsPolicy.setDecisionCreateddate(DateUtils.dateTimeNow());
        fmsPolicy.setDecisionCreator(SecurityUtils.getLoginUser().getUserId());

        String resUrl = fmsPolicyVo.getResUrl();

        System.out.println("=====================");
        System.out.println(fmsPolicyVo);
        System.out.println(resUrl);

        this.updateRes(resUrl, fmsPolicy.getDecisionId());
        return fmsPolicyMapper.insertFmsPolicy(fmsPolicy);
    }

    /**
     * 修改决策
     * 
     * @param fmsPolicyVo 决策
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateFmsPolicy(FmsPolicyVo fmsPolicyVo)
    {
        String decisionId = fmsPolicyVo.getDecisionId();
        FmsPolicyVo oldFmsPolicyVo = this.selectFmsPolicyByDecisionId(decisionId);

        FmsPolicy fmsPolicy = new FmsPolicy();
        BeanUtils.copyProperties(fmsPolicyVo, fmsPolicy);
        fmsPolicy.setDecisionId(decisionId);
        fmsPolicy.setDecisionCreateddate(DateUtils.dateTimeNow());
        fmsPolicy.setDecisionCreator(SecurityUtils.getLoginUser().getUserId());

        String resUrl = fmsPolicyVo.getResUrl();
        this.updateRes(resUrl, fmsPolicy.getDecisionId());

        // 清除旧资源文件的业务id
        String oldResUrl = oldFmsPolicyVo.getResUrl();
        this.updateRes(oldResUrl, null);
        return fmsPolicyMapper.updateFmsPolicy(fmsPolicyVo);
    }

    /**
     * 批量删除决策
     * 
     * @param decisionIds 需要删除的决策主键
     * @return 结果
     */
    @Override
    public int deleteFmsPolicyByDecisionIds(String[] decisionIds)
    {
        return fmsPolicyMapper.deleteFmsPolicyByDecisionIds(decisionIds);
    }

    /**
     * 删除决策信息
     * 
     * @param decisionId 决策主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteFmsPolicyByDecisionId(String decisionId)
    {
        FmsPolicyVo oldFmsPolicyVo = this.selectFmsPolicyByDecisionId(decisionId);
        int result = this.fmsPolicyMapper.deleteFmsPolicyByDecisionId(decisionId);
        if (result == 0){
            throw new BizException(ErrorCode.FMS_POLICY_DELETE_FAILED);
        }
        // 清除旧资源文件的业务id
        String oldResUrl = oldFmsPolicyVo.getResUrl();
        this.updateRes(oldResUrl, null);
        return result;
    }
}
