/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2018-01-13
 * 描述            ：客户三级账户信息表
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
 ***********************************************************/
package market.Slm.FundManagement.impl;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import market.Com.pb.DoubleUtil;
import market.Com.pb.PbMethod;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Slm.ContractManagement.mapper.SlmContractHeatInfoMapper;
import market.Slm.ContractManagement.vo.SlmContractHeatInfo;
import market.Slm.FundManagement.mapper.SlmFundSecondAccountMapper;
import market.Slm.FundManagement.mapper.SlmFundThirdAccountMapper;
import market.Slm.FundManagement.service.SlmFundThirdAccountService;
import market.Slm.FundManagement.vo.SlmFundSecondAccount;
import market.Slm.FundManagement.vo.SlmFundThirdAccount;

import com.hnshituo.basic.service.impl.BaseServiceImpl;
import com.hnshituo.basic.service.support.PageModel;
import com.hnshituo.basic.spring.remoting.annotation.RemoteService;
import com.hnshituo.core.auth.service.HomeService;

@RemoteService(path="SlmFundThirdAccountService")
public class SlmFundThirdAccountServiceImpl extends BaseServiceImpl<SlmFundThirdAccount,Object> implements SlmFundThirdAccountService {

	/**
	 * 三级(合同)账户查询
	 * */
	public ResultModel doQueryWeb(CommonPage<SlmFundThirdAccount> sum){
		ResultModel rm=new ResultModel();
		SlmFundThirdAccount account=sum.getObject();
		int pageIndex=sum.getPageIndex();
		int pageSize=sum.getPageSize();
		PageModel<SlmFundThirdAccount> pm=new PageModel<>(pageIndex, pageSize);
		try{
			List<SlmFundThirdAccount> list=this.getMapper(SlmFundThirdAccountMapper.class).findWithCondition(account, null, " used_fund desc", (pageIndex-1)*pageSize, pageSize);
			int count=this.getMapper(SlmFundThirdAccountMapper.class).countWithCondition(account, null);
			Double all_in_fund=0d;
			Double all_out_fund=0d;
			Double all_settlement_fund=0d;
			Double all_used_fund=0d;
			for(SlmFundThirdAccount accounts:list){
				all_in_fund+=accounts.getIn_fund()==null?0:accounts.getIn_fund();
				all_out_fund+=accounts.getOut_fund()==null?0:accounts.getOut_fund();
				all_settlement_fund+=accounts.getSettlement_fund()==null?0:accounts.getSettlement_fund();
				all_used_fund+=accounts.getUsed_fund()==null?0:accounts.getUsed_fund();
			}
			if(list.size()>0){
				list.get(0).setAll_in_fund(DoubleUtil.round(all_in_fund,DoubleUtil.SCALE_PRICE));
				list.get(0).setAll_out_fund(DoubleUtil.round(all_out_fund,DoubleUtil.SCALE_PRICE));
				list.get(0).setAll_settlement_fund(DoubleUtil.round(all_settlement_fund,DoubleUtil.SCALE_PRICE));
				list.get(0).setAll_used_fund(DoubleUtil.round(all_used_fund,DoubleUtil.SCALE_PRICE));
			}
			pm.setTotal(count);
			pm.setRows(list);
			rm.setData(pm);
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
			rm.setMsgInfo(e.getMessage());
		}
		return rm;
	}

