package cn.iocoder.yudao.module.mes.service.moldsubject;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.common.dal.dataobject.mold.MoldDO;
import cn.iocoder.yudao.module.iot.dal.dataobject.devicemodel.DeviceModelDO;
import cn.iocoder.yudao.module.mes.controller.admin.moldsubject.vo.MoldSubjectPageReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.moldsubject.vo.MoldSubjectSaveReqVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.devicetype.DeviceTypeDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.moldsubject.MoldSubjectDO;
import cn.iocoder.yudao.module.mes.dal.mysql.moldsubject.MoldSubjectMapper;
import cn.iocoder.yudao.module.mes.service.moldsubject.MoldSubjectService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.iot.enums.ErrorCodeConstants.DEVICE_MODEL_CODE_EXISTS;
import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.*;

/**
 * 维保项目 Service 实现类
 *
 * @author 内蒙必硕
 */
@Service
@Validated
public class MoldSubjectServiceImpl implements MoldSubjectService {

    @Resource
    private MoldSubjectMapper moldSubjectMapper;

    @Override
    public Long createMoldSubject(MoldSubjectSaveReqVO createReqVO) {

        validateCodeOnly(createReqVO.getSubjectCode());
        // 插入
        MoldSubjectDO moldSubject = BeanUtils.toBean(createReqVO, MoldSubjectDO.class);
        moldSubjectMapper.insert(moldSubject);
        // 返回
        return moldSubject.getId();
    }

    private void validateCodeOnly( String code) {
        if (moldSubjectMapper.exists(Wrappers.<MoldSubjectDO>lambdaQuery()
                .eq(MoldSubjectDO::getSubjectCode, code))) {
            throw exception(SUBJECT_EXISTS);
        }
    }

    @Override
    public void updateMoldSubject(MoldSubjectSaveReqVO updateReqVO) {
        // 校验存在
        validateMoldSubjectExists(updateReqVO.getId());

        //编码重复判断
        Long count = moldSubjectMapper.selectCount(new LambdaQueryWrapper<MoldSubjectDO>()
                .eq(MoldSubjectDO::getSubjectCode, updateReqVO.getSubjectCode())
                .ne(MoldSubjectDO::getId, updateReqVO.getId())
        );
        if (count > 0) {
            throw exception(SUBJECT_EXISTS);
        }
        // 更新
        MoldSubjectDO updateObj = BeanUtils.toBean(updateReqVO, MoldSubjectDO.class);
        moldSubjectMapper.updateById(updateObj);
    }

    @Override
    public void deleteMoldSubject(List<Long> idList) {

        for (Long id : idList) {
            // 校验存在
            validateMoldSubjectExists(id);
        }

        // 删除
        moldSubjectMapper.deleteByIds(idList);
    }

    private void validateMoldSubjectExists(Long id) {
        if (moldSubjectMapper.selectById(id) == null) {
            throw exception(DV_SUBJECT_NOT_EXISTS);
        }
    }

    @Override
    public MoldSubjectDO getMoldSubject(Long id) {
        return moldSubjectMapper.selectById(id);
    }

    @Override
    public PageResult<MoldSubjectDO> getMoldSubjectPage(MoldSubjectPageReqVO pageReqVO) {
        return moldSubjectMapper.selectPage(pageReqVO);
    }

    @Override
    public List<MoldSubjectDO> getAllList() {
        return moldSubjectMapper.selectList();
    }

}