/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：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.FundManagement.mapper.SlmFundFirstAccountMapper;
import market.Slm.FundManagement.mapper.SlmFundSecondAccountMapper;
import market.Slm.FundManagement.mapper.SlmFundThirdAccountMapper;
import market.Slm.FundManagement.service.SlmFundSecondAccountService;
import market.Slm.FundManagement.vo.SlmFundFirstAccount;
import market.Slm.FundManagement.vo.SlmFundSecondAccount;
import market.Slm.FundManagement.vo.SlmFundThirdAccount;

import org.springframework.transaction.annotation.Transactional;

import com.hnshituo.basic.service.impl.BaseServiceImpl;
import com.hnshituo.basic.spring.remoting.annotation.RemoteService;

@RemoteService(path="SlmFundSecondAccountService")
public class SlmFundSecondAccountServiceImpl extends BaseServiceImpl<SlmFundSecondAccount,Object> implements SlmFundSecondAccountService {

	/**
	 * 二级(产线)账户查询
	 * */
	public ResultModel doQueryWeb(CommonPage<SlmFundSecondAccount> sum){
		ResultModel rm=new ResultModel();
		SlmFundSecondAccount account=sum.getObject();
		try{
			List<SlmFundSecondAccount> list=this.getMapper(SlmFundSecondAccountMapper.class).findWithCondition(account, null, " is_closure desc,init_fund_time desc", 0, 0);
			Double all_in_fund=0d;
			Double all_out_fund=0d;
			for(SlmFundSecondAccount accounts:list){
				all_in_fund+=accounts.getIn_fund()==null?0:accounts.getIn_fund();
				all_out_fund+=accounts.getOut_fund()==null?0:accounts.getOut_fund();
				if(doJudge(accounts)) accounts.setHave_third_account("0");
				else accounts.setHave_third_account("1");
			}
			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));
			}
			rm.setData(list);
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
			rm.setMsgInfo(e.getMessage());
		}
		return rm;
	}

	/**
	 * 二级(产线)账户新增
	 * */
	public ResultModel doAddWeb(SlmFundSecondAccount 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()>limit_money){
//				rm.setState("500");
//				rm.setMsgInfo("填写的期初金额["+account.getInit_fund()+"]大于客户账户的剩余可新增金额["+limit_money+"]");
//				return rm;
//			}
			String id=new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date())+PbMethod.RandomCreate(10);
			account.setSecond_account_no(id);
			account.setInit_fund_time(new Date()); 		//期初时间
			account.setUse_fund(account.getInit_fund());  		//可用金额=期初
			account.setLock_flag("0");
			account.setIs_closure("0");  //不封存
			this.getMapper(SlmFundSecondAccountMapper.class).insert(account);
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
			rm.setMsgInfo(e.getMessage());
			e.printStackTrace();
		}
		return rm;
	}

	/**
	 * 二级(产线)账户修改
	 * */
	public ResultModel doUpdateWeb(SlmFundSecondAccount account){
		ResultModel rm=new ResultModel();
		try{
			if(!doJudge(account)){
				rm.setState("500");
				rm.setMsgInfo("请先将该账户下的合同账户删除");
				return rm;
			}
			SlmFundSecondAccount check=this.getMapper(SlmFundSecondAccountMapper.class).findById(account.getSecond_account_no());
			if(check!=null){
				if("1".equals(account.getLock_flag())){
					rm.setState("500");
					rm.setMsgInfo("该账户已经结转，如须修改请解锁");
					return rm;
				}
				Double total=check.getIn_fund()+check.getOut_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()); 			//账期可用金额=期初
				}
				this.getMapper(SlmFundSecondAccountMapper.class).update(account);
				rm.setState("200");
			}else{
				rm.setState("500");
				rm.setMsgInfo("该账户不存在");
				return rm;
			}
		}catch(Exception e){
			rm.setState("500");
			rm.setMsgInfo(e.getMessage());
		}
		return rm;
	}

	/**
	 * 二级(产线)账户删除
	 * */
	public ResultModel doDeleteWeb(SlmFundSecondAccount account){
		ResultModel rm=new ResultModel();
		try{
			if(!doJudge(account)){
				rm.setState("500");
				rm.setMsgInfo("请先将该账户下的合同账户删除");
				return rm;
			}
			SlmFundSecondAccount check=this.getMapper(SlmFundSecondAccountMapper.class).findById(account.getSecond_account_no());
			if(check!=null){
				if("1".equals(account.getLock_flag())){
					rm.setState("500");
					rm.setMsgInfo("该账户已经结转，如须修改请解锁");
					return rm;
				}
				Double total=check.getIn_fund()+check.getOut_fund();
				if(total>0){
					rm.setState("500");
					rm.setMsgInfo("该账户已发生资金拨动，无法删除");
					return rm;
				}
				this.getMapper(SlmFundSecondAccountMapper.class).delete(account.getSecond_account_no());
				rm.setState("200");
			}else{
				rm.setState("500");
				rm.setMsgInfo("该账户不存在");
				return rm;
			}
		}catch(Exception e){
			rm.setState("500");
			rm.setMsgInfo(e.getMessage());
		}
		return rm;
	}

	/**
	 * 账户弃用，封存
	 * @throws Exception 
	 * */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doClosureWeb(SlmFundSecondAccount account) throws Exception{
		ResultModel rm=new ResultModel();
		try{
			//二级账户封存
			account=this.getMapper(SlmFundSecondAccountMapper.class).findById(account.getSecond_account_no());
			if(account==null){
				rm.setState("500");
				rm.setMsgInfo("账户数据丢失，请联系管理员");
				return rm;
			}
			account.setIs_closure("1");
			this.getMapper(SlmFundSecondAccountMapper.class).update(account);
			//三级账户封存
			List<SlmFundThirdAccount> t_account_list=this.getMapper(SlmFundThirdAccountMapper.class).findWithCondition(null, " second_account_no='"+account.getSecond_account_no()+"'", null, 0, 0);
			for(SlmFundThirdAccount t_account:t_account_list){
				t_account.setIs_closure("1");
				this.getMapper(SlmFundThirdAccountMapper.class).update(t_account);
			}

			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
			rm.setMsgInfo(e.getMessage());
			//--19-10-15添加异常回滚
			throw new Exception(rm.getMsgInfo());
		}
		return rm;
	}

	/**
	 * 账户解封
	 * @throws Exception 
	 * */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doUnClosureWeb(SlmFundSecondAccount account) throws Exception{
		ResultModel rm=new ResultModel();
		try{
			//二级账户解封
			account=this.getMapper(SlmFundSecondAccountMapper.class).findById(account.getSecond_account_no());
			if(account==null){
				rm.setState("500");
				rm.setMsgInfo("账户数据丢失，请联系管理员");
				return rm;
			}
			account.setIs_closure("0");
			this.getMapper(SlmFundSecondAccountMapper.class).update(account);
			//三级账户解封
			List<SlmFundThirdAccount> t_account_list=this.getMapper(SlmFundThirdAccountMapper.class).findWithCondition(null, " second_account_no='"+account.getSecond_account_no()+"'", null, 0, 0);
			for(SlmFundThirdAccount t_account:t_account_list){
				t_account.setIs_closure("0");
				this.getMapper(SlmFundThirdAccountMapper.class).update(t_account);
			}

			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
			rm.setMsgInfo(e.getMessage());
			//--19-10-15添加异常回滚
			throw new Exception(rm.getMsgInfo());
		}
		return rm;
	}
	
	/**
	 * 检验是否重复
	 * 结算主体编号balance_object_no ,客户编号 customer_no
	 * */
	public boolean doCheckWeb(SlmFundSecondAccount account){
		SlmFundSecondAccount param=new SlmFundSecondAccount();
		param.setBalance_object_no(account.getBalance_object_no());
		param.setCustomer_no(account.getCustomer_no());
		param.setFirst_account_no(account.getFirst_account_no());
		param.setSecond_account_name(account.getSecond_account_name());
		param.setSecond_account_no(account.getSecond_account_no());
		int count=this.getMapper(SlmFundSecondAccountMapper.class).count(param);
		if(count>0){
			return false;
		}
		return true;
	}

	//检查是否拥有三级账户，有则不可修改 删除
	private boolean doJudge(SlmFundSecondAccount account){
		SlmFundThirdAccount third=new SlmFundThirdAccount();
		third.setSecond_account_no(account.getSecond_account_no());
		third.setFirst_account_no(account.getFirst_account_no());
		int count=this.getMapper(SlmFundThirdAccountMapper.class).count(third);
		if(count>0){
			return false;
		}
		return true;
	}

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