	/**
	 * 三级(合同)账户新增
	 * */
	public ResultModel doAddWeb(SlmFundThirdAccount account){
		ResultModel rm=new ResultModel();
		try{
			//验证重复
			if(!doCheckWeb(account)){
				rm.setState("500");
				rm.setMsgInfo("该合同账户已存在");
				return rm;
			}
//			double limit_money=doCheckAmount(account);
//			if(account.getInit_fund()!=null&&account.getInit_fund()>limit_money){
//				rm.setState("500");
//				rm.setMsgInfo("填写的期初金额["+account.getInit_fund()+"]大于产线账户的剩余可新增金额["+limit_money+"]");
//				return rm;
//			}
			SlmContractHeatInfo heat=new SlmContractHeatInfo();
			heat.setContract_no(account.getThird_account_no());
			List<SlmContractHeatInfo> heat_list=this.getMapper(SlmContractHeatInfoMapper.class).find(heat, 0, 0);
			if(heat_list.size()>0){
				if(!heat_list.get(0).getCustomer_no().equals(account.getCustomer_no())){
					rm.setState("500");
					rm.setMsgInfo("请输入正确的合同号");
					return rm;
				}
			}else{
				rm.setState("500");
				rm.setMsgInfo("请输入正确的合同号");
				return rm;
			}
			
			if(account.getThird_account_no()==null||"".equals(account.getThird_account_no())){
				String id=new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date())+PbMethod.RandomCreate(10);
				account.setThird_account_no(id);
			}
			account.setInit_time(new Date()); 		//期初时间
			account.setCreate_time(new Date());
			account.setUse_fund(account.getInit_fund());  		//可用金额=期初
			account.setLock_flag("0");
			account.setIs_closure("0");  //不封存
			this.getMapper(SlmFundThirdAccountMapper.class).insert(account);
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
			rm.setMsgInfo(e.getMessage());
		}
		return rm;
	}

	/**
	 * 三级(合同)账户修改
	 * */
	public ResultModel doUpdateWeb(SlmFundThirdAccount account){
		ResultModel rm=new ResultModel();
		try{
			//发生占用，拨进拨出则无法修改
			SlmFundThirdAccount check=this.getMapper(SlmFundThirdAccountMapper.class).findById(account.getThird_account_no());
			if(check!=null){
				if("1".equals(check.getLock_flag())){
					rm.setState("500");
					rm.setMsgInfo("该账户已经结转，如须修改请解锁");
					return rm;
				}
				Double total=check.getIn_fund()+check.getOut_fund()+check.getUsed_fund();
				if(total>0){
					rm.setState("500");
					rm.setMsgInfo("该账户已发生资金变动，无法修改");
					return rm;
				}
				if(check.getInit_fund()!=account.getInit_fund()){		//修改了期初金额
					double limit_money=doCheckAmount(account)+check.getInit_fund();
					if(account.getInit_fund()>limit_money){
						rm.setState("500");
						rm.setMsgInfo("填写的期初金额["+account.getInit_fund()+"]大于客户账户的剩余可新增金额["+limit_money+"]");
						return rm;
					}
					account.setUse_fund(account.getInit_fund()); 			//账期可用金额=期初
				}
				account.setUse_fund(account.getInit_fund());
				this.getMapper(SlmFundThirdAccountMapper.class).update(account);
			}else{
				rm.setState("500");
				rm.setMsgInfo("该账户不存在");
				return rm;
			}
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
			rm.setMsgInfo(e.getMessage());
		}
		return rm;
	}

	/**
	 * 三级(合同)账户删除
	 * */
	public ResultModel doDeleteWeb(SlmFundThirdAccount account){
		ResultModel rm=new ResultModel();
		try{
			//发生占用，拨进拨出则无法删除
			SlmFundThirdAccount check=this.getMapper(SlmFundThirdAccountMapper.class).findById(account.getThird_account_no());
			if(check!=null){
				if("1".equals(check.getLock_flag())){
					rm.setState("500");
					rm.setMsgInfo("该账户已经结转，如须删除请解锁");
					return rm;
				}
				Double total=check.getIn_fund()+check.getOut_fund()+check.getUsed_fund();
				if(total>0){
					rm.setState("500");
					rm.setMsgInfo("该账户已发生资金变动，无法删除");
					return rm;
				}
				this.getMapper(SlmFundThirdAccountMapper.class).delete(account.getThird_account_no());
			}else{
				rm.setState("500");
				rm.setMsgInfo("该账户不存在");
				return rm;
			}
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
			rm.setMsgInfo(e.getMessage());
		}
		return rm;
	}
	
	/**
	 * 账户弃用，封存
	 * */
	public ResultModel doClosureWeb(SlmFundThirdAccount account){
		ResultModel rm=new ResultModel();
		try{
			//三级账户封存
			account=this.getMapper(SlmFundThirdAccountMapper.class).findById(account.getThird_account_no());
			if(account==null){
				rm.setState("500");
				rm.setMsgInfo("账户数据丢失，请联系管理员");
				return rm;
			}
			account.setIs_closure("1");
			this.getMapper(SlmFundThirdAccountMapper.class).update(account);

			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
			rm.setMsgInfo(e.getMessage());
		}
		return rm;
	}
	
	/**
	 * 账户解封
	 * */
	public ResultModel doUnClosureWeb(SlmFundThirdAccount account){
		ResultModel rm=new ResultModel();
		try{
			//三级账户解封
			account=this.getMapper(SlmFundThirdAccountMapper.class).findById(account.getThird_account_no());
			if(account==null){
				rm.setState("500");
				rm.setMsgInfo("账户数据丢失，请联系管理员");
				return rm;
			}
			account.setIs_closure("0");
			this.getMapper(SlmFundThirdAccountMapper.class).update(account);

			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
			rm.setMsgInfo(e.getMessage());
		}
		return rm;
	}

	/**
	 * 检验是否重复
	 * 结算主体编号balance_object_no ,客户编号 customer_no
	 * */
	public boolean doCheckWeb(SlmFundThirdAccount account){
		SlmFundThirdAccount param=new SlmFundThirdAccount();
		param.setThird_account_no(account.getThird_account_no());
		int count=this.getMapper(SlmFundThirdAccountMapper.class).count(param);
		if(count>0){
			return false;
		}
		return true;
	}

	/**
	 * 金额验证，可填写多少期初金额
	 * 目前算法为，二级账户的期初金额-其余三级账户的期初金额
	 * */
	private double doCheckAmount(SlmFundThirdAccount account){
		SlmFundSecondAccount s_account=this.getMapper(SlmFundSecondAccountMapper.class).findById(account.getSecond_account_no());
		double money=0d;
		if(s_account!=null){
			List<SlmFundThirdAccount> list=this.getMapper(SlmFundThirdAccountMapper.class).findWithCondition(null, " Second_account_no='"+s_account.getSecond_account_no()+"'", null, 0, 0);
			for(SlmFundThirdAccount accounts:list){
				money=DoubleUtil.add(money, accounts.getInit_fund());
			}
			money=DoubleUtil.sub(s_account.getInit_fund(),money);
		}
		return money;
	}

	//=======================APP eason add 2019-07-15===================
	/**
	 * 三级(合同)账户查询
	 * */
	public ResultModel doQueryApp(CommonPage<SlmFundThirdAccount> sum){
		ResultModel rm=new ResultModel();
		SlmFundThirdAccount account=sum.getObject();
		account.setCustomer_no((String)this.getSession().getAttribute(HomeService.SESSION_DEPT_ID));
		int pageIndex=sum.getPageIndex();
		int pageSize=sum.getPageSize();
		PageModel<SlmFundThirdAccount> pm=new PageModel<>(pageIndex, pageSize);
		try{
			String sWhere=" 1=1 ";
			if(account.getThird_account_no()!=null&&!account.getThird_account_no().equals("")){
				sWhere+=" and third_account_no like '%"+account.getThird_account_no()+"%' ";
				account.setThird_account_no(null);
			}
			List<SlmFundThirdAccount> list=this.getMapper(SlmFundThirdAccountMapper.class).findWithCondition(account, sWhere, " used_fund desc", (pageIndex-1)*pageSize, pageSize);
			int count=this.getMapper(SlmFundThirdAccountMapper.class).countWithCondition(account, sWhere);
			Double all_in_fund=0d;
			Double all_out_fund=0d;
			Double all_settlement_fund=0d;
			Double all_used_fund=0d;
			for(SlmFundThirdAccount accounts:list){
				all_in_fund+=accounts.getIn_fund()==null?0:accounts.getIn_fund();
				all_out_fund+=accounts.getOut_fund()==null?0:accounts.getOut_fund();
				all_settlement_fund+=accounts.getSettlement_fund()==null?0:accounts.getSettlement_fund();
				all_used_fund+=accounts.getUsed_fund()==null?0:accounts.getUsed_fund();
			}
			if(list.size()>0){
				list.get(0).setAll_in_fund(DoubleUtil.round(all_in_fund,DoubleUtil.SCALE_PRICE));
				list.get(0).setAll_out_fund(DoubleUtil.round(all_out_fund,DoubleUtil.SCALE_PRICE));
				list.get(0).setAll_settlement_fund(DoubleUtil.round(all_settlement_fund,DoubleUtil.SCALE_PRICE));
				list.get(0).setAll_used_fund(DoubleUtil.round(all_used_fund,DoubleUtil.SCALE_PRICE));
			}
			pm.setTotal(count);
			pm.setRows(list);
			rm.setData(pm);
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
			rm.setMsgInfo(e.getMessage());
		}
		return rm;
	}
}