package cn.com.countrygarden.bi.main.service.investment.impl;

import cn.com.countrygarden.bi.main.model.PageData;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.transaction.Transactional;

import cn.com.countrygarden.bi.main.core.Result;
import cn.com.countrygarden.bi.main.model.AccessToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.countrygarden.bi.main.model.investment.InvestProjectAccount;
import cn.com.countrygarden.bi.main.model.investment.InvestProjectInvestment;
import cn.com.countrygarden.bi.main.model.investment.InvestProjectProfit;
import cn.com.countrygarden.bi.main.model.investment.InvestRegionInvestment;
import cn.com.countrygarden.bi.main.model.investment.InvestRegionTotal;
import cn.com.countrygarden.bi.main.dao.investment.InvestProjectAccountMapper;
import cn.com.countrygarden.bi.main.dao.investment.InvestProjectInvestmentMapper;
import cn.com.countrygarden.bi.main.dao.investment.InvestProjectProfitMapper;
import cn.com.countrygarden.bi.main.dao.investment.InvestRegionInvestmentMapper;
import cn.com.countrygarden.bi.main.dao.investment.InvestRegionTotalMapper;
import cn.com.countrygarden.bi.main.dto.investment.InvestProjectInvestmentVo;
import cn.com.countrygarden.bi.main.dto.investment.InvestProjectProfitVo;
import cn.com.countrygarden.bi.main.dto.investment.InvestRegionTotalVo;
import cn.com.countrygarden.bi.main.exception.ServiceException;
import cn.com.countrygarden.bi.main.service.investment.InvestProjectProfitService;
import cn.com.countrygarden.bi.main.service.investment.InvestRegionInvestmentService;
import cn.com.countrygarden.bi.main.service.investment.InvestRegionTotalService;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example.Criteria;
import cn.com.countrygarden.bi.main.core.Response;

/**
 * 个人项目收益-业务逻辑层实现
 *
 * @author 汤婷
 * @email www.justintoforest@gamail.com
 * @url
 * @date 2018-08-20
 */
@Service
@Transactional
public class InvestProjectProfitServiceImpl implements InvestProjectProfitService {
	@Autowired
	private InvestProjectProfitMapper investProjectProfitMapper;

	@Autowired
	private InvestRegionTotalMapper investRegionTotalMapper;

	@Autowired
	private InvestProjectInvestmentMapper investProjectInvestmentMapper;

	@Autowired
	private InvestRegionInvestmentService investRegionInvestmentService;

	@Autowired
	private InvestRegionInvestmentMapper investRegionInvestmentMapper;
	
	@Autowired
	private InvestProjectAccountMapper investProjectAccountMapper;
	@Autowired
	private InvestRegionTotalService investRegionTotalService;

	@Override
	public Result queryPageList(InvestProjectProfit investProjectProfit, int page, int size) {
		Page<Object> pageList = PageHelper.startPage(page, size);
		Condition condition = new Condition(InvestProjectProfit.class);
		Criteria criteria = condition.createCriteria();
		// TODO 添加条件
		investProjectProfitMapper.selectByCondition(condition);
		PageData data = new PageData(pageList);
		data.setData(pageList.getResult());
		return Response.response(data);
	}

	public Result queryById(Integer id) {
		return Response.response(investProjectProfitMapper.selectByPrimaryKey(id));
	}

	public Result add(InvestProjectProfit investProjectProfit, AccessToken accessToken) {
		investProjectProfitMapper.insert(investProjectProfit);
		return Response.response();
	}

	/**
	 * 区域编辑后重新计算（个人区域投资） 传参区域id
	 * 
	 * @param investRegionTotalId
	 */

	public void regionTotalEditComputeRegionInvestment(int investRegionTotalId) {
		// 1.根据区域id查询对应的个人区域投资信息（如果数据为null则不进行重新计算等操作）
		Condition condition = new Condition(InvestRegionInvestment.class);
		condition.createCriteria().andEqualTo("regionId", investRegionTotalId);
		List<InvestRegionInvestment> investRegionInvestmentList = investRegionInvestmentMapper
				.selectByCondition(condition);

		if (investRegionInvestmentList.size() > 0) {

			this.addOrUpdate(investRegionInvestmentList);
		}
	}
	/**
	 * 区域编辑后重新计算（个人区域投资） 传参区域id
	 * 
	 * @param investRegionTotalId
	 */
	/**
	 * 项目编辑后重新计算（个人区域投资） 第一个为编辑前的区域id，第二个为编辑后的区域id
	 */

