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

import market.Com.pb.PbMethod;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Slm.FundManagement.mapper.SlmFundDiscountMapper;
import market.Slm.FundManagement.mapper.SlmFundFirstAccountHisMapper;
import market.Slm.FundManagement.mapper.SlmFundFirstAccountMapper;
import market.Slm.FundManagement.mapper.SlmFundFirstDetailMapper;
import market.Slm.FundManagement.mapper.SlmFundSecondAccountMapper;
import market.Slm.FundManagement.mapper.SlmFundSecondDetailMapper;
import market.Slm.FundManagement.mapper.SlmFundSettleBillLineMapper;
import market.Slm.FundManagement.mapper.SlmFundSettleBillMapper;
import market.Slm.FundManagement.mapper.SlmFundSettleCarriageMapper;
import market.Slm.FundManagement.mapper.SlmFundSettleGoodMapper;
import market.Slm.FundManagement.mapper.SlmFundSettleOtherMapper;
import market.Slm.FundManagement.mapper.SlmFundSettleWriteDownMapper;
import market.Slm.FundManagement.mapper.SlmFundThirdAccountMapper;
import market.Slm.FundManagement.mapper.SlmFundThirdAccountUsedMapper;
import market.Slm.FundManagement.service.SlmFundFirstAccountHisService;
import market.Slm.FundManagement.vo.SlmFundDiscount;
import market.Slm.FundManagement.vo.SlmFundFirstAccount;
import market.Slm.FundManagement.vo.SlmFundFirstAccountHis;
import market.Slm.FundManagement.vo.SlmFundFirstDetail;
import market.Slm.FundManagement.vo.SlmFundSecondAccount;
import market.Slm.FundManagement.vo.SlmFundSecondDetail;
import market.Slm.FundManagement.vo.SlmFundSettleBill;
import market.Slm.FundManagement.vo.SlmFundSettleBillLine;
import market.Slm.FundManagement.vo.SlmFundSettleCarriage;
import market.Slm.FundManagement.vo.SlmFundSettleGood;
import market.Slm.FundManagement.vo.SlmFundSettleOther;
import market.Slm.FundManagement.vo.SlmFundSettleWriteDown;
import market.Slm.FundManagement.vo.SlmFundThirdAccount;
import market.Slm.FundManagement.vo.SlmFundThirdAccountUsed;

import org.springframework.transaction.annotation.Transactional;

import com.hnshituo.basic.service.impl.BaseServiceImpl;
import com.hnshituo.basic.spring.remoting.annotation.RemoteService;
import com.hnshituo.core.auth.service.HomeService;
import com.ibm.icu.text.SimpleDateFormat;

