/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2018-11-22
 * 描述            ：报支单主表
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
***********************************************************/
package market.Pms.Settlement.impl;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Pms.Settlement.mapper.PurBalanceGMapper;
import market.Pms.Settlement.mapper.PurBalanceHMapper;
import market.Pms.Settlement.service.PurBalanceGService;
import market.Pms.Settlement.vo.PurBalanceG;
import market.Pms.Settlement.vo.PurBalanceH;
import market.Pms.Supp.mapper.PurSuppBankMapper;
import market.Pms.Supp.vo.PurSuppBank;

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;

@RemoteService(path="PurBalanceGService")
public class PurBalanceGServiceImpl extends BaseServiceImpl<PurBalanceG,Object> implements PurBalanceGService {
	
	/**
	 * 
	 * @author 赵淋支
	 * @date   2018年11月27日
	 * @description 报支单主表查询
	 */
	public ResultModel doQuaryPurBalanceG(CommonPage<PurBalanceG> cp){
		ResultModel rm = new ResultModel();
		try {
			String sWhere = ""; 
			PurBalanceG pbg = cp.getObject();
			//报支单号
			if (pbg.getPayable_no()!=null && !"".equals(pbg.getPayable_no())) {
				sWhere += " and t.payable_no like '%"+pbg.getPayable_no()+"%'";
			}
			//供应商名称
			if (pbg.getSupp_name()!=null && !"".equals(pbg.getSupp_name())) {
				sWhere += " and t.supp_name like '%"+pbg.getSupp_name()+"%'";
			}
			//数据状态
			if (pbg.getValidflag()!=null && !"".equals(pbg.getValidflag())) {
				sWhere += " and t.validflag = '"+pbg.getValidflag()+"'";
			}
			//评审状态 
			if (pbg.getStatus()!=null && !"".equals(pbg.getStatus())) {
				sWhere += " and t.status = '"+pbg.getStatus()+"'";
			}
			if (cp.getStartTime()!=null && !"".equals(cp.getStartTime())) {
				sWhere += " and t.create_time >= to_date('"+cp.getStartTime()+" 00:00:00','yyyy-MM-dd HH24:MI:SS') ";
			}
			if (cp.getEndTime()!=null && !"".equals(cp.getEndTime())) {
				sWhere += " and t.create_time <= to_date('"+cp.getEndTime()+" 23:59:59','yyyy-MM-dd HH24:MI:SS') ";
			}
			//默认不查询无效的报支单
			if (!sWhere.contains("validflag")) {
				sWhere += " and t.validflag > '00'";
			}
			//报支单审批界面：前台传参memo1："1"。报支单申请界面，则memo1不需要传参
			if (cp.getMemo1()!=null && "1".equals(cp.getMemo1())) {
				sWhere += " and t.status > '00'";
			}
			List<PurBalanceG>  list =this.getMapper(PurBalanceGMapper.class).doQuaryPurBalanceG(sWhere);
			rm.setData(list);;
			rm.setState("200");
			rm.setMsgInfo("查询成功");		
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("查询失败！");
			throw e;
		}
		return rm;
	}