	public void projectInvestmentEditComputeProjectProfit(Map<String, String> investRegionTotalId) {
		String investRegionTotalEditPre = null;// 编辑前的区域id
		String investRegionTotalEditAfter = null;// 编辑后的区域id
		Set<String> k = investRegionTotalId.keySet();
		Iterator<String> it = k.iterator();
		while (it.hasNext()) {
			String key = it.next();
			String value = investRegionTotalId.get(key);
			if (key.equals("investRegionTotalEditPre")) {
				investRegionTotalEditPre = value;
			} else if (key.equals("investRegionTotalEditAfter")) {
				investRegionTotalEditAfter = value;
			} else {
				throw new ServiceException("InvestProjectProfitService接口异常,key值不存在！");
			}

		}
		// 情况一：区域前后有变化
		// 个人只与区域关联，不参与项目的选择，如果项目变化则只是相应的个人项目收益发生变化
		if (!investRegionTotalEditPre.equals(investRegionTotalEditAfter)) {		
			// 1.编辑前的区域id与个人投资区域是否有关联
			Condition condition = new Condition(InvestRegionInvestment.class);
			condition.createCriteria().andEqualTo("regionId", investRegionTotalEditPre);
			List<InvestRegionInvestment> investRegionInvestmentList = investRegionInvestmentMapper
					.selectByCondition(condition);
			// investRegionInvestmentList.size()>0则有关联，
			if (investRegionInvestmentList.size() > 0) {
				// 查询（项目收益）中是否还存在对此区域进行项目投资
				Condition condition2 = new Condition(InvestProjectInvestment.class);
				condition2.createCriteria().andEqualTo("regionId", investRegionTotalEditPre);
				List<InvestProjectInvestment> investProjectInvestmentList = investProjectInvestmentMapper
						.selectByCondition(condition2);
				// 此区域还有项目项目投资，个人收益需要重新计算
				if (investProjectInvestmentList.size() > 0) {
					this.addOrUpdate(investRegionInvestmentList);
				} else {
					// 此区域下无项目投资，个人收益关于此区域的全部删除
					Condition condition3 = new Condition(InvestProjectInvestment.class);
					condition3.createCriteria().andEqualTo("regionId", investRegionTotalEditPre);
						this.regionTotalEditComputeRegionInvestment(Integer.parseInt(investRegionTotalEditPre));

				}

			}

			// 2.项目编辑后的区域id处理
			this.regionTotalEditComputeRegionInvestment(Integer.parseInt(investRegionTotalEditAfter));

		} else {
			//项目利益模块重新计算
			this.regionTotalEditComputeRegionInvestment(Integer.parseInt(investRegionTotalEditAfter));

		}

	}

