package com.yc.ship.module.product.service.productCheckRule;

import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.common.util.object.BeanUtils;
import com.yc.ship.module.product.controller.admin.productSpu.checkRuleVO.ProductCheckRulePageReqVO;
import com.yc.ship.module.product.controller.admin.productSpu.checkRuleVO.ProductCheckRuleSaveReqVO;
import com.yc.ship.module.product.dal.dataobject.productCheckRule.ProductCheckRuleDO;
import com.yc.ship.module.product.dal.mysql.productCheckRule.ProductCheckRuleMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import static com.yc.ship.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.yc.ship.module.product.enums.ErrorCodeConstants.CHECK_RULE_NOT_EXISTS;


/**
 * 产品核销规则 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class ProductCheckRuleServiceImpl implements ProductCheckRuleService {

    @Resource
    private ProductCheckRuleMapper checkRuleMapper;

    @Override
    public Long createCheckRule(ProductCheckRuleSaveReqVO createReqVO) {
        // 插入
        ProductCheckRuleDO checkRule = BeanUtils.toBean(createReqVO, ProductCheckRuleDO.class);
        checkRuleMapper.insert(checkRule);
        // 返回
        return checkRule.getId();
    }

    @Override
    public void updateCheckRule(ProductCheckRuleSaveReqVO updateReqVO) {
        // 校验存在
        validateCheckRuleExists(updateReqVO.getId());
        // 更新
        ProductCheckRuleDO updateObj = BeanUtils.toBean(updateReqVO, ProductCheckRuleDO.class);
        checkRuleMapper.updateById(updateObj);
    }

    @Override
    public void deleteCheckRule(Long id) {
        // 校验存在
        validateCheckRuleExists(id);
        // 删除
        checkRuleMapper.deleteById(id);
    }

    private void validateCheckRuleExists(Long id) {
        if (checkRuleMapper.selectById(id) == null) {
            throw exception(CHECK_RULE_NOT_EXISTS);
        }
    }

    @Override
    public ProductCheckRuleDO getCheckRule(Long id) {
        return checkRuleMapper.selectById(id);
    }

    @Override
    public PageResult<ProductCheckRuleDO> getCheckRulePage(ProductCheckRulePageReqVO pageReqVO) {
        return checkRuleMapper.selectPage(pageReqVO);
    }

}