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

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.changerecord.vo.ProduceReportChangeRecordPageReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.changerecord.vo.ProduceReportChangeRecordSaveReqVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.changerecord.ProduceReportChangeRecordDO;
import cn.iocoder.yudao.module.mes.dal.mysql.changerecord.ProduceReportChangeRecordMapper;
import org.springframework.stereotype.Service;
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.PRODUCE_REPORT_CHANGE_RECORD_NOT_EXISTS;

/**
 * 报工变更记录 Service 实现类
 *
 * @author 内蒙必硕
 */
@Service
@Validated
public class ProduceReportChangeRecordServiceImpl implements ProduceReportChangeRecordService {

    @Resource
    private ProduceReportChangeRecordMapper produceReportChangeRecordMapper;

    @Override
    public Long createProduceReportChangeRecord(ProduceReportChangeRecordSaveReqVO createReqVO) {
        // 插入
        ProduceReportChangeRecordDO produceReportChangeRecord = BeanUtils.toBean(createReqVO, ProduceReportChangeRecordDO.class);
        produceReportChangeRecordMapper.insert(produceReportChangeRecord);
        // 返回
        return produceReportChangeRecord.getId();
    }

    @Override
    public void updateProduceReportChangeRecord(ProduceReportChangeRecordSaveReqVO updateReqVO) {
        // 校验存在
        validateProduceReportChangeRecordExists(updateReqVO.getId());
        // 更新
        ProduceReportChangeRecordDO updateObj = BeanUtils.toBean(updateReqVO, ProduceReportChangeRecordDO.class);
        produceReportChangeRecordMapper.updateById(updateObj);
    }

    @Override
    public void deleteProduceReportChangeRecord(Long id) {
        // 校验存在
        validateProduceReportChangeRecordExists(id);
        // 删除
        produceReportChangeRecordMapper.deleteById(id);
    }

    private void validateProduceReportChangeRecordExists(Long id) {
        if (produceReportChangeRecordMapper.selectById(id) == null) {
            throw exception(PRODUCE_REPORT_CHANGE_RECORD_NOT_EXISTS);
        }
    }

    @Override
    public ProduceReportChangeRecordDO getProduceReportChangeRecord(Long id) {
        return produceReportChangeRecordMapper.selectById(id);
    }

    @Override
    public PageResult<ProduceReportChangeRecordDO> getProduceReportChangeRecordPage(ProduceReportChangeRecordPageReqVO pageReqVO) {
        return produceReportChangeRecordMapper.selectPage(pageReqVO);
    }

    @Override
    public int saveChange(Long reportId,String beforeString, String afterString, String changeType) {
        ProduceReportChangeRecordDO recordDO = new ProduceReportChangeRecordDO();
        recordDO.setReportId(reportId);
        recordDO.setBeforeChange(beforeString);
        recordDO.setAfterChange(afterString);
        recordDO.setChangeType(changeType);
        return produceReportChangeRecordMapper.insert(recordDO);
    }

}