@RemoteService(path="SlmFundFirstAccountHisService")
public class SlmFundFirstAccountHisServiceImpl extends BaseServiceImpl<SlmFundFirstAccountHis,Object> implements SlmFundFirstAccountHisService {
//	private final static Log logger = LogFactory.getLog(SlmFundFirstAccountHisServiceImpl.class); 
	/**
	 * 结转 - 一级账户
	 * @throws Exception 
	 * */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doAddWeb(CommonPage<SlmFundFirstAccount> sum) throws Exception{
		ResultModel rm=new ResultModel();
		String userId=(String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
		String userName=(String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		List<SlmFundFirstAccount> param_list=sum.getObjectList();
		try{
			String date=new SimpleDateFormat("yyyyMMdd").format(new Date());
			Date now=new Date();
			if(param_list==null){
				rm.setState("500");
				rm.setMsgInfo("请勾选客户账户进行结转");
				return rm;
			}
			//各级账户 锁定
			for(SlmFundFirstAccount accounts:param_list){
				List<SlmFundFirstAccount> list=this.getMapper(SlmFundFirstAccountMapper.class).findWithCondition(null, " is_closure!='1' and lock_flag='0' and customer_no='"+accounts.getCustomer_no()+"'", null, 0, 0);
				if(list.size()>0){
					SlmFundFirstAccount account=list.get(0);
					//历史信息
					String seq=new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date())+PbMethod.RandomCreate(13);
					SlmFundFirstAccountHis his=new SlmFundFirstAccountHis();
					his.setFirst_account_no(account.getFirst_account_no());
					his.setSeq(seq);
					his.setFirst_account_name(account.getFirst_account_name());
					his.setBalance_object_no(account.getBalance_object_no());
					his.setBalance_object_name(account.getBalance_object_name());
					his.setCustomer_no(account.getCustomer_no());
					his.setCustomer_name(account.getCustomer_name());
					his.setDiscount_fund(account.getDiscount_fund());
					his.setWrite_down_fund(account.getWrite_down_fund());
					his.setInit_fund(account.getInit_fund());
					his.setInit_time(account.getInit_fund_time());
					his.setUse_fund(account.getUse_fund());
					his.setFinal_fund(account.getFinal_fund());
					his.setIn_fund(account.getIn_fund());
					his.setOut_fund(account.getOut_fund());
					his.setSettlement_fund(account.getSettlement_fund());
					his.setUsed_fund(account.getUsed_fund());
					his.setBack_pay_fund(account.getPay_back_fund());
					his.setBalance_fund(account.getBalance_fund());
					his.setInit_balance_fund(account.getInit_balance_fund());
					his.setInit_used_fund(account.getInit_used_fund());
					his.setCarryover_seq(date);
					his.setCarryover_flag("1"); 		//结转状态？
					his.setCarryover_man_no(userId);
					his.setCarryover_man_name(userName);
					his.setCarryover_time(new Date());
					this.getMapper(SlmFundFirstAccountHisMapper.class).insert(his);

					
					//可用不动，账期余额不动
					account.setInit_fund(account.getUse_fund());		//销售期初=可用
					account.setDiscount_fund(0d);
					account.setInit_balance_fund(account.getBalance_fund()); 		//财务期初=财务余额
//					account.setFinal_fund(account.getUse_fund()); 		//可拨=可用
					account.setIn_fund(0d);					//进账=0
					account.setOut_fund(0d);				//出账=0
					account.setSettlement_fund(0d); 			//结算金额=0
					//获取3级账号的占用资金
					SlmFundThirdAccountUsed re_used=this.getMapper(SlmFundThirdAccountUsedMapper.class).sumUsed(account.getFirst_account_no());
					account.setInit_used_fund(0d);
					if(re_used!=null&&re_used.getUsed_fund()!=null){
						account.setInit_used_fund(re_used.getUsed_fund());//期初占用金额
					}
					account.setUsed_fund(0d); 			//占用金额=0
					account.setPay_back_fund(0d); 	  //回款金额=0	
					account.setLock_flag("1");		//一级账户  已锁定
					account.setInit_time(account.getInit_fund_time());
					account.setInit_fund_time(now);
					account.setInit_man_no(userId);
					account.setInit_man_name(userName);
					this.getMapper(SlmFundFirstAccountMapper.class).update(account);

					//二级账户
					List<SlmFundSecondAccount> second_list=this.getMapper(SlmFundSecondAccountMapper.class).findWithCondition(null, " is_closure!='1' and lock_flag='0' and first_account_no='"+account.getFirst_account_no()+"'", null, 0, 0);
					for(SlmFundSecondAccount s_account:second_list){
						//生成历史信息
						rm=this.getBean(SlmFundSecondAccountHisServiceImpl.class).doAdd(s_account,date);
						if("500".equals(rm.getMsgInfo())) throw new Exception(rm.getMsgInfo());
						s_account.setInit_fund(s_account.getUse_fund());		//期初=可用
						s_account.setIn_fund(0d);					//进出账=0
						s_account.setOut_fund(0d);
						s_account.setLock_flag("1"); 		//二级账户  已锁定
						s_account.setInit_fund_time(now);
						this.getMapper(SlmFundSecondAccountMapper.class).update(s_account);
					}
					//三级账户
					List<SlmFundThirdAccount> third_list=this.getMapper(SlmFundThirdAccountMapper.class).findWithCondition(null, " is_closure!='1' and lock_flag='0' and first_account_no='"+account.getFirst_account_no()+"'", null, 0, 0);
					for(SlmFundThirdAccount t_account:third_list){
						//生成历史信息
						rm=this.getBean(SlmFundThirdAccountHisServiceImpl.class).doAdd(t_account,date);
						if("500".equals(rm.getMsgInfo())) throw new Exception(rm.getMsgInfo());
						//						t_account.setInit_fund(t_account.getUse_fund());		//期初=可用
						//						t_account.setIn_fund(0d);					//进出账=0
						//						t_account.setOut_fund(0d);
						//						t_account.setUsed_fund(0d); 			//占用金额=0
						//						t_account.setSettlement_fund(0d); 			//结算金额=0
						t_account.setLock_flag("1"); 		//三级账户  已锁定
						t_account.setInit_time(now);
						this.getMapper(SlmFundThirdAccountMapper.class).update(t_account);
					}
//					改--将以下结转按客户进行
					//回款信息结转
					SlmFundFirstDetail detail=new SlmFundFirstDetail();
					detail.setCarryover_man_no(userId);
					detail.setCarryover_man_name(userName);
					detail.setCarryover_time(new Date());
					detail.setCarryover_seq(date);
					detail.setCustomer_no(account.getCustomer_no());
					this.getMapper(SlmFundFirstDetailMapper.class).doCarryover(detail);

					//资金调拨结转
					SlmFundSecondDetail s_detail=new SlmFundSecondDetail();
					s_detail.setCarryover_man_no(userId);
					s_detail.setCarryover_man_name(userName);
					s_detail.setCarryover_seq(date);
					s_detail.setCustomer_no(account.getCustomer_no());
					this.getMapper(SlmFundSecondDetailMapper.class).doCarryover(s_detail);

					//资金占用结转
					SlmFundThirdAccountUsed used=new SlmFundThirdAccountUsed();
					used.setCarryover_man_no(userId);
					used.setCarryover_man_name(userName);
					used.setCarryover_seq(date);
					used.setCustomer_no(account.getCustomer_no());
					this.getMapper(SlmFundThirdAccountUsedMapper.class).doCarryover(used);

					//冲减信息结转
					SlmFundSettleWriteDown downs=new SlmFundSettleWriteDown();
					downs.setCarryover_man_no(userId);
					downs.setCarryover_man_name(userName);
					downs.setCarryover_seq(date);
					downs.setCustomer_no(account.getCustomer_no());
					this.getMapper(SlmFundSettleWriteDownMapper.class).doCarryover(downs);

					//贴息信息结转
					SlmFundDiscount discounts=new SlmFundDiscount();
					discounts.setCarryover_man_no(userId);
					discounts.setCarryover_man_name(userName);
					discounts.setCarryover_seq(date);
					discounts.setCustomer_no(account.getCustomer_no());
					this.getMapper(SlmFundDiscountMapper.class).doCarryover(discounts);

					//结算单以及三种费用结转
					List<SlmFundSettleBill> bill_list=this.getMapper(SlmFundSettleBillMapper.class).findWithCondition(null, " flag='6' and customer_no='"+account.getCustomer_no()+"'", null, 0, 0);
					for(SlmFundSettleBill bill:bill_list){
						bill.setCarryover_seq(date);
						bill.setCarryover_man_no(userId);
						bill.setCarryover_man_name(userName);
						bill.setCarryover_time(new Date());
						bill.setFlag("7"); 		//结转
						this.getMapper(SlmFundSettleBillMapper.class).update(bill);
						//修改三种费用的状态
						SlmFundSettleBillLine line=new SlmFundSettleBillLine();
						line.setSettle_bill_no(bill.getSettle_bill_no());
						List<SlmFundSettleBillLine> lineList=this.getMapper(SlmFundSettleBillLineMapper.class).find(line,0,0);
						for(SlmFundSettleBillLine lines:lineList){
							SlmFundSettleGood goods=new SlmFundSettleGood();
							goods.setCarryover_man_no(userId);
							goods.setCarryover_man_name(userName);
							goods.setCarryover_seq(date);
							goods.setBill_no(lines.getBill_no());
							this.getMapper(SlmFundSettleGoodMapper.class).doCarryover(goods);

							SlmFundSettleCarriage carriages=new SlmFundSettleCarriage();
							carriages.setCarryover_man_no(userId);
							carriages.setCarryover_man_name(userName);
							carriages.setCarryover_seq(date);
							carriages.setBill_no(lines.getBill_no());
							this.getMapper(SlmFundSettleCarriageMapper.class).doCarryover(carriages);

							SlmFundSettleOther others=new SlmFundSettleOther();
							others.setCarryover_man_no(userId);
							others.setCarryover_man_name(userName);
							others.setCarryover_seq(date);
							others.setBill_no(lines.getBill_no());
							this.getMapper(SlmFundSettleOtherMapper.class).doCarryover(others);
						}
					}
				}
				
			}
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
			rm.setMsgInfo(e.getMessage());
			//--19-10-15添加异常回滚
			throw new Exception(rm.getMsgInfo());
		}
		return rm;
	}

	/**
	 * 解锁
	 * */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doUnlockWeb(CommonPage<SlmFundFirstAccount> sum) throws Exception{
		ResultModel rm=new ResultModel();
		List<SlmFundFirstAccount> first_list=sum.getObjectList();
		try{
			for(SlmFundFirstAccount account:first_list){
				if(account.getFirst_account_no()!=null){
					account.setLock_flag("0");		//一级账户  解锁
					this.getMapper(SlmFundFirstAccountMapper.class).update(account);
					//二级账户
					this.getMapper(SlmFundSecondAccountMapper.class).doUnlock(account.getFirst_account_no());
					//三级账户
					this.getMapper(SlmFundThirdAccountMapper.class).doUnlock(account.getFirst_account_no());
					
//					List<SlmFundSecondAccount> second_list=this.getMapper(SlmFundSecondAccountMapper.class).findWithCondition(null, " first_account_no='"+account.getFirst_account_no()+"' and is_closure!='1'", null, 0, 0);
//					for(SlmFundSecondAccount s_account:second_list){
//						s_account.setLock_flag("0"); 		//二级账户  解锁
//						this.getMapper(SlmFundSecondAccountMapper.class).update(s_account);
//					}
//					//三级账户
//					List<SlmFundThirdAccount> third_list=this.getMapper(SlmFundThirdAccountMapper.class).findWithCondition(null, " first_account_no='"+account.getFirst_account_no()+"' and  is_closure!='1'", null, 0, 0);
//					for(SlmFundThirdAccount t_account:third_list){
//						t_account.setLock_flag("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;
	}

	/**
	 * 查询一级账户的结转信息
	 * */
	public ResultModel doQueryWeb(CommonPage<SlmFundFirstAccountHis> sum){
		ResultModel rm=new ResultModel();
		SlmFundFirstAccountHis his=sum.getObject();
		try{
			List<SlmFundFirstAccountHis> list=this.getMapper(SlmFundFirstAccountHisMapper.class).findWithCondition(null, " first_account_no='"+his.getFirst_account_no()+"'", " carryover_time desc ", 0, 0);
			rm.setState("200");
			rm.setData(list);
		}catch(Exception e){
			rm.setState("500");
			rm.setMsgInfo(e.getMessage());
		}
		return rm;
	}
}