package com.cf.imes.module.executor.service.remainplate;

import com.cf.imes.module.executor.dal.dataobject.remainplaten.RemainPlateDO;
import com.cf.imes.module.executor.dal.mysql.remainplaten.RemainPlateMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.cf.imes.module.executor.controller.admin.remainplate.vo.*;
import com.cf.imes.framework.common.pojo.PageResult;
import com.cf.imes.framework.common.pojo.PageParam;
import com.cf.imes.framework.common.util.object.BeanUtils;


import static com.cf.imes.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.cf.imes.module.executor.enums.ErrorCodeConstants.*;
import static com.cf.imes.module.system.enums.ErrorCodeConstants.REMAIN_PLATE_NOT_EXISTS;

/**
 * 生产单余料板表 order_remain_plate_{N} Service 实现类
 *
 * @author 晨丰科技
 */
@Service
@Validated
public class RemainPlateServiceImpl implements RemainPlateService {

    @Resource
    private RemainPlateMapper remainPlateMapper;

    @Override
    public Long createRemainPlate(RemainPlateSaveReqVO createReqVO) {
        // 插入
        RemainPlateDO remainPlate = BeanUtils.toBean(createReqVO, RemainPlateDO.class);
        remainPlateMapper.insert(remainPlate);
        // 返回
        return remainPlate.getId();
    }

    @Override
    public void updateRemainPlate(RemainPlateSaveReqVO updateReqVO) {
        // 校验存在
        validateRemainPlateExists(updateReqVO.getId());
        // 更新
        RemainPlateDO updateObj = BeanUtils.toBean(updateReqVO, RemainPlateDO.class);
        remainPlateMapper.updateById(updateObj);
    }

    @Override
    public void deleteRemainPlate(Long id) {
        // 校验存在
        validateRemainPlateExists(id);
        // 删除
        remainPlateMapper.deleteById(id);
    }

    private void validateRemainPlateExists(Long id) {
        if (remainPlateMapper.selectById(id) == null) {
            throw exception(REMAIN_PLATE_NOT_EXISTS);
        }
    }

    @Override
    public RemainPlateDO getRemainPlate(Long id) {
        return remainPlateMapper.selectById(id);
    }

    @Override
    public PageResult<RemainPlateDO> getRemainPlatePage(RemainPlatePageReqVO pageReqVO) {
        return remainPlateMapper.selectPage(pageReqVO);
    }

}