package org.jk.system.service.impl;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jk.common.core.domain.model.LoginUser;
import org.jk.common.core.enums.ProjectTypeEnum;
import org.jk.common.core.utils.DateUtils;
import org.jk.common.core.utils.SnowflakeDigitGenerator;
import org.jk.common.core.utils.StringUtils;
import org.jk.common.mybatis.core.page.PageQuery;
import org.jk.common.satoken.utils.LoginHelper;
import org.jk.system.domain.ProjectApplication;
import org.jk.system.domain.SysProject;
import org.jk.system.domain.SysProjectApp;
import org.jk.system.domain.vo.SysProjectExportVo;
import org.jk.system.mapper.SysProjectAppMapper;
import org.jk.system.mapper.SysProjectMapper;
import org.jk.system.service.ISysProjectService;
import org.springframework.stereotype.Service;

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

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author takai
 * @date 2025-05-28
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class SysProjectServiceImpl implements ISysProjectService
{
    private final SysProjectMapper sysProjectMapper;

    private final SysProjectAppMapper sysProjectAppMapper;

    /**
     * 查询【请填写功能名称】
     *
     * @param projectId 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public SysProject selectSysProjectByProjectId(String projectId)
    {
        SysProject vo = sysProjectMapper.selectSysProjectByProjectId(projectId);
        if(vo!= null){
            SysProjectApp params = new SysProjectApp();
            params.setProjectId(projectId);
            List<SysProjectApp> sysProjectApps = sysProjectAppMapper.selectSysProjectAppList(params);
            List<String> appIds = new ArrayList<>();
            for (SysProjectApp sysProjectApp : sysProjectApps){
                appIds.add(sysProjectApp.getAppId());
            }
            vo.setAppId(appIds.toArray(new String[appIds.size()]));
        }
        return vo;
    }

    /**
     * 查询【请填写功能名称】
     *
     * @param projectPid 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public SysProject selectSysProjectByProjectPid(String projectPid)
    {
        return sysProjectMapper.selectSysProjectByProjectPid(projectPid);
    }



    /**
     * 查询【请填写功能名称】列表
     *
     * @param sysProject 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<SysProject> selectSysProjectList(SysProject sysProject)
    {
        //管理员 获取所有项目
        log.info("selectSysProjectList:LoginHelper.getLoginUser()", LoginHelper.getLoginUser());

        LoginUser loginUser = LoginHelper.getLoginUser();
        Map<String,Object> userParams = new HashMap<>();
        userParams.put("userId",loginUser.getUserId());
        log.info("selectSysProjectList:userId",loginUser.getUserId());
        if(loginUser.getRoles().stream().anyMatch(role -> role.getRoleId() == 1)) {
            log.info("selectSysProjectList:isAdmin",true);
            userParams.put("isAdmin", true);
        } else {
            log.info("selectSysProjectList:isAdmin",false);
            userParams.put("isAdmin", false);
        }
        if(sysProject == null){
            sysProject = new SysProject();
        }
        sysProject.setParams(userParams);
        List<SysProject> list = sysProjectMapper.selectSysProjectList(sysProject);

        for (SysProject sysProjects : list){
            if(sysProjects.getSourceFrom() != null) {
                if(ProjectTypeEnum.HJL.getClientId().equals(sysProjects.getSourceFrom())) {
                    sysProjects.setSourceFromName(ProjectTypeEnum.HJL.getInfo());
                } else if (ProjectTypeEnum.HXG.getClientId().equals(sysProjects.getSourceFrom())) {
                    sysProjects.setSourceFromName(ProjectTypeEnum.HXG.getInfo());
                }

            }
            SysProjectApp params = new SysProjectApp();
            params.setProjectId(String.valueOf(sysProjects.getProjectId()));
            List<ProjectApplication> sysProjectApps = sysProjectAppMapper.selectSysProjectApplicationList(params);
            List<String> names = new ArrayList<>();
            for (ProjectApplication sysProjectApp : sysProjectApps){
                if(sysProjectApp != null) {
                    names.add(sysProjectApp.getName());
                }
            }
            sysProjects.setName(names.toArray(new String[names.size()]));
        }
        return list;
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param sysProject 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<SysProjectExportVo> selectSysProjectExcelList(SysProject sysProject)
    {
        //管理员 获取所有项目
        log.info("selectSysProjectList:LoginHelper.getLoginUser()", LoginHelper.getLoginUser());

        LoginUser loginUser = LoginHelper.getLoginUser();
        Map<String,Object> userParams = new HashMap<>();
        userParams.put("userId",loginUser.getUserId());
        log.info("selectSysProjectList:userId",loginUser.getUserId());
        if(loginUser.getRoles().stream().anyMatch(role -> role.getRoleId() == 1)) {
            log.info("selectSysProjectList:isAdmin",true);
            userParams.put("isAdmin", true);
        } else {
            log.info("selectSysProjectList:isAdmin",false);
            userParams.put("isAdmin", false);
        }
        if(sysProject == null){
            sysProject = new SysProject();
        }
        sysProject.setParams(userParams);
        List<SysProjectExportVo> list = sysProjectMapper.selectSysProjectExcelList(sysProject);
        return list;
    }
    /**
     * 新增【请填写功能名称】
     *
     * @param sysProject 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertSysProject(SysProject sysProject)
    {
        if(StringUtils.isEmpty(sysProject.getProjectPid())) {
            SnowflakeDigitGenerator snowflakeDigitGenerator = new SnowflakeDigitGenerator(1, 1);
            long projectPId = snowflakeDigitGenerator.nextId();
            sysProject.setProjectPid(String.valueOf(projectPId));
        }
        sysProject.setCreateTime(DateUtils.getNowDate());
        int i = sysProjectMapper.insertSysProject(sysProject);
        if(i > 0 && sysProject.getAppId() != null){
            for (String appId : sysProject.getAppId()){
                SysProjectApp sysProjectApp = new SysProjectApp();
                sysProjectApp.setProjectId(String.valueOf(sysProject.getProjectId()));
                sysProjectApp.setAppId(appId);
                sysProjectAppMapper.insertSysProjectApp(sysProjectApp);
            }
        }
        return i;
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param sysProject 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateSysProject(SysProject sysProject)
    {
        sysProject.setUpdateTime(DateUtils.getNowDate());
        int i = sysProjectMapper.updateSysProject(sysProject);
        if(i > 0){
            int successI = sysProjectAppMapper.deleteSysProjectAppByProjectId(String.valueOf(sysProject.getProjectId()));
            if(successI > 0){
                for (String appId : sysProject.getAppId()){
                    SysProjectApp sysProjectApp = new SysProjectApp();
                    sysProjectApp.setProjectId(String.valueOf(sysProject.getProjectId()));
                    sysProjectApp.setAppId(appId);
                    sysProjectAppMapper.insertSysProjectApp(sysProjectApp);
                }
            }
        }
        return i;
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param projectIds 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteSysProjectByProjectIds(String[] projectIds)
    {
        int i = sysProjectMapper.deleteSysProjectByProjectIds(projectIds);
        if(i > 0){
            sysProjectAppMapper.deleteSysProjectAppByProjectIds(projectIds);
        }
        return i;
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param projectId 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteSysProjectByProjectId(String projectId, String appId)
    {
        return sysProjectMapper.deleteSysProjectByProjectId(projectId);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param sysProject 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public Page<SysProject> selectPageList(SysProject sysProject, PageQuery pageQuery)
    {
        //管理员 获取所有项目
        log.info("selectSysProjectList:LoginHelper.getLoginUser()", LoginHelper.getLoginUser());

        LoginUser loginUser = LoginHelper.getLoginUser();
        Map<String,Object> userParams = new HashMap<>();
        userParams.put("userId",loginUser.getUserId());
        log.info("selectSysProjectList:userId",loginUser.getUserId());
        if(loginUser.getRoles().stream().anyMatch(role -> role.getRoleId() == 1)) {
            log.info("selectSysProjectList:isAdmin",true);
            userParams.put("isAdmin", true);
        } else {
            log.info("selectSysProjectList:isAdmin",false);
            userParams.put("isAdmin", false);
        }
        if(sysProject == null){
            sysProject = new SysProject();
        }
        sysProject.setParams(userParams);
        Page<SysProject> pageList = sysProjectMapper.selectPageList(sysProject,pageQuery.build());

        for (SysProject sysProjects : pageList.getRecords()){
            if(sysProjects.getSourceFrom() != null) {
                if(ProjectTypeEnum.HJL.getClientId().equals(sysProjects.getSourceFrom())) {
                    sysProjects.setSourceFromName(ProjectTypeEnum.HJL.getInfo());
                } else if (ProjectTypeEnum.HXG.getClientId().equals(sysProjects.getSourceFrom())) {
                    sysProjects.setSourceFromName(ProjectTypeEnum.HXG.getInfo());
                }

            }
            SysProjectApp params = new SysProjectApp();
            params.setProjectId(String.valueOf(sysProjects.getProjectId()));
            List<ProjectApplication> sysProjectApps = sysProjectAppMapper.selectSysProjectApplicationList(params);
            List<String> names = new ArrayList<>();
            for (ProjectApplication sysProjectApp : sysProjectApps){
                if(sysProjectApp != null) {
                    names.add(sysProjectApp.getName());
                }
            }
            sysProjects.setName(names.toArray(new String[names.size()]));
        }
        return pageList;
    }
}
