package com.community.huiju.service.impl;

import com.community.commom.session.UserElement;
import com.community.huiju.dao.*;
import com.community.huiju.model.TaSysRole;
import com.community.huiju.model.TaUser;
import com.community.huiju.model.TaUserVerify;
import com.community.huiju.model.TpBuildingOwnerInfo;
import com.community.huiju.service.BillServiceI;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @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;
	
	/**
	 * 根据payType分页获取缴费或未缴费的数据
	 * @param userElement
	 * @param payType
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@Override
	public Map<String, Object> getBillsList(UserElement userElement, Integer payType, Integer pageNum, Integer pageSize) {


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

		/**
		 * 获取这个房产的业主
		 */

		TaUserVerify userVerify = taUserVerifyMapper.selectCommunityAndAddressAndRoleId(userElement.getCommunityId(),
										userElement.getPhaseId(), userElement.getBuildingId(), userElement.getUnitId(),
										userElement.getLevelId(), userElement.getRoomNoId());
		// 业主信息
		TaUser user = taUserMapper.selectByPrimaryKey(userVerify.getUserId());

		TpBuildingOwnerInfo tpBuildingOwnerInfo = tpBuildingOwnerInfoMapper.selectCommunityAndAddressAndPhone(userElement.getCommunityId(),
				userElement.getPhaseId(), userElement.getBuildingId(), userElement.getUnitId(),
				userElement.getLevelId(), userElement.getRoomNoId(), user.getLoginName());


		//使用分页插件
		Page<Map<String,Object>> page = PageHelper.startPage(pageNum, pageSize);
		List<Map<String,Object>> billsList = tpBillInvoiceMapper.getBillsList(userElement.getCommunityId(), tpBuildingOwnerInfo.getId(), payType);
		billsList.forEach(e-> {
			String payPrice = String.valueOf(e.get("payPrice"));
			Double payPriceDouble = Double.parseDouble(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.parseDouble(payPrice) / 100;
		billInvoiceDetail.put("payPrice", payPriceDouble);
		return billInvoiceDetail;
	}
}
