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

import java.util.*;
import java.util.stream.Collectors;

import com.microservices.common.core.exception.ServiceException;
import com.microservices.common.core.utils.DateUtils;
import com.microservices.common.security.utils.SecurityUtils;
import com.microservices.dms.achievementLibrary.domain.Achievements;
import com.microservices.dms.achievementLibrary.mapper.AchievementTeamMapper;
import com.microservices.dms.achievementLibrary.mapper.AchievementsMapper;
import com.microservices.dms.achievementLibrary.service.IAchievementsService;
import com.microservices.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.microservices.dms.achievementLibrary.mapper.SchoolEnterpriseAchievementsMapper;
import com.microservices.dms.achievementLibrary.domain.SchoolEnterpriseAchievements;
import com.microservices.dms.achievementLibrary.service.ISchoolEnterpriseAchievementsService;

/**
 * 校企成果Service业务层处理
 * 
 * @author microservices
 * @date 2025-04-03
 */
@Service
public class SchoolEnterpriseAchievementsServiceImpl implements ISchoolEnterpriseAchievementsService 
{
    @Autowired
    private SchoolEnterpriseAchievementsMapper schoolEnterpriseAchievementsMapper;

    @Autowired
    private AchievementTeamMapper achievementTeamMapper;

    @Autowired
    private IAchievementsService achievementsService;
    @Autowired
    private AchievementsMapper achievementsMapper;

    /**
     * 查询校企成果
     *
     * @param id 校企成果主键
     * @return 校企成果
     */
    @Override
    public SchoolEnterpriseAchievements selectSchoolEnterpriseAchievementsById(Long id)
    {
        return schoolEnterpriseAchievementsMapper.selectSchoolEnterpriseAchievementsById(id);
    }

    /**
     * 查询校企成果列表
     *
     * @param schoolEnterpriseAchievements 校企成果
     * @return 校企成果
     */
    @Override
    public List<SchoolEnterpriseAchievements> selectSchoolEnterpriseAchievementsList(SchoolEnterpriseAchievements schoolEnterpriseAchievements)
    {
//        LoginUser loginUser = SecurityUtils.getLoginUser();
//        if (loginUser != null){
//            String username = loginUser.getUsername();
//            schoolEnterpriseAchievements.setCreateBy(username);
//        }
        List<SchoolEnterpriseAchievements> list = schoolEnterpriseAchievementsMapper.selectSchoolEnterpriseAchievementsList(schoolEnterpriseAchievements);
        for (SchoolEnterpriseAchievements a : list) {
            a.setImages(achievementsService.buildFileInfoByIdents(a.getImages(), "4"));
            a.setAttachments(achievementsService.buildFileInfoByIdents(a.getAttachments(), "4"));
        }
        return list;
    }

    @Override
    public List<SchoolEnterpriseAchievements> listFront(SchoolEnterpriseAchievements schoolEnterpriseAchievements) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser != null) {
            String username = loginUser.getUsername();
            schoolEnterpriseAchievements.setCreateBy(username);
        }
        return selectSchoolEnterpriseAchievementsList(schoolEnterpriseAchievements);
    }

    /**
     * 新增校企成果
     *
     * @param schoolEnterpriseAchievements 校企成果
     * @return 结果
     */
    @Override
    public Long insertSchoolEnterpriseAchievements(SchoolEnterpriseAchievements schoolEnterpriseAchievements)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser != null){
            String username = loginUser.getUsername();
            schoolEnterpriseAchievements.setCreateBy(username);
        }
        schoolEnterpriseAchievements.setCreateTime(DateUtils.getNowDate());
        schoolEnterpriseAchievementsMapper.insertSchoolEnterpriseAchievements(schoolEnterpriseAchievements);
        return schoolEnterpriseAchievements.getId();
    }

    /**
     * 修改校企成果
     *
     * @param schoolEnterpriseAchievements 校企成果
     * @return 结果
     */
    @Override
    public int updateSchoolEnterpriseAchievements(SchoolEnterpriseAchievements schoolEnterpriseAchievements)
    {
        schoolEnterpriseAchievements.setUpdateTime(DateUtils.getNowDate());
        return schoolEnterpriseAchievementsMapper.updateSchoolEnterpriseAchievements(schoolEnterpriseAchievements);
    }

    /**
     * 批量删除校企成果
     *
     * @param ids 需要删除的校企成果主键
     * @return 结果
     */
    @Override
    public int deleteSchoolEnterpriseAchievementsByIds(Long[] ids)
    {
        return schoolEnterpriseAchievementsMapper.deleteSchoolEnterpriseAchievementsByIds(ids);
    }

    /**
     * 删除校企成果信息
     *
     * @param id 校企成果主键
     * @return 结果
     */
    @Override
    public int deleteSchoolEnterpriseAchievementsById(Long id)
    {
        // 校企成果删除前判断数据仓库是否存在数据，存在则不允许删除该数据
        Achievements achievements = new Achievements();
        achievements.setSource("4");
        achievements.setSourceId(id);
        List<Achievements> ss = achievementsMapper.selectAchievementsList(achievements);
        if (ss != null && !ss.isEmpty()) {
            throw new ServiceException("数据仓库是否存在数据，不允许删除");
        }

        // 校企成果被删除，对应的成员也应该一并删除
        achievementTeamMapper.deleteAchievementTeamByAId(id);
        return schoolEnterpriseAchievementsMapper.deleteSchoolEnterpriseAchievementsById(id);
    }

    @Override
    public Long topStatistic(SchoolEnterpriseAchievements schoolEnterpriseAchievements) {
        return schoolEnterpriseAchievementsMapper.topStatistic(schoolEnterpriseAchievements);
    }
}
