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

import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Lms.Equipment.mapper.YdmEquipmentInlistMapper;
import market.Lms.Equipment.vo.YdmEquipmentInlist;
import market.Lms.Warehouse.mapper.YdmProductInlistMapper;
import market.Lms.Warehouse.vo.YdmProductInlist;
import market.Pms.Order.mapper.PurOrderCMapper;
import market.Pms.Order.vo.PurOrderC;
import market.Pms.Settlement.mapper.PurInterfaceInbillMapper;
import market.Pms.Settlement.service.PurInterfaceInbillService;
import market.Pms.Settlement.vo.PurInterfaceInbill;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service("PurInterfaceInbillService")
@RemoteService(path="PurInterfaceInbillService")
public class PurInterfaceInbillServiceImpl extends BaseServiceImpl<PurInterfaceInbill,Object> implements PurInterfaceInbillService {
	/***
	 * 
	 * 入库单信息查询
	 * */
	public ResultModel  doQueryWeb(CommonPage<PurInterfaceInbill> sum){
		ResultModel rm=new ResultModel();
		PurInterfaceInbill  pfb=sum.getObject();
		try {
//			String swhere="1=1 and VALIDFLAG='00'";//查询初始数据
//			if(pfb.getContract_pk()!=null&&!"".equals(pfb.getContract_pk())){
//				swhere+=" and  Contract_pk  like "+pfb.getContract_pk()+"";
//			}
//			if(pfb.getContract_no()!=null&&!"".equals(pfb.getContract_no())){
//				swhere+=" and  Contract_no  like "+pfb.getContract_no()+"";
//			}//合同号
//			List<PurInterfaceInbill>  list =this.getMapper(PurInterfaceInbillMapper.class).findWithCondition(null, swhere, "CREATE_TIME desc", 0, 0);
			List<PurInterfaceInbill> list = this.getMapper(
					PurInterfaceInbillMapper.class).findPurInterfaceInbill(pfb);
			rm.setData(list);;
			rm.setState("200");
			rm.setMsgInfo("查询成功");		
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("查询失败，请联系管理员！");
			throw e;
		}
		return rm;
		
	}
	
	/***
	 * 
	 * 异议单————入库单信息查询
	 * */
	public ResultModel  doQueryException(CommonPage<PurInterfaceInbill> sum){
		ResultModel rm=new ResultModel();
		PurInterfaceInbill  pfb=sum.getObject();
		String startTime=sum.getStartTime();
		String endTime=sum.getEndTime();
		try {
			List<PurInterfaceInbill> list = this.getMapper(
					PurInterfaceInbillMapper.class).findExceptionInbill(pfb,startTime,endTime);
			rm.setData(list);;
			rm.setState("200");
			rm.setMsgInfo("查询成功");		
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("查询失败，请联系管理员！");
			throw e;
		}
		return rm;
		
	}
	
