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

import com.github.pagehelper.Page;
import com.github.pagehelper.page.PageMethod;
import net.northking.cctp.common.cache.EntityNameCache;
import net.northking.cctp.common.cache.EntityNameCatalog;
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.DwAtuScriptInfoDao;
import net.northking.cctp.dw.db.service.UIDwAtuScriptExecutionService;
import net.northking.cctp.dw.dto.UIDwAtuScriptInfoQueryDto;
import net.northking.cctp.dw.dto.atu.DwAtuScriptExecutionDataDto;
import net.northking.cctp.dw.feign.CltTestTeamDto;
import net.northking.cctp.dw.feign.PlatformFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

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

@Service
public class UIDwAtuScriptExecutionServiceImpl implements UIDwAtuScriptExecutionService {

    @Autowired
    private DwAtuScriptInfoDao atuScriptInfoDao;
    @Autowired
    private EntityNameCache entityNameCache;
    @Autowired
    private PlatformFeignClient platformFeignClient;

    @Override
    public Pagination<DwAtuScriptExecutionDataDto> pagingQuery(QueryByPage<UIDwAtuScriptInfoQueryDto> queryByPage) {
        DefaultPagination<DwAtuScriptExecutionDataDto> pagination = new DefaultPagination<>();
        if (queryByPage.getQuery().getIsPage()) {
            Page<DwAtuScriptExecutionDataDto> page = PageMethod.startPage(queryByPage.getPageNo(), queryByPage.getPageSize());
            return buildPagination(pagination, queryByPage, page);
        } else {
            return buildPagination(pagination, queryByPage, null);
        }
    }

    private DefaultPagination<DwAtuScriptExecutionDataDto> buildPagination(DefaultPagination<DwAtuScriptExecutionDataDto> pagination, QueryByPage<UIDwAtuScriptInfoQueryDto> queryByPage, Page<DwAtuScriptExecutionDataDto> page) {
        List<DwAtuScriptExecutionDataDto> dwAtuPlanExecutionDataDtos = atuScriptInfoDao.getScriptExecution(queryByPage.getQuery());

        if (!CollectionUtils.isEmpty(dwAtuPlanExecutionDataDtos)) {
            dwAtuPlanExecutionDataDtos.forEach(dwAtuScriptExecutionDataDto -> {
                CltTestTeamDto cltTestTeamDto = platformFeignClient.getPlanTeamInfo(dwAtuScriptExecutionDataDto.getManagerId());
                if (cltTestTeamDto!=null) {
                    dwAtuScriptExecutionDataDto.setTestGroup(cltTestTeamDto.getName());
                    if (StringUtils.hasText(cltTestTeamDto.getManagerId())) {
                        dwAtuScriptExecutionDataDto.setTestGroupLeader(entityNameCache.translateById(EntityNameCatalog.MONSTER_USER,cltTestTeamDto.getManagerId()));
                    } else {
                        dwAtuScriptExecutionDataDto.setTestGroupLeader("");
                    }
                } else {
                    dwAtuScriptExecutionDataDto.setTestGroup("");
                    dwAtuScriptExecutionDataDto.setTestGroupLeader("");
                }
            });
            pagination.setRecords(dwAtuPlanExecutionDataDtos);
        }else {
            pagination.setRecords(new ArrayList<>());
        }
        if (queryByPage.getQuery().getIsPage()) {
            pagination.setPageNo(queryByPage.getPageNo());
            pagination.setPageSize(queryByPage.getPageSize());
            pagination.setRecordCount(page.getTotal());
        } else {
            pagination.setPageNo(1);
            pagination.setPageSize(10);
            pagination.setRecordCount(0);
        }
        return pagination;
    }
}
