/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2018-01-13
 * 描述            ：客户发票管理表
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
 ***********************************************************/
package market.Slm.FundManagement.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
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.mapper.SlmContractPaymentMapper;
import market.Slm.ContractManagement.vo.SlmContractHeatInfo;
import market.Slm.ContractManagement.vo.SlmContractPayment;
import market.Slm.DeliveryManagement.mapper.SlmLadingBillHeatMapper;
import market.Slm.DeliveryManagement.mapper.SlmLadingBillLineMapper;
import market.Slm.DeliveryManagement.vo.SlmLadingBillHeat;
import market.Slm.DeliveryManagement.vo.SlmLadingBillLine;
import market.Slm.FundManagement.mapper.SlmFundFirstAccountMapper;
import market.Slm.FundManagement.mapper.SlmFundInvoiceApplyMapper;
import market.Slm.FundManagement.mapper.SlmFundInvoiceManageMapper;
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.SlmFundInvoiceManageService;
import market.Slm.FundManagement.vo.SlmFundFirstAccount;
import market.Slm.FundManagement.vo.SlmFundInvoiceApply;
import market.Slm.FundManagement.vo.SlmFundInvoiceManage;
import market.Slm.FundManagement.vo.SlmFundSettleBill;
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.apache.commons.collections.CollectionUtils;
import org.springframework.transaction.annotation.Transactional;

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="SlmFundInvoiceManageService")
public class SlmFundInvoiceManageServiceImpl extends BaseServiceImpl<SlmFundInvoiceManage,Object> implements SlmFundInvoiceManageService {

	/**
	 * 票据查询
	 * */
	public ResultModel doQueryWeb(CommonPage<SlmFundInvoiceManage> sum){
		ResultModel rm=new ResultModel();
		SlmFundInvoiceManage invoice=sum.getObject();
		String startTime=sum.getStartTime();
		String endTime=sum.getEndTime();
		String flag=sum.getMemo1();
		try{
			//			String sWhere=" 1=1 ";
			//			if(startTime!=null&&!"".equals(startTime)){		//页面上为开票时间
			//				sWhere+=" and to_char(invoice_time, 'yyyy-mm-dd hh24:mi:ss') >='"+startTime+"'";
			//			}
			//			if(endTime!=null&&!"".equals(endTime)){
			//				sWhere+=" and to_char(invoice_time, 'yyyy-mm-dd hh24:mi:ss') <= '"+endTime+"'";
			//			}
			//			if(invoice.getInvoice_no()!=null){
			//				sWhere+=" and invoice_no like concat(CONCAT('%','"+invoice.getInvoice_no()+"'),'%')";
			//				invoice.setInvoice_no(null);
			//			}
			//			if(invoice.getSettle_bill_no()!=null){
			//				sWhere+=" and settle_bill_no like concat(CONCAT('%','"+invoice.getSettle_bill_no()+"'),'%')";
			//				invoice.setSettle_bill_no(null);
			//			}
			//			List<SlmFundInvoiceManage> list=this.getMapper(SlmFundInvoiceManageMapper.class).findWithCondition(invoice, sWhere, " invoice_flag,invoice_time desc ", 0, 0);
			invoice.setHave_red(flag);
			List<SlmFundInvoiceManage> list=this.getMapper(SlmFundInvoiceManageMapper.class).findbill(invoice, startTime, endTime);
			for(SlmFundInvoiceManage manage:list){
				if(manage.getSettle_bill_no()!=null){
					SlmFundSettleBill bill=this.getMapper(SlmFundSettleBillMapper.class).findById(manage.getSettle_bill_no());
					if(bill!=null)
						manage.setContract_no(bill.getContract_no());
				}
			}
			rm.setState("200");
			rm.setData(list);
		}catch(Exception e){
			rm.setState("500");
			rm.setMsgInfo(e.getMessage());
		}
		return rm;
	}

