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

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import cn.newfeifan.mall.module.trade.controller.app.ptsettlement.vo.*;
import cn.newfeifan.mall.module.trade.dal.dataobject.ptsettlement.PtSettlementDO;
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.ptsettlement.PtSettlementMapper;

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 PtSettlementServiceImpl implements PtSettlementService {

    @Resource
    private PtSettlementMapper ptSettlementMapper;

    @Override
    public Long createPtSettlement(AppPtSettlementSaveReqVO createReqVO) {
        // 插入
        PtSettlementDO ptSettlement = BeanUtils.toBean(createReqVO, PtSettlementDO.class);
        ptSettlementMapper.insert(ptSettlement);
        // 返回
        return ptSettlement.getId();
    }

    @Override
    public void updatePtSettlement(AppPtSettlementSaveReqVO updateReqVO) {
        // 校验存在
        validatePtSettlementExists(updateReqVO.getId());
        // 更新
        PtSettlementDO updateObj = BeanUtils.toBean(updateReqVO, PtSettlementDO.class);
        ptSettlementMapper.updateById(updateObj);
    }

    @Override
    public void deletePtSettlement(Long id) {
        // 校验存在
        validatePtSettlementExists(id);
        // 删除
        ptSettlementMapper.deleteById(id);
    }

    private void validatePtSettlementExists(Long id) {
        if (ptSettlementMapper.selectById(id) == null) {
            throw exception(PT_SETTLEMENT_NOT_EXISTS);
        }
    }

    @Override
    public PtSettlementDO getPtSettlement(Long id) {
        return ptSettlementMapper.selectById(id);
    }

    @Override
    public PageResult<PtSettlementDO> getPtSettlementPage(AppPtSettlementPageReqVO pageReqVO) {
        return ptSettlementMapper.selectPage(pageReqVO);
    }

}