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

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.web.work.api.util.AppUtil;
import com.ruoyi.web.work.domain.Packages;
import com.ruoyi.web.work.domain.Project;
import com.ruoyi.web.work.domain.vo.PackagesListVo;
import com.ruoyi.web.work.mapper.PackagesMapper;
import com.ruoyi.web.work.service.IPackagesService;
import com.ruoyi.web.work.service.IProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author lsw
 * @date 2024-04-19
 */
@Service
public class PackagesServiceImpl extends ServiceImpl<PackagesMapper, Packages> implements IPackagesService {
    @Autowired
    private PackagesMapper packagesMapper;

    @Autowired
    private IProjectService projectService;

    @Override
    public List<Packages> selectList(Packages packages) {
        return packagesMapper.selectList(packages);
    }

    @Override
    public List<PackagesListVo> selectPackagesList(Packages packages) {
        packages.setUserId(AppUtil.getUser().getId());
        return packagesMapper.selectPackagesList(packages);
    }

    @Transactional
    @Override
    public AjaxResult add(Packages packages) {
        Project project = projectService.getById(packages.getProjectId());
        if (project == null || project.getState() == 1) {
            return AjaxResult.error("项目不存在或被停用");
        }
        packages.setUserId(AppUtil.getUser().getId());
        Packages check = packagesMapper.check(packages);
        if (check != null) {
            return AjaxResult.error("请勿重复接包");
        }
        save(packages);
        return AjaxResult.success();
    }
}
