package com.yc.ship.module.resource.service.project;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.common.util.object.BeanUtils;
import com.yc.ship.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.yc.ship.module.resource.controller.admin.project.vo.ProjectPageReqVO;
import com.yc.ship.module.resource.controller.admin.project.vo.ProjectSaveReqVO;
import com.yc.ship.module.resource.controller.admin.vo.SimpleListReqVO;
import com.yc.ship.module.resource.dal.dataobject.project.ProjectDO;
import com.yc.ship.module.resource.dal.mysql.project.ProjectMapper;
import com.yc.ship.module.resource.enums.ErrorCodeConstants;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static com.yc.ship.framework.common.exception.util.ServiceExceptionUtil.exception;


/**
 * 产品项目 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class ProjectServiceImpl implements ProjectService {

    @Resource
    private ProjectMapper projectMapper;

    @Override
    @Transactional
    public Long createProject(ProjectSaveReqVO createReqVO) {
        // 插入
        ProjectDO project = BeanUtils.toBean(createReqVO, ProjectDO.class);
        project.setId(IdWorker.getId(project));
        project.setDeleted(false);
        projectMapper.insert(project);

//        if (Objects.equals(createReqVO.getType(), ProjectTypeEnum.PROJECT.getValue())) {
//            ProjectDO childrenProject = new ProjectDO();
//            childrenProject.setId(IdWorker.getId(childrenProject));
//            childrenProject.setParentId(project.getId());
//            childrenProject.setCode(project.getCode());
//            childrenProject.setType(ProjectTypeEnum.DEFAULT.getValue());
//            childrenProject.setName("常规");
//            childrenProject.setUseStatus(UseStatusEnum.ENABLE.getStatus());
//            childrenProject.setSort(UseStatusEnum.ENABLE.getStatus());
//            childrenProject.setSpecificationType(project.getSpecificationType());
//            childrenProject.setDeleted(false);
//            projectMapper.insert(childrenProject);
//        }

        // 返回
        return project.getId();
    }

    @Override
    public void updateProject(ProjectSaveReqVO updateReqVO) {
        // 校验存在
        validateProjectExists(updateReqVO.getId());
        // 更新
        ProjectDO updateObj = BeanUtils.toBean(updateReqVO, ProjectDO.class);
        projectMapper.updateById(updateObj);
    }

    @Override
    @Transactional
    public void deleteProject(Long id) {
        // 校验存在
        validateProjectExists(id);
        // 删除
        projectMapper.deleteById(id);
//        projectMapper.delete(new LambdaQueryWrapperX<ProjectDO>().eq(ProjectDO::getParentId, id));
    }

    private void validateProjectExists(Long id) {
        if (projectMapper.selectById(id) == null) {
            throw exception(ErrorCodeConstants.PROJECT_NOT_EXISTS);
        }
    }

    @Override
    public ProjectDO getProject(Long id) {
        return projectMapper.selectById(id);
    }

    @Override
    public PageResult<ProjectDO> getProjectPage(ProjectPageReqVO pageReqVO) {
        return projectMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ProjectDO> getAllProject() {
        return projectMapper.selectList();
    }

    @Override
    public List<ProjectDO> getProjectList(ProjectPageReqVO reqVO) {
        return projectMapper.getProjectList(reqVO);
    }

    @Override
    public List<ProjectDO> getProjectListByTenant(SimpleListReqVO reqVO) {
        return projectMapper.getProjectList(reqVO);
    }

    @Override
    public List<ProjectDO> getProjectList() {
        return projectMapper.getProjectList();
    }
}