package com.microservices.dms.resourceLibrary.service.impl;

import com.alibaba.fastjson.JSON;
import com.microservices.common.core.utils.DateUtils;
import com.microservices.common.core.utils.StringUtils;
import com.microservices.dms.behaviorImage.domain.AchievementBehaviorSumVo;
import com.microservices.dms.resourceLibrary.domain.ExpertResourceLibrary;
import com.microservices.dms.resourceLibrary.domain.vo.ExpertAttachmentVo;
import com.microservices.dms.resourceLibrary.mapper.ExpertResourceLibraryMapper;
import com.microservices.dms.resourceLibrary.service.IExpertResourceLibraryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

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

import static com.microservices.dms.constant.TaskConstant.MARKER_SPACE_DOWNLOAD_API_BY_ID;

/**
 * 专家资源库Service业务层处理
 *
 * @author microservices
 * @date 2025-04-11
 */
@Service
public class ExpertResourceLibraryServiceImpl implements IExpertResourceLibraryService {
    @Autowired
    private ExpertResourceLibraryMapper expertResourceLibraryMapper;

    @Value("${markerSpaceUrl}")
    public String markerSpaceUrl;
    /**
     * 查询专家资源库
     *
     * @param id 专家资源库主键
     * @return 专家资源库
     */
    @Override
    public ExpertResourceLibrary selectExpertResourceLibraryById(Long id) {
        ExpertResourceLibrary res = expertResourceLibraryMapper.selectExpertResourceLibraryById(id);
       // getSomeAttaches(res);
        return res;
    }

    private ExpertResourceLibrary getSomeAttaches(ExpertResourceLibrary res) {
        List<Map<String, Object>> mmp = new ArrayList<>();
        ExpertAttachmentVo attachmentVo = expertResourceLibraryMapper.getExpertInfoById(res.getExpertId());
        if (attachmentVo == null) {
            return res;
        }
        Map<String, Object> honorsAttachments = expertResourceLibraryMapper.getAttachmentInfo(attachmentVo.getHonorsAttachments());
        buildFullUrl(honorsAttachments);
        Map<String, Object> resumeAttachments = expertResourceLibraryMapper.getAttachmentInfo(attachmentVo.getResumeAttachments());
        buildFullUrl(resumeAttachments);
        Map<String, Object> titleCertificateAttachments = expertResourceLibraryMapper.getAttachmentInfo(attachmentVo.getTitleCertificateAttachments());
        buildFullUrl(titleCertificateAttachments);
        Map<String, Object> academicAchievementsAttachments = expertResourceLibraryMapper.getAttachmentInfo(attachmentVo.getAcademicAchievementsAttachments());
        buildFullUrl(academicAchievementsAttachments);
        res.getParams().put("academicAchievementsAttachments", academicAchievementsAttachments);
        res.getParams().put("honorsAttachments", honorsAttachments);
        res.getParams().put("resumeAttachments", resumeAttachments);
        res.getParams().put("titleCertificateAttachments", titleCertificateAttachments);

        mmp.add(academicAchievementsAttachments);
        mmp.add(honorsAttachments);
        mmp.add(resumeAttachments);
        mmp.add(titleCertificateAttachments);
        res.setOtherAttachments(JSON.toJSONString(mmp));
        return res;
    }

    private void buildFullUrl(Map<String, Object> map) {
        if (map != null && map.containsKey("id") && map.get("id") != null) {
            String id = map.get("id").toString();
            map.put("url", String.format(MARKER_SPACE_DOWNLOAD_API_BY_ID, markerSpaceUrl, id));
        }
    }

    /**
     * 查询专家资源库列表
     *
     * @param expertResourceLibrary 专家资源库
     * @return 专家资源库
     */
    @Override
    public List<ExpertResourceLibrary> selectExpertResourceLibraryList(ExpertResourceLibrary expertResourceLibrary) {
        List<ExpertResourceLibrary>  expertResourceLibraryList = expertResourceLibraryMapper.selectExpertResourceLibraryList(expertResourceLibrary);
        for (ExpertResourceLibrary a : expertResourceLibraryList) {
            String gender = a.getGender();
            String img = "images/avatars/User/boy.jpg";
            if(StringUtils.isNotEmpty(gender) && gender.equals("1")){
               img = "images/avatars/User/girl.jpg";
            }
            a.setUserImg(img);

            getSomeAttaches(a);
        }
        return expertResourceLibraryList;
    }


    /**
     * 查询专家资源库列表
     *
     * @param expertResourceLibrary 专家资源库
     * @return 专家资源库
     */
    @Override
    public List<ExpertResourceLibrary> selectExpertResourceLibraryList2(ExpertResourceLibrary expertResourceLibrary) {
        return expertResourceLibraryMapper.selectExpertResourceLibraryList2(expertResourceLibrary);
    }

    @Override
    public Long auditExpertCount() {
        return expertResourceLibraryMapper.auditExpertCount();
    }

    @Override
    public Long competitionAuditCount() {
        return expertResourceLibraryMapper.competitionAuditCount();
    }

    @Override
    public Long taskAuditCount() {
        return expertResourceLibraryMapper.taskAuditCount();
    }

    @Override
    public AchievementBehaviorSumVo getActDataStatisticById(Long id) {
        return expertResourceLibraryMapper.getActDataStatisticById(id);
    }

    @Override
    public AchievementBehaviorSumVo getWatchFavoriteStatusById(Long id, Long userId) {
        return expertResourceLibraryMapper.getWatchFavoriteStatusById(id, userId);
    }

    /**
     * 新增专家资源库
     *
     * @param expertResourceLibrary 专家资源库
     * @return 结果
     */
    @Override
    public int insertExpertResourceLibrary(ExpertResourceLibrary expertResourceLibrary) {
        expertResourceLibrary.setCreateTime(DateUtils.getNowDate());
        return expertResourceLibraryMapper.insertExpertResourceLibrary(expertResourceLibrary);
    }

    /**
     * 修改专家资源库
     *
     * @param expertResourceLibrary 专家资源库
     * @return 结果
     */
    @Override
    public int updateExpertResourceLibrary(ExpertResourceLibrary expertResourceLibrary) {
        expertResourceLibrary.setUpdateTime(DateUtils.getNowDate());
        return expertResourceLibraryMapper.updateExpertResourceLibrary(expertResourceLibrary);
    }

    /**
     * 批量删除专家资源库
     *
     * @param ids 需要删除的专家资源库主键
     * @return 结果
     */
    @Override
    public int deleteExpertResourceLibraryByIds(Long[] ids) {
        return expertResourceLibraryMapper.deleteExpertResourceLibraryByIds(ids);
    }

    /**
     * 删除专家资源库信息
     *
     * @param id 专家资源库主键
     * @return 结果
     */
    @Override
    public int deleteExpertResourceLibraryById(Long id) {
        return expertResourceLibraryMapper.deleteExpertResourceLibraryById(id);
    }
}