	/**
	 * 
	 * @author 赵淋支
	 * @date   2018年11月27日
	 * @parameter 
	 * @return 
	 * @throws Exception 
	 * @description 新增报支单主信息
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doAddPurBalanceG(CommonPage<PurBalanceG> cp) throws Exception{
		String userId = (String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		String deptId = (String) this.getSession().getAttribute(HomeService.SESSION_DEPT_ID);
		String deptName = (String) this.getSession().getAttribute(HomeService.SESSION_DEPT_NAME);
		ResultModel rm = new ResultModel();	
		List<PurBalanceG> pbgList = cp.getObjectList();
		if (pbgList == null || pbgList.size() == 0) {
			rm.setState("500");
			rm.setMsgInfo("请编辑报支单数据！");
			return rm;
		}
		Date now = new Date();
		for (PurBalanceG PBG : pbgList) {
			PurBalanceG pbg = new PurBalanceG();
			//生成报支单号 BZYYYYMMDD
			String payable_no = "BZ"+ new SimpleDateFormat("yyyyMMdd").format(now); 
			String maxD = this.getMapper(PurBalanceGMapper.class).findPayableNo(payable_no);
			if (maxD != null && maxD.length() > 3) {
				String seq = maxD.substring(maxD.length() - 3);//最后三位流水号
				int iseq = Integer.parseInt(seq) + 1;//流水号+1
				payable_no += String.format("%03d", iseq);//补零操作
			} else {
				payable_no += "001";
			}
			pbg.setPayable_no(payable_no);//报支单号(YFYYYYMMDD+3位流水))
			pbg.setSupp_code(PBG.getSupp_code());
			pbg.setSupp_name(PBG.getSupp_name());
			pbg.setBills_nos(PBG.getBills_nos());
			pbg.setBills_codes(PBG.getBills_codes());
			pbg.setBalance_no(PBG.getBalance_no());//结算单号
			// 银行信息
			PurSuppBank psb = new PurSuppBank();
			psb.setSupp_code(PBG.getSupp_code());
			psb.setSupp_name(PBG.getSupp_name());
			psb.setValidflag("1");
			List<PurSuppBank> bankList = this.getMapper(PurSuppBankMapper.class).find(psb, 0, 0);
			if (bankList!=null && bankList.size()>0) {
				psb = bankList.get(0); 
				pbg.setBank_code(psb.getBank_code());
				pbg.setBank_name(psb.getBank_name());
				pbg.setBank_city(psb.getBank_city());
				pbg.setBank_area(psb.getBank_area());
				pbg.setBank_account(psb.getBank_account());
			}
			pbg.setCurrency_code(PBG.getCurrency_code());//货币
			pbg.setCurrency_name(PBG.getCurrency_name());
			pbg.setBal_year_month(PBG.getBal_year_month());//会计年月
			pbg.setCurrency_code(PBG.getCurrency_code());//币种
			pbg.setCurrency_name(PBG.getCurrency_name());
			pbg.setExchange_rate_cur(PBG.getExchange_rate_cur());//当前兑换汇率
			pbg.setVoucher_affix_num(PBG.getVoucher_affix_num());//凭证附件张数
			pbg.setBills_purpose_code(PBG.getBills_purpose_code());//款项用途
			pbg.setBills_purpose_name(PBG.getBills_purpose_name());
			pbg.setBills_open_date(PBG.getBills_open_date());//开票日期
			pbg.setRemark(PBG.getRemark());//备注
			pbg.setValidflag("10");
			pbg.setStatus("00");
			pbg.setDept_code(deptId);//管理部门代码
			pbg.setDept_desc(deptName);//管理部门描叙 
			pbg.setCreate_name(userName);
			pbg.setCreate_userid(userId);
			pbg.setCreate_time(now);
			this.getMapper(PurBalanceGMapper.class).insert(pbg);
		}
		rm.setState("200");
		return rm;
	}
	
	/**
	 * 
	 * @author 赵淋支
	 * @date   2018年11月27日
	 * @parameter 
	 * @return 
	 * @description 报支单申请界面：自动审批
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doExamPurBalanceG(CommonPage<PurBalanceG> cp)
			throws Exception {
		String userId = (String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		ResultModel rm = new ResultModel();
		if (cp.getObjectList() == null || cp.getObjectList().size() == 0) {
			rm.setState("500");
			rm.setMsgInfo("请选择数据！");
			return rm;
		}
		Date now  = new Date();
		for (PurBalanceG PBG : cp.getObjectList()) {
			PurBalanceG pbg=this.getMapper(PurBalanceGMapper.class).findById(PBG.getPayable_no());
			if (pbg!=null && "10".equals(pbg.getValidflag())//数据状态=10
					&& ("00".equals(pbg.getStatus()) || "40".equals(pbg
							.getStatus()))) {//审批状态=待提报OR审批退回
				// 主表
				PurBalanceG newPbg = new PurBalanceG();
				newPbg.setPayable_no(pbg.getPayable_no());
				newPbg.setValidflag("20");
				newPbg.setStatus("50"); //审批通过					
				newPbg.setUpdate_userid(userId);
				newPbg.setUpdate_name(userName);
				newPbg.setUpdate_time(now);
				this.getMapper(PurBalanceGMapper.class).update(newPbg);
				// 从表
				PurBalanceH pbh = new PurBalanceH();
				pbh.setPayable_no(pbg.getPayable_no());
				pbh.setValidflag("10");
				pbh.setStatus("00");
				List<PurBalanceH> pbhList = this.getMapper(
						PurBalanceHMapper.class).find(pbh, 0, 0);
				if (pbhList!=null && pbhList.size() > 0) {
					for (PurBalanceH PBH : pbhList) {
						pbh = new PurBalanceH();
						pbh.setPayable_sqe(PBH.getPayable_sqe());//ID
						pbh.setValidflag("20");
						pbh.setStatus("50"); //待审批						
						pbh.setUpdate_userid(userId);
						pbh.setUpdate_name(userName);
						pbh.setUpdate_time(now);
						this.getMapper(PurBalanceHMapper.class).update(pbh);
					}
				}
			} else {
				throw new Exception("当前数据状态不允许进行自动审批");
			}
		} 
		rm.setState("200");
		rm.setMsgInfo("自审审批成功！");
		return rm;
	}
	
	/**
	 * 
	 * @author 赵淋支
	 * @date   2018年11月27日
	 * @parameter 
	 * @return 
	 * @description 报支单申请界面：撤销审批
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doCancelExamPurBalanceG(CommonPage<PurBalanceG> cp)
			throws Exception {
		String userId = (String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		ResultModel rm = new ResultModel();
		if (cp.getObjectList() == null || cp.getObjectList().size() == 0) {
			rm.setState("500");
			rm.setMsgInfo("请选择数据！");
			return rm;
		}
		Date now  = new Date();
		for (PurBalanceG PBG : cp.getObjectList()) {
			PurBalanceG pbg=this.getMapper(PurBalanceGMapper.class).findById(PBG.getPayable_no());
			if (pbg!=null && "20".equals(pbg.getValidflag())//数据状态=20生效
					&& "50".equals(pbg.getStatus())) {//审批状态=审批通过
				// 主表
				PurBalanceG newPbg = new PurBalanceG();
				newPbg.setPayable_no(pbg.getPayable_no());
				newPbg.setValidflag("10");
				newPbg.setStatus("00"); //待提报					
				newPbg.setUpdate_userid(userId);
				newPbg.setUpdate_name(userName);
				newPbg.setUpdate_time(now);
				this.getMapper(PurBalanceGMapper.class).update(newPbg);
				// 从表
				PurBalanceH pbh = new PurBalanceH();
				pbh.setPayable_no(pbg.getPayable_no());
				pbh.setValidflag("20");
				pbh.setStatus("50");
				List<PurBalanceH> pbhList = this.getMapper(
						PurBalanceHMapper.class).find(pbh, 0, 0);
				if (pbhList!=null && pbhList.size() > 0) {
					for (PurBalanceH PBH : pbhList) {
						pbh = new PurBalanceH();
						pbh.setPayable_sqe(PBH.getPayable_sqe());//ID
						pbh.setValidflag("10");//草稿
						pbh.setStatus("00"); //待提报					
						pbh.setUpdate_userid(userId);
						pbh.setUpdate_name(userName);
						pbh.setUpdate_time(now);
						this.getMapper(PurBalanceHMapper.class).update(pbh);
					}
				}
			} else {
				throw new Exception("当前数据状态不允许进行撤销审批");
			}
		} 
		rm.setState("200");
		rm.setMsgInfo("撤销审批成功！");
		return rm;
	}
	
	 
	/**
	 * 
	 * @author 赵淋支
	 * @date   2018年11月6日
	 * @parameter 
	 * @return 
	 * @description 报支单申请界面：主信息：修改编辑
	 */
	public ResultModel doEditPurBalanceG(CommonPage<PurBalanceG> cp){
		String userId = (String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);		
		ResultModel rm = new ResultModel();	
		PurBalanceG PBG = cp.getObject();
		 
		try {
			if (PBG!=null && PBG.getPayable_no()!=null) {
				PurBalanceG pbg = this.getMapper(PurBalanceGMapper.class).findById(PBG.getPayable_no());
				if (pbg!=null && !"00".equals(pbg.getValidflag())) { 
					PBG.setUpdate_name(userName);
					PBG.setUpdate_userid(userId);
					PBG.setUpdate_time(new Date());
					this.getMapper(PurBalanceGMapper.class).update(PBG);
				}else {
					rm.setState("500");
					rm.setMsgInfo("当前数据不允许进行修改");
					return rm;
				}
			}
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("修改失败："+ex.getMessage());
			return rm;
		}
		return rm;
	}
	 