	// 一个区域可以影响多个个人
	public void addOrUpdate(List<InvestRegionInvestment> investRegionInvestmentList) {
		// 存放InvestRegionInvestment(个人区域)的id,后面批量删除个人项目收益
		List<String> investRegionInvestmentIds = new ArrayList<String>();
		// 用来存放添加的个人项目收益对象,批量增加
		LinkedList<InvestProjectProfit> projectProfitLinked = new LinkedList<InvestProjectProfit>();
		InvestProjectProfit investProjectProfit = null;
		List<InvestProjectInvestment> investProjectInvestmentList = null;

		InvestProjectInvestmentVo investProjectInvestmentVo = null;

		// 根据区域id(这里的区域id指invest_region_total的id)查询（项目投资）表得到相当的项目id及名字
		investProjectInvestmentList = investProjectInvestmentMapper
				.selectProjectIdAndprojectName(investRegionInvestmentList.get(0).getRegionId());
		// 当前区域下没有项目时处理
		if (investProjectInvestmentList.size() < 1 || investProjectInvestmentList == null) {
			for (InvestRegionInvestment investRegionInvestment : investRegionInvestmentList) {
				investRegionInvestmentIds.add(String.valueOf(investRegionInvestment.getId()));
				investRegionInvestment.setFinancialIncomeCapital(new BigDecimal(0));
				investRegionInvestment.setFinancialIncomeCapitalAccount(new BigDecimal(0));
				investRegionInvestment.setRaisingBalanceCapital(new BigDecimal(0));
				investRegionInvestment.setRaisingBalanceCapitalAccount(new BigDecimal(0));
				investRegionInvestment.setAvailableBalanceCapital(new BigDecimal(0));
				investRegionInvestment.setAvailableBalanceCapitalAccount(new BigDecimal(0));

				investRegionInvestmentMapper.updateByPrimaryKeySelective(investRegionInvestment);

			}

			// 批量删除个人项目收益
			investProjectProfitMapper.deleteInvestProjectProfitIds(investRegionInvestmentIds);
			return;
		}
		// 1.investRegionInvestmentList为个人区域投资信息
		// 2.根据个人区域投资id删除对应的个人项目收益信息

		// 3. 根据区域总资金id得到项目总投资的数据，因为些数据所有的区域id是一样的，所以只取第一位就可以了
		InvestRegionTotal investRegionTotal = investRegionTotalMapper
				.selectByPrimaryKey(Integer.parseInt(investRegionInvestmentList.get(0).getRegionId()));

		// 通过区域总资金id得到investRegionTotalVo相应数据
		InvestRegionTotalVo investRegionTotalVo = investRegionTotalMapper
				.findInvestRegionTotalVo(investRegionInvestmentList.get(0).getRegionId());
		if(investRegionTotalVo==null) {
			investRegionTotalVo=new InvestRegionTotalVo();
		}

		for (InvestRegionInvestment investRegionInvestment : investRegionInvestmentList) {
			investRegionInvestmentIds.add(String.valueOf(investRegionInvestment.getId()));

			// 5.更改个人项目收益信息

			for (InvestProjectInvestment investProjectInvestment : investProjectInvestmentList) {
				Condition condition=new Condition(InvestProjectAccount.class);
				 Integer investProjectInvestmentId=investProjectAccountMapper.findInvestProjectAccountInvestProjectInvestmentId(investRegionInvestment.getId(),investProjectInvestment.getProjectName());
				
				
				// 遍历得到区域下的各项目id及项目的小计
				investProjectInvestmentVo = investProjectInvestmentMapper.queryInvestProjectInvestmentVoExcel(
						investProjectInvestment.getProjectName(), investRegionInvestment.getRegionId());
               if(investProjectInvestmentVo==null) {
            	   investProjectInvestmentVo=new InvestProjectInvestmentVo();
               }
				investProjectProfit = new InvestProjectProfit();
				investProjectProfit.setCreateBy(investRegionInvestment.getCreateBy());
				investProjectProfit.setCreateTime(new Date());
				investProjectProfit.setFlag(investRegionInvestment.getFlag());
				investProjectProfit.setUpdateBy(investRegionInvestment.getUpdateBy());
				investProjectProfit.setUpdateTime(new Date());
				investProjectProfit.setProjectId(String.valueOf(investProjectInvestmentId));// 项目id
				investProjectProfit.setProjectName(investProjectInvestment.getProjectName());// 项目名字
				investProjectProfit.setPersonInvestmentId(String.valueOf(investRegionInvestment.getId()));// 个人区域投资id
				// '资金池（区域跟投）',=(个人-资金池跟投本金)/(区域总募集金额)*(项目-资金池跟投项目金额小计)
				if(investRegionTotal.getRegionAmountRaised().doubleValue()==0.0) {
					investProjectProfit
					.setRegionFollowCapital(new BigDecimal(0));
				}else {
					investProjectProfit
					.setRegionFollowCapital(new BigDecimal(investRegionInvestment.getFollowCapital().doubleValue()
							/ investRegionTotal.getRegionAmountRaised().doubleValue()
							* investProjectInvestmentVo.getProjectFollowCapitalTotal().doubleValue()).setScale(2,
									BigDecimal.ROUND_HALF_UP));
				}
				
			
			BigDecimal investProjectAccountSum=investProjectAccountMapper.findInvestProjectAccountSum(investRegionInvestment.getId(),investProjectInvestment.getProjectName());
				
				// '单独资金账户（区域跟投）',=单独资金账户跟投本金follow_capital_account
			
				investProjectProfit.setRegionFollowCapitalAccount(investProjectAccountSum);
				
				// '个人-资金池（利润分配）',=资金池（区域跟投）/项目总跟投金额小计*（项目-资金池（利润分配）小计）
				if(investProjectInvestmentVo.getProjectFollowTotalCapitalTotal().doubleValue()==0.0) {
					investProjectProfit.setProfitDistributionCapital(
							new BigDecimal(0));
				}else {
					investProjectProfit.setProfitDistributionCapital(
							new BigDecimal(investProjectProfit.getRegionFollowCapital().doubleValue()
									/ investProjectInvestmentVo.getProjectFollowTotalCapitalTotal().doubleValue()
									* investProjectInvestmentVo.getProfitDistributionCapitalTotal().doubleValue())
											.setScale(2, BigDecimal.ROUND_HALF_UP));
				}
				
				// 个人-单独资金账户（利润分配）',=单独资金账户（区域跟投）/（项目总跟投金额小计）*(单独资金账户（利润分配）小计)
				if(investProjectInvestmentVo.getProjectFollowTotalCapitalTotal().doubleValue()==0.0){
					investProjectProfit.setProfitDistributionCapitalAccount(
							new BigDecimal(0));
				}else {
					investProjectProfit.setProfitDistributionCapitalAccount(
							new BigDecimal(investProjectProfit.getRegionFollowCapitalAccount().doubleValue()
									/ investProjectInvestmentVo.getProjectFollowTotalCapitalTotal().doubleValue()
									* investProjectInvestmentVo.getProfitDistributionCapitalAccountTotal().doubleValue())
											.setScale(2, BigDecimal.ROUND_HALF_UP));
				}
				// '个人-资金池（房产发放）'=个人-资金池（利润分配）,
				investProjectProfit.setPropertyDistributionCapital(investProjectProfit.getProfitDistributionCapital());

				// '个人-单独资金账户（房产发放）'=个人-单独资金账户（利润分配）
				investProjectProfit.setPropertyDistributionCapitalAccount(
						investProjectProfit.getProfitDistributionCapitalAccount());
				// '资金池（个人已发放）'=(个人-区域跟投金额-资金池)/(项目-项目跟投情况-项目总跟投金额小计)*（项目-已发放项目收益-资金池小计issuedIncomeCapitalTotal）
				if(investProjectInvestmentVo.getProjectFollowTotalCapitalTotal().doubleValue()==0.0) {
					investProjectProfit
					.setIssuedCapital(new BigDecimal(0));
				}else {
					investProjectProfit
					.setIssuedCapital(new BigDecimal(investProjectProfit.getRegionFollowCapital().doubleValue()
							/ investProjectInvestmentVo.getProjectFollowTotalCapitalTotal().doubleValue()
							* investProjectInvestmentVo.getIssuedIncomeCapitalTotal().doubleValue()).setScale(2,
									BigDecimal.ROUND_HALF_UP));
				}
				// 单独资金账户（个人已发放）=(个人-区域跟投金额-单独资金账户)/(项目-项目跟投情况-项目总跟投金额小计)*(项目-已发放项目收益-单独资金账户小计）
				if(investProjectInvestmentVo.getProjectFollowTotalCapitalTotal().doubleValue()==0.0) {
					investProjectProfit.setIssuedCapitalAccount(
							new BigDecimal(0));
				}else {
					investProjectProfit.setIssuedCapitalAccount(
							new BigDecimal(investProjectProfit.getRegionFollowCapitalAccount().doubleValue()
									/ investProjectInvestmentVo.getProjectFollowTotalCapitalTotal().doubleValue()
									* investProjectInvestmentVo.getIssuedIncomeCapitalAccountTotal().doubleValue())
											.setScale(2, BigDecimal.ROUND_HALF_UP));
				}
				// 资金池（个人未发放）'=(个人-房产发放利润（累计数）-资金池)-（个人-已发放个人利润（累计数）-资金池）
				investProjectProfit.setUnissuedCapital(
						new BigDecimal(investProjectProfit.getPropertyDistributionCapital().doubleValue()
								- investProjectProfit.getIssuedCapital().doubleValue()).setScale(2,
										BigDecimal.ROUND_HALF_UP));
				// 单独资金账户（个人未发放）
				investProjectProfit.setUnissuedCapitalAccount(
						new BigDecimal(investProjectProfit.getPropertyDistributionCapitalAccount().doubleValue()
								- investProjectProfit.getIssuedCapitalAccount().doubleValue()).setScale(2,
										BigDecimal.ROUND_HALF_UP));
				// '资金池（阶段性现金返还）'=(个人-区域跟投金额-资金池)/(项目-项目跟投情况-资金池跟投项目金额小计)*（项目-阶段性现金返还-资金池小计）
				if(investProjectInvestmentVo.getProjectFollowCapitalTotal().doubleValue()==0.0) {
					investProjectProfit
					.setCashReturnCapital(new BigDecimal(0));
				}else {
					investProjectProfit
					.setCashReturnCapital(new BigDecimal(investProjectProfit.getRegionFollowCapital().doubleValue()
							/ investProjectInvestmentVo.getProjectFollowCapitalTotal().doubleValue()
							* investProjectInvestmentVo.getCashReturnCapitalTotal().doubleValue()).setScale(2,
									BigDecimal.ROUND_HALF_UP));
				}
				// '单独资金账户（阶段性现金返还）'=(个人-区域跟投金额-单独资金账户)/(项目-项目跟投情况-单独资金账户跟投项目金额小计)*（项目-阶段性现金返还-单独资金账户小计）
				if(investProjectInvestmentVo.getProjectFollowSingleCapitalTotal().doubleValue()!=0.0) {
					investProjectProfit.setCashReturnCapitalAccount(
							new BigDecimal(investProjectProfit.getRegionFollowCapitalAccount().doubleValue()
									/ investProjectInvestmentVo.getProjectFollowSingleCapitalTotal().doubleValue()
									* investProjectInvestmentVo.getCashReturnCapitalAccountTotal().doubleValue())
											.setScale(2, BigDecimal.ROUND_HALF_UP));
				}else {
					investProjectProfit.setCashReturnCapitalAccount(
							new BigDecimal(0));
				}
				
				
				// '资金池（本金返还）'=(个人-区域跟投金额-资金池)/(项目-项目跟投情况-资金池跟投项目金额小计)*(项目-本金返还-资金池小计)
				if(investProjectInvestmentVo.getProjectFollowCapitalTotal().doubleValue()==0.0){
					investProjectProfit.setPrincipalReturnCapital(
						new BigDecimal(0));
				}else {
					investProjectProfit.setPrincipalReturnCapital(
							new BigDecimal(investProjectProfit.getRegionFollowCapital().doubleValue()
									/ investProjectInvestmentVo.getProjectFollowCapitalTotal().doubleValue()
									* investProjectInvestmentVo.getPrincipalReturnCapitalTotal().doubleValue()).setScale(2,
											BigDecimal.ROUND_HALF_UP));
				}
				
				if(investProjectInvestmentVo.getProjectFollowSingleCapitalTotal().doubleValue()==0.0) {
					// '单独资金账户（本金返还）',
					investProjectProfit.setPrincipalReturnCapitalAccount(
							new BigDecimal(0));
				}else {
					// '单独资金账户（本金返还）',
					investProjectProfit.setPrincipalReturnCapitalAccount(
							new BigDecimal(investProjectProfit.getRegionFollowCapitalAccount().doubleValue()
									/ investProjectInvestmentVo.getProjectFollowSingleCapitalTotal().doubleValue()
									* investProjectInvestmentVo.getPrincipalReturnCapitalAccountTotal().doubleValue())
											.setScale(2, BigDecimal.ROUND_HALF_UP));
				}
				
				
				// '资金池（本金未收回）'=(个人-区域跟投金额-资金池)-（个人-本金返回-资金池）
				investProjectProfit.setPrincipalNotrecoveredCapital(
						new BigDecimal(investProjectProfit.getRegionFollowCapital().doubleValue()
								- investProjectProfit.getPrincipalReturnCapital().doubleValue()).setScale(2,
										BigDecimal.ROUND_HALF_UP));

				// '单独资金账户（本金未收回）',
				investProjectProfit.setPrincipalNotrecoveredCapitalAccount(
						new BigDecimal(investProjectProfit.getRegionFollowCapitalAccount().doubleValue()
								- investProjectProfit.getPrincipalReturnCapitalAccount().doubleValue()).setScale(2,
										BigDecimal.ROUND_HALF_UP));
				projectProfitLinked.add(investProjectProfit);

			}

		}

		// 先批量删除个人项目收益
		investProjectProfitMapper.deleteInvestProjectProfitIds(investRegionInvestmentIds);
		// 再添加个人项目收益
		if (projectProfitLinked.size() > 0) {

			if (investProjectProfitMapper.insertList(projectProfitLinked) < 1) {
				throw new ServiceException("添加个人项目收益异常");
			} else {

				// 重新计算个人区域投资部分的数据

				for (InvestRegionInvestment investRegionInvestment : investRegionInvestmentList) {
					// 个人区域投资算计部分
					InvestProjectProfitVo investProjectProfitVo = investProjectProfitMapper
							.findInvestProjectProfitVo(String.valueOf(investRegionInvestment.getId()),null);
					// '资金池（理财收益）'=(个人-资金池跟投本金)/区域总募集金额*(区域-理财收益-资金池总计)
					if(investRegionTotal.getRegionAmountRaised().doubleValue()==0.0) {
						investRegionInvestment.setFinancialIncomeCapital(new BigDecimal(0));
					}else {
					investRegionInvestment.setFinancialIncomeCapital(
							new BigDecimal(investRegionInvestment.getFollowCapital().doubleValue()
									/ investRegionTotal.getRegionAmountRaised().doubleValue()
									* investRegionTotalVo.getFinancialIncomeCapitalRegionTotal().doubleValue())
											.setScale(2, BigDecimal.ROUND_HALF_UP));
					}
					// '单独资金账户（理财收益）'
					if(investRegionTotal.getSingleAmountRaised().doubleValue()==0.0) {
						investRegionInvestment.setFinancialIncomeCapitalAccount(new BigDecimal(0));
					}else {
					investRegionInvestment.setFinancialIncomeCapitalAccount(
							new BigDecimal(investRegionInvestment.getFollowCapitalAccount().doubleValue()
									/ investRegionTotal.getSingleAmountRaised().doubleValue()
									* investRegionTotalVo.getFinancialIncomeCapitalAccountRegionTotal().doubleValue())
											.setScale(2, BigDecimal.ROUND_HALF_UP));
					}
					// '资金池（募集资金余额）'=(个人-资金池跟投本金)-(个人-区域跟投金额总计)+（个人-本金返回资金池总计）
					investRegionInvestment.setRaisingBalanceCapital(
							new BigDecimal(investRegionInvestment.getFollowCapital().doubleValue()
									- investProjectProfitVo.getRegionFollowCapitalTotal().doubleValue()
									+ investProjectProfitVo.getPrincipalReturnCapitalTotal().doubleValue()).setScale(2,
											BigDecimal.ROUND_HALF_UP));
					// '单独资金账户（募集资金余额）',
					investRegionInvestment.setRaisingBalanceCapitalAccount(
							new BigDecimal(investRegionInvestment.getFollowCapitalAccount().doubleValue()
									- investProjectProfitVo.getRegionFollowCapitalAccountTotal().doubleValue()
									+ investProjectProfitVo.getPrincipalReturnCapitalAccountTotal().doubleValue())
											.setScale(2, BigDecimal.ROUND_HALF_UP));
					// 资金池（可用资金余额)=(个人-资金池跟投本金)-(个人-区域跟投金额-资金池总计)+（个人-本金返还-资金池总计+（个人-利润分配-资金池总计）-(个人-已发放个人利润（累计数）-资金池总计)+（个人-阶段性现金返还-资金池总计））
					investRegionInvestment.setAvailableBalanceCapital(
							new BigDecimal(investRegionInvestment.getFollowCapital().doubleValue()
									- investProjectProfitVo.getRegionFollowCapitalTotal().doubleValue()
									+ investProjectProfitVo.getPrincipalReturnCapitalTotal().doubleValue()
									+ investProjectProfitVo.getProfitDistributionCapitalTotal().doubleValue()
									- investProjectProfitVo.getIssuedCapitalTotal().doubleValue()
									+ investProjectProfitVo.getCashReturnCapitalTotal().doubleValue()).setScale(2,
											BigDecimal.ROUND_HALF_UP));

					// '单独资金账户（可用资金余额）'
					investRegionInvestment.setAvailableBalanceCapitalAccount(
							new BigDecimal(investRegionInvestment.getFollowCapitalAccount().doubleValue()
									- investProjectProfitVo.getRegionFollowCapitalAccountTotal().doubleValue()
									+ investProjectProfitVo.getPrincipalReturnCapitalAccountTotal().doubleValue()
									+ investProjectProfitVo.getProfitDistributionCapitalAccountTotal().doubleValue()
									- investProjectProfitVo.getIssuedCapitalAccountTotal().doubleValue()
									+ investProjectProfitVo.getCashReturnCapitalAccountTotal().doubleValue())
											.setScale(2, BigDecimal.ROUND_HALF_UP));

					investRegionInvestmentMapper.updateByPrimaryKeySelective(investRegionInvestment);
				}

			}

		}

	}

	@Override
	public Result edit(InvestProjectProfit investProjectProfit, AccessToken accessToken) {
		investProjectProfitMapper.updateByPrimaryKey(investProjectProfit);
		return Response.response();
	}

	@Override

	public Result del(String personInvestmentId) {
		investProjectProfitMapper.deleteinvestProjectProfit(personInvestmentId);
		return Response.response();
	}

}