package com.jsowell.pile.service.impl;

import com.google.common.collect.Lists;
import com.jsowell.common.util.DateUtils;
import com.jsowell.pile.domain.OrderInvoiceRecord;
import com.jsowell.pile.dto.QueryInvoiceRecordDTO;
import com.jsowell.pile.mapper.OrderInvoiceRecordMapper;
import com.jsowell.pile.service.IMemberInvoiceTitleService;
import com.jsowell.pile.service.IOrderBasicInfoService;
import com.jsowell.pile.service.IOrderInvoiceRecordService;
import com.jsowell.pile.vo.base.OrderAmountDetailVO;
import com.jsowell.pile.vo.uniapp.InvoiceTitleVO;
import com.jsowell.pile.vo.web.InvoiceRecordVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 申请开票Service业务层处理
 *
 * @author jsowell
 * @date 2023-04-10
 */
@Service
public class OrderInvoiceRecordServiceImpl implements IOrderInvoiceRecordService {
	@Autowired
	private OrderInvoiceRecordMapper orderInvoiceRecordMapper;

	@Autowired
	private IMemberInvoiceTitleService memberInvoiceTitleService;

	@Autowired
	private IOrderBasicInfoService orderBasicInfoService;

	/**
	 * 查询申请开票
	 *
	 * @param id 申请开票主键
	 * @return 申请开票
	 */
	@Override
	public OrderInvoiceRecord selectOrderInvoiceRecordById(Integer id) {
		return orderInvoiceRecordMapper.selectOrderInvoiceRecordById(id);
	}

	@Override
	public InvoiceRecordVO selectInvoiceTitleVO(Integer id) {
		// 申请记录信息
		OrderInvoiceRecord orderInvoiceRecord = selectOrderInvoiceRecordById(id);
		if (orderInvoiceRecord == null) {
			return null;
		}
		// 发票抬头信息
		InvoiceTitleVO invoiceTitleVO = memberInvoiceTitleService.selectByTitleId(orderInvoiceRecord.getTitleId());

		// 订单信息
		List<String> orderCodes = Lists.newArrayList(orderInvoiceRecord.getOrderCodes().split(","));
		List<OrderAmountDetailVO> orderAmountDetailVOS = orderBasicInfoService.queryOrderAmountDetail(orderCodes);

		return InvoiceRecordVO.builder()
				.id(orderInvoiceRecord.getId() + "")
				.memberId(orderInvoiceRecord.getMemberId())
				.status(orderInvoiceRecord.getStatus())
				.invoiceTitle(invoiceTitleVO)
				.orderList(orderAmountDetailVOS)
				.createTime(DateUtils.formatDateTime(orderInvoiceRecord.getCreateTime()))
				.updateTime(DateUtils.formatDateTime(orderInvoiceRecord.getUpdateTime()))
				.build();
	}

	/**
	 * 查询申请开票列表
	 *
	 * @param orderInvoiceRecord 申请开票
	 * @return 申请开票
	 */
	@Override
	public List<OrderInvoiceRecord> selectOrderInvoiceRecordList(OrderInvoiceRecord orderInvoiceRecord) {
		return orderInvoiceRecordMapper.selectOrderInvoiceRecordList(orderInvoiceRecord);
	}

	@Override
	public List<OrderInvoiceRecord> selectInvoiceRecordList(QueryInvoiceRecordDTO dto) {
		return orderInvoiceRecordMapper.selectInvoiceRecordList(dto);
	}

	/**
	 * 新增申请开票
	 *
	 * @param orderInvoiceRecord 申请开票
	 * @return 结果
	 */
	@Override
	public int insertOrderInvoiceRecord(OrderInvoiceRecord orderInvoiceRecord) {
		return orderInvoiceRecordMapper.insertOrderInvoiceRecord(orderInvoiceRecord);
	}

	/**
	 * 修改申请开票
	 *
	 * @param orderInvoiceRecord 申请开票
	 * @return 结果
	 */
	@Override
	public int updateOrderInvoiceRecord(OrderInvoiceRecord orderInvoiceRecord) {
		return orderInvoiceRecordMapper.updateOrderInvoiceRecord(orderInvoiceRecord);
	}

	/**
	 * 批量删除申请开票
	 *
	 * @param ids 需要删除的申请开票主键
	 * @return 结果
	 */
	@Override
	public int deleteOrderInvoiceRecordByIds(Integer[] ids) {
		return orderInvoiceRecordMapper.deleteOrderInvoiceRecordByIds(ids);
	}

	/**
	 * 删除申请开票信息
	 *
	 * @param id 申请开票主键
	 * @return 结果
	 */
	@Override
	public int deleteOrderInvoiceRecordById(Integer id) {
		return orderInvoiceRecordMapper.deleteOrderInvoiceRecordById(id);
	}
}
