package com.securitycontrol.system.base.service.impl;

import com.securitycontrol.common.core.constant.Constant;
import com.securitycontrol.common.core.utils.StringUtils;
import com.securitycontrol.common.core.utils.aes.DateTimeHelper;
import com.securitycontrol.common.core.utils.uuid.IdUtils;
import com.securitycontrol.common.core.web.domain.AjaxResult;
import com.securitycontrol.common.security.utils.ValidatorsUtils;
import com.securitycontrol.entity.system.base.dto.GxPlanDto;
import com.securitycontrol.entity.system.base.dto.ProScheduleDto;
import com.securitycontrol.entity.system.base.vo.GxPlanProgressVo;
import com.securitycontrol.entity.system.base.vo.GxPlanVo;
import com.securitycontrol.entity.system.base.vo.ProScheduleVo;
import com.securitycontrol.system.base.mapper.IProScheduleMapper;
import com.securitycontrol.system.base.service.IProScheduleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.UUID;

/**
 * @author：cwchen
 * @date：2024-03-13-19:08
 * @version：1.0
 * @description：工程进度-业务逻辑层
 */
@Slf4j
@Service(value = "IProScheduleService")
public class ProScheduleServiceImpl implements IProScheduleService {

    @Resource(name = "IProScheduleMapper")
    private IProScheduleMapper mapper;

    @Resource(name = "ValidatorsUtils")
    private ValidatorsUtils validatorsUtils;

    @Override
    public List<ProScheduleVo> getProScheduleLists(ProScheduleDto dto) {
        List<ProScheduleVo> list = new ArrayList<>();
        try {
            list = mapper.getProScheduleLists(dto);
        } catch (Exception e) {
            log.error("获取工程进度列表",e);
        }
        return list;
    }

    @Override
    public List<GxPlanVo> getGxPlanLists(GxPlanDto dto) {
        List<GxPlanVo> list = new ArrayList<>();
        try {
            list = mapper.getGxPlanLists(dto);
        } catch (Exception e) {
            log.error("获取工序计划列表",e);
        }
        return list;
    }

    @Override
    public AjaxResult getGxPlansIsDela(ProScheduleDto dto) {
        List<String> list = new ArrayList<>();
        try {
            list = mapper.getGxPlansIsDela(dto);
        } catch (Exception e) {
            log.error("工序计划延期提醒",e);
        }
        return AjaxResult.success(list);
    }

    @Override
    public List<GxPlanProgressVo> getGxPlanProgressLists(ProScheduleDto dto) {
        List<GxPlanProgressVo> list = new ArrayList<>();
        try {
            list = mapper.getGxPlanProgressLists(dto);
        } catch (Exception e) {
            log.error("获取工序计划进度列表",e);
        }
        return list;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult fillingGxProgress(GxPlanProgressVo vo) {
        try {
            String validResult = validatorsUtils.valid(vo, GxPlanProgressVo.Query.class);
            if (StringUtils.isNotBlank(validResult)) {
                return AjaxResult.error(validResult);
            }
            int result = mapper.isFirstFillingGxProgress(vo);
            if (result == 0) {
                // 更新 工序计划开始时间
                vo.setStartTime(DateTimeHelper.getNowDate());
                vo.setType(1);
                mapper.updateGxPlanData(vo);
            }
            // 获取当前工序计划的填报进度
            String nowGxPlanProgress = mapper.getGxPlanProgress(vo);
            if(StringUtils.isNotEmpty(nowGxPlanProgress)){
                double aDouble = Double.parseDouble(nowGxPlanProgress);
                double bDouble = Double.parseDouble(vo.getGxProgress());
                if(bDouble < aDouble){
                    return AjaxResult.error("当前进度填报需大于上一次填报进度");
                }
            }
            Long timestamp = DateTimeHelper.convertDateStringToTimestamp(vo.getPlanEndTime(), "yyyy-MM-dd");
            if (timestamp == null) {
                return AjaxResult.error("计划结束时间格式不正确");
            }
            Long timestamp2 = DateTimeHelper.convertDateStringToTimestamp(DateTimeHelper.getNowDate(), "yyyy-MM-dd");
            if (timestamp2 > timestamp && StringUtils.isEmpty(vo.getDelaReason())) {
                return AjaxResult.error("计划延期原因不能为空");
            }
            // 计划填报进度为100 更新工序计划
            if (vo.getGxProgress().contains(Constant.GX_PLAN_PROGRESS)) {
                vo.setEndTime(DateTimeHelper.getNowDate());
            }
            // 计划有延期更新到工序计划
            if (StringUtils.isNotEmpty(vo.getDelaReason()) || vo.getGxProgress().contains(Constant.GX_PLAN_PROGRESS)) {
                vo.setType(2);
                mapper.updateGxPlanData(vo);
            }
            // 填报工序计划
            vo.setCreateTime(DateTimeHelper.getNowTime());
            String progressId = IdUtils.getUUId();
            vo.setProgressId(progressId);
            mapper.addGxPlanProgress(vo);
            // 工程为线路时 更新杆塔当前工序
            if (Objects.equals(Constant.XL, vo.getProType())) {
                mapper.updateGtData(vo);
            }
        } catch (Exception e) {
            log.error("填报工序计划进度列表", e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error();
        }
        return AjaxResult.success();
    }
}
