package com.jsowell.pile.service.impl;

import com.jsowell.common.enums.TitleTypeEnum;
import com.jsowell.common.util.StringUtils;
import com.jsowell.pile.domain.MemberInvoiceTitle;
import com.jsowell.pile.mapper.MemberInvoiceTitleMapper;
import com.jsowell.pile.service.IMemberInvoiceTitleService;
import com.jsowell.pile.vo.uniapp.InvoiceTitleVO;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 会员发票抬头Service业务层处理
 *
 * @author jsowell
 * @date 2023-04-13
 */
@Service
public class MemberInvoiceTitleServiceImpl implements IMemberInvoiceTitleService {
	@Autowired
	private MemberInvoiceTitleMapper memberInvoiceTitleMapper;

	@Override
	public InvoiceTitleVO selectByTitleId(String titleId) {
		MemberInvoiceTitle memberInvoiceTitle = selectMemberInvoiceTitleById(Long.parseLong(titleId));
		if (memberInvoiceTitle == null) {
			return null;
		}
		return InvoiceTitleVO.builder()
				.titleId(memberInvoiceTitle.getId() + "")
				.titleName(memberInvoiceTitle.getName())
				.titleType(TitleTypeEnum.getLabel(memberInvoiceTitle.getTitleType()))
				.taxId(memberInvoiceTitle.getTaxId())
				.unitAddress(memberInvoiceTitle.getUnitAddress())
				.email(memberInvoiceTitle.getEmail())
				.phoneNumber(memberInvoiceTitle.getPhoneNumber())
				.bankName(memberInvoiceTitle.getBankName())
				.bankAccountNumber(memberInvoiceTitle.getBankAccountNumber())
				.build();
	}

	/**
	 * 查询会员发票抬头
	 *
	 * @param id 会员发票抬头主键
	 * @return 会员发票抬头
	 */
	@Override
	public MemberInvoiceTitle selectMemberInvoiceTitleById(Long id) {
		return memberInvoiceTitleMapper.selectMemberInvoiceTitleById(id);
	}

	@Override
	public List<MemberInvoiceTitle> selectMemberInvoiceTitleList(String memberId) {
		MemberInvoiceTitle queryEntity = new MemberInvoiceTitle();
		queryEntity.setMemberId(memberId);
		return selectMemberInvoiceTitleList(queryEntity);
	}

	/**
	 * 查询会员发票抬头列表
	 *
	 * @param memberInvoiceTitle 会员发票抬头
	 * @return 会员发票抬头
	 */
	@Override
	public List<MemberInvoiceTitle> selectMemberInvoiceTitleList(MemberInvoiceTitle memberInvoiceTitle) {
		return memberInvoiceTitleMapper.selectMemberInvoiceTitleList(memberInvoiceTitle);
	}

	/**
	 * 新增会员发票抬头
	 *
	 * @param memberInvoiceTitle 会员发票抬头
	 * @return 结果
	 */
	@Override
	public int insertMemberInvoiceTitle(MemberInvoiceTitle memberInvoiceTitle) {
		int i = memberInvoiceTitleMapper.insertMemberInvoiceTitle(memberInvoiceTitle);
		if (StringUtils.equals(memberInvoiceTitle.getDefaultFlag(), "1")) {
			// 如果新增这条是默认的，那么就把这个会员下所有的其他抬头的默认取消
			List<MemberInvoiceTitle> memberInvoiceTitles = selectMemberInvoiceTitleList(memberInvoiceTitle.getMemberId());
			// 排除刚刚新增的抬头数据
			List<Long> collect = memberInvoiceTitles.stream()
					.map(MemberInvoiceTitle::getId)
					.filter(x -> !Objects.equals(x, memberInvoiceTitle.getId()))
					.collect(Collectors.toList());
			if (CollectionUtils.isNotEmpty(collect)) {
				memberInvoiceTitleMapper.batchUpdateDefaultFlag(collect);
			}
		}
		return i;
	}

	/**
	 * 修改会员发票抬头
	 *
	 * @param memberInvoiceTitle 会员发票抬头
	 * @return 结果
	 */
	@Override
	public int updateMemberInvoiceTitle(MemberInvoiceTitle memberInvoiceTitle) {
		if (StringUtils.equals(memberInvoiceTitle.getDefaultFlag(), "1")) {
			// 如果新增这条是默认的，那么就把这个会员下所有的其他抬头的默认取消
			List<MemberInvoiceTitle> memberInvoiceTitles = selectMemberInvoiceTitleList(memberInvoiceTitle.getMemberId());
			// 排除刚刚新增的抬头数据
			List<Long> collect = memberInvoiceTitles.stream()
					.map(MemberInvoiceTitle::getId)
					.filter(x -> !Objects.equals(x, memberInvoiceTitle.getId()))
					.collect(Collectors.toList());
			if (CollectionUtils.isNotEmpty(collect)) {
				memberInvoiceTitleMapper.batchUpdateDefaultFlag(collect);
			}
		}
		return memberInvoiceTitleMapper.updateMemberInvoiceTitle(memberInvoiceTitle);
	}

	/**
	 * 批量删除会员发票抬头
	 *
	 * @param ids 需要删除的会员发票抬头主键
	 * @return 结果
	 */
	@Override
	public int deleteMemberInvoiceTitleByIds(Long[] ids) {
		return memberInvoiceTitleMapper.deleteMemberInvoiceTitleByIds(ids);
	}

	/**
	 * 删除会员发票抬头信息
	 *
	 * @param id 会员发票抬头主键
	 * @return 结果
	 */
	@Override
	public int deleteMemberInvoiceTitleById(Long id) {
		return memberInvoiceTitleMapper.deleteMemberInvoiceTitleById(id);
	}
}
