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

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.otherRuleVO.ProductOtherRulePageReqVO;
import com.yc.ship.module.product.controller.admin.productSpu.otherRuleVO.ProductOtherRuleSaveReqVO;
import com.yc.ship.module.product.dal.dataobject.productOtherRule.ProductOtherRuleDO;
import com.yc.ship.module.product.dal.mysql.productOtherRule.ProductOtherRuleMapper;
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.OTHER_RULE_NOT_EXISTS;


/**
 * 产品其他规则 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class ProductOtherRuleServiceImpl implements ProductOtherRuleService {

    @Resource
    private ProductOtherRuleMapper otherRuleMapper;

    @Override
    public Long createOtherRule(ProductOtherRuleSaveReqVO createReqVO) {
        // 插入
        ProductOtherRuleDO otherRule = BeanUtils.toBean(createReqVO, ProductOtherRuleDO.class);
        otherRuleMapper.insert(otherRule);
        // 返回
        return otherRule.getId();
    }

    @Override
    public void updateOtherRule(ProductOtherRuleSaveReqVO updateReqVO) {
        // 校验存在
        validateOtherRuleExists(updateReqVO.getId());
        // 更新
        ProductOtherRuleDO updateObj = BeanUtils.toBean(updateReqVO, ProductOtherRuleDO.class);
        otherRuleMapper.updateById(updateObj);
    }

    @Override
    public void deleteOtherRule(Long id) {
        // 校验存在
        validateOtherRuleExists(id);
        // 删除
        otherRuleMapper.deleteById(id);
    }

    private void validateOtherRuleExists(Long id) {
        if (otherRuleMapper.selectById(id) == null) {
            throw exception(OTHER_RULE_NOT_EXISTS);
        }
    }

    @Override
    public ProductOtherRuleDO getOtherRule(Long id) {
        return otherRuleMapper.selectById(id);
    }

    @Override
    public PageResult<ProductOtherRuleDO> getOtherRulePage(ProductOtherRulePageReqVO pageReqVO) {
        return otherRuleMapper.selectPage(pageReqVO);
    }

}