package com.yc.ship.module.otc.service.storecreditlog;

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.otc.controller.admin.storecreditlog.vo.StoreCreditLogPageReqVO;
import com.yc.ship.module.otc.controller.admin.storecreditlog.vo.StoreCreditLogSaveReqVO;
import com.yc.ship.module.otc.dal.dataobject.storecreditlog.StoreCreditLogDO;
import com.yc.ship.module.otc.dal.mysql.storecreditlog.StoreCreditLogMapper;
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.otc.enums.ErrorCodeConstants.STORE_CREDIT_LOG_NOT_EXISTS;

/**
 * 旅行社门店授信额度变更 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class StoreCreditLogServiceImpl implements StoreCreditLogService {

    @Resource
    private StoreCreditLogMapper storeCreditLogMapper;


    @Override
    public Long createStoreCreditLog(StoreCreditLogSaveReqVO createReqVO) {
        // 插入
        StoreCreditLogDO storeCreditLog = BeanUtils.toBean(createReqVO, StoreCreditLogDO.class);
        storeCreditLog.setId(IdWorker.getId(storeCreditLog));
        storeCreditLogMapper.insert(storeCreditLog);
        return storeCreditLog.getId();
    }

    @Override
    public void updateStoreCreditLog(StoreCreditLogSaveReqVO updateReqVO) {
        // 校验存在
        validateStoreCreditLogExists(updateReqVO.getId());
        // 更新
        StoreCreditLogDO updateObj = BeanUtils.toBean(updateReqVO, StoreCreditLogDO.class);
        storeCreditLogMapper.updateById(updateObj);
    }

    @Override
    public void deleteStoreCreditLog(Long id) {
        // 校验存在
        validateStoreCreditLogExists(id);
        // 删除
        storeCreditLogMapper.deleteById(id);
    }

    private void validateStoreCreditLogExists(Long id) {
        if (storeCreditLogMapper.selectById(id) == null) {
            throw exception(STORE_CREDIT_LOG_NOT_EXISTS);
        }
    }

    @Override
    public StoreCreditLogDO getStoreCreditLog(Long id) {
        return storeCreditLogMapper.selectById(id);
    }

    @Override
    public PageResult<StoreCreditLogDO> getStoreCreditLogPage(StoreCreditLogPageReqVO pageReqVO) {
        return storeCreditLogMapper.selectPage(pageReqVO);
    }

}
