package com.zcloud.domain.persistence.repository.impl;

import com.alibaba.cola.dto.PageResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jjb.saas.framework.auth.utils.AuthContext;
import com.jjb.saas.framework.repository.common.PageHelper;
import com.jjb.saas.framework.repository.repo.impl.BaseRepositoryImpl;
import com.zcloud.domain.domain.enums.MenuEnum;
import com.zcloud.domain.domain.enums.ProjectTaskExecuteStatusEnum;
import com.zcloud.domain.domain.enums.ScoreStatusEnum;
import com.zcloud.domain.domain.model.CorpInfoE;
import com.zcloud.domain.persistence.dataobject.ProjectTaskDO;
import com.zcloud.domain.persistence.mapper.ProjectTaskMapper;
import com.zcloud.domain.persistence.repository.CorpInfoRepository;
import com.zcloud.domain.persistence.repository.ProjectTaskRepository;
import com.zcloud.gbscommon.utils.PageQueryHelper;
import com.zcloud.gbscommon.utils.Query;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * web-infrastructure
 *
 * @Author zhaokai
 * @Date 2026-01-29 11:28:41
 */
@Service
@RequiredArgsConstructor
public class ProjectTaskRepositoryImpl extends BaseRepositoryImpl<ProjectTaskMapper, ProjectTaskDO> implements ProjectTaskRepository {
    private final ProjectTaskMapper projectTaskMapper;
    private final CorpInfoRepository corpInfoRepository;
    @Override
    public PageResponse<ProjectTaskDO> listPage(Map<String, Object> params) {
        if (params.get("corpinfoId") != null && StringUtils.isNotEmpty(params.get("corpinfoId").toString())) {
            params.put("corpinfoId", params.get("corpinfoId"));
        } else {
            /*if(!corpInfoRepository.isSupper()){
                params.put("corpinfoId", AuthContext.getTenantId());
            }*/
        }
        String menuPerms = "";
        if (!org.springframework.util.ObjectUtils.isEmpty(params.get("menuPath"))){
            menuPerms = MenuEnum.getMenuKeyByPath(params.get("menuPath").toString());
        }
        IPage<ProjectTaskDO> iPage = new Query<ProjectTaskDO>().getPage(params);
        IPage<ProjectTaskDO> result = projectTaskMapper.listPage(iPage, params,menuPerms);


        return PageHelper.pageToResponse(result, result.getRecords());
    }

    @Override
    public ProjectTaskDO getInfoById(Long id) {
        return projectTaskMapper.getInfoById(id);
    }

    @Override
    public void updateScoreStatus(String projectTaskId, Integer code, BigDecimal averageScore ) {
        projectTaskMapper.updateScoreStatus(projectTaskId, code,averageScore);
    }

    //获取待执行的数据转换成执行中
    @Override
    public List<ProjectTaskDO> getWaitPerfectList() {
        QueryWrapper<ProjectTaskDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("execute_status", ProjectTaskExecuteStatusEnum.WAIT_PERFECT.getCode());
        queryWrapper.le("executed_start_date", new Date());
        return projectTaskMapper.selectList(queryWrapper);
    }

    //获取执行中的，转成已经结束
    @Override
    public List<ProjectTaskDO> getExecutingList() {
        QueryWrapper<ProjectTaskDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("execute_status", ProjectTaskExecuteStatusEnum.EXECUTING.getCode());
        queryWrapper.apply("DATE(executed_end_date) < CURDATE()");
        return projectTaskMapper.selectList(queryWrapper);
    }

    @Override
    public void updateExecuteStatusBatchById(Integer code, List<Long> idList) {
        projectTaskMapper.updateExecuteStatusBatchById(code, idList);
    }

    @Override
    public ProjectTaskDO getInfoByTaskId(String projectTaskId) {
        QueryWrapper<ProjectTaskDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("project_task_id", projectTaskId);
        return projectTaskMapper.selectOne(queryWrapper);
    }

    @Override
    public List<ProjectTaskDO> getNoFinishByDomainDepartmentId(List<String> domainDepartmentIds) {
        QueryWrapper<ProjectTaskDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("score_status", ScoreStatusEnum.SCORED.getCode());
        queryWrapper.in("domain_department_id", domainDepartmentIds);
        return projectTaskMapper.selectList(queryWrapper);
    }

    @Override
    public List<ProjectTaskDO> getNoFinishByDomainGroupId(List<String> groupIdList) {
        QueryWrapper<ProjectTaskDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("score_status", ScoreStatusEnum.SCORED.getCode());
        queryWrapper.in("domain_group_id", groupIdList);
        return projectTaskMapper.selectList(queryWrapper);
    }
}

