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

import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Pms.Settlement.mapper.PurBalanceCMapper;
import market.Pms.Settlement.mapper.PurBalanceGMapper;
import market.Pms.Settlement.mapper.PurBalanceHMapper;
import market.Pms.Settlement.service.PurBalanceHService;
import market.Pms.Settlement.vo.PurBalanceC;
import market.Pms.Settlement.vo.PurBalanceG;
import market.Pms.Settlement.vo.PurBalanceH;

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="PurBalanceHService")
public class PurBalanceHServiceImpl extends BaseServiceImpl<PurBalanceH,Object> implements PurBalanceHService {
	
	/**
	 * @author 赵淋支
	 * @date   2018年11月27日
	 * @description 报支申请，报支审批界面：查询报支单行信息
	 */
	public ResultModel doQuaryPurBalanceH(CommonPage<PurBalanceH> cp){
		ResultModel rm = new ResultModel();
		try {
			PurBalanceH pbh = cp.getObject();
			String sWhere=""; 
			//报支单号			 
			if (pbh.getPayable_no()!=null && !"".equals(pbh.getPayable_no())) {
				sWhere += " and t.payable_no = '"+pbh.getPayable_no()+"'";
			}
			sWhere += " and t.validflag > '00' ";
			List<PurBalanceH>  pbhList =this.getMapper(PurBalanceHMapper.class).doQuaryPurBalanceH(sWhere);
			rm.setData(pbhList);;
			rm.setState("200");
			rm.setMsgInfo("查询成功");		
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("查询失败！");
			throw e;
		}
		return rm;
	}
	
	
	/**
	 * @author 赵淋支
	 * @date   2018年11月27日
	 * @description 报支单申请界面：报支单行信息：删除
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doDeletePurBalanceH(CommonPage<PurBalanceH> 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 (PurBalanceH PBH : cp.getObjectList()) {
			PurBalanceH pbh=this.getMapper(PurBalanceHMapper.class).findById(PBH.getPayable_sqe());				
			if (pbh!=null && "10".equals(pbh.getValidflag())) {//草稿
				PurBalanceH newPbh = new PurBalanceH();
				newPbh.setPayable_sqe(pbh.getPayable_sqe());//ID
				newPbh.setValidflag("00");//失效
				newPbh.setUpdate_userid(userId);
				newPbh.setUpdate_name(userName);
				newPbh.setUpdate_time(now);
				this.getMapper(PurBalanceHMapper.class).update(newPbh); 
			} else {
				throw new Exception("当前数据状态不允许删除！");
			}
		}
		rm.setState("200");
		rm.setMsgInfo("删除成功");
		return rm;
	}
	
	/**
	 * @author 赵淋支
	 * @date   2018年11月27日
	 * @description 报支单申请界面：新增报支单行信息
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doAddPurBalanceH(CommonPage<PurBalanceC> cp){
		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<PurBalanceC> PBCList = cp.getObjectList();
		if (PBCList==null || PBCList.size()<=0) {
			rm.setState("500");
			rm.setMsgInfo("请选择数据");
			return rm;
		}
		if (cp.getMemo1()==null || "".equals(cp.getMemo1())) {
			rm.setState("500");
			rm.setMsgInfo("请传入报支单号");
			return rm;
		}
		 			
		String payable_no = cp.getMemo1(); //报支单号
		PurBalanceG pbg = this.getMapper(PurBalanceGMapper.class).findById(payable_no);
		if (pbg==null || !"10".equals(pbg.getValidflag())) {
			rm.setState("500");
			rm.setMsgInfo("报支单号["+payable_no+"]对应的报支单主表不能新增报支单行信息");
			return rm;
		}
		Date now = new Date();
		for (PurBalanceC PBC : PBCList) {
			PurBalanceC pbc = this.getMapper(PurBalanceCMapper.class).findById(PBC.getBalance_sqe());
			if (pbc!=null) {
				PurBalanceH pbh = new PurBalanceH();
				String payableSqe = payable_no;
				String maxD = this.getMapper(PurBalanceHMapper.class).findPayableSqe(payable_no);
				if (maxD != null && maxD.length() > 3) {
					String seq = maxD.substring(maxD.length() - 3);//最后三位流水号
					int iseq = Integer.parseInt(seq) + 1;//流水号+1
					payableSqe += String.format("%03d", iseq);//补零操作
				} else {
					payableSqe += "001";
				}
				pbh.setPayable_sqe(payableSqe);
				pbh.setPayable_no(payable_no);
				pbh.setRefe_no(pbc.getBalance_sqe());//参考单号
				pbh.setRefe_type_code("603005");
				pbh.setRefe_type_name("结算单");
				pbh.setContract_pk(pbc.getContract_pk());
				pbh.setContract_no(pbc.getContract_no());
				pbh.setOrder_pk(pbc.getOrder_pk());
				pbh.setOrder_no(pbc.getOrder_no());
				pbh.setItem_code(pbc.getItem_code());
				pbh.setItem_name(pbc.getItem_name());
				pbh.setItem_desc(pbc.getItem_desc());
				pbh.setItem_desc_e(pbc.getItem_desc_e());
				pbh.setItem_idx(pbc.getItem_idx());
				pbh.setMat_class_code(pbc.getMat_class_code());
				pbh.setMat_class_name(pbc.getMat_class_name());
				pbh.setSpec_name(pbc.getSpec_name());
				pbh.setStandards_code(pbc.getStandards_code());
				pbh.setStandards_name(pbc.getStandards_name());
				pbh.setSteel_code(pbc.getSteel_code());
				pbh.setSteel_name(pbc.getSteel_name());
				pbh.setAttr_code(pbc.getAttr_code());
				pbh.setAttr_name(pbc.getAttr_name());
				pbh.setDelivery_state_code(pbc.getDelivery_state_code());
				pbh.setDelivery_state_name(pbc.getDelivery_state_name());
				//pbh.setQty(pbc.getBase_qty());
				pbh.setPayable_qty(pbc.getBalance_qty());//票据数量
				pbh.setUom_code(pbc.getUom_code());
				pbh.setUom_name(pbc.getUom_name());
				pbh.setSupp_code(pbc.getSupp_code());
				pbh.setSupp_name(pbc.getSupp_name());
				pbh.setBills_tax_rate_val(pbc.getTax_rate_val());//税率
				//不含税单价
				pbh.setBills_with_tax(pbc.getPrice_without_tax()+pbc.getPrice_without_freight());				
				//票据总金额
				pbh.setBills_with_tax_amt(pbc.getWith_tax_amt()+pbc.getFreight_with_amt());
				//票据不含税金额
				pbh.setBills_without_tax_amt(pbc.getWithout_tax_amt()+pbc.getPrice_without_tax());
				//票据税额
				pbh.setBills_tax_amt(pbh.getBills_with_tax_amt()-pbh.getBills_without_tax_amt());
				//pbh.setBills_open_date(bills_open_date);//开票日期(YYYY-MM-DD)
				//pbh.setBills_confirm_userid(bills_confirm_userid);//票据签收人ID
				//pbh.setBills_confirm_name(bills_confirm_name);//票据签收人
				//pbh.setBills_confirm_date(bills_confirm_date);//签收日期(YYYY-MM-DD)
				pbh.setCompany_code(pbc.getCompany_code());
				pbh.setCompany_name(pbc.getCompany_code());
				pbh.setRemark(pbc.getRemark());											
				pbh.setDept_code(deptId);//管理部门代码
				pbh.setDept_desc(deptName);//管理部门描叙 
				pbh.setCreate_name(userName);
				pbh.setCreate_userid(userId);
				pbh.setCreate_time(now);
				pbh.setBalance_sqe(pbc.getBalance_sqe());
				this.getMapper(PurBalanceHMapper.class).insert(pbh);
				
				/*PurBalanceE PBE = new PurBalanceE();
				PBE.setBalance_sqe(PBC.getBalance_sqe());
				PBE.setValidflag("20");
				List<PurBalanceE> pbeList = this.getMapper(PurBalanceEMapper.class).find(PBE, 0, 0);
				if (pbeList!=null && pbeList.size() > 0) {
					for (PurBalanceE pbe : pbeList) {
						pbh = new PurBalanceH();
						payableSqe = payable_no;
						maxD = this.getMapper(PurBalanceHMapper.class).findPayableSqe(payable_no);
						if (maxD != null && maxD.length() > 3) {
							String seq = maxD.substring(maxD.length() - 3);//最后三位流水号
							int iseq = Integer.parseInt(seq) + 1;//流水号+1
							payableSqe += String.format("%03d", iseq);//补零操作
						} else {
							payableSqe += "001";
						}
						pbh.setPayable_sqe(payableSqe);
						pbh.setPayable_no(payable_no);
						pbh.setRefe_no(pbe.getTicket_seq());//参考单号(冲减单号)
						pbh.setRefe_type_code("603006");
						pbh.setRefe_type_name("冲减单");
						pbh.setContract_pk(pbe.getContract_pk());
						pbh.setContract_no(pbe.getContract_no());
						pbh.setOrder_pk(pbe.getOrder_pk());
						pbh.setOrder_no(pbe.getOrder_no());
						pbh.setItem_code(pbe.getItem_code());
						pbh.setItem_name(pbe.getItem_name());
						pbh.setItem_desc(pbe.getItem_desc());
						pbh.setItem_desc_e(pbe.getItem_desc_e());
						pbh.setItem_idx(pbe.getItem_idx());
						pbh.setMat_class_code(pbe.getMat_class_code());
						pbh.setMat_class_name(pbe.getMat_class_name());
						pbh.setSpec_name(pbc.getSpec_name());
						pbh.setStandards_code(pbe.getStandards_code());
						pbh.setStandards_name(pbe.getStandards_name());
						pbh.setSteel_code(pbe.getSteel_code());
						pbh.setSteel_name(pbe.getSteel_name());
						pbh.setAttr_code(pbe.getAttr_code());
						pbh.setAttr_name(pbe.getAttr_name());
						pbh.setDelivery_state_code(pbe.getDelivery_state_code());
						pbh.setDelivery_state_name(pbe.getDelivery_state_name());
						//pbh.setQty(pbc.getBase_qty());
						//pbh.setPayable_qty(pbe.getBalance_qty());//票据数量
						//pbh.setBills_tax_rate_val(pbe.getTax_rate_val());
						pbh.setUom_code(pbe.getUom_code());
						pbh.setUom_name(pbe.getUom_name());
						pbh.setSupp_code(pbe.getSupp_code());
						pbh.setSupp_name(pbe.getSupp_name());
						
						//pbh.setBills_tax_rate_val(pbe.getTax_rate_val());//税率
						//pbh.setBills_with_tax(pbe.getPrice_with_tax());//含税单价
						//pbh.setBills_with_tax_amt(pbe.getWith_tax_amt());//票据总金额
						//pbh.setBills_without_tax_amt(pbe.getWithout_tax_amt());//票据不含税金额
						pbh.setBills_with_tax_amt(pbe.getTicket_atm());//报支金额
						
						pbh.setCompany_code(pbe.getCompany_code());
						pbh.setCompany_name(pbe.getCompany_code());
						pbh.setRemark(pbe.getRemark());											
						pbh.setDept_code(deptId);//管理部门代码
						pbh.setDept_desc(deptName);//管理部门描叙 
						pbh.setCreate_name(userName);
						pbh.setCreate_userid(userId);
						pbh.setCreate_time(now);
						pbh.setBalance_sqe(pbe.getBalance_sqe());
						this.getMapper(PurBalanceHMapper.class).insert(pbh);				
					}
				}*/
			}
		}
		rm.setState("200");
		rm.setMsgInfo("新增成功");
		return rm;
	}
	
}