	/**
	 * 
	 * @author 赵淋支
	 * @date   2018年10月11日
	 * @parameter 合同号
	 * @return List<PurInterfaceInbill>
	 * @description 结算单编制界面：通过合同号查询待结算的入库单
	 */
	public ResultModel doQuaryInBillByContractno(CommonPage<PurInterfaceInbill> sum){
		ResultModel rm=new ResultModel();
		PurInterfaceInbill  pfb=sum.getObject();
		String startTime = sum.getStartTime();
		String endTime = sum.getEndTime();
		try {
			List<PurInterfaceInbill> list = this.getMapper(
					PurInterfaceInbillMapper.class).doQuaryInBillByContractno(pfb,startTime,endTime);
			rm.setData(list);;
			rm.setState("200");
			rm.setMsgInfo("查询成功");		
		} catch (Exception ex) { 
			rm.setState("500"); 
			rm.setMsgInfo("错误："+ex.getMessage());
		}
		return rm;  
	}
	
	
	/**
	 * 
	 * @author 赵淋支
	 * @date   2018年10月13日
	 * @parameter oci.getObjectList()：入库单号
	 * @return 
	 * @description 结算单编制界面：退回入库单
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel cancelInterfaceInBill(CommonPage<PurInterfaceInbill> oci)
			throws Exception {
		ResultModel rm = new ResultModel();
		if (oci.getObjectList() == null || oci.getObjectList().size() == 0) {
			rm.setState("500");
			rm.setMsgInfo("请选择数据！");
			return rm;
		}
		try {
			for (PurInterfaceInbill pii : oci.getObjectList()) {
				PurInterfaceInbill pi=this.getMapper(PurInterfaceInbillMapper.class).findById(pii.getInstock_no());
				if (pi!=null && "00".equals(pi.getValidflag())) {
					if (14 == pi.getInstock_no().length()) {
						//数据来源：ydm_product_inlist 原料
						YdmProductInlist ypi=new YdmProductInlist();
						ypi.setInbillno(pi.getInstock_no());
						ypi.setUp_flag("-1"); 
						this.getMapper(YdmProductInlistMapper.class).update(ypi);
					}else if (15 == pi.getInstock_no().length()) {
						//数据来源：ydm_equipment_inlist 资材备件
						YdmEquipmentInlist yei=new YdmEquipmentInlist();
						yei.setIn_bill_no(pi.getInstock_no());
						yei.setUp_flag("-1");
						this.getMapper(YdmEquipmentInlistMapper.class).update(yei);
					}
					//删除接口数据
					this.getMapper(PurInterfaceInbillMapper.class).delete(pi.getInstock_no()); 
				} else {
					rm.setState("500");
					rm.setMsgInfo("该数据不能退回入库单！");
					return rm;
				}
			} 
			rm.setState("200");
			rm.setMsgInfo("退回入库单成功！");
		} catch (Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("退回入库单失败："+ex.getMessage());
		}
		return rm;
	}
	
	/**
	 *  赵淋支
	 *  线程刷新原料的数据到入库接口表
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public void interfaceEquipToInBill(){
		//TODO 根据要求查询数据
		/*YdmEquipmentInlist yi = new YdmEquipmentInlist();
		yi.setIn_type("500408");//采购入库
		yi.setUp_flag("0");		*/
		String swhere = "IN_TYPE = '500408' and UP_FLAG = '0' and purc_order_pk is not null";
		List<YdmEquipmentInlist> list = this.getMapper(YdmEquipmentInlistMapper.class).findWithCondition(null, swhere, "CREATE_TIME desc", 0, 10);
		if (list!=null && list.size()>0) {
			try {
				for (YdmEquipmentInlist yei : list) {
					//1.新增数据到入库接口表
					PurInterfaceInbill pil=new PurInterfaceInbill();
					pil.setInstock_no(yei.getIn_bill_no());//入库单号
					pil.setInstock_type(yei.getIn_type());//入库类型(5004) 
					PurOrderC poc = this.getMapper(PurOrderCMapper.class).findById(yei.getPurc_order_pk());
					if (poc!=null) {
						pil.setOrder_no(poc.getOrder_no());//订单号
						pil.setOrder_pk(poc.getOrder_pk());
						pil.setContract_pk(poc.getContract_pk());//合同号
						pil.setContract_no(poc.getContract_no());
						pil.setSupp_code(poc.getSupp_code());//供应商
						pil.setSupp_name(poc.getSupp_name());
						pil.setMat_class_code(poc.getMat_class_code());//物料分类
						pil.setMat_class_name(poc.getMat_class_name());
						pil.setItem_code(poc.getItem_code());//物料
						pil.setItem_name(poc.getItem_name());
						pil.setItem_desc(poc.getItem_desc());//物料描述
						pil.setItem_desc_e(poc.getItem_desc_e());
						pil.setItem_idx(poc.getItem_idx());//物料检索(拼音助记码)
						pil.setStandards_code(poc.getStandards_code());//标准代码
						pil.setStandards_name(poc.getStandards_name());
						pil.setSpec_name(poc.getSpec_name());//规格
						pil.setSteel_code(poc.getSteel_code());// 钢种/钢级/牌号
						pil.setSteel_name(poc.getSteel_name());
						pil.setAttr_code(poc.getAttr_code());//属性代码
						pil.setAttr_name(poc.getAttr_name());
						pil.setDelivery_state_code(poc.getDelivery_state_code());//交货状态
						pil.setDelivery_state_name(poc.getDelivery_state_name());
						pil.setPrimary_amount(poc.getWith_tax_amt());//原币金额(含税金额)
						pil.setBug_price(poc.getPrice_with_tax()); //预算价(含税单价)
					}
					pil.setIn_num(yei.getQuantity().doubleValue());//入库量
					/*pil.setNotice_no(notice_no);//发货单号
					pil.setLocation_no();//仓库储位
					pil.setIn_stock_amt(in_stock_amt);//入库金额（不含税）
					pil.setCurrency_code(currency_code);//币种代码
					pil.setExchange_rate(exchange_rate);//兑换汇率
					
					pil.setExpense_type(expense_type);//费用类型
					pil.setCost_center(cost_center);//成本中心
					pil.setInv_material_type(inv_material_type);//库存物料方式(预留)
					pil.setInv_price_type(inv_price_type);//库存计价方式(6002)
					pil.setInv_location_user(inv_location_user);//库区储位指定人
					pil.setInv_location_date(inv_location_date);//库区储位指定日期
					pil.setInstock_date(yei.getIn_date());//入库时间
					pil.setCompany_code(company_code);//公司代码
					pil.setCompany_name(company_name);*/
					pil.setValidflag("00");
					pil.setCreate_name(yei.getCreate_man_name());
					pil.setCreate_time(yei.getOpr_time());
					pil.setCreate_userid(yei.getCreate_man_no()); 
					this.getMapper(PurInterfaceInbillMapper.class).insert(pil);
					 
					//2.修改资材备件入库表的upflag
					yei.setUp_flag("1");
					this.getMapper(YdmEquipmentInlistMapper.class).update(yei);
				}
			} catch (Exception ex) {
				ex.getMessage();
				throw ex;
			} 
		} 
	}
	
	
	/**
	 *  赵淋支
	 *  线程刷新原料的数据到入库接口表
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public void interfaceProductToInBill(){
		//TODO 根据要求查询数据
		/*YdmProductInlist yi = new YdmProductInlist(); 
		yi.setStorageattr("500205");//贸易库
		yi.setUp_flag("0");*/
		String swhere = " storageattr = '500205' and up_flag = '0' and purc_order_pk is not null ";
		List<YdmProductInlist> ylist = this.getMapper(YdmProductInlistMapper.class).findWithCondition(null, swhere, "CREATEDATE desc", 0, 0);
		if (ylist!=null && ylist.size()>0) {
			try {
				for (YdmProductInlist yei : ylist) {
					//1.新增数据到入库接口表
					PurInterfaceInbill pil=new PurInterfaceInbill();
					pil.setInstock_no(yei.getInbillno());//入库单号
					pil.setInstock_type(yei.getIntype());//入库类型(5004) 
					PurOrderC poc = this.getMapper(PurOrderCMapper.class).findById(yei.getPurc_order_pk());
					if (poc!=null) {
						pil.setOrder_no(poc.getOrder_no());//订单号
						pil.setOrder_pk(poc.getOrder_pk());
						pil.setContract_pk(poc.getContract_pk());//合同号
						pil.setContract_no(poc.getContract_no());
						pil.setSupp_code(poc.getSupp_code());//供应商
						pil.setSupp_name(poc.getSupp_name());
						pil.setMat_class_code(poc.getMat_class_code());//物料分类
						pil.setMat_class_name(poc.getMat_class_name());
						pil.setItem_code(poc.getItem_code());//物料
						pil.setItem_name(poc.getItem_name());
						pil.setItem_desc(poc.getItem_desc());//物料描述
						pil.setItem_desc_e(poc.getItem_desc_e());
						pil.setItem_idx(poc.getItem_idx());//物料检索(拼音助记码)
						pil.setStandards_code(poc.getStandards_code());//标准代码
						pil.setStandards_name(poc.getStandards_name());
						pil.setSpec_name(poc.getSpec_name());//规格
						pil.setSteel_code(poc.getSteel_code());// 钢种/钢级/牌号
						pil.setSteel_name(poc.getSteel_name());
						pil.setAttr_code(poc.getAttr_code());//属性代码
						pil.setAttr_name(poc.getAttr_name());
						pil.setDelivery_state_code(poc.getDelivery_state_code());//交货状态
						pil.setDelivery_state_name(poc.getDelivery_state_name());	
						pil.setPrimary_amount(poc.getWith_tax_amt());//原币金额(含税金额)
						pil.setBug_price(poc.getPrice_with_tax()); //预算价(含税价)
					}
					pil.setIn_num(yei.getFactweight().doubleValue()/1000);//入库量
					/*pil.setNotice_no(notice_no);//发货单号
					pil.setLocation_no();//仓库储位
					pil.setIn_stock_amt(in_stock_amt);//入库金额（不含税）
					pil.setCurrency_code(currency_code);//币种代码
					pil.setExchange_rate(exchange_rate);//兑换汇率
					
					pil.setExpense_type(expense_type);//费用类型
					pil.setCost_center(cost_center);//成本中心
					pil.setInv_material_type(inv_material_type);//库存物料方式(预留)
					pil.setInv_price_type(inv_price_type);//库存计价方式(6002)
					pil.setInv_location_user(inv_location_user);//库区储位指定人
					pil.setInv_location_date(inv_location_date);//库区储位指定日期
					pil.setInstock_date(yei.getIn_date());//入库时间
					pil.setCompany_code(company_code);//公司代码
					pil.setCompany_name(company_name);*/
					pil.setValidflag("00");
					pil.setCreate_name(yei.getCreater());
					pil.setCreate_time(yei.getCreatedate());
					//pil.setCreate_userid(yei.getCreate_man_no()); 
					this.getMapper(PurInterfaceInbillMapper.class).insert(pil);
					 
					//2.修改资材备件入库表的upflag
					yei.setUp_flag("1");
					this.getMapper(YdmProductInlistMapper.class).update(yei);
				}
			} catch (Exception ex) {
				ex.getMessage();
				throw ex;
			} 
		} 
	}
	
	
}
