package com.community.huiju.service.impl;

import com.community.commom.mode.ResponseBean;
import com.community.commom.session.UserElement;
import com.community.huiju.dao.*;
import com.community.huiju.model.*;
import com.community.huiju.service.BillServiceI;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

/**
 * @author FXF
 * @date 2018-10-25
 */
@Service("billService")
public class BillServiceImpl implements BillServiceI {
	
	@Autowired
	private TpBillInvoiceMapper tpBillInvoiceMapper;

	@Autowired
	private TaUserMapper taUserMapper;

	@Autowired
	private TaSysRoleMapper taSysRoleMapper;

	@Autowired
	private TaUserVerifyMapper taUserVerifyMapper;

	@Autowired
	private TpBuildingOwnerInfoMapper tpBuildingOwnerInfoMapper;

	@Autowired
	private TpBillOrderMapper tpBillOrderMapper;

	@Autowired
	private TpBillMapper tpBillMapper;

	@Autowired
	private TpBillStatementMapper tpBillStatementMapper;
	
	/**
	 * 根据payType分页获取缴费或未缴费的数据
	 * @param userElement
	 * @param payType
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@Override
	public Map<String, Object> getBillsList(UserElement userElement, Integer payType, Integer pageNum, Integer pageSize) {


		/**
		 * 账单 关联的是房产,
 		 */

		//使用分页插件
		Page<Map<String,Object>> page = PageHelper.startPage(pageNum, pageSize);
		List<Map<String,Object>> billsList = tpBillInvoiceMapper.getBillsList(userElement.getCommunityId(), userElement.getRoomNoId(), payType);
		billsList.forEach(e-> {
			String payPrice = String.valueOf(e.get("payPrice"));
			Double payPriceDouble = Double.valueOf(payPrice) / 100;
			e.put("payPrice", payPriceDouble);
		});
		Map<String,Object> result = Maps.newHashMap();
		result.put("list",billsList);
		result.put("total",page.getTotal());
		return result;
	}
	
	/**
	 * 获取账单的详细信息
	 * @param billInvoiceId
	 * @return
	 */
	@Override
	public Map<String, Object> getBillInvoiceDetail(Integer communityId,Integer billInvoiceId) {
		Map<String, Object> billInvoiceDetail = tpBillInvoiceMapper.getBillInvoiceDetail(communityId, billInvoiceId);
		String payPrice = String.valueOf(billInvoiceDetail.get("payPrice"));
		Double payPriceDouble = Double.valueOf(payPrice) / 100;
		billInvoiceDetail.put("payPrice", payPriceDouble);
		return billInvoiceDetail;
	}

	@Override
	public ResponseBean payHistory(UserElement userElement) {
		ResponseBean responseBean = new ResponseBean();

		//1. 查询所有订单
		//2. 根据订单


		// 所有订单
		List<TpBillOrder> billOrderList = tpBillOrderMapper.selectByRoomNoId(userElement.getRoomNoId());
		billOrderList.forEach(e->{
			// 这个订单的所有缴费信息
			List<TpBillOrder> orderList = tpBillOrderMapper.selectByOrderBumber(e.getOrderBumber());

			// 缴费组
			List<TpBillInvoice> tpBillInvoices = orderList.stream().map(billOrder -> tpBillInvoiceMapper.selectByIdBillInvoiceInfo(billOrder.getTpBillInvoiceId())).collect(Collectors.toList());
			tpBillInvoices.forEach( billInvoice -> billInvoice.setSumPayPrice(Double.valueOf(billInvoice.getPayPrice() + "") / 100));

			// 所有缴费组的总额
			Double billCountPrice = Double.valueOf(tpBillInvoices.stream().map(billCount -> billCount.getPayPrice()).count() + "") / 100;


			e.setBillList(tpBillInvoices);
			e.setSumPrice(billCountPrice);
		});

		responseBean.addSuccess(billOrderList);
		return responseBean;
	}

	@Override
	public ResponseBean getOrderInfo(String orderNumber) {
		ResponseBean responseBean = new ResponseBean();

		/**
		 * 1.根据订单号获取所有缴费组
		 * 2.获取其中的一条缴费订单数据, 因为订单号再库里面是多条，取第一条就可以
		 * 3.计算出所有缴费组的总额
		 * 4.获取一个缴费的流水记录, 因为 订单号对应多个缴费单，每一个缴费单对应一个缴费流水, 然后通过缴费流水可以知道缴费人，缴费类型，缴费备注等等
		 */

		// 这个订单所有的缴费组记录, 拿到每一个缴费组的id
		List<TpBillOrder> billOrderList = tpBillOrderMapper.selectByOrderNumberBill(orderNumber);
		if (CollectionUtils.isEmpty(billOrderList)) {
			responseBean.addError("订单号不存在！");
			return responseBean;
		}
		// 每个缴费单数据
		List<TpBillInvoice> tpBillInvoices = billOrderList.stream().map(e -> tpBillInvoiceMapper.selectByIdBillInvoiceInfo(e.getTpBillInvoiceId())).collect(Collectors.toList());
		tpBillInvoices.forEach(e -> e.setSumPayPrice(Double.valueOf(e.getPayPrice() + "") / 100));

		// 这个订单总额
		Double billCountPrice = Double.valueOf(tpBillInvoices.stream().map(e -> e.getPayPrice()).count() + "") / 100;

		// 因为订单号是多条，取第一条即可
		TpBillOrder tpBillOrder = billOrderList.get(0);

		// 获取一条流水
		TpBillStatement tpBillStatement = tpBillStatementMapper.selectByCommunityIdAndBillInvoiceId(tpBillOrder.getCommunityId(), tpBillOrder.getTpBillInvoiceId());


		Map<String, Object> map = Maps.newHashMap();
		map.put("orderNumber", orderNumber);
		map.put("createDate", tpBillOrder.getCreateDate());
		map.put("sumPrice", billCountPrice);
		map.put("payName", tpBillStatement.getPayName());
		map.put("updateDate", tpBillOrder.getUpdateDate());
		map.put("payType", tpBillStatement.getPayType());
		map.put("payRemark", tpBillStatement.getPayRemark());
		map.put("list", tpBillInvoices);

		responseBean.addSuccess(map);
		return responseBean;
	}
}
