package com.microservices.dms.resourceLibrary.service;


import com.alibaba.fastjson.JSON;
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.ProjectResourceLibrary;
import com.microservices.dms.resourceLibrary.domain.vo.KeyValVo;
import com.microservices.dms.resourceLibrary.domain.vo.ProjectListVo;
import com.microservices.dms.resourceLibrary.domain.vo.UserActionDataVo;
import com.microservices.dms.resourceLibrary.mapper.ProjectResourceLibraryMapper;
import com.microservices.dms.utils.DateUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

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

@Service
@RequiredArgsConstructor
public class ProjectResourceLibraryService {

    private final ProjectResourceLibraryMapper projectResourceLibraryMapper;

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

    public List<ProjectListVo> selectProjectListCondition(ProjectListVo projectListVo) {
        return projectResourceLibraryMapper.selectProjectListCondition(projectListVo);
    }

    public List<UserActionDataVo> selectCodeCommitCondition(UserActionDataVo userActionDataVo) {
        return projectResourceLibraryMapper.selectCodeCommitCondition(userActionDataVo);
    }

    public List<UserActionDataVo> selectForkCondition(UserActionDataVo userActionDataVo) {
        return projectResourceLibraryMapper.selectForkCondition(userActionDataVo);
    }

    public List<UserActionDataVo> selectIssueCondition(UserActionDataVo userActionDataVo) {
        return projectResourceLibraryMapper.selectIssueCondition(userActionDataVo);
    }

    public List<UserActionDataVo> selectPRCondition(UserActionDataVo userActionDataVo) {
        return projectResourceLibraryMapper.selectPRCondition(userActionDataVo);
    }

    public List<UserActionDataVo> selectPraisesCondition(UserActionDataVo userActionDataVo) {
        return projectResourceLibraryMapper.selectPraisesCondition(userActionDataVo);
    }

    public List<UserActionDataVo> selectSearchCondition(UserActionDataVo userActionDataVo) {
        return projectResourceLibraryMapper.selectSearchCondition(userActionDataVo);
    }


    public ProjectResourceLibrary selectProjectResourceLibraryById(Long id) {
        return projectResourceLibraryMapper.selectProjectResourceLibraryById(id);
    }


    public List<ProjectResourceLibrary> selectProjectResourceLibraryList(ProjectResourceLibrary projectResourceLibrary) {
        List<ProjectResourceLibrary> list = projectResourceLibraryMapper.selectProjectResourceLibraryList(projectResourceLibrary);
        //  /api/attachments/10e9c28d-f8cb-4fbd-83c3-e46d2ff1b325
        for (ProjectResourceLibrary l : list) {
            //根据领域key,获取领域名称
            String projectDomain  = l.getProjectDomain();
            if(StringUtils.isNotBlank(projectDomain)){
                String projectDomainName = projectResourceLibraryMapper.getProjectDomainNameByKey(projectDomain);
                l.setProjectDomainName(projectDomainName);
            }
            Date releaseDate = l.getReleaseDate();
            l.setReleaseDate(DateUtil.plusHours(releaseDate, 8));
            List<KeyValVo<String, String>> attachments = projectResourceLibraryMapper.getAttachments(l.getVersionReleasesId());
            for (KeyValVo<String, String> a : attachments) {
                String v = a.getV();
                if (StringUtils.isNotEmpty(v)) {
                    if (v.startsWith("http")) {
                        String urlPath = getUrlPath(v);
                        if (StringUtils.isNotEmpty(urlPath)) {
                            if (urlPath.startsWith("/api")) {
                                a.setV(gitLinkUrl + urlPath);
                            }else {
                                a.setV(gitLinkUrl +"/api"+ urlPath);
                            }
                        }
                    } else {
                        a.setV(String.format(PROJECT_DOWNLOAD_API, gitLinkUrl, v));
                    }
                }
            }
            l.setAttachmentList(attachments);
            l.setAttachments(JSON.toJSONString(attachments));
        }
        return list;
    }


    public Long insertProjectResourceLibrary(ProjectResourceLibrary projectResourceLibrary) {
        projectResourceLibrary.setCreateTime(DateUtils.getNowDate());
        projectResourceLibrary.setTransferDate(DateUtils.getNowDate());
        projectResourceLibrary.setCreateBy(SecurityUtils.getUsername());
        projectResourceLibraryMapper.insertProjectResourceLibrary(projectResourceLibrary);
        return projectResourceLibrary.getId();
    }


    public int updateProjectResourceLibrary(ProjectResourceLibrary projectResourceLibrary) {
        projectResourceLibrary.setUpdateTime(DateUtils.getNowDate());
        return projectResourceLibraryMapper.updateProjectResourceLibrary(projectResourceLibrary);
    }


    public int deleteProjectResourceLibraryByIds(Long[] ids) {
        return projectResourceLibraryMapper.deleteProjectResourceLibraryByIds(ids);
    }


    public int deleteProjectResourceLibraryById(Long id) {
        return projectResourceLibraryMapper.deleteProjectResourceLibraryById(id);
    }

    public long getTotalProjectCount() {
        return projectResourceLibraryMapper.getTotalProjectCount();
    }

    public long getPublicProjectCount() {
        return projectResourceLibraryMapper.getPublicProjectCount();
    }

    public long getTotalReleaseCount() {
        return projectResourceLibraryMapper.getTotalReleaseCount();
    }

    public long getTotalConversionCount() {
        return projectResourceLibraryMapper.getTotalConversionCount();
    }
}
