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

import com.baomidou.mybatisplus.core.metadata.IPage;
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.framework.mybatis.core.util.MyBatisUtils;
import com.yc.ship.framework.tenant.core.context.TenantContextHolder;
import com.yc.ship.module.otc.controller.admin.storeplatcreditlog.vo.StorePlatCreditLogPageReqVO;
import com.yc.ship.module.otc.controller.admin.storeplatcreditlog.vo.StorePlatCreditLogRespVO;
import com.yc.ship.module.otc.controller.admin.storeplatcreditlog.vo.StorePlatCreditLogSaveReqVO;
import com.yc.ship.module.otc.dal.dataobject.storecreditlog.StorePlatCreditLogDO;
import com.yc.ship.module.otc.dal.mysql.storecreditlog.StorePlatCreditLogMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

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

    @Resource
    private StorePlatCreditLogMapper storePlatCreditLogMapper;

    @Override
    public Long createStorePlatCreditLog(StorePlatCreditLogSaveReqVO createReqVO) {
        // 插入
        StorePlatCreditLogDO storePlatCreditLog = BeanUtils.toBean(createReqVO, StorePlatCreditLogDO.class);
        storePlatCreditLog.setId(IdWorker.getId(storePlatCreditLog));
        storePlatCreditLogMapper.insert(storePlatCreditLog);
        // 返回
        return storePlatCreditLog.getId();
    }


    @Override
    public StorePlatCreditLogDO getStorePlatCreditLog(Long id) {
        return storePlatCreditLogMapper.selectById(id);
    }

    @Override
    public PageResult<StorePlatCreditLogDO> getStorePlatCreditLogPage(StorePlatCreditLogPageReqVO pageReqVO) {
        pageReqVO.setTenantId(TenantContextHolder.getTenantId());
        TenantContextHolder.setIgnore(true);
        IPage<StorePlatCreditLogRespVO> page = MyBatisUtils.buildPage(pageReqVO);
        IPage iPage = storePlatCreditLogMapper.selectLogPage(page, pageReqVO);
        TenantContextHolder.setIgnore(false);
        return new PageResult<>(iPage.getRecords(), iPage.getTotal());
    }

}