package com.persagy.dmp.codetable.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.persagy.dmp.codetable.dao.CodeTableTypeMapper;
import com.persagy.dmp.codetable.entity.CodeTableType;
import com.persagy.dmp.codetable.service.ICodeTableTypeService;
import com.persagy.dmp.common.constant.ValidEnum;
import com.persagy.dmp.common.exception.BusinessException;
import com.persagy.dmp.mybatis.utils.ConditionUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 码表类型 实现类
 * @author Charlie Yu
 * @date 2021-07-27
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class CodeTableTypeServiceImpl implements ICodeTableTypeService {

    @Resource
    private CodeTableTypeMapper dao;

    @Override
    public CodeTableType insert(CodeTableType vo) {
        dao.insert(vo);
        return vo;
    }

    @Override
    public CodeTableType update(CodeTableType vo) {
        dao.updateById(vo);
        return vo;
    }

    @Override
    public void delete(String id) {
        CodeTableType vo = load(id);
        if(vo == null) {
            throw new BusinessException("找不到对应的数据！");
        }
        vo.setValid(ValidEnum.FALSE.getType());
        dao.updateById(vo);
    }

    @Override
    public CodeTableType load(String id) {
        return dao.selectById(id);
    }

    @Override
    public CodeTableType loadByCode(String projectId, String code) {
        LambdaQueryWrapper<CodeTableType> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CodeTableType::getCode, code);
        wrapper.eq(CodeTableType::getValid, ValidEnum.TRUE.getType());
        ConditionUtil.ensureProjectCriteria(wrapper, CodeTableType::getProjectId, projectId);
        return dao.selectOne(wrapper);
    }

    @Override
    public List<CodeTableType> queryByCondition(Wrapper<CodeTableType> queryWrapper) {
        return dao.selectList(queryWrapper);
    }
}
