package com.ruoyi.web.work.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.web.work.api.util.AppUtil;
import com.ruoyi.web.work.domain.*;
import com.ruoyi.web.work.domain.dto.ProjectChangeDto;
import com.ruoyi.web.work.domain.dto.ProjectDto;
import com.ruoyi.web.work.domain.vo.ProjectListVo;
import com.ruoyi.web.work.domain.vo.RelateCompanyListVo;
import com.ruoyi.web.work.mapper.ProjectMapper;
import com.ruoyi.web.work.service.IPackagesService;
import com.ruoyi.web.work.service.IProjectService;
import com.ruoyi.web.work.service.IRelateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.rmi.ServerException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author lsw
 * @date 2024-04-15
 */
@Service
public class ProjectServiceImpl extends ServiceImpl<ProjectMapper, Project> implements IProjectService {
    @Autowired
    private ProjectMapper projectMapper;

    @Autowired
    private IRelateService relateService;

    @Autowired
    private IPackagesService packagesService;

    @Autowired
    private RedisCache redisCache;

    @Override
    public List<Project> selectList(Project project) {
        return projectMapper.selectList(project);
    }

    @Override
    public List<ProjectListVo> selectProjectList(Project project) {
        List<RelateCompanyListVo> list = relateService.selectRelateCompanyList(new Relate().setUserId(AppUtil.getUser().getId()));
        if (list.size() == 0 || list.isEmpty()) {
            return new ArrayList<>();
        }
        List<Packages> packagesList = packagesService.selectList(new Packages().setUserId(AppUtil.getUser().getId()));
        project.setCompanyIds(list.stream().map(RelateCompanyListVo::getCompanyId).collect(Collectors.toList()));
        project.setProjectIds(packagesList.stream().map(Packages::getProjectId).collect(Collectors.toList()));
        return projectMapper.selectProjectList(project);
    }

    @Override
    public AjaxResult add(Project project) throws ServerException {
        Platform platform = redisCache.getCacheObject(CacheConstants.APP_PLATFORM);
        project.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        project.setNum(StringUtils.generateNumber());
        project.setServiceCompany(platform.getServiceCompany());
        project.setState(0);
        if (!save(project)) {
            throw new ServerException("创建失败,请联系管理员");
        }
        return AjaxResult.success();
    }

    @Override
    public AjaxResult edit(ProjectDto dto) throws ServerException {
        Project project = getById(dto.getId());
        if (project == null || !project.getCompanyId().equals(SecurityUtils.getLoginUser().getUser().getCompanyId())) {
            return AjaxResult.error("项目不存在或非法操作");
        }
        BeanUtils.copyProperties(dto, project);
        if (!updateById(project)) {
            throw new ServerException("编辑项目失败,请联系管理员");
        }
        return AjaxResult.success();
    }

    @Override
    public AjaxResult change(ProjectChangeDto dto) throws ServerException {
        Project project = getById(dto.getId());
        if (project == null || !project.getCompanyId().equals(SecurityUtils.getLoginUser().getUser().getCompanyId())) {
            return AjaxResult.error("项目不存在或非法操作");
        }
        project.setState(dto.getState());
        if (!updateById(project)) {
            throw new ServerException("停用或启用项目失败,请联系管理员");
        }
        return AjaxResult.success();
    }
}
