package com.yc.ship.module.trade.service.invoice.impl;

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.trade.controller.admin.invoicelog.vo.InvoiceLogPageReqVO;
import com.yc.ship.module.trade.controller.admin.invoicelog.vo.InvoiceLogSaveReqVO;
import com.yc.ship.module.trade.dal.dataobject.invoice.InvoiceLogDO;
import com.yc.ship.module.trade.dal.mysql.invoice.InvoiceLogMapper;
import com.yc.ship.module.trade.service.invoice.InvoiceLogService;
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.trade.enums.ErrorCodeConstants.INVOICE_LOG_NOT_EXISTS;


/**
 * 发票记录 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class InvoiceLogServiceImpl implements InvoiceLogService {

    @Resource
    private InvoiceLogMapper invoiceLogMapper;

    @Override
    public void saveInvoiceLog(Long invoiceId, String type, String url, String params, String reqContent,String resContent) {
        InvoiceLogDO invoiceLog = new InvoiceLogDO();
        Long id = IdWorker.getId(invoiceLog);
        invoiceLog.setId(id);
        invoiceLog.setInvoiceId(invoiceId);
        invoiceLog.setParams(params);
        invoiceLog.setType(type);
        invoiceLog.setState(-1);
        invoiceLog.setUrl(url);
        invoiceLog.setParams(params);
        invoiceLog.setReqContent(reqContent);
        invoiceLog.setResContent(resContent);
        invoiceLog.setDeleted(false);
        invoiceLogMapper.insert(invoiceLog);
    }

    @Override
    public Long createInvoiceLog(InvoiceLogSaveReqVO createReqVO) {
        // 插入
        InvoiceLogDO invoiceLog = BeanUtils.toBean(createReqVO, InvoiceLogDO.class);
        invoiceLogMapper.insert(invoiceLog);
        // 返回
        return invoiceLog.getId();
    }

    @Override
    public void updateInvoiceLog(InvoiceLogSaveReqVO updateReqVO) {
        // 校验存在
        validateInvoiceLogExists(updateReqVO.getId());
        // 更新
        InvoiceLogDO updateObj = BeanUtils.toBean(updateReqVO, InvoiceLogDO.class);
        invoiceLogMapper.updateById(updateObj);
    }


    @Override
    public void deleteInvoiceLog(Long id) {
        // 校验存在
        validateInvoiceLogExists(id);
        // 删除
        invoiceLogMapper.deleteById(id);
    }

    private void validateInvoiceLogExists(Long id) {
        if (invoiceLogMapper.selectById(id) == null) {
            throw exception(INVOICE_LOG_NOT_EXISTS);
        }
    }

    @Override
    public InvoiceLogDO getInvoiceLog(Long id) {
        return invoiceLogMapper.selectById(id);
    }

    @Override
    public PageResult<InvoiceLogDO> getInvoiceLogPage(InvoiceLogPageReqVO pageReqVO) {
        return invoiceLogMapper.selectPage(pageReqVO);
    }

}