package cn.iocoder.yudao.module.inspection.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.inspection.entity.DlInspectionProject;
import cn.iocoder.yudao.module.inspection.mapper.DlInspectionProjectMapper;
import cn.iocoder.yudao.module.inspection.service.DlInspectionProjectService;
import cn.iocoder.yudao.module.inspection.vo.DlInspectionProjectPageReqVO;
import cn.iocoder.yudao.module.inspection.vo.DlInspectionProjectRespVO;
import cn.iocoder.yudao.module.inspection.vo.DlInspectionProjectSaveReqVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;


/**
 * 检测项目 Service 实现类
 *
 * @author 若依
 */
@Service
@Validated
public class DlInspectionProjectServiceImpl extends ServiceImpl<DlInspectionProjectMapper, DlInspectionProject> implements DlInspectionProjectService {

    @Resource
    private DlInspectionProjectMapper dlInspectionProjectMapper;

    @Override
    public String createDlInspectionProject(DlInspectionProjectSaveReqVO createReqVO) {
        //查询项目名称是否存在
        DlInspectionProject dlInspectionProject1 = baseMapper.selectOne(new LambdaQueryWrapper<DlInspectionProject>()
                .eq(DlInspectionProject::getProjectName, createReqVO.getProjectName()));
        if (ObjectUtil.isNotNull(dlInspectionProject1)) {
            throw new RuntimeException("检测项目已存在");
        }
        // 插入
        DlInspectionProject dlInspectionProject = BeanUtils.toBean(createReqVO, DlInspectionProject.class);
        dlInspectionProject.setDeleted(false);
        dlInspectionProjectMapper.insert(dlInspectionProject);
        // 返回
        return dlInspectionProject.getId();
    }

    @Override
    public void updateDlInspectionProject(DlInspectionProjectSaveReqVO updateReqVO) {

        // 更新
        DlInspectionProject updateObj = BeanUtils.toBean(updateReqVO, DlInspectionProject.class);
        dlInspectionProjectMapper.updateById(updateObj);
    }

    @Override
    public void deleteDlInspectionProject(String id) {
        // 删除
        dlInspectionProjectMapper.deleteById(id);
    }



    @Override
    public DlInspectionProject getDlInspectionProject(String id) {
        return dlInspectionProjectMapper.selectById(id);
    }

    @Override
    public IPage<DlInspectionProjectRespVO> getDlInspectionProjectPage(IPage page, DlInspectionProjectPageReqVO pageReqVO) {
        return baseMapper.selectListPage(page, pageReqVO);
    }

}