package cn.newfeifan.mall.module.sale.service.merchantapply;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import cn.newfeifan.mall.module.sale.controller.admin.merchantapply.vo.*;
import cn.newfeifan.mall.module.sale.dal.dataobject.merchantapply.MerchantApplyDO;
import cn.newfeifan.mall.framework.common.pojo.PageResult;
import cn.newfeifan.mall.framework.common.util.object.BeanUtils;

import cn.newfeifan.mall.module.sale.dal.mysql.merchantapply.MerchantApplyMapper;

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

/**
 * 商户申请 Service 实现类
 *
 * @author 非繁人
 */
@Service
@Validated
public class MerchantApplyServiceImpl implements MerchantApplyService {

    @Resource
    private MerchantApplyMapper merchantApplyMapper;

    @Override
    public Long createMerchantApply(MerchantApplySaveReqVO createReqVO) {
        // 插入
        MerchantApplyDO merchantApply = BeanUtils.toBean(createReqVO, MerchantApplyDO.class);
        merchantApplyMapper.insert(merchantApply);
        // 返回
        return merchantApply.getId();
    }

    @Override
    public void updateMerchantApply(MerchantApplySaveReqVO updateReqVO) {
        // 校验存在
        validateMerchantApplyExists(updateReqVO.getId());
        // 更新
        MerchantApplyDO updateObj = BeanUtils.toBean(updateReqVO, MerchantApplyDO.class);
        merchantApplyMapper.updateById(updateObj);
    }

    @Override
    public void deleteMerchantApply(Long id) {
        // 校验存在
        validateMerchantApplyExists(id);
        // 删除
        merchantApplyMapper.deleteById(id);
    }

    private void validateMerchantApplyExists(Long id) {
        if (merchantApplyMapper.selectById(id) == null) {
            throw exception(MERCHANT_APPLY_NOT_EXISTS);
        }
    }

    @Override
    public MerchantApplyDO getMerchantApply(Long id) {
        return merchantApplyMapper.selectById(id);
    }

    @Override
    public PageResult<MerchantApplyDO> getMerchantApplyPage(MerchantApplyPageReqVO pageReqVO) {
        return merchantApplyMapper.selectPage(pageReqVO);
    }

}