	/**
	 * 
	 * @author 赵淋支
	 * @date   2018年11月27日
	 * @parameter 
	 * @return 
	 * @description 报支单申请界面：主信息：删除
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doDeletePurBalanceG(CommonPage<PurBalanceG> cp)
			throws Exception {
		String userId = (String) this.getSession().getAttribute(
				HomeService.SESSION_USER_ID);
		String userName = (String) this.getSession().getAttribute(
				HomeService.SESSION_USER_NAME);
		ResultModel rm = new ResultModel();
		if (cp.getObjectList() == null || cp.getObjectList().size() == 0) {
			rm.setState("500");
			rm.setMsgInfo("请选择数据！");
			return rm;
		}
		Date now  = new Date();
		for (PurBalanceG PBG : cp.getObjectList()) {
			PurBalanceG pbg=this.getMapper(PurBalanceGMapper.class).findById(PBG.getPayable_no());
			if (pbg!=null && "10".equals(pbg.getValidflag())) {//数据状态=10
				// 主表
				PurBalanceG newPbg = new PurBalanceG();
				newPbg.setPayable_no(pbg.getPayable_no());
				newPbg.setValidflag("00");//失效			
				newPbg.setUpdate_userid(userId);
				newPbg.setUpdate_name(userName);
				newPbg.setUpdate_time(now);
				this.getMapper(PurBalanceGMapper.class).update(newPbg);
				// 从表
				PurBalanceH pbh = new PurBalanceH();
				pbh.setPayable_no(pbg.getPayable_no());
				pbh.setValidflag("10");
				List<PurBalanceH> pbhList = this.getMapper(PurBalanceHMapper.class).find(pbh, 0, 0);
				if (pbhList!=null && pbhList.size() > 0) {
					for (PurBalanceH PBH : pbhList) {
						pbh = new PurBalanceH();
						pbh.setPayable_sqe(PBH.getPayable_sqe());//ID
						pbh.setValidflag("00");//无效			
						pbh.setUpdate_userid(userId);
						pbh.setUpdate_name(userName);
						pbh.setUpdate_time(now);
						this.getMapper(PurBalanceHMapper.class).update(pbh);
					}
				}
			} else {
				throw new Exception("当前数据状态不允许进行删除");
			}
		} 
		rm.setState("200");
		rm.setMsgInfo("删除成功！");
		return rm;
	}
	
}
