package com.dhcc.finance.main.service.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dhcc.common.constant.SysConstants;
import com.dhcc.common.constant.SysStatusCode;
import com.dhcc.common.model.CurrencyMeta;
import com.dhcc.common.model.response.RestfulResponse;
import com.dhcc.finance.main.dao.FinancialGlorgbookMapper;
import com.dhcc.finance.main.model.CwBdAccsubjWeb;
import com.dhcc.finance.main.service.FinancialGlorgbookService;
import com.dhcc.finance.util.MetaDataUtil;

import brave.Tracer;
@Service
public class FinancialGlorgbookServiceImpl implements FinancialGlorgbookService{
	@Autowired
	FinancialGlorgbookMapper financialGlorgbookMapper;
	@Autowired
	private Tracer tracer;
	/*
	 * (非 Javadoc)  
	 * <p>Title: selectCwBdGlorgbookForDetail</p>  
	 * <p>Description: 查看账套详情数据</p>
	 * @param pkGlorgbook
	 * @param dateTime
	 * @return  
	 * @see com.dhcc.finance.main.service.FinancialGlorgbookService#selectCwBdGlorgbookForDetail(java.lang.String, java.lang.String)    
	 * @author huangpeng
	 * @date 2019年8月30日 下午3:18:15
	 * @修改日志：
	 */
	@Override
	public RestfulResponse<Object, CurrencyMeta> selectCwBdGlorgbookForDetail(String xtzxmbh, String dateTime) {
		Map<String, Object> map= new HashMap<String, Object>();
		List<CwBdAccsubjWeb> list = financialGlorgbookMapper.selectCwBdGlorgbookForDetail(xtzxmbh,dateTime);
		//map.put("list", list);
		BigDecimal zcBalance = new BigDecimal("0"), 
				fzBalance = new BigDecimal("0"),
				jzBalance = new BigDecimal("0"),
				dwjzBalance = new BigDecimal("0"),balance = new BigDecimal("0");
		for (CwBdAccsubjWeb cwBdAccsubjWeb : list) {
			if(cwBdAccsubjWeb.getSubjcode().startsWith("1")&&cwBdAccsubjWeb.getSubjcode().length()==4) {
				zcBalance=zcBalance.add(cwBdAccsubjWeb.getBalance());
			}
			if(cwBdAccsubjWeb.getSubjcode().startsWith("2")&&cwBdAccsubjWeb.getSubjcode().length()==4) {
				fzBalance=fzBalance.add(cwBdAccsubjWeb.getBalance());
			}
			if("4001".equals(cwBdAccsubjWeb.getSubjcode())) {
				balance=cwBdAccsubjWeb.getBalance();
			}
		}
		jzBalance=zcBalance.subtract(fzBalance);
		dwjzBalance=jzBalance.divide(balance,4,BigDecimal.ROUND_HALF_UP);
		map.put("fzBalance", fzBalance);
		map.put("dwjzBalance", dwjzBalance);
		map.put("jzBalance", jzBalance);
		map.put("zcBalance", zcBalance);
		System.err.println(map.toString());
		return new RestfulResponse<Object, CurrencyMeta>(SysConstants.CODE.SUCCESS,SysStatusCode.OPERATION_SUCCESS.message(),map,MetaDataUtil.getMeta(tracer,SysStatusCode.OPERATION_SUCCESS));
	}

}
