package com.hdkj.lt.bf.service.impl;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hdkj.lt.bf.entity.dto.StrategyConfigDTO;
import com.hdkj.lt.bf.entity.dto.StrategyConfigDetailDTO;
import com.hdkj.lt.bf.entity.dto.StrategyConfigEntryDTO;
import com.hdkj.lt.bf.entity.dto.StrategyconfigInfoDTO;
import com.hdkj.lt.bf.entity.vo.FhzgStrategyConfigDetailVO;
import com.hdkj.lt.bf.entity.vo.FhzgStrategyConfigVO;
import com.hdkj.lt.bf.mapper.FhzgPlanStrategyDetialMapper;
import com.hdkj.lt.bf.mapper.FhzgPlanStrategyMapper;
import com.hdkj.lt.bf.service.FhzgPlanStrategyService;
import com.hdkj.lt.core.bizms.modle.dto.StrategyconfigDetailEntryDTO;
import com.hdkj.lt.core.bizms.modle.po.plan.FhzgPlanTransferStrategyConfigDetail;
import com.hdkj.lt.utils.security.SqlInjectionUtil;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.model.LoginUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;

import static cn.hutool.core.date.DatePattern.NORM_DATETIME_PATTERN;

@Service
@Slf4j
public class FhzgPlanStrategyServiceImpl implements FhzgPlanStrategyService {
    @Autowired
    private FhzgPlanStrategyMapper strategyMapper;

    @Autowired
    private FhzgPlanStrategyDetialMapper strategyDetialMapper;

    @Override
    public Page<FhzgStrategyConfigVO> getStrategyconfigList(StrategyConfigDTO dto) {
        Page<StrategyConfigDTO> page = new Page<>();
        page.setCurrent(dto.getPageNum());
        page.setSize(dto.getPageSize());
        if (StringUtils.isNotEmpty(dto.getDownTime())) {
            LocalDate date = LocalDate.parse(dto.getDownTime());
            LocalDateTime downEndTime = date.atTime(23, 59, 59);
            LocalDateTime downStartTime = date.atTime(0, 0, 0);
            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(NORM_DATETIME_PATTERN);
            String downEndTimeStr = downEndTime.format(dateTimeFormatter);
            String downStartTimeStr = downStartTime.format(dateTimeFormatter);
            dto.setDownBeginTime(downStartTimeStr);
            dto.setDownEndTime(downEndTimeStr);
        }
        if (StringUtils.isNotEmpty(dto.getStartTime())) {
            LocalDate date = LocalDate.parse(dto.getStartTime());
            LocalDateTime startEndTime = date.atTime(23, 59, 59);
            LocalDateTime startStartTime = date.atTime(0, 0, 0);
            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(NORM_DATETIME_PATTERN);
            String startEndTimeStr = startEndTime.format(dateTimeFormatter);
            String startStartTimeStr = startStartTime.format(dateTimeFormatter);
            dto.setStartBeginTime(startStartTimeStr);
            dto.setStartEndTime(startEndTimeStr);
        }
        dto.setVersionName(SqlInjectionUtil.xxsEscape(dto.getVersionName()));
        Page<FhzgStrategyConfigVO> fhzgPlanTransferStrategyConfigList = strategyMapper.getStrategyconfigList(page, dto);
        return fhzgPlanTransferStrategyConfigList;
    }

    @Override
    public FhzgStrategyConfigDetailVO getStrategyconfigDetail(StrategyConfigDetailDTO dto) {
        FhzgStrategyConfigDetailVO StrategyConfigDetailVO = new FhzgStrategyConfigDetailVO();
        HashMap<String, Object> map = strategyMapper.getVersionName(dto.getStrategyId());
        String versionName = (String) map.get("version_name");
        Integer transferStrategy = (Integer) map.get("transfer_strategy");
        StrategyConfigDetailVO.setId(dto.getStrategyId());
        StrategyConfigDetailVO.setVersionName(versionName);
        StrategyConfigDetailVO.setTransferStrategy(transferStrategy);
        List<FhzgPlanTransferStrategyConfigDetail> planTransferStrategyConfigDetail = strategyDetialMapper.getStrategyDetial(dto.getStrategyId());
        StrategyConfigDetailVO.setPlanTransferStrategyConfigDetail(planTransferStrategyConfigDetail);
        return StrategyConfigDetailVO;
    }

