package com.yc.ship.module.ota.service.distributorauditlog;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.common.util.object.BeanUtils;
import com.yc.ship.module.ota.controller.admin.distributorauditlog.vo.DistributorAuditLogPageReqVO;
import com.yc.ship.module.ota.controller.admin.distributorauditlog.vo.DistributorAuditLogSaveReqVO;
import com.yc.ship.module.ota.dal.dataobject.distributorauditlog.DistributorAuditLogDO;
import com.yc.ship.module.ota.dal.mysql.distributorauditlog.DistributorAuditLogMapper;
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.ota.enums.ErrorCodeConstants.DISTRIBUTOR_AUDIT_LOG_NOT_EXISTS;


/**
 * 分销商授信额度变更 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class DistributorAuditLogServiceImpl implements DistributorAuditLogService {

    @Resource
    private DistributorAuditLogMapper distributorAuditLogMapper;

    @Override
    public Long createDistributorAuditLog(DistributorAuditLogSaveReqVO createReqVO) {
        // 插入
        DistributorAuditLogDO distributorAuditLog = BeanUtils.toBean(createReqVO, DistributorAuditLogDO.class);
        distributorAuditLog.setId(IdWorker.getId(distributorAuditLog));
        distributorAuditLogMapper.insert(distributorAuditLog);
        // 返回
        return distributorAuditLog.getId();
    }

    @Override
    public void updateDistributorAuditLog(DistributorAuditLogSaveReqVO updateReqVO) {
        // 校验存在
        validateDistributorAuditLogExists(updateReqVO.getId());
        // 更新
        DistributorAuditLogDO updateObj = BeanUtils.toBean(updateReqVO, DistributorAuditLogDO.class);
        distributorAuditLogMapper.updateById(updateObj);
    }

    @Override
    public void deleteDistributorAuditLog(Long id) {
        // 校验存在
        validateDistributorAuditLogExists(id);
        // 删除
        distributorAuditLogMapper.deleteById(id);
    }

    private void validateDistributorAuditLogExists(Long id) {
        if (distributorAuditLogMapper.selectById(id) == null) {
            throw exception(DISTRIBUTOR_AUDIT_LOG_NOT_EXISTS);
        }
    }

    @Override
    public DistributorAuditLogDO getDistributorAuditLog(Long id) {
        return distributorAuditLogMapper.selectById(id);
    }

    @Override
    public PageResult<DistributorAuditLogDO> getDistributorAuditLogPage(DistributorAuditLogPageReqVO pageReqVO) {
        return distributorAuditLogMapper.selectPage(pageReqVO);
    }

}
