package cn.newfeifan.mall.module.trade.service.shopsettlement;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import cn.newfeifan.mall.module.trade.controller.app.shopsettlement.vo.*;
import cn.newfeifan.mall.module.trade.dal.dataobject.shopsettlement.ShopSettlementDO;
import cn.newfeifan.mall.framework.common.pojo.PageResult;
import cn.newfeifan.mall.framework.common.util.object.BeanUtils;

import cn.newfeifan.mall.module.trade.dal.mysql.shopsettlement.ShopSettlementMapper;

import static cn.newfeifan.mall.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.newfeifan.mall.module.trade.enums.ErrorCodeConstants.*;

/**
 * 订单结算信息表，记录店铺的结算信息 Service 实现类
 *
 * @author 非繁人
 */
@Service
@Validated
public class ShopSettlementServiceImpl implements ShopSettlementService {

    @Resource
    private ShopSettlementMapper shopSettlementMapper;

    @Override
    public Long createShopSettlement(AppShopSettlementSaveReqVO createReqVO) {
        // 插入
        ShopSettlementDO shopSettlement = BeanUtils.toBean(createReqVO, ShopSettlementDO.class);
        shopSettlementMapper.insert(shopSettlement);
        // 返回
        return shopSettlement.getId();
    }

    @Override
    public void updateShopSettlement(AppShopSettlementSaveReqVO updateReqVO) {
        // 校验存在
        validateShopSettlementExists(updateReqVO.getId());
        // 更新
        ShopSettlementDO updateObj = BeanUtils.toBean(updateReqVO, ShopSettlementDO.class);
        shopSettlementMapper.updateById(updateObj);
    }

    @Override
    public void deleteShopSettlement(Long id) {
        // 校验存在
        validateShopSettlementExists(id);
        // 删除
        shopSettlementMapper.deleteById(id);
    }

    private void validateShopSettlementExists(Long id) {
        if (shopSettlementMapper.selectById(id) == null) {
            throw exception(SHOP_SETTLEMENT_NOT_EXISTS);
        }
    }

    @Override
    public ShopSettlementDO getShopSettlement(Long id) {
        return shopSettlementMapper.selectById(id);
    }

    @Override
    public PageResult<ShopSettlementDO> getShopSettlementPage(AppShopSettlementPageReqVO pageReqVO) {
        return shopSettlementMapper.selectPage(pageReqVO);
    }

}