    @Override
    public String addStrategyconfig(StrategyconfigInfoDTO dto) {
        StrategyConfigEntryDTO strategyConfigDTO = dto.getStrategyConfigDTO();
        strategyConfigDTO.setStatus(1);
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (null != loginUser) {
            strategyConfigDTO.setCreateName(loginUser.getUsername());
            strategyConfigDTO.setCreator(loginUser.getUserid());
            strategyConfigDTO.setLastEditor(loginUser.getUserid());
        }
        strategyConfigDTO.setCreateTime(LocalDateTime.now());
        strategyConfigDTO.setLastTime(LocalDateTime.now());
        strategyMapper.insert(strategyConfigDTO);
        List<StrategyconfigDetailEntryDTO> strategyconfigDetailDTOs = dto.getStrategyconfigDetailDTOS();
        if (!CollectionUtils.isEmpty(strategyconfigDetailDTOs)) {
            for (StrategyconfigDetailEntryDTO strategyconfigDetailDTO : strategyconfigDetailDTOs) {
                strategyconfigDetailDTO.setStrategyId(strategyConfigDTO.getId());
                strategyconfigDetailDTO.setIndicatorCode(strategyconfigDetailDTO.getIndicatorCode());
            }
            strategyDetialMapper.batchInsert(strategyconfigDetailDTOs);
        }
        return "新增策略配置成功";
    }

    @Override
    public String updateStrategyconfig(StrategyconfigInfoDTO dto) {
        StrategyConfigEntryDTO strategyConfigDTO = dto.getStrategyConfigDTO();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (null != loginUser) {
            strategyConfigDTO.setLastEditor(SecurityUtils.getUserId());
        }
        strategyConfigDTO.setLastTime(LocalDateTime.now());
        strategyMapper.update(strategyConfigDTO);
        List<StrategyconfigDetailEntryDTO> strategyconfigDetailDTOs = dto.getStrategyconfigDetailDTOS();
        if (!CollectionUtils.isEmpty(strategyconfigDetailDTOs)) {
            for (StrategyconfigDetailEntryDTO strategyconfigDetailDTO : strategyconfigDetailDTOs) {
                strategyconfigDetailDTO.setStrategyId(strategyConfigDTO.getId());
                strategyDetialMapper.batchUpdate(strategyconfigDetailDTO);
            }
        }
        return "更新策略配置成功";
    }

    @Override
    public String usedStrategyconfig(StrategyConfigDetailDTO dto) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (null != loginUser) {
            dto.setLastEditor(SecurityUtils.getUserId());
        }
        LocalDateTime now = LocalDateTime.now();
        dto.setLastTime(now);
        dto.setDownTime(now);
        dto.setStartTime(now);
        strategyMapper.updateDownStatus(dto);
        strategyMapper.updateStartStatus(dto);
        return "启用策略配置成功";
    }

    /**
     * 查询某个计划，使用的评分策略字段表头信息
     **/
    @Override
    public List<FhzgPlanTransferStrategyConfigDetail> getStrategyDetailByPlanId(Long planId) {
        return strategyDetialMapper.getStrategyDetailByPlanId(planId);
    }

    /**
     *
     *查询某个故障，使用的评分策略字段表头信息
     **/
    @Override
    public List<FhzgPlanTransferStrategyConfigDetail> getStrategyDetailByFaultId(Long faultId) {
        return strategyDetialMapper.getStrategyDetailByFaultId(faultId);
    }



}
