package com.jeesharp.business.modules.finance.invoice.service.impl;

import cn.hutool.core.util.StrUtil;
import com.jeesharp.business.modules.finance.invoice.entity.MemberInvoiceTitle;
import com.jeesharp.business.modules.finance.invoice.enums.InvoiceTitleDefaultEnum;
import com.jeesharp.business.modules.finance.invoice.enums.InvoiceTitleTypeEnum;
import com.jeesharp.business.modules.finance.invoice.mapper.MemberInvoiceTitleMapper;
import com.jeesharp.business.modules.finance.invoice.service.MemberInvoiceTitleService;
import com.jeesharp.business.modules.finance.invoice.vo.MemberInvoiceTitleQueryVO;
import com.jeesharp.framework.exception.BizErrorException;
import com.jeesharp.framework.mybatis.core.service.CrudService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 发票抬头领域服务实现。
 *
 * @author jeesharp
 * @since 2026-06-12
 */
@Service
@Transactional(readOnly = true)
public class MemberInvoiceTitleServiceImpl
        extends CrudService<MemberInvoiceTitleMapper, MemberInvoiceTitle, MemberInvoiceTitleQueryVO>
        implements MemberInvoiceTitleService {

    @Override
    @Transactional(readOnly = false)
    public MemberInvoiceTitle createTitle(MemberInvoiceTitle title) {
        if (StrUtil.isNotBlank(title.getId())) {
            throw new BizErrorException("新增抬头不应预置主键");
        }
        normalizeTitle(title);
        if (InvoiceTitleDefaultEnum.YES.getValue().equals(title.getIsDefault())) {
            cancelDefaultTitle(title.getMemberId());
        }
        save(title);
        return title;
    }

    @Override
    @Transactional(readOnly = false)
    public void updateTitle(MemberInvoiceTitle title) {
        if (StrUtil.isBlank(title.getId())) {
            throw new BizErrorException("更新抬头主键不能为空");
        }
        if (get(title.getId()) == null) {
            throw new BizErrorException("抬头不存在");
        }
        normalizeTitle(title);
        if (InvoiceTitleDefaultEnum.YES.getValue().equals(title.getIsDefault())) {
            cancelDefaultTitle(title.getMemberId());
        }
        update(title);
    }

    @Override
    public List<MemberInvoiceTitle> listByMember(String memberId) {
        MemberInvoiceTitleQueryVO query = new MemberInvoiceTitleQueryVO();
        query.setMemberId(memberId);
        return findList(query);
    }

    @Override
    @Transactional(readOnly = false)
    public void setDefaultTitle(String titleId, String memberId) {
        MemberInvoiceTitle title = get(titleId);
        if (title == null || !memberId.equals(title.getMemberId())) {
            throw new BizErrorException("抬头不存在");
        }
        cancelDefaultTitle(memberId);
        title.setIsDefault(InvoiceTitleDefaultEnum.YES.getValue());
        update(title);
    }

    private void cancelDefaultTitle(String memberId) {
        MemberInvoiceTitleQueryVO query = new MemberInvoiceTitleQueryVO();
        query.setMemberId(memberId);
        query.setIsDefault(InvoiceTitleDefaultEnum.YES.getValue());
        for (MemberInvoiceTitle item : findList(query)) {
            item.setIsDefault(InvoiceTitleDefaultEnum.NO.getValue());
            update(item);
        }
    }

    private void normalizeTitle(MemberInvoiceTitle title) {
        if (title.getTitleType() == null) {
            title.setTitleType(InvoiceTitleTypeEnum.PERSONAL.getValue());
        }
        if (title.getIsDefault() == null) {
            title.setIsDefault(InvoiceTitleDefaultEnum.NO.getValue());
        }
        if (InvoiceTitleTypeEnum.COMPANY.getValue().equals(title.getTitleType())
                && StrUtil.isBlank(title.getTaxNo())) {
            throw new BizErrorException("企业抬头须填写税号");
        }
    }
}
