package cn.iocoder.yudao.module.member.service.invoiceinformation;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.member.controller.admin.invoiceinformation.vo.InvoiceInformationPageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.invoiceinformation.vo.InvoiceInformationSaveReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.invoiceinformation.InvoiceInformationDO;
import cn.iocoder.yudao.module.member.dal.mysql.invoiceinformation.InvoiceInformationMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import cn.iocoder.yudao.framework.common.util.object.BeanUtils;


import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.INVOICE_INFORMATION_NOT_EXISTS;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.INVOICE_INFORMATION_NOT_TAX_EXISTS;

/**
 * 用户发票信息 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class InvoiceInformationServiceImpl implements InvoiceInformationService {

    @Resource
    private InvoiceInformationMapper invoiceInformationMapper;

    @Override
    public Long createInvoiceInformation(InvoiceInformationSaveReqVO createReqVO) {
        String taxNum = createReqVO.getTaxNum();
        List<InvoiceInformationDO> invoiceInformationDOS = invoiceInformationMapper
                .selectList(new LambdaQueryWrapperX<InvoiceInformationDO>()
                .eq(InvoiceInformationDO::getTaxNum, taxNum));
        if(CollUtil.isNotEmpty(invoiceInformationDOS)){
                throw  exception(INVOICE_INFORMATION_NOT_TAX_EXISTS);
        }
        // 插入
        InvoiceInformationDO invoiceInformation = BeanUtils.toBean(createReqVO, InvoiceInformationDO.class);
        invoiceInformationMapper.insert(invoiceInformation);
        // 返回
        return invoiceInformation.getId();
    }

    @Override
    public void updateInvoiceInformation(InvoiceInformationSaveReqVO updateReqVO) {
        // 校验存在
        validateInvoiceInformationExists(updateReqVO.getId());
        String taxNum = updateReqVO.getTaxNum();
        List<InvoiceInformationDO> invoiceInformationDOS = invoiceInformationMapper
                .selectList(new LambdaQueryWrapperX<InvoiceInformationDO>()
                        .ne(InvoiceInformationDO::getId,updateReqVO.getId())
                        .eq(InvoiceInformationDO::getTaxNum, taxNum));
        if(CollUtil.isNotEmpty(invoiceInformationDOS)){
            throw  exception(INVOICE_INFORMATION_NOT_TAX_EXISTS);
        }
        // 更新
        InvoiceInformationDO updateObj = BeanUtils.toBean(updateReqVO, InvoiceInformationDO.class);
        invoiceInformationMapper.updateById(updateObj);
    }

    @Override
    public void deleteInvoiceInformation(Long id) {
        // 校验存在
        validateInvoiceInformationExists(id);
        // 删除
        invoiceInformationMapper.deleteById(id);
    }

    private void validateInvoiceInformationExists(Long id) {
        if (invoiceInformationMapper.selectById(id) == null) {
            throw exception(INVOICE_INFORMATION_NOT_EXISTS);
        }
    }

    @Override
    public InvoiceInformationDO getInvoiceInformation(Long id) {
        return invoiceInformationMapper.selectById(id);
    }

    @Override
    public PageResult<InvoiceInformationDO> getInvoiceInformationPage(InvoiceInformationPageReqVO pageReqVO) {
        return invoiceInformationMapper.selectPage(pageReqVO);
    }

}