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

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.mes.controller.admin.record.vo.RecordTemplatePageReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.record.vo.RecordTemplateSaveReqVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.record.*;
import cn.iocoder.yudao.module.mes.dal.mysql.record.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

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

/**
 * 记录表 Service 实现类
 *
 * @author 内蒙必硕
 */
@Service
@Validated
public class RecordTemplateServiceImpl implements RecordTemplateService {

    @Resource
    private RecordTemplateMapper recordTemplateMapper;
    @Resource
    private RecordChengxingMapper recordChengxingMapper;
    @Resource
    private RecordSuijiangMapper recordSuijiangMapper;
    @Resource
    private RecordReyaMapper recordReyaMapper;
    @Resource
    private RecordQiebianMapper recordQiebianMapper;

    @Override
    public Long createRecordTemplate(RecordTemplateSaveReqVO createReqVO) {
        // 插入
        RecordTemplateDO recordTemplate = BeanUtils.toBean(createReqVO, RecordTemplateDO.class);
        recordTemplateMapper.insert(recordTemplate);
        // 返回
        return recordTemplate.getId();
    }

    @Override
    public void updateRecordTemplate(RecordTemplateSaveReqVO updateReqVO) {
        // 校验存在
        validateRecordTemplateExists(updateReqVO.getId());
        // 更新
        RecordTemplateDO updateObj = BeanUtils.toBean(updateReqVO, RecordTemplateDO.class);
        recordTemplateMapper.updateById(updateObj);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteRecordTemplate(Long id) {
        // 校验存在
        validateRecordTemplateExists(id);
        // 删除
        recordTemplateMapper.deleteById(id);

        // 删除子表
        deleteRecordChengxingByRecordId(id);
    }

    private void validateRecordTemplateExists(Long id) {
        if (recordTemplateMapper.selectById(id) == null) {
            throw exception(RECORD_TEMPLATE_NOT_EXISTS);
        }
    }

    @Override
    public RecordTemplateDO getRecordTemplate(Long id) {
        return recordTemplateMapper.selectById(id);
    }

    @Override
    public PageResult<RecordTemplateDO> getRecordTemplatePage(RecordTemplatePageReqVO pageReqVO) {
        return recordTemplateMapper.selectPage(pageReqVO);
    }
// ==================== 子表（碎浆记录） ====================

    @Override
    public PageResult<RecordSuijiangDO> getRecordSuijiangPage(PageParam pageReqVO, Long recordId) {
        return recordSuijiangMapper.selectPage(pageReqVO, recordId);
    }

    @Override
    public Long createRecordSuijiang(RecordSuijiangDO recordSuijiang) {
        recordSuijiangMapper.insert(recordSuijiang);
        return recordSuijiang.getId();
    }

    @Override
    public void updateRecordSuijiang(RecordSuijiangDO recordSuijiang) {
        // 校验存在
        validateRecordSuijiangExists(recordSuijiang.getId());
        // 更新
        recordSuijiangMapper.updateById(recordSuijiang);
    }

    @Override
    public void deleteRecordSuijiang(Long id) {
        // 校验存在
        validateRecordSuijiangExists(id);
        // 删除
        recordSuijiangMapper.deleteById(id);
    }

    @Override
    public RecordSuijiangDO getRecordSuijiang(Long id) {
        return recordSuijiangMapper.selectById(id);
    }

    private void validateRecordSuijiangExists(Long id) {
        if (recordSuijiangMapper.selectById(id) == null) {
            throw exception(RECORD_SUIJIANG_NOT_EXISTS);
        }
    }

    private void deleteRecordSuijiangByRecordId(Long recordId) {
        recordSuijiangMapper.deleteByRecordId(recordId);
    }
    // ==================== 子表（成型记录） ====================

    @Override
    public PageResult<RecordChengxingDO> getRecordChengxingPage(PageParam pageReqVO, Long recordId) {
        return recordChengxingMapper.selectPage(pageReqVO, recordId);
    }

    @Override
    public Long createRecordChengxing(RecordChengxingDO recordChengxing) {
        recordChengxingMapper.insert(recordChengxing);
        return recordChengxing.getId();
    }

    @Override
    public void updateRecordChengxing(RecordChengxingDO recordChengxing) {
        // 校验存在
        validateRecordChengxingExists(recordChengxing.getId());
        // 更新
        recordChengxingMapper.updateById(recordChengxing);
    }

    @Override
    public void deleteRecordChengxing(Long id) {
        // 校验存在
        validateRecordChengxingExists(id);
        // 删除
        recordChengxingMapper.deleteById(id);
    }

    @Override
    public RecordChengxingDO getRecordChengxing(Long id) {
        return recordChengxingMapper.selectById(id);
    }

    private void validateRecordChengxingExists(Long id) {
        if (recordChengxingMapper.selectById(id) == null) {
            throw exception(RECORD_CHENGXING_NOT_EXISTS);
        }
    }

    private void deleteRecordChengxingByRecordId(Long recordId) {
        recordChengxingMapper.deleteByRecordId(recordId);
    }

    // ==================== 子表（热压记录） ====================

    @Override
    public PageResult<RecordReyaDO> getRecordReyaPage(PageParam pageReqVO, Long recordId) {
        return recordReyaMapper.selectPage(pageReqVO, recordId);
    }

    @Override
    public Long createRecordReya(RecordReyaDO recordReya) {
        recordReyaMapper.insert(recordReya);
        return recordReya.getId();
    }

    @Override
    public void updateRecordReya(RecordReyaDO recordReya) {
        // 校验存在
        validateRecordReyaExists(recordReya.getId());
        // 更新
        recordReyaMapper.updateById(recordReya);
    }

    @Override
    public void deleteRecordReya(Long id) {
        // 校验存在
        validateRecordReyaExists(id);
        // 删除
        recordReyaMapper.deleteById(id);
    }

    @Override
    public RecordReyaDO getRecordReya(Long id) {
        return recordReyaMapper.selectById(id);
    }

    private void validateRecordReyaExists(Long id) {
        if (recordReyaMapper.selectById(id) == null) {
            throw exception(RECORD_REYA_NOT_EXISTS);
        }
    }

    private void deleteRecordReyaByRecordId(Long recordId) {
        recordReyaMapper.deleteByRecordId(recordId);
    }

    // ==================== 子表（切边记录） ====================

    @Override
    public PageResult<RecordQiebianDO> getRecordQiebianPage(PageParam pageReqVO, Long recordId) {
        return recordQiebianMapper.selectPage(pageReqVO, recordId);
    }

    @Override
    public Long createRecordQiebian(RecordQiebianDO recordQiebian) {
        recordQiebianMapper.insert(recordQiebian);
        return recordQiebian.getId();
    }

    @Override
    public void updateRecordQiebian(RecordQiebianDO recordQiebian) {
        // 校验存在
        validateRecordQiebianExists(recordQiebian.getId());
        // 更新
        recordQiebianMapper.updateById(recordQiebian);
    }

    @Override
    public void deleteRecordQiebian(Long id) {
        // 校验存在
        validateRecordQiebianExists(id);
        // 删除
        recordQiebianMapper.deleteById(id);
    }

    @Override
    public RecordQiebianDO getRecordQiebian(Long id) {
        return recordQiebianMapper.selectById(id);
    }

    private void validateRecordQiebianExists(Long id) {
        if (recordQiebianMapper.selectById(id) == null) {
            throw exception(RECORD_QIEBIAN_NOT_EXISTS);
        }
    }

    private void deleteRecordQiebianByRecordId(Long recordId) {
        recordQiebianMapper.deleteByRecordId(recordId);
    }

    @Resource
    private RecordDabaoMapper recordDabaoMapper;
    // ==================== 子表（打包记录） ====================

    @Override
    public PageResult<RecordDabaoDO> getRecordDabaoPage(PageParam pageReqVO, Long recordId) {
        return recordDabaoMapper.selectPage(pageReqVO, recordId);
    }

    @Override
    public Long createRecordDabao(RecordDabaoDO recordDabao) {
        recordDabaoMapper.insert(recordDabao);
        return recordDabao.getId();
    }

    @Override
    public void updateRecordDabao(RecordDabaoDO recordDabao) {
        // 校验存在
        validateRecordDabaoExists(recordDabao.getId());
        // 更新
        recordDabaoMapper.updateById(recordDabao);
    }

    @Override
    public void deleteRecordDabao(Long id) {
        // 校验存在
        validateRecordDabaoExists(id);
        // 删除
        recordDabaoMapper.deleteById(id);
    }

    @Override
    public RecordDabaoDO getRecordDabao(Long id) {
        return recordDabaoMapper.selectById(id);
    }

    private void validateRecordDabaoExists(Long id) {
        if (recordDabaoMapper.selectById(id) == null) {
            throw exception(RECORD_DABAO_NOT_EXISTS);
        }
    }

    private void deleteRecordDabaoByRecordId(Long recordId) {
        recordDabaoMapper.deleteByRecordId(recordId);
    }

    @Resource
    private RecordDajiangMapper recordDajiangMapper;
    // ==================== 子表（打浆记录） ====================

    @Override
    public PageResult<RecordDajiangDO> getRecordDajiangPage(PageParam pageReqVO, Long recordId) {
        return recordDajiangMapper.selectPage(pageReqVO, recordId);
    }

    @Override
    public Long createRecordDajiang(RecordDajiangDO recordDajiang) {
        recordDajiangMapper.insert(recordDajiang);
        return recordDajiang.getId();
    }

    @Override
    public void updateRecordDajiang(RecordDajiangDO recordDajiang) {
        // 校验存在
        validateRecordDajiangExists(recordDajiang.getId());
        // 更新
        recordDajiangMapper.updateById(recordDajiang);
    }

    @Override
    public void deleteRecordDajiang(Long id) {
        // 校验存在
        validateRecordDajiangExists(id);
        // 删除
        recordDajiangMapper.deleteById(id);
    }

    @Override
    public RecordDajiangDO getRecordDajiang(Long id) {
        return recordDajiangMapper.selectById(id);
    }

    private void validateRecordDajiangExists(Long id) {
        if (recordDajiangMapper.selectById(id) == null) {
            throw exception(RECORD_DAJIANG_NOT_EXISTS);
        }
    }

    private void deleteRecordDajiangByRecordId(Long recordId) {
        recordDajiangMapper.deleteByRecordId(recordId);
    }

    @Resource
    private RecordGanzaoMapper recordGanzaoMapper;
    // ==================== 子表（干燥记录） ====================

    @Override
    public PageResult<RecordGanzaoDO> getRecordGanzaoPage(PageParam pageReqVO, Long recordId) {
        return recordGanzaoMapper.selectPage(pageReqVO, recordId);
    }

    @Override
    public Long createRecordGanzao(RecordGanzaoDO recordGanzao) {
        recordGanzaoMapper.insert(recordGanzao);
        return recordGanzao.getId();
    }

    @Override
    public void updateRecordGanzao(RecordGanzaoDO recordGanzao) {
        // 校验存在
        validateRecordGanzaoExists(recordGanzao.getId());
        // 更新
        recordGanzaoMapper.updateById(recordGanzao);
    }

    @Override
    public void deleteRecordGanzao(Long id) {
        // 校验存在
        validateRecordGanzaoExists(id);
        // 删除
        recordGanzaoMapper.deleteById(id);
    }

    @Override
    public RecordGanzaoDO getRecordGanzao(Long id) {
        return recordGanzaoMapper.selectById(id);
    }

    private void validateRecordGanzaoExists(Long id) {
        if (recordGanzaoMapper.selectById(id) == null) {
            throw exception(RECORD_GANZAO_NOT_EXISTS);
        }
    }

    private void deleteRecordGanzaoByRecordId(Long recordId) {
        recordGanzaoMapper.deleteByRecordId(recordId);
    }

    @Resource
    private RecordJiashiMapper recordJiashiMapper;
    // ==================== 子表（加湿记录） ====================

    @Override
    public PageResult<RecordJiashiDO> getRecordJiashiPage(PageParam pageReqVO, Long recordId) {
        return recordJiashiMapper.selectPage(pageReqVO, recordId);
    }

    @Override
    public Long createRecordJiashi(RecordJiashiDO recordJiashi) {
        recordJiashiMapper.insert(recordJiashi);
        return recordJiashi.getId();
    }

    @Override
    public void updateRecordJiashi(RecordJiashiDO recordJiashi) {
        // 校验存在
        validateRecordJiashiExists(recordJiashi.getId());
        // 更新
        recordJiashiMapper.updateById(recordJiashi);
    }

    @Override
    public void deleteRecordJiashi(Long id) {
        // 校验存在
        validateRecordJiashiExists(id);
        // 删除
        recordJiashiMapper.deleteById(id);
    }

    @Override
    public RecordJiashiDO getRecordJiashi(Long id) {
        return recordJiashiMapper.selectById(id);
    }

    private void validateRecordJiashiExists(Long id) {
        if (recordJiashiMapper.selectById(id) == null) {
            throw exception(RECORD_JIASHI_NOT_EXISTS);
        }
    }

    private void deleteRecordJiashiByRecordId(Long recordId) {
        recordJiashiMapper.deleteByRecordId(recordId);
    }

    // ==================== 子表（检验记录） ====================
    @Resource
    private RecordPinjianMapper recordPinjianMapper;

    @Override
    public PageResult<RecordPinjianDO> getRecordPinjianPage(PageParam pageReqVO, Long recordId) {
        return recordPinjianMapper.selectPage(pageReqVO, recordId);
    }

    @Override
    public Long createRecordPinjian(RecordPinjianDO recordPinjian) {
        recordPinjianMapper.insert(recordPinjian);
        return recordPinjian.getId();
    }

    @Override
    public void updateRecordPinjian(RecordPinjianDO recordPinjian) {
        // 校验存在
        validateRecordPinjianExists(recordPinjian.getId());
        // 更新
        recordPinjianMapper.updateById(recordPinjian);
    }

    @Override
    public void deleteRecordPinjian(Long id) {
        // 校验存在
        validateRecordPinjianExists(id);
        // 删除
        recordPinjianMapper.deleteById(id);
    }

    @Override
    public RecordPinjianDO getRecordPinjian(Long id) {
        return recordPinjianMapper.selectById(id);
    }

    private void validateRecordPinjianExists(Long id) {
        if (recordPinjianMapper.selectById(id) == null) {
            throw exception(RECORD_PINJIAN_NOT_EXISTS);
        }
    }

    private void deleteRecordPinjianByRecordId(Long recordId) {
        recordPinjianMapper.deleteByRecordId(recordId);
    }

}