package net.northking.cctp.dw.db.impl;

import com.github.pagehelper.Page;
import com.hzbank.testteam.autotest.dependencies.monsterDependency.dto.SystemDTO;
import net.northking.cctp.common.db.DefaultPagination;
import net.northking.cctp.common.db.Pagination;
import net.northking.cctp.common.http.QueryByPage;
import net.northking.cctp.dw.db.dao.DwAtuPlanInfoDao;
import net.northking.cctp.dw.db.entity.DwAtuPlanInfo;
import net.northking.cctp.dw.db.service.UIDwAtuPlanExecutionService;
import net.northking.cctp.dw.dto.UIDwAtuPlanQueryDto;
import net.northking.cctp.dw.dto.atu.DwAtuPlanExecutionDataDto;
import net.northking.cctp.dw.dto.atu.DwDataAndSumDto;
import net.northking.cctp.dw.dto.atu.DwOptionDto;
import net.northking.cctp.dw.dto.atu.ScriptSuccessQuery;
import net.northking.cctp.dw.feign.CltTestTeamDto;
import net.northking.cctp.dw.feign.HZBankFeignClient;
import net.northking.cctp.dw.feign.PlatformFeignClient;
import net.northking.cctp.dw.feign.ProjectsFeignForScheduleClient;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class UIDwAtuPlanExecutionServiceImpl implements UIDwAtuPlanExecutionService {

    @Autowired
    private DwAtuPlanInfoDao dwAtuPlanInfoDao;
    @Autowired
    private PlatformFeignClient platformFeignClient;
    @Autowired
    private ProjectsFeignForScheduleClient projectsFeignClient;
    @Autowired
    private HZBankFeignClient hzBankFeignClient;

    @Override
    public Pagination<DwAtuPlanExecutionDataDto> pagingQuery(QueryByPage<UIDwAtuPlanQueryDto> queryByPage) {
        DefaultPagination<DwAtuPlanExecutionDataDto> pagination = new DefaultPagination<>();

        return buildPagination(pagination, queryByPage, null);

    }

    private DefaultPagination<DwAtuPlanExecutionDataDto> buildPagination(DefaultPagination<DwAtuPlanExecutionDataDto> pagination, QueryByPage<UIDwAtuPlanQueryDto> queryByPage, Page<DwAtuPlanExecutionDataDto> page) {
        List<DwAtuPlanExecutionDataDto> dwAtuPlanExecutionDataDtos = dwAtuPlanInfoDao.pageQuery(queryByPage.getQuery());
        if (!CollectionUtils.isEmpty(dwAtuPlanExecutionDataDtos)) {
            for (DwAtuPlanExecutionDataDto dwAtuPlanExecutionDataDto : dwAtuPlanExecutionDataDtos) {
                CltTestTeamDto cltTestTeamDto = platformFeignClient.getPlanTeamInfo(dwAtuPlanExecutionDataDto.getPlanManagerId());
                if (cltTestTeamDto!=null) {
                    dwAtuPlanExecutionDataDto.setPlanTeam(cltTestTeamDto.getName());
                } else {
                    dwAtuPlanExecutionDataDto.setPlanTeam("");
                }
                SystemDTO systemDTO = hzBankFeignClient.getProjectById(Long.parseLong(dwAtuPlanExecutionDataDto.getProjectId()));
                if (!Objects.isNull(systemDTO) && !Objects.isNull(systemDTO.getSystemManager())) {
                    CltTestTeamDto projectTestTeam = platformFeignClient.getPlanTeamInfo(String.valueOf(systemDTO.getSystemManager()));
                    if (projectTestTeam!=null) {
                        dwAtuPlanExecutionDataDto.setProjectTeam(projectTestTeam.getName());
                    } else {
                        dwAtuPlanExecutionDataDto.setProjectTeam("");
                    }
                } else {
                    dwAtuPlanExecutionDataDto.setProjectTeam("");
                }
                ScriptSuccessQuery query = new ScriptSuccessQuery();
                query.setEnvId(dwAtuPlanExecutionDataDto.getEnvId());
                query.setPlanId(dwAtuPlanExecutionDataDto.getPlanId());
                if(StringUtils.isNotBlank(queryByPage.getQuery().getStartTime())){
                    query.setStartTime(queryByPage.getQuery().getStartTime());
                }
                if(StringUtils.isNotBlank(queryByPage.getQuery().getEndTime())){
                    query.setEndTime(queryByPage.getQuery().getEndTime());
                }
                DwDataAndSumDto firstDateAndSum = dwAtuPlanInfoDao.getFirstDateAndSum(query);
                if(firstDateAndSum!=null){
                    dwAtuPlanExecutionDataDto.setFirstSuccessTime(firstDateAndSum.getSuccessTime());
                    dwAtuPlanExecutionDataDto.setFirstSuccessScriptNum(firstDateAndSum.getSuccessNum());
                }
                DwDataAndSumDto endDateAndSum = dwAtuPlanInfoDao.getEndDateAndSum(query);
                if(endDateAndSum!=null){
                    dwAtuPlanExecutionDataDto.setEndSuccessTime(endDateAndSum.getSuccessTime());
                    dwAtuPlanExecutionDataDto.setEndSuccessScriptNum(endDateAndSum.getSuccessNum());
                }
            }
            pagination.setRecords(dwAtuPlanExecutionDataDtos);

        }else {
            pagination.setRecords(new ArrayList<>());
        }
        if (queryByPage.getQuery().getIsPage()) {
            pagination.setPageNo(queryByPage.getPageNo());
            pagination.setPageSize(queryByPage.getPageSize());
            pagination.setRecordCount(dwAtuPlanExecutionDataDtos.size());
        } else {
            pagination.setPageNo(1);
            pagination.setPageSize(10);
            pagination.setRecordCount(0);
        }
        return pagination;
    }

    @Override
    public List<DwOptionDto> getPlanList() {
        return dwAtuPlanInfoDao.getPlanList();
    }

    @Override
    public DwAtuPlanInfo getNameByPlanId(String planId) {
        return this.dwAtuPlanInfoDao.getNameByPlanId(planId);
    }


}