	/**
	 * 票据新增
	 * */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doAddWeb(CommonPage<SlmFundInvoiceManage> 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);
		SlmFundInvoiceManage invoice=sum.getObject();
		try{
			String state="1";			//正
			SlmFundInvoiceManage check=this.getMapper(SlmFundInvoiceManageMapper.class).findById(invoice.getInvoice_no());
			if(check!=null){
				rm.setState("500");
				rm.setMsgInfo("该发票编号已被使用");
				return rm;
			}
			SlmFundInvoiceApply apply=this.getMapper(SlmFundInvoiceApplyMapper.class).findById(invoice.getApply_no());
			if(apply==null){
				rm.setState("500");
				rm.setMsgInfo("未找到对应的开票申请信息");
				return rm;
			}
			if(apply.getInvoice_fund()!=null&&apply.getInvoice_fund()<0)
				state="0";			//负
			//加入冲减信息
			List<SlmFundSettleWriteDown> downList=this.getMapper(SlmFundSettleWriteDownMapper.class).findWithCondition(null, " settle_bill_no='"+invoice.getSettle_bill_no()+"' and valid_flag='1' and flag in ('3','4','5')", null,0,0);		
			double down_money=0;
			if(downList.size()>0){
				for(SlmFundSettleWriteDown downs:downList){
					if("1".equals(downs.getSettle_write_down_type())){ 		//冲减
						down_money=DoubleUtil.sub(down_money, downs.getSettle_write_down_fund());
					}else{
						down_money=DoubleUtil.add(down_money, downs.getSettle_write_down_fund());
					}
				}
				invoice.setInvoice_fund(DoubleUtil.add(invoice.getInvoice_fund(),down_money));
			}


			//新增票据信息
			if(invoice.getValid_flag()==null||"".equals(invoice.getValid_flag()))
				invoice.setValid_flag("1"); 		//有效
			invoice.setInvoice_flag("1"); 		//1：待接收
			if("0".equals(state))
				invoice.setInvoice_fund(-invoice.getInvoice_fund());
			invoice.setCreate_man_no(userId);
			invoice.setCreate_man_name(userName);
			invoice.setCreate_time(new Date());
			invoice.setInvoice_time(new Date());
			if(invoice.getCreate_man_name()==null) invoice.setCreate_man_name("用友");
			if("1".equals(state)){
				doUpdateInvoiceNo(invoice,1);
				invoice.setHave_red("0");
				this.getMapper(SlmFundInvoiceManageMapper.class).insert(invoice);
			}else if("0".equals(state)){			//如果对应的结算单已结算 则吐出结算金额
				SlmFundSettleBill bill=this.getMapper(SlmFundSettleBillMapper.class).findById(invoice.getSettle_bill_no());
				if(bill!=null){
					invoice.setHave_red("1");
					if(!"6".equals(bill.getFlag())&&!"7".equals(bill.getFlag())){
						rm.setMsgInfo("该申请对应的结算单未结算，请操作对应的发票和结算单信息");
						rm.setState("500");
						return rm;
					}
					if(invoice.getCustomer_no()!=null){
						SlmFundFirstAccount account=new SlmFundFirstAccount();
						account.setCustomer_no(invoice.getCustomer_no());
						List<SlmFundFirstAccount> account_list=this.getMapper(SlmFundFirstAccountMapper.class).find(account, 0, 0);
						if(account_list.size()>0){
							//账户改钱
							double fund=-invoice.getInvoice_fund();
							account=account_list.get(0);
							account.setUse_fund(DoubleUtil.add(account.getUse_fund(),fund));
							//							account.setFinal_fund(DoubleUtil.add(account.getFinal_fund(),fund));
							account.setBalance_fund(DoubleUtil.add(account.getBalance_fund(),fund));
							account.setSettlement_fund(DoubleUtil.sub(account.getSettlement_fund(),fund));
							account.setWrite_down_fund(DoubleUtil.add(account.getWrite_down_fund(), down_money));		//冲减总额 + 冲减金额
							this.getMapper(SlmFundFirstAccountMapper.class).update(account);
							//三级账户
							SlmFundThirdAccount t_account=this.getMapper(SlmFundThirdAccountMapper.class).findById(bill.getContract_no());
							if(t_account!=null){
								t_account.setUse_fund(DoubleUtil.add(DoubleUtil.add(t_account.getUse_fund(),fund),down_money));
								t_account.setSettlement_fund(DoubleUtil.sub(t_account.getSettlement_fund(),fund));
								this.getMapper(SlmFundThirdAccountMapper.class).update(t_account);
							}
							//开票申请-已开票
							apply.setApply_flag("4");
							this.getMapper(SlmFundInvoiceApplyMapper.class).update(apply);
							this.getMapper(SlmFundInvoiceManageMapper.class).insert(invoice);
							//结算单 回到确认，可重新申请开票
							bill.setFlag("3");
							this.getMapper(SlmFundSettleBillMapper.class).update(bill);
						}
					}
				}else{
					rm.setState("500");
					rm.setMsgInfo("未找到对应的结算单，请检查参数settle_bill_no");
				}
			}
			//			rm=doUpdateInvoiceNo(invoice,1);
			//			if("500".equals(rm.getState())){
			//				throw new Exception(rm.getMsgInfo());
			//			}
			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 doUpdateWeb(CommonPage<SlmFundInvoiceManage> sum){
		ResultModel rm=new ResultModel();
		String userId=(String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
		String userName=(String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		SlmFundInvoiceManage invoice=sum.getObject();
		try{
			if(invoice==null||invoice.getApply_no()==null){
				rm.setState("500");
				rm.setMsgInfo("请选择数据进行修改");
				return rm;
			}
			SlmFundInvoiceManage check=this.getMapper(SlmFundInvoiceManageMapper.class).findById(invoice.getInvoice_no());
			if(check==null||!"1".equals(check.getInvoice_flag())){
				rm.setState("500");
				rm.setMsgInfo("票据状态不为待接收无法修改");
				return rm;
			}
			invoice.setUpdate_man_no(userId);
			invoice.setUpdate_man_name(userName);
			invoice.setUpdate_time(new Date());
			this.getMapper(SlmFundInvoiceManageMapper.class).update(invoice);
			if(check.getInvoice_fund()>0){
				rm=doUpdateInvoiceNo(invoice,2);
				if("500".equals(rm.getState())){
					throw new Exception(rm.getMsgInfo());
				}
			}
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
			rm.setMsgInfo(e.getMessage());
		}
		return rm;
	}

	/**
	 * 票据删除
	 * @throws Exception 
	 * */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doDeleteWeb(CommonPage<SlmFundInvoiceManage> 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<SlmFundInvoiceManage> invoice_list=sum.getObjectList();
		try{
			for(SlmFundInvoiceManage invoice:invoice_list){
				invoice=this.getMapper(SlmFundInvoiceManageMapper.class).findById(invoice.getInvoice_no());
				if(invoice==null){
					rm.setState("500");
					rm.setMsgInfo("数据不存在，请联系管理员处理");
					return rm;
				}
				SlmFundInvoiceApply apply=this.getMapper(SlmFundInvoiceApplyMapper.class).findById(invoice.getApply_no());
				if(apply==null){
					rm.setState("500");
					rm.setMsgInfo("发票申请信息丢失，请联系管理员");
					return rm;
				}
				if(!"1".equals(invoice.getInvoice_flag())){
					rm.setState("500");
					rm.setMsgInfo("只有待发送状态的数据才能删除");
					return rm;
				}
				if(apply.getInvoice_fund()<0){
					rm.setState("500");
					rm.setMsgInfo("红字票据暂时不允许删除");
					return rm;
				}
				invoice.setValid_flag("0"); 		//无效
				invoice.setDelete_man_no(userId);
				invoice.setDelete_man_name(userName);
				invoice.setDelete_time(new Date());
				this.getMapper(SlmFundInvoiceManageMapper.class).update(invoice);
				if(invoice.getInvoice_fund()>0){
					rm=doUpdateInvoiceNo(invoice,3);
					if("500".equals(rm.getState())){
						throw new Exception(rm.getMsgInfo());
					}
				}
			}
			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 doSubmitWeb(CommonPage<SlmFundInvoiceManage> 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<SlmFundInvoiceManage> billList=sum.getObjectList();
		try{
			for(SlmFundInvoiceManage bill:billList){
				bill=this.getMapper(SlmFundInvoiceManageMapper.class).findById(bill.getInvoice_no());
				if(bill!=null&&"1".equals(bill.getInvoice_flag())){
					bill.setUpdate_man_no(userId);
					bill.setUpdate_man_name(userName);
					bill.setUpdate_time(new Date());
					bill.setInvoice_flag("2");		//待邮寄
					this.getMapper(SlmFundInvoiceManageMapper.class).update(bill);
				}else{
					rm.setState("500");
					rm.setMsgInfo("状态为待接收的数据才能提交");
					return rm;
				}
			}
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
			rm.setMsgInfo(e.getMessage());
			e.printStackTrace();
			//--19-10-15添加异常回滚
			throw new Exception(rm.getMsgInfo());
		}
		return rm;
	}

	/**
	 * 接收
	 * */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doReceiveWeb(CommonPage<SlmFundInvoiceManage> 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<SlmFundInvoiceManage> billList=sum.getObjectList();
		try{
			for(SlmFundInvoiceManage bill:billList){
				bill=this.getMapper(SlmFundInvoiceManageMapper.class).findById(bill.getInvoice_no());
				if(bill!=null&&"1".equals(bill.getInvoice_flag())){
					bill.setUpdate_man_no(userId);
					bill.setUpdate_man_name(userName);
					bill.setUpdate_time(new Date());
					bill.setInvoice_flag("2");		//待邮寄
					this.getMapper(SlmFundInvoiceManageMapper.class).update(bill);
				}else{
					rm.setState("500");
					rm.setMsgInfo("状态为待接收的数据才能接收");
					return rm;
				}
			}
			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 doMailWeb(CommonPage<SlmFundInvoiceManage> 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<SlmFundInvoiceManage> billList=sum.getObjectList();
		try{
			for(SlmFundInvoiceManage bill:billList){
				SlmFundInvoiceManage check=this.getMapper(SlmFundInvoiceManageMapper.class).findById(bill.getInvoice_no());
				if(check!=null&&"2".equals(check.getInvoice_flag())){
					check.setUpdate_man_no(userId);
					check.setUpdate_man_name(userName);
					check.setUpdate_time(new Date());
					check.setInvoice_flag("3");   		//待确认
					check.setThe_recipient(bill.getThe_recipient());
					check.setThe_recipient_tel(bill.getThe_recipient_tel());
					this.getMapper(SlmFundInvoiceManageMapper.class).update(check);
				}else{
					rm.setState("500");
					rm.setMsgInfo("状态为待邮寄的数据才能邮寄");
					return rm;
				}
			}
			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 doCheckWeb(CommonPage<SlmFundInvoiceManage> 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<SlmFundInvoiceManage> invoiceList=sum.getObjectList();
		try{
			for(SlmFundInvoiceManage invoice:invoiceList){
				invoice=this.getMapper(SlmFundInvoiceManageMapper.class).findById(invoice.getInvoice_no());
				if(invoice!=null&&("3".equals(invoice.getInvoice_flag())||"2".equals(invoice.getInvoice_flag()))){
					invoice.setUpdate_man_no(userId);
					invoice.setUpdate_man_name(userName);
					invoice.setUpdate_time(new Date());
					invoice.setInvoice_flag("4");   		//已确认
					this.getMapper(SlmFundInvoiceManageMapper.class).update(invoice);

				}else{
					rm.setState("500");
					rm.setMsgInfo("状态为待确认的数据才能确认");
					return rm;
				}
			}
			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 doRollbackWeb(CommonPage<SlmFundInvoiceManage> 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<SlmFundInvoiceManage> invoiceList=sum.getObjectList();
		try{
			for(SlmFundInvoiceManage invoice:invoiceList){
				invoice=this.getMapper(SlmFundInvoiceManageMapper.class).findById(invoice.getInvoice_no());
				if(invoice!=null&&"4".equals(invoice.getInvoice_flag())){
					invoice.setUpdate_man_no(userId);
					invoice.setUpdate_man_name(userName);
					invoice.setUpdate_time(new Date());
					invoice.setInvoice_flag("3");   		//待确认

					//修改结算单
					SlmFundSettleBill bill=this.getMapper(SlmFundSettleBillMapper.class).findById(invoice.getSettle_bill_no());
					if(bill!=null&&"5".equals(bill.getFlag())){		//发票勾稽 未被使用
						bill.setFlag("4");		
						bill.setUpdate_man_no(userId);
						bill.setUpdate_man_name(userName);
						bill.setUpdate_time(new Date());
						this.getMapper(SlmFundSettleBillMapper.class).update(bill);
					}else{
						rm.setState("500");
						rm.setMsgInfo("结算单已进行资金结算，无法退回");
						return rm;
					}
					this.getMapper(SlmFundInvoiceManageMapper.class).update(invoice);
				}else{
					rm.setState("500");
					rm.setMsgInfo("状态为已确认的数据才能退回");
					return rm;
				}
			}
			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 doAbolishWeb(CommonPage<SlmFundInvoiceManage> 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<SlmFundInvoiceManage> invoiceList=sum.getObjectList();
		try{
			for(SlmFundInvoiceManage invoice:invoiceList){
				invoice=this.getMapper(SlmFundInvoiceManageMapper.class).findById(invoice.getInvoice_no());
				if(invoice!=null){
					invoice.setUpdate_man_no(userId);
					invoice.setUpdate_man_name(userName);
					invoice.setUpdate_time(new Date());
					invoice.setInvoice_flag("5");   		//作废
					invoice.setValid_flag("0");
					this.getMapper(SlmFundInvoiceManageMapper.class).update(invoice);
					if(invoice.getInvoice_fund()>0){
						rm=doUpdateInvoiceNo(invoice,3);
						if("500".equals(rm.getState())){
							throw new Exception(rm.getMsgInfo());
						}
					}
				}else{
					rm.setState("500");
					rm.setMsgInfo("数据丢失，请联系管理员");
					return rm;
				}
			}
			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 doQueryByContractWeb(CommonPage<SlmContractHeatInfo> sum){
		ResultModel rm=new ResultModel();
		SlmContractHeatInfo heat=sum.getObject();
		List<SlmFundInvoiceManage> invoice_list=new ArrayList<SlmFundInvoiceManage>();
		try{
			List<SlmContractHeatInfo> heat_list=this.getMapper(SlmContractHeatInfoMapper.class).findWithCondition(null, " contract_no='"+heat.getContract_no()+"'", null, 0, 0);
			if(heat_list.size()==0){
				rm.setState("500");
				rm.setMsgInfo("合同信息已丢失，请联系管理员");
				return rm;
			}
			heat=heat_list.get(0);
			//结算单列表
			SlmFundSettleBill bill=new SlmFundSettleBill();
			bill.setContract_no(heat.getContract_no());
			bill.setValid_flag("1");
			List<SlmFundSettleBill> bill_list=this.getMapper(SlmFundSettleBillMapper.class).find(bill, 0, 0);
			for(SlmFundSettleBill bills:bill_list){
				SlmFundInvoiceManage invoice=new SlmFundInvoiceManage();
				invoice.setSettle_bill_no(bills.getSettle_bill_no());
				invoice.setValid_flag("1");
				List<SlmFundInvoiceManage> list=this.getMapper(SlmFundInvoiceManageMapper.class).find(invoice, 0, 0);
				invoice_list.addAll(list);
			}
			rm.setState("200");
			rm.setData(invoice_list);
		}catch(Exception e){
			rm.setMsgInfo(e.getMessage());
			rm.setState("500");
		}
		return rm;
	}

	/**
	 * 更新发票编号，发票代码
	 * 开票申请、结算主表、三种费用表
	 * 冲减
	 * @throws Exception 
	 * */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	private ResultModel doUpdateInvoiceNo(SlmFundInvoiceManage invoice,int type) 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);
		try{
			SlmFundInvoiceApply apply=this.getMapper(SlmFundInvoiceApplyMapper.class).findById(invoice.getApply_no());
			if(apply==null){
				rm.setState("500");
				rm.setMsgInfo("开票申请信息丢失，请联系管理员");
				return rm;
			}
			//更新结算单
			SlmFundSettleBill bill=this.getMapper(SlmFundSettleBillMapper.class).findById(invoice.getSettle_bill_no());
			if(bill==null){
				rm.setState("500");
				rm.setMsgInfo("结算单信息丢失，请联系管理员");
				return rm;
			}

			switch(type){
			case 1: 		//新增
				bill.setFlag("5");		//结算单 发票勾稽
				apply.setApply_flag("4"); 		//开票申请 4：已开票
				break;
			case 2:		//修改
				break;
			case 3:		//删除 or 作废
				bill.setFlag("4"); 		//结算单 开票申请
				apply.setApply_flag("3"); 		//开票申请 回到 3待发送
				invoice.setInvoice_no("");
				invoice.setInvoice_code("");
				break;
			default:break;
			}
			//更新发票申请
			apply.setInvoice_no(invoice.getInvoice_no());
			apply.setInvoice_code(invoice.getInvoice_code());
			apply.setUpdate_man_no(userId);
			apply.setUpdate_man_name(userName);
			apply.setUpdate_time(new Date());
			//更新结算单
			bill.setUpdate_man_no(userId);
			bill.setUpdate_man_name(userName);
			bill.setUpdate_time(new Date());
			bill.setInvoice_no(invoice.getInvoice_no());
			bill.setInvoice_code(invoice.getInvoice_code());

			//更新冲减单
			List<SlmFundSettleWriteDown> down_list=this.getMapper(SlmFundSettleWriteDownMapper.class).findWithCondition(null, " settle_bill_no='"+bill.getSettle_bill_no()+"'", null, 0, 0);
			for(SlmFundSettleWriteDown down:down_list){
				down.setInvoice_no(invoice.getInvoice_no());
				down.setInvoice_code(invoice.getInvoice_code());
				this.getMapper(SlmFundSettleWriteDownMapper.class).update(down);
			}
			//修改三种费用的状态
			List<SlmFundSettleGood> goodList=this.getMapper(SlmFundSettleGoodMapper.class).findWithCondition(null, " contract_no='"+bill.getContract_no()+"'", null, 0, 0);
			for(SlmFundSettleGood goods:goodList){
				goods.setUpdate_man_no(userId);
				goods.setUpdate_man_name(userName);
				goods.setUpdate_time(new Date());
				goods.setInvoice_no(invoice.getInvoice_no());
				goods.setInvoice_code(invoice.getInvoice_code());
				this.getMapper(SlmFundSettleGoodMapper.class).update(goods);
			}
			List<SlmFundSettleCarriage> carriageList=this.getMapper(SlmFundSettleCarriageMapper.class).findWithCondition(null, " contract_no='"+bill.getContract_no()+"'", null, 0, 0);
			for(SlmFundSettleCarriage carriages:carriageList){
				carriages.setUpdate_man_no(userId);
				carriages.setUpdate_man_name(userName);
				carriages.setUpdate_time(new Date());
				carriages.setInvoice_no(invoice.getInvoice_no());
				carriages.setInvoice_code(invoice.getInvoice_code());
				this.getMapper(SlmFundSettleCarriageMapper.class).update(carriages);
			}
			List<SlmFundSettleOther> otherList=this.getMapper(SlmFundSettleOtherMapper.class).findWithCondition(null, " contract_no='"+bill.getContract_no()+"'", null, 0, 0);
			for(SlmFundSettleOther others:otherList){
				others.setUpdate_man_no(userId);
				others.setUpdate_man_name(userName);
				others.setUpdate_time(new Date());
				others.setInvoice_no(invoice.getInvoice_no());
				others.setInvoice_code(invoice.getInvoice_code());
				this.getMapper(SlmFundSettleOtherMapper.class).update(others);
			}
			this.getMapper(SlmFundSettleBillMapper.class).update(bill);
			this.getMapper(SlmFundInvoiceApplyMapper.class).update(apply);

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

	/**
	 * 用友 接收单据
	 * 0作废 1开票 2红字
	 * @throws Exception 
	 * */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doReceiveInvoice(CommonPage<SlmFundInvoiceManage> sum) throws Exception{
//		this.getSession().setAttribute(HomeService.SESSION_USER_ID,"admin");//补红字发票时用到
//		this.getSession().setAttribute(HomeService.SESSION_USER_NAME,"管理员");
		ResultModel rm=new ResultModel(); 
		List<SlmFundInvoiceManage> list=sum.getList();
		List<SlmFundInvoiceManage> list2=sum.getObjectList();
		rm.setState("200");
		try{
			if(list==null){
				if(list2!=null){
					list=list2;
				}else{
					rm.setMsgInfo("请传入参数，格式 {\"list\":[{\"invoice_no\":\"发票编号\"},{}]}");
					rm.setState("500");
					return rm;
				}
			}
			for(SlmFundInvoiceManage manages:list){
				if(manages.getSettle_bill_no()!=null){
					if("0".equals(manages.getValid_flag())){				//作废
						CommonPage<SlmFundInvoiceManage> param_sum=new CommonPage<SlmFundInvoiceManage>();
						List<SlmFundInvoiceManage> invoice_list=new ArrayList<SlmFundInvoiceManage>();
						invoice_list.add(manages);
						param_sum.setObjectList(invoice_list);
						doDeleteWeb(param_sum);			//回退到开票申请的待开票
						//开票申请再反审再回撤到编辑状态
						SlmFundInvoiceApply apply=new SlmFundInvoiceApply();
						apply.setSettle_bill_no(manages.getSettle_bill_no());
						apply.setValid_flag("1");
						List<SlmFundInvoiceApply> apply_list=this.getMapper(SlmFundInvoiceApplyMapper.class).find(apply, 0, 0);
						for(SlmFundInvoiceApply applys:apply_list){
							applys.setMemo(applys.getMemo()+"|NC回退，发票编号："+applys.getInvoice_no());
							this.getMapper(SlmFundInvoiceApplyMapper.class).update(applys);
						}
						CommonPage<SlmFundInvoiceApply> apply_sum=new CommonPage<SlmFundInvoiceApply>();
						apply_sum.setObjectList(apply_list);
						this.getBean(SlmFundInvoiceApplyServiceImpl.class).doNotPassWeb(apply_sum);
						this.getBean(SlmFundInvoiceApplyServiceImpl.class).doRollbackWeb(apply_sum);
					}else if("2".equals(manages.getValid_flag())){			//红字
						// =================eason 2019-06-21 add==================
						
						// =======================================================
					}else{			//正常接收
						//=================eason 2019-07-09 add===================
						boolean redFontFlag=false;//红字发票
						//========================================================
						
						SlmFundInvoiceApply apply=new SlmFundInvoiceApply();
						apply.setSettle_bill_no(manages.getSettle_bill_no());
//						apply.setApply_no(manages.getApply_no());  //此参数需要与NC对接后才会有，补红字发票时用到
						apply.setValid_flag("1");
						List<SlmFundInvoiceApply> apply_list=this.getMapper(SlmFundInvoiceApplyMapper.class).findWithCondition(apply, "", "create_time desc", 0, 0);
						if(apply_list.size()>0){
							//=================eason 2019-07-09 add===================
							if(apply_list.get(0).getInvoice_fund()<0){
								redFontFlag = true;
							}
							//========================================================
							manages.setApply_no(apply_list.get(0).getApply_no());
							apply_list.get(0).setInvoice_no(manages.getInvoice_no());
							SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
							apply_list.get(0).setMemo(apply_list.get(0).getMemo()+"|"+sdf.format(new Date())+"NC已返回发票号["+manages.getInvoice_no()+"]");
							this.getMapper(SlmFundInvoiceApplyMapper.class).update(apply_list.get(0));
						}else{
							manages.setMemo("未找到对应的开票申请"+manages.getSettle_bill_no());
						}
						
						SlmFundSettleBill bill=this.getMapper(SlmFundSettleBillMapper.class).findById(manages.getSettle_bill_no());
						if(bill!=null){
							manages.setCustomer_no(bill.getCustomer_no());
							manages.setCustomer_name(bill.getCustomer_name());
							manages.setBalance_object_no(bill.getBalance_object_no());
							manages.setBalance_object_name(bill.getBalance_object_name());
							manages.setBalance_unit_no(bill.getBalance_unit_no());
							manages.setBalance_unit_name(bill.getBalance_unit_name());
							manages.setInvoice_fund(bill.getBalance_amount());
						}
						CommonPage<SlmFundInvoiceManage> temp_sum=new CommonPage<SlmFundInvoiceManage>();
						temp_sum.setObject(manages);
						if(!redFontFlag){
							rm=doAddWeb(temp_sum);
						}else{
							rm=doRedAddWeb(temp_sum);
						}
					}
				}
			}
		}catch(Exception e){
			rm.setState("500");
			e.printStackTrace();
			rm.setMsgInfo(e.getMessage());
			throw new Exception("接收失败");
		}
		return rm;
	}
	
	/**
	 * 红字票据新增
	 * */
	public ResultModel doRedAddWeb(CommonPage<SlmFundInvoiceManage> 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);
		SlmFundInvoiceManage invoice=sum.getObject();
		try{
			double invoiceFound=0d;
			String state="1";			//正
			SlmFundInvoiceManage check=this.getMapper(SlmFundInvoiceManageMapper.class).findById(invoice.getInvoice_no());
			if(check!=null){
				rm.setState("500");
				rm.setMsgInfo("该发票编号已被使用");
				return rm;
			}
			SlmFundInvoiceApply apply=this.getMapper(SlmFundInvoiceApplyMapper.class).findById(invoice.getApply_no());
			if(apply==null){
				rm.setState("500");
				rm.setMsgInfo("未找到对应的开票申请信息");
				return rm;
			}
			if(apply.getInvoice_fund()!=null&&apply.getInvoice_fund()<0)
				state="0";			//负
			
			invoiceFound = apply.getInvoice_fund()==null?0d:apply.getInvoice_fund();
			
			
			//加入冲减信息
			List<SlmFundSettleWriteDown> downList=this.getMapper(SlmFundSettleWriteDownMapper.class).findWithCondition(null, " settle_bill_no='"+invoice.getSettle_bill_no()+"' and valid_flag='1' and flag in ('3','4','5')", null,0,0);		
			double down_money=0;
			if(downList.size()>0){
				for(SlmFundSettleWriteDown downs:downList){
					if("1".equals(downs.getSettle_write_down_type())){ 		//冲减
						down_money=DoubleUtil.sub(down_money, downs.getSettle_write_down_fund());
					}else{
						down_money=DoubleUtil.add(down_money, downs.getSettle_write_down_fund());
					}
				}
				invoice.setInvoice_fund(DoubleUtil.add(invoice.getInvoice_fund(),down_money));
			}


			//新增票据信息
			if(invoice.getValid_flag()==null||"".equals(invoice.getValid_flag()))
				invoice.setValid_flag("1"); 		//有效
			invoice.setInvoice_flag("1"); 		//1：待接收
			if("0".equals(state)){
				invoice.setInvoice_fund(-invoice.getInvoice_fund());
				invoiceFound = -invoiceFound;
			}
			invoice.setCreate_man_no(userId);
			invoice.setCreate_man_name(userName);
			invoice.setCreate_time(new Date());
			invoice.setInvoice_time(new Date());
			if(invoice.getCreate_man_name()==null) invoice.setCreate_man_name("用友");
			if("1".equals(state)){
				doUpdateInvoiceNo(invoice,1);
				invoice.setHave_red("0");
				this.getMapper(SlmFundInvoiceManageMapper.class).insert(invoice);
			}else if("0".equals(state)){			//如果对应的结算单已结算 则吐出结算金额
				SlmFundSettleBill bill=this.getMapper(SlmFundSettleBillMapper.class).findById(invoice.getSettle_bill_no());
				if(bill!=null){
					invoice.setHave_red("1");
					if(!"6".equals(bill.getFlag())&&!"7".equals(bill.getFlag())){
						rm.setMsgInfo("该申请对应的结算单未结算，请操作对应的发票和结算单信息");
						rm.setState("500");
						return rm;
					}
					if(invoice.getCustomer_no()!=null){
						SlmFundFirstAccount account=new SlmFundFirstAccount();
						account.setCustomer_no(invoice.getCustomer_no());
						List<SlmFundFirstAccount> account_list=this.getMapper(SlmFundFirstAccountMapper.class).find(account, 0, 0);
						if(account_list.size()>0){
							//账户改钱
							double fund=-invoice.getInvoice_fund();
							account=account_list.get(0);
							//account.setUse_fund(DoubleUtil.add(account.getUse_fund(),fund));//可用增加
							account.setUsed_fund(DoubleUtil.add(account.getUsed_fund(),invoiceFound)); //占用增加
							account.setBalance_fund(DoubleUtil.add(account.getBalance_fund(),fund)); //账期财务余额增加
							account.setSettlement_fund(DoubleUtil.sub(account.getSettlement_fund(),fund));//账期结算资金减少
							account.setWrite_down_fund(DoubleUtil.add(account.getWrite_down_fund(), down_money));//账期冲减总额增加		//冲减总额 + 冲减金额
							this.getMapper(SlmFundFirstAccountMapper.class).update(account);
							
							//==================提单信息=============
							List<SlmLadingBillHeat> bill_list=this.getMapper(SlmLadingBillHeatMapper.class).findContractNo(bill.getSettle_bill_no());
							int icount=0;
							for(SlmLadingBillHeat heats:bill_list){
								String contract_no=heats.getContract_no(); 			//合同账户
								List<SlmLadingBillHeat> lslbh=this.getMapper(SlmLadingBillHeatMapper.class).getBillNo(bill.getSettle_bill_no());
								for(SlmLadingBillHeat slbh:lslbh){
									if(contract_no.equals(slbh.getContract_no())){
										String sBillNo="'"+slbh.getBill_no()+"'";
										double money = this.getMapper(SlmLadingBillHeatMapper.class).getMoney(sBillNo,contract_no);
										//********第一个合同账户占用资金含冲减/补收的金额？**********
										if(icount==0){
											money=DoubleUtil.add(money, down_money);
											icount++;
										}
										//********************************************************
										//三级账户
										SlmFundThirdAccount t_account=this.getMapper(SlmFundThirdAccountMapper.class).findById(bill.getContract_no());
										if(t_account!=null){
											//t_account.setUse_fund(DoubleUtil.add(DoubleUtil.add(t_account.getUse_fund(),fund),down_money));
											t_account.setSettlement_fund(DoubleUtil.sub(t_account.getSettlement_fund(),money));//结算=结算+提单金额   如果是第一个提单则：还要   “-冲减+补收”
											t_account.setUsed_fund(DoubleUtil.add(t_account.getUsed_fund(),money));//占用=占用+提单金额  如果是第一个提单则：还要  “-冲减+补收”
											this.getMapper(SlmFundThirdAccountMapper.class).update(t_account);
										}
										
										
										//===============eason add 2019-07-09 新增占用信息====================
										//如果可占用，生成占用信息
										SlmContractHeatInfo heat=new SlmContractHeatInfo();
										heat.setContract_no(t_account.getThird_account_no());
										List<SlmContractHeatInfo> heat_list=this.getMapper(SlmContractHeatInfoMapper.class).find(heat,0,0);
										if(heat_list.size()>0){
											heat.setContract_no(heat_list.get(0).getContract_no());
											heat.setContract_pk(heat_list.get(0).getContract_pk());
										}
										
										SlmContractPayment param_payment=new SlmContractPayment();
										param_payment.setContract_pk(heat.getContract_pk());
										param_payment.setPayment_point_no("102703");
										param_payment.setCustomer_no(heat.getCustomer_no());
										param_payment.setValid_flag("1");
										List<SlmContractPayment> payment_list=this.getMapper(SlmContractPaymentMapper.class).find(param_payment, 0, 0);
										if(payment_list.size()==0){
											rm.setState("200");
											return rm;
										}
										SlmContractPayment payment=payment_list.get(0);
										
										//=====================================
										SlmLadingBillLine slbl=new SlmLadingBillLine();
										slbl.setContract_no(contract_no);
										List<SlmLadingBillLine> lslbl=this.getMapper(SlmLadingBillLineMapper.class).findWithCondition(slbl," bill_no in("+sBillNo+")", "", 0, 0);
										for(SlmLadingBillLine line:lslbl){
											double usedMoney = this.getMapper(SlmLadingBillHeatMapper.class).getMoneyChild(line);
											
											String used_id=new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+PbMethod.RandomCreate(6);
											SlmFundThirdAccountUsed used=new SlmFundThirdAccountUsed();	
											used=this.getBean(SlmFundThirdAccountUsedServiceImpl.class).doAdd(t_account,payment,heat);
											used.setFlag("0"); //???????????
											used.setContract_line_no(line.getContract_line_no());
											used.setContract_line_seq(line.getContract_line_seq());
											used.setOrder_seq(line.getOrder_seq());
											used.setSeq(used_id);
											used.setUsed_fund(usedMoney);
											used.setBill_no(line.getBill_no());
											used.setMemo("红字冲红资金占用"+(icount==1?",含冲减/补收数据":""));
											if(icount==1){//有冲减/补收信息，那么填充到第一个合同的占用里面
												used.setUsed_fund(DoubleUtil.add(usedMoney, down_money));
											}
											if(Math.abs(usedMoney)>0){
												this.getMapper(SlmFundThirdAccountUsedMapper.class).insert(used);
											}
										}
									}
								}
							}
							//===================================================
							//开票申请-已开票
							apply.setApply_flag("4");
							this.getMapper(SlmFundInvoiceApplyMapper.class).update(apply);
							this.getMapper(SlmFundInvoiceManageMapper.class).insert(invoice);
							//结算单 回到确认，可重新申请开票
							bill.setFlag("3");
							this.getMapper(SlmFundSettleBillMapper.class).update(bill);
						}
					}
				}else{
					rm.setState("500");
					rm.setMsgInfo("未找到对应的结算单，请检查参数settle_bill_no");
				}
			}
			rm.setState("200");
		}catch(Exception e){
			rm.setState("500");
			rm.setMsgInfo(e.getMessage());
			//--19-10-15添加异常回滚
			throw new Exception(rm.getMsgInfo());
		}
		return rm;
	}
	
	//=================APP eason add 2019-07-15===========================
	/**
	 * 票据查询
	 * */
	public ResultModel doQueryApp(CommonPage<SlmFundInvoiceManage> sum){
		ResultModel rm=new ResultModel();
		SlmFundInvoiceManage invoice=sum.getObject();
		String startTime=sum.getStartTime();
		String endTime=sum.getEndTime();
		String flag=sum.getMemo1();
		int pageIndex=sum.getPageIndex();
		int pageSize=sum.getPageSize();
		PageModel<SlmFundInvoiceManage> pm=new PageModel<>(pageIndex, pageSize);
		try{
			invoice.setHave_red(flag);
			invoice.setCustomer_no((String)this.getSession().getAttribute(HomeService.SESSION_DEPT_ID));
			List<SlmFundInvoiceManage> list=this.getMapper(SlmFundInvoiceManageMapper.class).findbillApp(invoice, startTime, endTime, (pageIndex-1)*pageSize, pageIndex*pageSize);
			for(SlmFundInvoiceManage manage:list){
				if(manage.getSettle_bill_no()!=null){
					SlmFundSettleBill bill=this.getMapper(SlmFundSettleBillMapper.class).findById(manage.getSettle_bill_no());
					if(bill!=null)
						manage.setContract_no(bill.getContract_no());
				}
			}
			pm.setRows(list);
			rm.setState("200");
			rm.setData(list);
		}catch(Exception e){
			rm.setState("500");
			rm.setMsgInfo(e.getMessage());
		}
		return rm;
	}
	
	/**
	 * 票据签收
	 * */
	public ResultModel doSignApp(CommonPage<SlmFundInvoiceManage> sum){
		ResultModel rm=new ResultModel();
		SlmFundInvoiceManage invoice=sum.getObject();
		try{
			String userId= (String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
			String userName= (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
			SlmFundInvoiceManage sfim=this.getMapper(SlmFundInvoiceManageMapper.class).findById(invoice.getInvoice_no());
			if(sfim!=null&&sfim.getInvoice_no()!=null){
				SlmFundInvoiceManage s=new SlmFundInvoiceManage();
				s.setInvoice_no(sfim.getInvoice_no());
				s.setSign_flag("1");
				s.setSign_man_no(userId);
				s.setSign_man_name(userName);
				s.setSign_time(new Date());
				this.getMapper(SlmFundInvoiceManageMapper.class).update(s);
				rm.setState("200");
			}else{
				rm.setMsgInfo("未找到发票数据");
				rm.setState("500");
			}
		}catch(Exception e){
			rm.setState("500");
			rm.setMsgInfo(e.getMessage());
		}
		return rm;
	}
	
	/**
	 * 前端接收单据
	 * 0作废 1开票 2红字
	 * @throws Exception 
	 * */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doReceiveInvoice2(CommonPage<SlmFundInvoiceManage> sum) throws Exception{
//		this.getSession().setAttribute(HomeService.SESSION_USER_ID,"admin");//补红字发票时用到
//		this.getSession().setAttribute(HomeService.SESSION_USER_NAME,"管理员");
		ResultModel rm=new ResultModel(); 
		List<SlmFundInvoiceManage> list=sum.getList();
		List<SlmFundInvoiceManage> list2=sum.getObjectList();
		rm.setState("200");
		try{
			if(list==null){
				if(list2!=null){
					list=list2;
				}else{
					rm.setMsgInfo("请传入参数，格式 {\"list\":[{\"invoice_no\":\"发票编号\"},{}]}");
					rm.setState("500");
					return rm;
				}
			}
			for(SlmFundInvoiceManage manages:list){
				if(manages.getSettle_bill_no()!=null){
					if("0".equals(manages.getValid_flag())){				//作废
						CommonPage<SlmFundInvoiceManage> param_sum=new CommonPage<SlmFundInvoiceManage>();
						List<SlmFundInvoiceManage> invoice_list=new ArrayList<SlmFundInvoiceManage>();
						invoice_list.add(manages);
						param_sum.setObjectList(invoice_list);
						doDeleteWeb(param_sum);			//回退到开票申请的待开票
						//开票申请再反审再回撤到编辑状态
						SlmFundInvoiceApply apply=new SlmFundInvoiceApply();
						apply.setSettle_bill_no(manages.getSettle_bill_no());
						apply.setValid_flag("1");
						List<SlmFundInvoiceApply> apply_list=this.getMapper(SlmFundInvoiceApplyMapper.class).find(apply, 0, 0);
						for(SlmFundInvoiceApply applys:apply_list){
							applys.setMemo(applys.getMemo()+"|MES回退，发票编号："+applys.getInvoice_no());
							this.getMapper(SlmFundInvoiceApplyMapper.class).update(applys);
						}
						CommonPage<SlmFundInvoiceApply> apply_sum=new CommonPage<SlmFundInvoiceApply>();
						apply_sum.setObjectList(apply_list);
						this.getBean(SlmFundInvoiceApplyServiceImpl.class).doNotPassWeb(apply_sum);
						this.getBean(SlmFundInvoiceApplyServiceImpl.class).doRollbackWeb(apply_sum);
					}else if("2".equals(manages.getValid_flag())){			//红字
						// =================eason 2019-06-21 add==================
						
						// =======================================================
					}else{			//正常接收
						//=================eason 2019-07-09 add===================
						boolean redFontFlag=false;//红字发票
						//========================================================
						
						SlmFundInvoiceApply apply=new SlmFundInvoiceApply();
						apply.setSettle_bill_no(manages.getSettle_bill_no());
						apply.setApply_no(manages.getApply_no());  //此参数需要与NC对接后才会有，补红字发票时用到
						apply.setValid_flag("1");
						List<SlmFundInvoiceApply> apply_list=this.getMapper(SlmFundInvoiceApplyMapper.class).findWithCondition(apply, "", "create_time desc", 0, 0);
						if(apply_list.size()>0){
							//=================eason 2019-07-09 add===================
							if(apply_list.get(0).getInvoice_fund()<0){
								redFontFlag = true;
							}
							//========================================================
							manages.setApply_no(apply_list.get(0).getApply_no());
							apply_list.get(0).setInvoice_no(manages.getInvoice_no());
							SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
							apply_list.get(0).setMemo(apply_list.get(0).getMemo()+"|"+sdf.format(new Date())+"MES已录入发票号["+manages.getInvoice_no()+"]");
							this.getMapper(SlmFundInvoiceApplyMapper.class).update(apply_list.get(0));
						}else{
							manages.setMemo("未找到对应的开票申请"+manages.getSettle_bill_no());
						}
						
						SlmFundSettleBill bill=this.getMapper(SlmFundSettleBillMapper.class).findById(manages.getSettle_bill_no());
						if(bill!=null){
							manages.setCustomer_no(bill.getCustomer_no());
							manages.setCustomer_name(bill.getCustomer_name());
							manages.setBalance_object_no(bill.getBalance_object_no());
							manages.setBalance_object_name(bill.getBalance_object_name());
							manages.setBalance_unit_no(bill.getBalance_unit_no());
							manages.setBalance_unit_name(bill.getBalance_unit_name());
							manages.setInvoice_fund(bill.getBalance_amount());
						}
						CommonPage<SlmFundInvoiceManage> temp_sum=new CommonPage<SlmFundInvoiceManage>();
						temp_sum.setObject(manages);
						if(!redFontFlag){
							rm=doAddWeb(temp_sum);
						}else{
							rm=doRedAddWeb(temp_sum);
						}
					}
				}
			}
			//20210219 红字开票提单退到票据对齐状态
			for (SlmFundInvoiceManage sfim : list) {
				String condition = "bill_no in (select sfsbl.bill_no from slm_fund_settle_bill_line sfsbl "
						+ "where sfsbl.settle_bill_no = '"+sfim.getSettle_bill_no()+"')";
				List<SlmLadingBillHeat> slbhList = this.getMapper(SlmLadingBillHeatMapper.class).findWithCondition(null, condition, null, 0, 0);
				if (CollectionUtils.isNotEmpty(slbhList)) {
					for (SlmLadingBillHeat slbh : slbhList) {
						slbh.setValid_status("13");//票据对齐
						slbh.setMemo("红字开票");
						this.getMapper(SlmLadingBillHeatMapper.class).update(slbh);
					}
				}
			}
		}catch(Exception e){
			rm.setState("500");
			e.printStackTrace();
			rm.setMsgInfo(e.getMessage());
			throw new Exception("接收失败"+e.getMessage());
		}
		return rm;
	}
}