package com.microservices.dms.resourceLibrary.service;

import com.microservices.common.core.utils.DateUtils;
import com.microservices.common.core.utils.StringUtils;
import com.microservices.common.security.utils.SecurityUtils;
import com.microservices.dms.resourceLibrary.domain.CompetitionResourceLibrary;
import com.microservices.dms.resourceLibrary.domain.ProjectResourceLibrary;
import com.microservices.dms.resourceLibrary.domain.vo.CompetitionListVo;
import com.microservices.dms.resourceLibrary.domain.vo.KeyValVo;
import com.microservices.dms.resourceLibrary.mapper.CompetitionResourceLibraryMapper;
import com.microservices.dms.resourceLibrary.domain.vo.UserActionDataVo;
import com.microservices.dms.resourceLibrary.mapper.TaskResourceLibraryMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.microservices.dms.constant.TaskConstant.COMPETITION_TYPE_DOWNLOAD;
import static com.microservices.dms.constant.TaskConstant.PROJECT_DOWNLOAD_API;
import static com.microservices.dms.utils.UrlUtil.getUrlPath;

@Service
@RequiredArgsConstructor
public class CompetitionResourceLibraryService {

    @Value("${http.gitLinkUrl}")
    public String gitLinkUrl;

    private final CompetitionResourceLibraryMapper competitionResourceLibraryMapper;
    private final TaskResourceLibraryMapper taskResourceLibraryMapper;

    public List<UserActionDataVo> selectRegisterCondition(UserActionDataVo vo) {
        return competitionResourceLibraryMapper.selectRegisterCondition(vo);
    }

    public List<UserActionDataVo> selectOpusCondition(UserActionDataVo vo) {
        return competitionResourceLibraryMapper.selectOpusCondition(vo);
    }

    public List<CompetitionListVo> selectCompetitionListCondition(CompetitionListVo dataVo) {
        List<CompetitionListVo> competitionListVos = competitionResourceLibraryMapper.selectCompetitionListCondition(dataVo);
        for (CompetitionListVo vo : competitionListVos) {
            UserActionDataVo c = new UserActionDataVo();
            c.setItemId(vo.getId());
            c.setItemType(COMPETITION_TYPE_DOWNLOAD);
            long opusDownload = taskResourceLibraryMapper.countDownloaderCondition(c);
            vo.setOpusDownloadCount(opusDownload);
            Long register = competitionResourceLibraryMapper.countRegisterCondition(c);
            vo.setRegistrationsCount(register);
            c.setStatus(3);
            Long opus = competitionResourceLibraryMapper.countOpusCondition(c);
            vo.setOpusCount(opus);
        }

        return competitionListVos;
    }

    public CompetitionResourceLibrary selectCompetitionResourceLibraryById(Long id) {
        return competitionResourceLibraryMapper.selectCompetitionResourceLibraryById(id);
    }

    public List<CompetitionResourceLibrary> selectCompetitionResourceLibraryList(CompetitionResourceLibrary competitionResourceLibrary) {
        List<CompetitionResourceLibrary> list = competitionResourceLibraryMapper.selectCompetitionResourceLibraryList(competitionResourceLibrary);
        //  /api/attachments/10e9c28d-f8cb-4fbd-83c3-e46d2ff1b325
        for (CompetitionResourceLibrary l : list) {
            List<KeyValVo<String, String>> attachments = competitionResourceLibraryMapper.getAttachments(l.getCompetitionUserId());
            for (KeyValVo<String, String> a : attachments) {
                String v = a.getV();
                if (StringUtils.isNotEmpty(v)) {
                    a.setV(String.format(PROJECT_DOWNLOAD_API, gitLinkUrl, v));
                }
                l.setAttachmentList(attachments);
            }
        }
        return list;
    }

    public Long insertCompetitionResourceLibrary(CompetitionResourceLibrary competitionResourceLibrary) {
        competitionResourceLibrary.setCreateTime(DateUtils.getNowDate());
        competitionResourceLibrary.setCreateBy(SecurityUtils.getUsername());
        competitionResourceLibrary.setTransferredToResultLibraryTime(DateUtils.getNowDate());
        competitionResourceLibraryMapper.insertCompetitionResourceLibrary(competitionResourceLibrary);
        return competitionResourceLibrary.getId();
    }

    public int updateCompetitionResourceLibrary(CompetitionResourceLibrary competitionResourceLibrary) {
        competitionResourceLibrary.setUpdateTime(DateUtils.getNowDate());
        return competitionResourceLibraryMapper.updateCompetitionResourceLibrary(competitionResourceLibrary);
    }

    public int deleteCompetitionResourceLibraryByIds(Long[] ids) {
        return competitionResourceLibraryMapper.deleteCompetitionResourceLibraryByIds(ids);
    }

    public int deleteCompetitionResourceLibraryById(Long id) {
        return competitionResourceLibraryMapper.deleteCompetitionResourceLibraryById(id);
    }


    /**
     * 返回开放竞赛总数
     *
     * @return 开放竞赛总数
     */
    public long getTotalOpenCompetitions() {
        // 这里替换成你的实际逻辑，例如从数据库查询
        return competitionResourceLibraryMapper.getTotalOpenCompetitions();
    }

    /**
     * 返回竞赛转换成果数
     *
     * @return 竞赛转换成果数
     */
    public long getTotalCompetitionAchievements() {
        // 这里替换成你的实际逻辑
        return competitionResourceLibraryMapper.getTotalCompetitionAchievements();
    }

    /**
     * 返回竞赛提交作品数
     *
     * @return 竞赛提交作品数
     */
    public long getTotalCompetitionSubmissions() {
        // 这里替换成你的实际逻辑
        return competitionResourceLibraryMapper.getTotalCompetitionSubmissions();
    }

    /**
     * 返回已结束竞赛数
     *
     * @return 已结束竞赛数
     */
    public long getTotalFinishedCompetitions() {
        // 这里替换成你的实际逻辑
        return competitionResourceLibraryMapper.getTotalFinishedCompetitions();
    }

    /**
     * 返回需评审竞赛数
     *
     * @return 需评审竞赛数
     */
    public long getReviewCompetitionCount() {
        // 这里替换成你的实际逻辑
        return competitionResourceLibraryMapper.getReviewCompetitionCount();
    }
}
