/***文档注释***********************************************
 * 作者               :                   龙文航
 * 创建日期      :                   2017.11.22
 * 描述               :                   仓库管理-原料库在制品库成品库管理-库存查询
 * 注意事项      :                   需要确定质量后台是否已开发判定结果相关接口
 * 遗留BUG :                   无
 * 修改日期      :                   
 * 修改人员      :                   
 * 修改内容      :                   
 ***********************************************************/
package market.Lms.Warehouse.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Pattern;

import market.Com.mapper.ComBaseInfoMapper;
import market.Com.mapper.ComBaseMachineMapper;
import market.Com.mapper.ComBaseSteelMapper;
import market.Com.vo.ComBaseCbYdd;
import market.Com.vo.ComBaseMachine;
import market.Com.vo.ComBaseSteel;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Lms.Warehouse.mapper.YdmProductDetailMapper;
import market.Lms.Warehouse.mapper.YdmProductInlistMapper;
import market.Lms.Warehouse.mapper.YdmProductOutlistMapper;
import market.Lms.Warehouse.service.YdmProductDetailService;
import market.Lms.Warehouse.service.YdmProductInlistService;
import market.Lms.Warehouse.vo.YdmProductAB;
import market.Lms.Warehouse.vo.YdmProductDetail;
import market.Lms.Warehouse.vo.YdmProductInlist;
import market.Lms.Warehouse.vo.YdmProductOutlist;
import market.Mes.Lengthwise.Performance.mapper.MesCrSlittingDMapper;
import market.Mes.Lengthwise.Performance.vo.MesCrSlittingD;
import market.Mes.Lengthwise.Plan.mapper.PlnMatReplaceLogMapper;
import market.Mes.Lengthwise.Plan.vo.PlnMatReplaceLog;
import market.Mes.Opr.mapper.MesOprLogMapper;
import market.Mes.Opr.vo.MesOprLog;
import market.Mes.OrderManager.Plan.mapper.PlnProdorderMapper;
import market.Mes.OrderManager.Plan.vo.PlnProdorder;
import market.Mes.Pickle.Performance.mapper.MesPlmDMapper;
import market.Mes.Pickle.Performance.vo.MesPlmD;
import market.Mes.Rolling.Performance.mapper.MesRcmDMapper;
import market.Mes.Rolling.Performance.vo.MesRcmD;
import market.Mes.Tpm.mapper.MesTpmDMapper;
import market.Mes.Tpm.mapper.MesTpmMMapper;
import market.Mes.Tpm.vo.MesTpmD;
import market.Mes.Tpm.vo.MesTpmM;
import market.Mes.anneal.mapper.MesCrAnnealMMapper;
import market.Mes.anneal.vo.MesCrAnnealM;
import market.Slm.DeliveryManagement.mapper.SlmLadingBillHeatMapper;
import market.Slm.FundManagement.service.SlmFundFirstDetailService;
import market.Slm.OrderManagement.mapper.SlmOrderInfoMapper;
import market.Slm.OrderManagement.vo.SlmOrderInfo;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
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;

@SuppressWarnings("rawtypes")
@Service("YdmProductDetailService")
@RemoteService(path="YdmProductDetailService")
public class YdmProductDetailServiceImpl extends BaseServiceImpl<YdmProductDetail,Object> implements YdmProductDetailService {
	/**
	 * 库存查询
	 * */
	public ResultModel doQueryWeb(CommonPage<YdmProductDetail> cp) {
		ResultModel rm=new ResultModel();
		int pageIndex=cp.getPageIndex();
		int pageSize=cp.getPageSize();
		PageModel<YdmProductDetail> pm = new PageModel<YdmProductDetail>(pageIndex,pageSize);
		try {
			YdmProductDetail yp=cp.getObject();
			String sWhere="1=1";
			if(cp.getStartTime()!=null&&cp.getStartTime()!="") {
				String starttime=cp.getStartTime();
				sWhere=" and INDATE>=to_date(concat("+starttime+",' 00:00:00'),'yyyy-MM-dd HH24:MI:SS') ";
			}
			if(cp.getEndTime()!=null&&cp.getEndTime()!="") {
				String endtime=cp.getEndTime();
				sWhere=" and INDATE<=to_date(concat("+endtime+",' 00:00:00'),'yyyy-MM-dd HH24:MI:SS') ";
			}
			if(cp.getObject().getCoilno()!=null&&cp.getObject().getCoilno()!="")
			{
				String coilno=cp.getObject().getCoilno();
				sWhere=" and COILNO like '%"+coilno+"%'";
			}
			if(cp.getObject().getJudgesteelcode()!=null&&cp.getObject().getJudgesteelcode()!="")
			{
				String judgesteelcodeString=cp.getObject().getJudgesteelcode();
				sWhere=" and judgesteelcode like '%"+judgesteelcodeString+"%'";
			}

			if(cp.getObject().getMaterailcoilno()!=null&&cp.getObject().getMaterailcoilno()!="")
			{
				String MaterailcoilnoString=cp.getObject().getMaterailcoilno();
				sWhere=" and materailcoilno like '%"+MaterailcoilnoString+"%'";
			}

			if(cp.getMemo1()!=null&&cp.getMemo1()!="")
			{
				double hmin=Double.valueOf(cp.getMemo1()).doubleValue();
				sWhere=" and THICK>="+hmin;
			}
			if(cp.getMemo2()!=null&&cp.getMemo2()!="")
			{
				double hmax=Double.valueOf(cp.getMemo2()).doubleValue();
				sWhere=" and THICK<="+hmax;
			}
			if(cp.getMemo3()!=null&&cp.getMemo3()!="")
			{
				double wmin=Double.valueOf(cp.getMemo3()).doubleValue();
				sWhere=" and WIDTH>="+wmin;
			}
			if(cp.getMemo4()!=null&&cp.getMemo4()!="")
			{
				double wmax=Double.valueOf(cp.getMemo4()).doubleValue();
				sWhere=" and WIDTH<="+wmax;
			}
			pm.setRows(this.getMapper(YdmProductDetailMapper.class).findWithCondition(yp, sWhere, "INDATE", (pageIndex-1)*pageSize, pageSize));
			pm.setTotal(this.getMapper(YdmProductDetailMapper.class).countWithCondition(yp,sWhere));
			rm.setState("200");
			rm.setData(pm);
		}
		catch(Exception ex)
		{
			rm.setState("500");
			rm.setMsgInfo("查询失败!");
			ex.printStackTrace();
		}
		return rm;
	}

	/**
	 * 倒垛管理  库存查询(入库管理 待入库查询) 查询入库时间
	 * memo5 剁位名称
	 * */
	public ResultModel doQueryWebBySQL(CommonPage<YdmProductDetail> cp)
	{
		ResultModel rm=new ResultModel();
		int pageSize=cp.getPageSize();
		int pageIndex=cp.getPageIndex();
		PageModel<YdmProductDetail> pm = new PageModel<YdmProductDetail>(pageIndex,pageSize);
		try
		{
			String regex = "^(\\d+\\.?\\d*)$";	
			boolean isMatch = true;
			Object hmin="",hmax="",wmin="",wmax="";
			if (cp.getMemo1() != null && cp.getMemo1() != "") {
				isMatch = Pattern.matches(regex, cp.getMemo1().trim());
				if (!isMatch) {
					rm.setState("500");
					rm.setMsgInfo("请输入正确的厚度和宽度!");
					return rm;
				}

				hmin = Double.valueOf(cp.getMemo1()).doubleValue();
			}
			if (cp.getMemo2() != null && cp.getMemo2() != "") {
				isMatch = Pattern.matches(regex, cp.getMemo2().trim());
				if (!isMatch) {
					rm.setState("500");
					rm.setMsgInfo("请输入正确的厚度和宽度!");
					return rm;
				}
				hmax = Double.valueOf(cp.getMemo2()).doubleValue();
			}
			if (cp.getMemo3() != null && cp.getMemo3() != "") {
				isMatch = Pattern.matches(regex, cp.getMemo3().trim());
				if (!isMatch) {
					rm.setState("500");
					rm.setMsgInfo("请输入正确的厚度和宽度!");
					return rm;
				}
				wmin = Double.valueOf(cp.getMemo3()).doubleValue();
			}
			if (cp.getMemo4() != null && cp.getMemo4() != "") {
				isMatch = Pattern.matches(regex, cp.getMemo4().trim());
				if (!isMatch) {
					rm.setState("500");
					rm.setMsgInfo("请输入正确的厚度和宽度!");
					return rm;
				}
				wmax = Double.valueOf(cp.getMemo4()).doubleValue();
			}
			String regex2 = "^\\d+$";// 非负整数
			int day = 0;
			if (cp.getObject().getJgdate() != null && !"".equals(cp.getObject().getJgdate().trim())) {// 输入天数验证
				isMatch = Pattern.matches(regex2, cp.getObject().getJgdate());
				if (!isMatch) {
					rm.setState("500");
					rm.setMsgInfo("请输入正确的天数！（为非负整数）");
					return rm;
				}
				day = Integer.parseInt(cp.getObject().getJgdate());
			}
		     //货位编码  buttressno        区域编码      areano     customer_no 
			List<YdmProductDetail> list =this.getMapper(YdmProductDetailMapper.class).doQueryWebBySQL
					((pageIndex-1)*pageSize, (pageIndex-1)*pageSize+pageSize, cp.getStartTime(), cp.getEndTime(), cp.getObject().getCoilno(), cp.getObject().getJudgesteelcode(), 
							hmin,hmax,wmin,wmax,cp.getObject().getStorageattr(),cp.getObject().getState(),cp.getObject().getMaterailcoilno(),cp.getObject().getOrderno(),cp.getObject().getProdorderno(),
							cp.getObject().getButtressno(),cp.getObject().getAreano(),cp.getObject().getCustomer_no(),cp.getMemo5(),cp.getObject().getObject_attr(),cp.getObject().getDestpcd(),day);
			pm.setRows(list);
			pm.setTotal(this.getMapper(YdmProductDetailMapper.class).doQueryWebBySQLCount
					(cp.getStartTime(), cp.getEndTime(), cp.getObject().getCoilno(), cp.getObject().getJudgesteelcode(), 
							hmin,hmax,wmin,wmax,cp.getObject().getStorageattr(),cp.getObject().getState(),cp.getObject().getMaterailcoilno(),cp.getObject().getOrderno(),cp.getObject().getProdorderno(),
							cp.getObject().getButtressno(),cp.getObject().getAreano(),cp.getObject().getCustomer_no(),cp.getMemo5(),cp.getObject().getObject_attr(),day));
			rm.setState("200");
			rm.setData(pm);
		}
		catch(Exception ex)
		{
			rm.setState("500");
			rm.setMsgInfo("查询失败!");
			ex.printStackTrace();
		}
		return rm;
	}
	
	/**
	 * 倒垛管理  库存查询(入库管理 待入库查询) 查询入库时间
	 * memo5 剁位名称 20230221刘侣
	 * */
	public ResultModel doQueryWebBySQLMulti(CommonPage<YdmProductDetail> cp)
	{
		ResultModel rm=new ResultModel();
		int pageSize=cp.getPageSize();
		int pageIndex=cp.getPageIndex();
		PageModel<YdmProductDetail> pm = new PageModel<YdmProductDetail>(pageIndex,pageSize);
		try
		{
			String regex = "^(\\d+\\.?\\d*)$";	
			boolean isMatch = true;
			Object hmin="",hmax="",wmin="",wmax="";
			if (cp.getMemo1() != null && cp.getMemo1() != "") {
				isMatch = Pattern.matches(regex, cp.getMemo1().trim());
				if (!isMatch) {
					rm.setState("500");
					rm.setMsgInfo("请输入正确的厚度和宽度!");
					return rm;
				}

				hmin = Double.valueOf(cp.getMemo1()).doubleValue();
			}
			if (cp.getMemo2() != null && cp.getMemo2() != "") {
				isMatch = Pattern.matches(regex, cp.getMemo2().trim());
				if (!isMatch) {
					rm.setState("500");
					rm.setMsgInfo("请输入正确的厚度和宽度!");
					return rm;
				}
				hmax = Double.valueOf(cp.getMemo2()).doubleValue();
			}
			if (cp.getMemo3() != null && cp.getMemo3() != "") {
				isMatch = Pattern.matches(regex, cp.getMemo3().trim());
				if (!isMatch) {
					rm.setState("500");
					rm.setMsgInfo("请输入正确的厚度和宽度!");
					return rm;
				}
				wmin = Double.valueOf(cp.getMemo3()).doubleValue();
			}
			if (cp.getMemo4() != null && cp.getMemo4() != "") {
				isMatch = Pattern.matches(regex, cp.getMemo4().trim());
				if (!isMatch) {
					rm.setState("500");
					rm.setMsgInfo("请输入正确的厚度和宽度!");
					return rm;
				}
				wmax = Double.valueOf(cp.getMemo4()).doubleValue();
			}
			String regex2 = "^\\d+$";// 非负整数
			int day = 0;
			if (cp.getObject().getJgdate() != null && !"".equals(cp.getObject().getJgdate().trim())) {// 输入天数验证
				isMatch = Pattern.matches(regex2, cp.getObject().getJgdate());
				if (!isMatch) {
					rm.setState("500");
					rm.setMsgInfo("请输入正确的天数！（为非负整数）");
					return rm;
				}
				day = Integer.parseInt(cp.getObject().getJgdate());
			}
		     //货位编码  buttressno        区域编码      areano     customer_no 
			List<YdmProductDetail> list =this.getMapper(YdmProductDetailMapper.class).doQueryWebBySQLMulti
					(cp.getObjectList());
			pm.setRows(list);
//			pm.setTotal(this.getMapper(YdmProductDetailMapper.class).doQueryWebBySQLCountMulti
//					(cp.getStartTime(), cp.getEndTime(), cp.getObject().getCoilno(), cp.getObject().getJudgesteelcode(), 
//							hmin,hmax,wmin,wmax,cp.getObject().getStorageattr(),cp.getObject().getState(),cp.getObject().getMaterailcoilno(),cp.getObject().getOrderno(),cp.getObject().getProdorderno(),
//							cp.getObject().getButtressno(),cp.getObject().getAreano(),cp.getObject().getCustomer_no(),cp.getMemo5(),cp.getObject().getObject_attr(),day));
			rm.setState("200");
			rm.setData(pm);
		}
		catch(Exception ex)
		{
			rm.setState("500");
			rm.setMsgInfo("查询失败!");
			ex.printStackTrace();
		}
		return rm;
	}
	
	public ResultModel doQueryWebBySQLApp(CommonPage<YdmProductDetail> cp)
	{
		ResultModel rm=new ResultModel();
		int pageSize=cp.getPageSize();
		int pageIndex=cp.getPageIndex();
		PageModel<YdmProductDetail> pm = new PageModel<YdmProductDetail>(pageIndex,pageSize);
		try
		{
			List<YdmProductDetail> list =this.getMapper(YdmProductDetailMapper.class).doQueryWebBySQLApp(cp.getObject().getCoilno(),cp.getObject().getStorageattr(),cp.getObject().getState());
			if (list.size()==0) {
				
				List<YdmProductDetail> datailA =this.getMapper(YdmProductDetailMapper.class).doQueryYxxA(cp.getObject().getCoilno());
				
				if(datailA==null||datailA.size()==0)
				{
					rm.setMsgInfo("数据库不存在此卷信息!");
					rm.setState("500");
				}
				else 
				{
					rm.setMsgInfo("操作失败,钢材仓库状态不正确 "+datailA.get(0).getMaterailcoilno()+"_"+datailA.get(0).getCoilno()+"卷, 仓库:"+ datailA.get(0).getStorageattr()+",状态:"+datailA.get(0).getState());
					rm.setState("500");
				}
			}else {
				pm.setRows(list);
				rm.setState("200");
				rm.setData(pm);
			}
		}
		catch(Exception ex)
		{
			rm.setState("500");
			rm.setMsgInfo("查询失败!");
			ex.printStackTrace();
		}
		return rm;
	}

	/**
	 * 倒垛管理  库存查询(入库管理 待入库查询) 查询生产时间
	 * */
	public ResultModel doQueryWebBySQLs(CommonPage<YdmProductDetail> cp) {
		ResultModel rm=new ResultModel();
		int pageSize=cp.getPageSize();
		int pageIndex=cp.getPageIndex();
		PageModel<YdmProductDetail> pm = new PageModel<YdmProductDetail>(pageIndex,pageSize);
		try {
			String regex = "^(\\d+\\.?\\d*)$";	
			boolean isMatch = true;
			Object hmin="",hmax="",wmin="",wmax="";
			if (cp.getMemo1() != null && cp.getMemo1() != "") {
				isMatch = Pattern.matches(regex, cp.getMemo1().trim());
				if (!isMatch) {
					rm.setState("500");
					rm.setMsgInfo("请输入正确的厚度和宽度!");
					return rm;
				}
				hmin = Double.valueOf(cp.getMemo1()).doubleValue();
			}
			if (cp.getMemo2() != null && cp.getMemo2() != "") {
				isMatch = Pattern.matches(regex, cp.getMemo2().trim());
				if (!isMatch) {
					rm.setState("500");
					rm.setMsgInfo("请输入正确的厚度和宽度!");
					return rm;
				}
				hmax = Double.valueOf(cp.getMemo2()).doubleValue();
			}
			if (cp.getMemo3() != null && cp.getMemo3() != "") {
				isMatch = Pattern.matches(regex, cp.getMemo3().trim());
				if (!isMatch) {
					rm.setState("500");
					rm.setMsgInfo("请输入正确的厚度和宽度!");
					return rm;
				}
				wmin = Double.valueOf(cp.getMemo3()).doubleValue();
			}
			if (cp.getMemo4() != null && cp.getMemo4() != "") {
				isMatch = Pattern.matches(regex, cp.getMemo4().trim());
				if (!isMatch) {
					rm.setState("500");
					rm.setMsgInfo("请输入正确的厚度和宽度!");
					return rm;
				}
				wmax = Double.valueOf(cp.getMemo4()).doubleValue();
			}
			pm.setRows(this.getMapper(YdmProductDetailMapper.class).doQueryWebBySQLs
					((pageIndex-1)*pageSize, (pageIndex-1)*pageSize+pageSize, cp.getStartTime(), cp.getEndTime(), cp.getObject(),  
							hmin,hmax,wmin,wmax));
			pm.setTotal(this.getMapper(YdmProductDetailMapper.class).doQueryWebBySQLCounts
					(cp.getStartTime(), cp.getEndTime(), cp.getObject(),
							hmin,hmax,wmin,wmax));
			rm.setData(pm);
			rm.setState("200");
		}
		catch(Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("查询失败!");
			ex.printStackTrace();
		}
		return rm;
	}

	/**
	 * 有效性检查
	 */

	public ResultModel Effectiveness(CommonPage<YdmProductDetail> cp) {
		ResultModel rm =new ResultModel();
		try {
			if (cp.getMemo1()==null||"".equals(cp.getMemo1())) {
				//rm.setMsgInfo("物料号不能为空");
				//rm.setState("500");
				//return rm;
			}else if (cp.getMemo2()==null||"".equals(cp.getMemo2())) {
				//rm.setMsgInfo("仓库编码不能为空");
				//rm.setState("500");
				//return rm;
			}else if(cp.getMemo3()==null||"".equals(cp.getMemo3())) {
				rm.setMsgInfo("状态编码不能为空");
				rm.setState("500");
				return rm;
			}else {
				List<YdmProductDetail> datail=this.getMapper(YdmProductDetailMapper.class).doQueryYxx(cp.getMemo1(), cp.getMemo2(), cp.getMemo3());
				if (datail==null||datail.size()==0) {
					List<YdmProductDetail> datailA =this.getMapper(YdmProductDetailMapper.class).doQueryYxxA(cp.getMemo1());				
					if(datailA==null||datailA.size()==0) {
						rm.setMsgInfo("数据库不存在此卷信息!");
						rm.setState("500");
					}
					else {
						rm.setMsgInfo(datailA.get(0).getMaterailcoilno()+"_"+datailA.get(0).getCoilno()+"卷, 仓库:"+ datailA.get(0).getStorageattr()+",状态:"+datailA.get(0).getState());
						rm.setState("500");
						rm.setData(datailA.get(0));
					}
				}else {
					datail.get(0).setIndate(new Date());
					rm.setState("200");
					rm.setData(datail);
				}
			}
		} catch (Exception e) {
			
		}
		return rm;
	}
	
	/**
	 * AB跨发运计划日报表汇总
	 * @param memo1:2018-04
	 * @return
	 */
    public ResultModel doQueryABWeb(CommonPage<YdmProductDetail> sum){
    	ResultModel rm = new ResultModel();
    	if(sum.getMemo1()==null||"".equals(sum.getMemo1())){
    		rm.setState("500");
    		rm.setMsgInfo("请传入时间");
    		return rm;
    	}
    	try {
    		String month = sum.getMemo1();
    		String storageattr = sum.getMemo2();
    		List<YdmProductAB> ypab= this.getMapper(YdmProductDetailMapper.class)._doQueryABWeb(month,storageattr);
          rm.setData(ypab);
          rm.setState("200");
    	} catch (Exception e) {
	    	rm.setState("500");
			e.getStackTrace();
			rm.setMsgInfo("查询失败！"+e.getMessage());
    	}
    	return rm;
    }
    
   
    
    /**
     * AB跨成品库发运计划日报表明细
     * @param memo1:2018-04-19
     * @return
     */
    public ResultModel doQueryABWeb1(CommonPage<YdmProductDetail> sum){
        	ResultModel rm = new ResultModel();
        	if(sum.getMemo1()==null||"".equals(sum.getMemo1())){
        		rm.setState("500");
        		rm.setMsgInfo("请传入开始时间");
        		return rm;
        	}
        	if(sum.getMemo2()==null ||"".equals(sum.getMemo2())){
        		rm.setState("500");
        		rm.setMsgInfo("请传入结束时间");
        		return rm;
        	}
        	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        	try{
        	Date startDate = sdf.parse(sum.getMemo1());
			Date endDate = sdf.parse(sum.getMemo2());
			if (startDate.after(endDate)) {//startDate>endDate
				rm.setState("500");
				rm.setMsgInfo("开始时间不能大于结束时间");
				return rm;
			}
			if((int) ((endDate.getTime() - startDate.getTime())/(1000*3600*24))>30){
				rm.setState("500");
				rm.setMsgInfo("开始时间与结束时间差不能大于30天。");
				return rm;
			}
        	YdmProductDetail object = sum.getObject();
        	rm.setData(this.getMapper(YdmProductDetailMapper.class).doQueryABWeb1(sum.getMemo1(),sum.getMemo2(),object));
    		rm.setState("200");
    		} catch (Exception e) {
    			rm.setState("500");
    			e.getStackTrace();
    			rm.setMsgInfo("查询失败！"+e.getMessage());
    		}
        	return rm;
       }
   
    /**
	 * 报表：XX库库存查询
	 * */
	public ResultModel doReportWebBySQL(CommonPage<YdmProductDetail> cp) {
		ResultModel rm=new ResultModel();
		int pageSize=cp.getPageSize();
		int pageIndex=cp.getPageIndex();
		PageModel<YdmProductDetail> pm = new PageModel<YdmProductDetail>(pageIndex,pageSize);
		try {
			String regex = "^(\\d+\\.?\\d*)$";	
			boolean isMatch = true;
			Object hmin="",hmax="",wmin="",wmax="";
			if (cp.getMemo1() != null &&!"".equals(cp.getMemo1())) {
				isMatch = Pattern.matches(regex, cp.getMemo1().trim());
				if (!isMatch) {
					rm.setState("500");
					rm.setMsgInfo("请输入正确的厚度和宽度!");
					return rm;
				}
				hmin = Double.valueOf(cp.getMemo1()).doubleValue();
			}
			if (cp.getMemo2() != null &&  !"".equals(cp.getMemo2())) {
				isMatch = Pattern.matches(regex, cp.getMemo2().trim());
				if (!isMatch) {
					rm.setState("500");
					rm.setMsgInfo("请输入正确的厚度和宽度!");
					return rm;
				}
				hmax = Double.valueOf(cp.getMemo2()).doubleValue();
			}
			if (cp.getMemo3() != null &&  !"".equals(cp.getMemo3())) {
				isMatch = Pattern.matches(regex, cp.getMemo3().trim());
				if (!isMatch) {
					rm.setState("500");
					rm.setMsgInfo("请输入正确的厚度和宽度!");
					return rm;
				}
				wmin = Double.valueOf(cp.getMemo3()).doubleValue();
			}
			if (cp.getMemo4() != null && !"".equals(cp.getMemo4())) {
				isMatch = Pattern.matches(regex, cp.getMemo4().trim());
				if (!isMatch) {
					rm.setState("500");
					rm.setMsgInfo("请输入正确的厚度和宽度!");
					return rm;
				}
				wmax = Double.valueOf(cp.getMemo4()).doubleValue();
			}
			String regex2 = "^\\d+$";// 非负整数
			int day = 0;
			if (cp.getObject().getJgdate() != null && !"".equals(cp.getObject().getJgdate().trim())) {// 输入天数验证
				isMatch = Pattern.matches(regex2, cp.getObject().getJgdate());
				if (!isMatch) {
					rm.setState("500");
					rm.setMsgInfo("请输入正确的天数！（为非负整数）");
					return rm;
				}
				day = Integer.parseInt(cp.getObject().getJgdate());
			}
			if (StringUtils.isNotEmpty(cp.getObject().getIs_kucun_new()) && "1".equals(cp.getObject().getIs_kucun_new())) {
				cp.getObject().setStorageattr("");
			}
			
			String condtion = " and (1=1";
			YdmProductDetail ypd = cp.getObject();
			if (StringUtils.isNotEmpty(cp.getStartTime())) {			//入库时间
				condtion += " and ypd.indate >= to_date('"+cp.getStartTime()+" 00:00:00','yyyy-MM-dd HH24:MI:SS')";
			}
			if (StringUtils.isNotEmpty(cp.getEndTime())) {
				condtion += " and ypd.indate <= to_date('"+cp.getEndTime()+" 23:59:59','yyyy-MM-dd HH24:MI:SS')";
			}
			if (StringUtils.isNotEmpty(ypd.getCoilno())) {				//卷号
				condtion += " and ypd.coilno like '%"+ypd.getCoilno()+"%'";
			}
			if (StringUtils.isNotEmpty(ypd.getJudgesteelcode())) {		//牌号：中文，如Q195
				condtion += " and cbs.steel_name like '%"+ypd.getJudgesteelcode().trim()+"%'";
			}
			if (StringUtils.isNotEmpty(hmin.toString())) {				//厚度
				condtion += " and ypd.thick >= " + hmin;
			}
			if (StringUtils.isNotEmpty(hmax.toString())) {
				condtion += " and ypd.thick <= " + hmax;
			}
			if (StringUtils.isNotEmpty(wmin.toString())) {				//宽度
				condtion += " and ypd.width >= " + wmin;
			}
			if (StringUtils.isNotEmpty(wmax.toString())) {
				condtion += " and ypd.width <= " + wmax;
			}			
			if (StringUtils.isNotEmpty(ypd.getStorageattr())) {			//仓库
				condtion += " and ypd.storageattr = '"+ypd.getStorageattr()+"'";
			}
			if (StringUtils.isNotEmpty(ypd.getState())) {   			//库存状态
				// 由于state:8是查询的二级库存的，不是状态，所以得区分 20240411刘侣
				if ("8".equals(ypd.getState())) {
					// 加上二级库存标志：
					condtion += " and ypd.IS_KUCUN_NEW = '1'";
					condtion += " and ypd.state in ('0','2','6','7')";
				} else {
					condtion += " and ypd.state = '"+ypd.getState()+"'";
				}
				
			}
			if (StringUtils.isEmpty(ypd.getState())) {					//
				condtion += " and ypd.state in ('0','2','6','7')";
			}
			if (StringUtils.isNotEmpty(ypd.getMaterailcoilno())) {		//原料卷号
				condtion += " and ypd.materailcoilno like '%"+ypd.getMaterailcoilno().trim()+"%'";
			}
			if (StringUtils.isNotEmpty(ypd.getOrderno())) {				//订单号
				condtion += " and ypd.orderno like '%"+ypd.getOrderno().trim()+"%'";
			}
			if (StringUtils.isNotEmpty(ypd.getProdorderno())) {			//生产订单号
				condtion += " and ypd.prodorderno like '%"+ypd.getProdorderno().trim()+"%'";
			}
			if (StringUtils.isNotEmpty(ypd.getButtressno())) {			//垛位
				condtion += " and ypb.buttressno like '%"+ypd.getButtressno().trim()+"%'";
			}
			if (StringUtils.isNotEmpty(ypd.getAreano())) {				//区域
				condtion += " and ybb.buttressno like '%"+ypd.getAreano().trim()+"%'";
			}
			if (StringUtils.isNotEmpty(ypd.getCustomer_no())) {			//客户
				condtion += " and ypd.customer_no like '%"+ypd.getCustomer_no().trim()+"%'";
			}
			if (StringUtils.isNotEmpty(cp.getMemo5())) {				//根据垛位名称进行查询
				condtion += " and yb.buttressname like '%"+cp.getMemo5().trim()+"%'";
			}
			if (StringUtils.isNotEmpty(ypd.getButtressname())) {		//AB跨，BC跨 CD跨进行查询
				if ("AB跨".equals(ypd.getButtressname())) {
					condtion += " and ybb.buttressname not like '%跨%'";
				}else if (!"AB跨".equals(ypd.getButtressname())) {
					condtion += " and ybb.buttressname like '%"+ypd.getButtressname()+"%'";
				}
			}
			if (StringUtils.isNotEmpty(ypd.getObject_attr())) {			//物料属性
				condtion += " and ypd.object_attr = '"+ypd.getObject_attr()+"'";
			}
			if (StringUtils.isNotEmpty(ypd.getMachine_code())) {		//机台查询
				condtion += " and ypd.machine_code = '"+ypd.getMachine_code()+"'";
			}
			if (StringUtils.isNotEmpty(ypd.getMaterailtype())) {		//原料卷类型
				condtion += " and ypd.materailtype = '"+ypd.getMaterailtype()+"'";
			}
			if (StringUtils.isNotEmpty(ypd.getIr_trans_car_no())) {		//到站查询
				condtion += " and irmc.trans_car_no = '"+ypd.getIr_trans_car_no()+"'";
			}
			if (StringUtils.isNotEmpty(ypd.getDestpcd())) {				//贸易库的到站信息
				if ("直发库".equals(ypd.getDestpcd().trim())) {
					condtion += " and irmc.destpcd not in ('新钢板加公司','新余翔威库（新钢优特钢带）','新余佳材开平厂（新钢特钢）','新余广瑞开平厂（新钢优特钢带）','新余丽佳库（新钢优特钢带）')";
				}else if ("板加库".equals(ypd.getDestpcd().trim())) {
					condtion += " and irmc.destpcd = '新钢板加公司'";
				}else if ("翔威库".equals(ypd.getDestpcd().trim())) {
					condtion += " and irmc.destpcd in ('新余翔威库（新钢优特钢带）','新余佳材开平厂（新钢特钢）')";
				}else if ("广瑞库".equals(ypd.getDestpcd().trim())) {
					condtion += " and irmc.destpcd = '新余广瑞开平厂（新钢优特钢带）'";
				}else if ("丽佳库".equals(ypd.getDestpcd().trim())) {
					condtion += " and irmc.destpcd = '新余丽佳库（新钢优特钢带）'";
				}
			}
			if (StringUtils.isNotEmpty(ypd.getSx_valid_flg())) {
				condtion += " and ypd.sx_valid_flg = '"+ypd.getSx_valid_flg()+"'";
			}			
			if (StringUtils.isNotEmpty(cp.getMemo6()) && "外".equals(cp.getMemo6())) { //转外库可发资源查询
				condtion += " and ypd.judgeresult is not null and nvl(ypd.is_package,'1') <> '0' and ypd.state = '0' and (select listagg(t.coillist, ',') within group(order by coillist) from lms_passportinfo t where t.platenumber is null and t.coillist is not null) not like '%'||ypd.coilno||'%'";
			}
			if (day > 0) {												//入库天数
				condtion += " and (trunc(sysdate)-trunc(ypd.indate)) >= " + day;
			}
			if (StringUtils.isNotEmpty(ypd.getIs_kucun_new()) && "1".equals(ypd.getIs_kucun_new())) {
				condtion += " and (trunc(sysdate)-trunc(ypd.indate)) >= 30 and ypd.judgeresult in ('40740802','40740803')";
			}
			condtion += ")";
			String condition2 = "";
			if (StringUtils.isNotEmpty(ypd.getIslock()) && "true".equals(ypd.getIslock())) {
				condition2 += " or exists("
						+ " (select 1 from ( "
						+ " select t.material_no as coil_no,t.machine_code from mes_cr_slitting_m t where 1=1 and t.machine_code='M0001' and nvl(hand_over_status,'0') = '0'"
						+ " union "
						+ " select t.material_no as coil_no,t.machine_code from mes_cr_slitting_m t where 1=1 and t.machine_code='M0002' and nvl(hand_over_status,'0') = '0' "
						+ " union "
						+ " select t.material_no as coil_no,t.machine_code from mes_cr_cross_cutting_m t  where 1=1 and t.machine_code='M0003' and nvl(hand_over_status,'0') = '0' "
						+ " union "
						+ " select t.material_no as coil_no,t.machine_code from mes_cr_anneal_m t  where 1=1 and  nvl(hand_over_status,'0') = '0' "
						+ " union "
						+ " select t.material_no as coil_no,t.machine_code from mes_tpm_m t  where 1=1 and  nvl(hand_over_status,'0') = '0' ) x "
						+ " where x.coil_no = ypd.coilno "+condtion+"))";
			}
			String conditonTotal = condtion + condition2;
			List<YdmProductDetail> list = this.getMapper(YdmProductDetailMapper.class).doReportWebBySQL(conditonTotal, ypd, (pageIndex - 1) * pageSize, (pageIndex - 1) * pageSize + pageSize);
			pm.setRows(list);
			pm.setTotal(this.getMapper(YdmProductDetailMapper.class).doReportWebBySQLCount(conditonTotal, ypd));
			rm.setState("200");
			rm.setData(pm);
		}
		catch(Exception ex)
		{
			rm.setState("500");
			rm.setMsgInfo("查询失败!");
			ex.printStackTrace();
		}
		return rm;
	}
	
	/**
	 * 报表：XX库库存查询 包含在生产中的：20250331刘侣 
	 * */
	public ResultModel doReportWebBySQLProIng(CommonPage<YdmProductDetail> cp) {
		ResultModel rm=new ResultModel();
		int pageSize=cp.getPageSize();
		int pageIndex=cp.getPageIndex();
		PageModel<YdmProductDetail> pm = new PageModel<YdmProductDetail>(pageIndex,pageSize);
		try {
			String regex = "^(\\d+\\.?\\d*)$";	
			boolean isMatch = true;
			Object hmin="",hmax="",wmin="",wmax="";
			if (cp.getMemo1() != null &&!"".equals(cp.getMemo1())) {
				isMatch = Pattern.matches(regex, cp.getMemo1().trim());
				if (!isMatch) {
					rm.setState("500");
					rm.setMsgInfo("请输入正确的厚度和宽度!");
					return rm;
				}
				hmin = Double.valueOf(cp.getMemo1()).doubleValue();
			}
			if (cp.getMemo2() != null &&  !"".equals(cp.getMemo2())) {
				isMatch = Pattern.matches(regex, cp.getMemo2().trim());
				if (!isMatch) {
					rm.setState("500");
					rm.setMsgInfo("请输入正确的厚度和宽度!");
					return rm;
				}
				hmax = Double.valueOf(cp.getMemo2()).doubleValue();
			}
			if (cp.getMemo3() != null &&  !"".equals(cp.getMemo3())) {
				isMatch = Pattern.matches(regex, cp.getMemo3().trim());
				if (!isMatch) {
					rm.setState("500");
					rm.setMsgInfo("请输入正确的厚度和宽度!");
					return rm;
				}
				wmin = Double.valueOf(cp.getMemo3()).doubleValue();
			}
			if (cp.getMemo4() != null && !"".equals(cp.getMemo4())) {
				isMatch = Pattern.matches(regex, cp.getMemo4().trim());
				if (!isMatch) {
					rm.setState("500");
					rm.setMsgInfo("请输入正确的厚度和宽度!");
					return rm;
				}
				wmax = Double.valueOf(cp.getMemo4()).doubleValue();
			}
			String regex2 = "^\\d+$";// 非负整数
			int day = 0;
			if (cp.getObject().getJgdate() != null && !"".equals(cp.getObject().getJgdate().trim())) {// 输入天数验证
				isMatch = Pattern.matches(regex2, cp.getObject().getJgdate());
				if (!isMatch) {
					rm.setState("500");
					rm.setMsgInfo("请输入正确的天数！（为非负整数）");
					return rm;
				}
				day = Integer.parseInt(cp.getObject().getJgdate());
			}
			if (StringUtils.isNotEmpty(cp.getObject().getIs_kucun_new()) && "1".equals(cp.getObject().getIs_kucun_new())) {
				cp.getObject().setStorageattr("");
			}
			
			String condtion = " and (1=1";
			YdmProductDetail ypd = cp.getObject();
			if (StringUtils.isNotEmpty(cp.getStartTime())) {			//入库时间
				condtion += " and ypd.indate >= to_date('"+cp.getStartTime()+" 00:00:00','yyyy-MM-dd HH24:MI:SS')";
			}
			if (StringUtils.isNotEmpty(cp.getEndTime())) {
				condtion += " and ypd.indate <= to_date('"+cp.getEndTime()+" 23:59:59','yyyy-MM-dd HH24:MI:SS')";
			}
			if (StringUtils.isNotEmpty(ypd.getCoilno())) {				//卷号
				condtion += " and ypd.coilno like '%"+ypd.getCoilno()+"%'";
			}
			if (StringUtils.isNotEmpty(ypd.getJudgesteelcode())) {		//牌号：中文，如Q195
				condtion += " and cbs.steel_name like '%"+ypd.getJudgesteelcode().trim()+"%'";
			}
			if (StringUtils.isNotEmpty(hmin.toString())) {				//厚度
				condtion += " and ypd.thick >= " + hmin;
			}
			if (StringUtils.isNotEmpty(hmax.toString())) {
				condtion += " and ypd.thick <= " + hmax;
			}
			if (StringUtils.isNotEmpty(wmin.toString())) {				//宽度
				condtion += " and ypd.width >= " + wmin;
			}
			if (StringUtils.isNotEmpty(wmax.toString())) {
				condtion += " and ypd.width <= " + wmax;
			}			
			if (StringUtils.isNotEmpty(ypd.getStorageattr())) {			//仓库
				condtion += " and ypd.storageattr = '"+ypd.getStorageattr()+"'";
			}
			if (StringUtils.isNotEmpty(ypd.getIslock()) && "true".equals(ypd.getIslock())) {
				// 包含在生产中的卷信息：
				// 先查出所有在生产中的卷号：
				List<String> allProdIng = this.getMapper(YdmProductDetailMapper.class).getProdIng();
				String bus = "and (";
				String sqlBu = "or (ypd.STATE ='5' and ypd.COILNO in (";
				if (allProdIng.isEmpty()) {
					// 没有生产中的卷信息：
					if (StringUtils.isNotEmpty(ypd.getState())) {   			//库存状态
						// 由于state:8是查询的二级库存的，不是状态，所以得区分 20240411刘侣
						if ("8".equals(ypd.getState())) {
							// 加上二级库存标志：
							condtion += " and ypd.IS_KUCUN_NEW = '1'";
							condtion += " and ypd.state in ('0','2','6','7')";
						} else {
							condtion += " and ypd.state = '"+ypd.getState()+"'";
						}
						
					}
					if (StringUtils.isEmpty(ypd.getState())) {					//
						condtion += " and ypd.state in ('0','2','6','7')";
					}
				} else {
					// 存在生产中的卷
					for(String coilno : allProdIng) {
						sqlBu += ("'" + coilno + "',");
					}
					sqlBu = sqlBu.substring(0,sqlBu.length()-1);
					sqlBu += ")";
					if (StringUtils.isNotEmpty(ypd.getState())) {   			//库存状态
						// 由于state:8是查询的二级库存的，不是状态，所以得区分 20240411刘侣
						if ("8".equals(ypd.getState())) {
							// 加上二级库存标志：
							bus += "  ypd.IS_KUCUN_NEW = '1'";
							bus += " and ypd.state in ('0','2','6','7')";
							bus += sqlBu;
							
						} else {
							bus += "  ypd.state = '"+ypd.getState()+"'";
							bus += sqlBu;
						}
						
						
					}
					if (StringUtils.isEmpty(ypd.getState())) {					//
						bus += "  ypd.state in ('0','2','6','7')";
						bus += sqlBu;
					}
					bus += "))";
					condtion += bus;
				}
				
			} else {
				// 不包含在生产中的卷信息：
				if (StringUtils.isNotEmpty(ypd.getState())) {   			//库存状态
					// 由于state:8是查询的二级库存的，不是状态，所以得区分 20240411刘侣
					if ("8".equals(ypd.getState())) {
						// 加上二级库存标志：
						condtion += " and ypd.IS_KUCUN_NEW = '1'";
						condtion += " and ypd.state in ('0','2','6','7')";
					} else {
						condtion += " and ypd.state = '"+ypd.getState()+"'";
					}
					
				}
				if (StringUtils.isEmpty(ypd.getState())) {					//
					condtion += " and ypd.state in ('0','2','6','7')";
				}
			}
			
			if (StringUtils.isNotEmpty(ypd.getMaterailcoilno())) {		//原料卷号
				condtion += " and ypd.materailcoilno like '%"+ypd.getMaterailcoilno().trim()+"%'";
			}
			if (StringUtils.isNotEmpty(ypd.getOrderno())) {				//订单号
				condtion += " and ypd.orderno like '%"+ypd.getOrderno().trim()+"%'";
			}
			if (StringUtils.isNotEmpty(ypd.getProdorderno())) {			//生产订单号
				condtion += " and ypd.prodorderno like '%"+ypd.getProdorderno().trim()+"%'";
			}
			if (StringUtils.isNotEmpty(ypd.getButtressno())) {			//垛位
				condtion += " and ypb.buttressno like '%"+ypd.getButtressno().trim()+"%'";
			}
			if (StringUtils.isNotEmpty(ypd.getAreano())) {				//区域
				condtion += " and ybb.buttressno like '%"+ypd.getAreano().trim()+"%'";
			}
			if (StringUtils.isNotEmpty(ypd.getCustomer_no())) {			//客户
				condtion += " and ypd.customer_no like '%"+ypd.getCustomer_no().trim()+"%'";
			}
			if (StringUtils.isNotEmpty(cp.getMemo5())) {				//根据垛位名称进行查询
				condtion += " and yb.buttressname like '%"+cp.getMemo5().trim()+"%'";
			}
			if (StringUtils.isNotEmpty(ypd.getButtressname())) {		//AB跨，BC跨 CD跨进行查询
				if ("AB跨".equals(ypd.getButtressname())) {
					condtion += " and ybb.buttressname not like '%跨%'";
				}else if (!"AB跨".equals(ypd.getButtressname())) {
					condtion += " and ybb.buttressname like '%"+ypd.getButtressname()+"%'";
				}
			}
			if (StringUtils.isNotEmpty(ypd.getObject_attr())) {			//物料属性
				condtion += " and ypd.object_attr = '"+ypd.getObject_attr()+"'";
			}
			if (StringUtils.isNotEmpty(ypd.getMachine_code())) {		//机台查询
				condtion += " and ypd.machine_code = '"+ypd.getMachine_code()+"'";
			}
			if (StringUtils.isNotEmpty(ypd.getMaterailtype())) {		//原料卷类型
				condtion += " and ypd.materailtype = '"+ypd.getMaterailtype()+"'";
			}
			if (StringUtils.isNotEmpty(ypd.getIr_trans_car_no())) {		//到站查询
				condtion += " and irmc.trans_car_no = '"+ypd.getIr_trans_car_no()+"'";
			}
			if (StringUtils.isNotEmpty(ypd.getDestpcd())) {				//贸易库的到站信息
				if ("直发库".equals(ypd.getDestpcd().trim())) {
					condtion += " and irmc.destpcd not in ('新钢板加公司','新余翔威库（新钢优特钢带）','新余佳材开平厂（新钢特钢）','新余广瑞开平厂（新钢优特钢带）','新余丽佳库（新钢优特钢带）')";
				}else if ("板加库".equals(ypd.getDestpcd().trim())) {
					condtion += " and irmc.destpcd = '新钢板加公司'";
				}else if ("翔威库".equals(ypd.getDestpcd().trim())) {
					condtion += " and irmc.destpcd in ('新余翔威库（新钢优特钢带）','新余佳材开平厂（新钢特钢）')";
				}else if ("广瑞库".equals(ypd.getDestpcd().trim())) {
					condtion += " and irmc.destpcd = '新余广瑞开平厂（新钢优特钢带）'";
				}else if ("丽佳库".equals(ypd.getDestpcd().trim())) {
					condtion += " and irmc.destpcd = '新余丽佳库（新钢优特钢带）'";
				}
			}
			if (StringUtils.isNotEmpty(ypd.getSx_valid_flg())) {
				condtion += " and ypd.sx_valid_flg = '"+ypd.getSx_valid_flg()+"'";
			}			
			if (StringUtils.isNotEmpty(cp.getMemo6()) && "外".equals(cp.getMemo6())) { //转外库可发资源查询
				condtion += " and ypd.judgeresult is not null and nvl(ypd.is_package,'1') <> '0' and ypd.state = '0' and (select listagg(t.coillist, ',') within group(order by coillist) from lms_passportinfo t where t.platenumber is null and t.coillist is not null) not like '%'||ypd.coilno||'%'";
			}
			if (day > 0) {												//入库天数
				condtion += " and (trunc(sysdate)-trunc(ypd.indate)) >= " + day;
			}
			if (StringUtils.isNotEmpty(ypd.getIs_kucun_new()) && "1".equals(ypd.getIs_kucun_new())) {
				condtion += " and (trunc(sysdate)-trunc(ypd.indate)) >= 30 and ypd.judgeresult in ('40740802','40740803')";
			}
			condtion += ")";
			String condition2 = "";
			
			String conditonTotal = condtion + condition2;
			List<YdmProductDetail> list = this.getMapper(YdmProductDetailMapper.class).doReportWebBySQL(conditonTotal, ypd, (pageIndex - 1) * pageSize, (pageIndex - 1) * pageSize + pageSize);
			pm.setRows(list);
			pm.setTotal(this.getMapper(YdmProductDetailMapper.class).doReportWebBySQLCount(conditonTotal, ypd));
			rm.setState("200");
			rm.setData(pm);
		}
		catch(Exception ex)
		{
			rm.setState("500");
			rm.setMsgInfo("查询失败!");
			ex.printStackTrace();
		}
		return rm;
	}
	
	
	/**
	 * 已经脱单库存查询：20231129刘侣
	 * */
	public ResultModel doReportYjTD(CommonPage<YdmProductDetail> cp) {
		ResultModel rm=new ResultModel();
		int pageSize=cp.getPageSize();
		int pageIndex=cp.getPageIndex();
		PageModel<HashMap> pm = new PageModel<HashMap>(pageIndex,pageSize);
		try {
			String regex = "^(\\d+\\.?\\d*)$";	
			boolean isMatch = true;
			Object hmin="",hmax="",wmin="",wmax="";
			if (cp.getMemo1() != null &&!"".equals(cp.getMemo1())) {
				isMatch = Pattern.matches(regex, cp.getMemo1().trim());
				if (!isMatch) {
					rm.setState("500");
					rm.setMsgInfo("请输入正确的厚度和宽度!");
					return rm;
				}
				hmin = Double.valueOf(cp.getMemo1()).doubleValue();
			}
			if (cp.getMemo2() != null &&  !"".equals(cp.getMemo2())) {
				isMatch = Pattern.matches(regex, cp.getMemo2().trim());
				if (!isMatch) {
					rm.setState("500");
					rm.setMsgInfo("请输入正确的厚度和宽度!");
					return rm;
				}
				hmax = Double.valueOf(cp.getMemo2()).doubleValue();
			}
			if (cp.getMemo3() != null &&  !"".equals(cp.getMemo3())) {
				isMatch = Pattern.matches(regex, cp.getMemo3().trim());
				if (!isMatch) {
					rm.setState("500");
					rm.setMsgInfo("请输入正确的厚度和宽度!");
					return rm;
				}
				wmin = Double.valueOf(cp.getMemo3()).doubleValue();
			}
			if (cp.getMemo4() != null && !"".equals(cp.getMemo4())) {
				isMatch = Pattern.matches(regex, cp.getMemo4().trim());
				if (!isMatch) {
					rm.setState("500");
					rm.setMsgInfo("请输入正确的厚度和宽度!");
					return rm;
				}
				wmax = Double.valueOf(cp.getMemo4()).doubleValue();
			}
			
			if (StringUtils.isNotEmpty(cp.getObject().getIs_kucun_new()) && "1".equals(cp.getObject().getIs_kucun_new())) {
				cp.getObject().setStorageattr("");
			}
			
			String condtion = " and (1=1";
			YdmProductDetail ypd = cp.getObject();
			if (StringUtils.isNotEmpty(cp.getStartTime())) {			//入库时间
				condtion += " and ypd.indate >= to_date('"+cp.getStartTime()+" 00:00:00','yyyy-MM-dd HH24:MI:SS')";
			}
			if (StringUtils.isNotEmpty(cp.getEndTime())) {
				condtion += " and ypd.indate <= to_date('"+cp.getEndTime()+" 23:59:59','yyyy-MM-dd HH24:MI:SS')";
			}
			if (StringUtils.isNotEmpty(ypd.getCoilno())) {				//卷号
				condtion += " and ypd.coilno like '%"+ypd.getCoilno()+"%'";
			}
			if (StringUtils.isNotEmpty(ypd.getJudgesteelcode())) {		//牌号：中文，如Q195
				condtion += " and cbs.steel_name like '%"+ypd.getJudgesteelcode().trim()+"%'";
			}
			if (StringUtils.isNotEmpty(hmin.toString())) {				//厚度
				condtion += " and ypd.thick >= " + hmin;
			}
			if (StringUtils.isNotEmpty(hmax.toString())) {
				condtion += " and ypd.thick <= " + hmax;
			}
			if (StringUtils.isNotEmpty(wmin.toString())) {				//宽度
				condtion += " and ypd.width >= " + wmin;
			}
			if (StringUtils.isNotEmpty(wmax.toString())) {
				condtion += " and ypd.width <= " + wmax;
			}			
			
//			condtion += " and ypd.storageattr in ('500201','500202','500203')";
			if (StringUtils.isNotEmpty(ypd.getStorageattr())) {   			//仓库
				condtion += " and ypd.storageattr = '"+ypd.getStorageattr()+"'";
			} else {
				condtion += " and ypd.storageattr in ('500201','500202','500203')";
			}
			
			
			condtion += " and ypd.OBJECT_ATTR in ('507101')";
			
			if (StringUtils.isNotEmpty(ypd.getState())) {   			//库存状态
				condtion += " and ypd.state = '"+ypd.getState()+"'";
			}
			if (StringUtils.isEmpty(ypd.getState())) {					//
				condtion += " and ypd.state in ('0','7')";
			}
			if (StringUtils.isNotEmpty(ypd.getMaterailcoilno())) {		//原料卷号
				condtion += " and ypd.materailcoilno like '%"+ypd.getMaterailcoilno().trim()+"%'";
			}
			if (StringUtils.isNotEmpty(ypd.getOrderno())) {				//订单号
				condtion += " and y.ORDER_NO like '%"+ypd.getOrderno().trim()+"%'";
			}
			if (StringUtils.isNotEmpty(ypd.getProdorderno())) {			//生产订单号
				condtion += " and y.PROD_ORDER_NO like '%"+ypd.getProdorderno().trim()+"%'";
			}
			if (StringUtils.isNotEmpty(ypd.getButtressno())) {			//垛位
				condtion += " and ypb.buttressno like '%"+ypd.getButtressno().trim()+"%'";
			}
			if (StringUtils.isNotEmpty(ypd.getAreano())) {				//区域
				condtion += " and ybb.buttressno like '%"+ypd.getAreano().trim()+"%'";
			}
			if (StringUtils.isNotEmpty(ypd.getCustomer_no())) {			//客户
				condtion += " and ypd.customer_no like '%"+ypd.getCustomer_no().trim()+"%'";
			}
			if (StringUtils.isNotEmpty(cp.getMemo5())) {				//根据垛位名称进行查询
				condtion += " and yb.buttressname like '%"+cp.getMemo5().trim()+"%'";
			}
			if (StringUtils.isNotEmpty(ypd.getButtressname())) {		//AB跨，BC跨 CD跨进行查询
				if ("AB跨".equals(ypd.getButtressname())) {
					condtion += " and ybb.buttressname not like '%跨%'";
				}else if (!"AB跨".equals(ypd.getButtressname())) {
					condtion += " and ybb.buttressname like '%"+ypd.getButtressname()+"%'";
				}
			}
			if (StringUtils.isNotEmpty(ypd.getObject_attr())) {			//物料属性
				condtion += " and ypd.object_attr = '"+ypd.getObject_attr()+"'";
			}
			if (StringUtils.isNotEmpty(ypd.getMachine_code())) {		//机台查询
				condtion += " and ypd.machine_code = '"+ypd.getMachine_code()+"'";
			}
			if (StringUtils.isNotEmpty(ypd.getMaterailtype())) {		//原料卷类型
				condtion += " and ypd.materailtype = '"+ypd.getMaterailtype()+"'";
			}
			
			condtion += ")";
			String conditonTotal = condtion;
			List<HashMap> list = this.getMapper(YdmProductDetailMapper.class).doQueryYjTd(conditonTotal, ypd, (pageIndex - 1) * pageSize, (pageIndex - 1) * pageSize + pageSize);
			pm.setRows(list);
			pm.setTotal(list.size());
			rm.setState("200");
			rm.setData(pm);
		}
		catch(Exception ex)
		{
			rm.setState("500");
			rm.setMsgInfo("查询失败!");
			ex.printStackTrace();
		}
		return rm;
	}
	
	
	
	/**
	 * 报表：XX库库存查询(只查询酸轧卷)20230630刘侣
	 * */
	public ResultModel doReportWebBySQLSZJ(CommonPage<YdmProductDetail> cp) {
		ResultModel rm=new ResultModel();
		int pageSize=cp.getPageSize();
		int pageIndex=cp.getPageIndex();
		PageModel<YdmProductDetail> pm = new PageModel<YdmProductDetail>(pageIndex,pageSize);
		try {
			String regex = "^(\\d+\\.?\\d*)$";	
			boolean isMatch = true;
			Object hmin="",hmax="",wmin="",wmax="";
			if (cp.getMemo1() != null &&!"".equals(cp.getMemo1())) {
				isMatch = Pattern.matches(regex, cp.getMemo1().trim());
				if (!isMatch) {
					rm.setState("500");
					rm.setMsgInfo("请输入正确的厚度和宽度!");
					return rm;
				}
				hmin = Double.valueOf(cp.getMemo1()).doubleValue();
			}
			if (cp.getMemo2() != null &&  !"".equals(cp.getMemo2())) {
				isMatch = Pattern.matches(regex, cp.getMemo2().trim());
				if (!isMatch) {
					rm.setState("500");
					rm.setMsgInfo("请输入正确的厚度和宽度!");
					return rm;
				}
				hmax = Double.valueOf(cp.getMemo2()).doubleValue();
			}
			if (cp.getMemo3() != null &&  !"".equals(cp.getMemo3())) {
				isMatch = Pattern.matches(regex, cp.getMemo3().trim());
				if (!isMatch) {
					rm.setState("500");
					rm.setMsgInfo("请输入正确的厚度和宽度!");
					return rm;
				}
				wmin = Double.valueOf(cp.getMemo3()).doubleValue();
			}
			if (cp.getMemo4() != null && !"".equals(cp.getMemo4())) {
				isMatch = Pattern.matches(regex, cp.getMemo4().trim());
				if (!isMatch) {
					rm.setState("500");
					rm.setMsgInfo("请输入正确的厚度和宽度!");
					return rm;
				}
				wmax = Double.valueOf(cp.getMemo4()).doubleValue();
			}
			String regex2 = "^\\d+$";// 非负整数
			int day = 0;
			if (cp.getObject().getJgdate() != null && !"".equals(cp.getObject().getJgdate().trim())) {// 输入天数验证
				isMatch = Pattern.matches(regex2, cp.getObject().getJgdate());
				if (!isMatch) {
					rm.setState("500");
					rm.setMsgInfo("请输入正确的天数！（为非负整数）");
					return rm;
				}
				day = Integer.parseInt(cp.getObject().getJgdate());
			}
			if (StringUtils.isNotEmpty(cp.getObject().getIs_kucun_new()) && "1".equals(cp.getObject().getIs_kucun_new())) {
				cp.getObject().setStorageattr("");
			}
			
			String condtion = " and (1=1 and MATERAILTYPE = '2' ";
			YdmProductDetail ypd = cp.getObject();
			if (StringUtils.isNotEmpty(cp.getStartTime())) {			//入库时间
				condtion += " and ypd.indate >= to_date('"+cp.getStartTime()+" 00:00:00','yyyy-MM-dd HH24:MI:SS')";
			}
			if (StringUtils.isNotEmpty(cp.getEndTime())) {
				condtion += " and ypd.indate <= to_date('"+cp.getEndTime()+" 23:59:59','yyyy-MM-dd HH24:MI:SS')";
			}
			if (StringUtils.isNotEmpty(ypd.getCoilno())) {				//卷号
				condtion += " and ypd.coilno like '%"+ypd.getCoilno()+"%'";
			}
			if (StringUtils.isNotEmpty(ypd.getJudgesteelcode())) {		//牌号：中文，如Q195
				condtion += " and cbs.steel_name like '%"+ypd.getJudgesteelcode().trim()+"%'";
			}
			if (StringUtils.isNotEmpty(hmin.toString())) {				//厚度
				condtion += " and ypd.thick >= " + hmin;
			}
			if (StringUtils.isNotEmpty(hmax.toString())) {
				condtion += " and ypd.thick <= " + hmax;
			}
			if (StringUtils.isNotEmpty(wmin.toString())) {				//宽度
				condtion += " and ypd.width >= " + wmin;
			}
			if (StringUtils.isNotEmpty(wmax.toString())) {
				condtion += " and ypd.width <= " + wmax;
			}			
			if (StringUtils.isNotEmpty(ypd.getStorageattr())) {			//仓库
				condtion += " and ypd.storageattr = '"+ypd.getStorageattr()+"'";
			}
			if (StringUtils.isNotEmpty(ypd.getState())) {   			//库存状态
				condtion += " and ypd.state = '"+ypd.getState()+"'";
			}
			if (StringUtils.isEmpty(ypd.getState())) {					//
				condtion += " and ypd.state in ('0','2','6','7')";
			}
			if (StringUtils.isNotEmpty(ypd.getMaterailcoilno())) {		//原料卷号
				condtion += " and ypd.materailcoilno like '%"+ypd.getMaterailcoilno().trim()+"%'";
			}
			if (StringUtils.isNotEmpty(ypd.getOrderno())) {				//订单号
				condtion += " and ypd.orderno like '%"+ypd.getOrderno().trim()+"%'";
			}
			if (StringUtils.isNotEmpty(ypd.getProdorderno())) {			//生产订单号
				condtion += " and ypd.prodorderno like '%"+ypd.getProdorderno().trim()+"%'";
			}
			if (StringUtils.isNotEmpty(ypd.getButtressno())) {			//垛位
				condtion += " and ypb.buttressno like '%"+ypd.getButtressno().trim()+"%'";
			}
			if (StringUtils.isNotEmpty(ypd.getAreano())) {				//区域
				condtion += " and ybb.buttressno like '%"+ypd.getAreano().trim()+"%'";
			}
			if (StringUtils.isNotEmpty(ypd.getCustomer_no())) {			//客户
				condtion += " and ypd.customer_no like '%"+ypd.getCustomer_no().trim()+"%'";
			}
			if (StringUtils.isNotEmpty(cp.getMemo5())) {				//根据垛位名称进行查询
				condtion += " and yb.buttressname like '%"+cp.getMemo5().trim()+"%'";
			}
			if (StringUtils.isNotEmpty(ypd.getButtressname())) {		//AB跨，BC跨 CD跨进行查询
				if ("AB跨".equals(ypd.getButtressname())) {
					condtion += " and ybb.buttressname not like '%跨%'";
				}else if (!"AB跨".equals(ypd.getButtressname())) {
					condtion += " and ybb.buttressname like '%"+ypd.getButtressname()+"%'";
				}
			}
			if (StringUtils.isNotEmpty(ypd.getObject_attr())) {			//物料属性
				condtion += " and ypd.object_attr = '"+ypd.getObject_attr()+"'";
			}
			if (StringUtils.isNotEmpty(ypd.getMachine_code())) {		//机台查询
				condtion += " and ypd.machine_code = '"+ypd.getMachine_code()+"'";
			}
			if (StringUtils.isNotEmpty(ypd.getMaterailtype())) {		//原料卷类型
				condtion += " and ypd.materailtype = '"+ypd.getMaterailtype()+"'";
			}
			if (StringUtils.isNotEmpty(ypd.getIr_trans_car_no())) {		//到站查询
				condtion += " and irmc.trans_car_no = '"+ypd.getIr_trans_car_no()+"'";
			}
			if (StringUtils.isNotEmpty(ypd.getDestpcd())) {				//贸易库的到站信息
				if ("直发库".equals(ypd.getDestpcd().trim())) {
					condtion += " and irmc.destpcd not in ('新钢板加公司','新余翔威库（新钢优特钢带）','新余佳材开平厂（新钢特钢）','新余广瑞开平厂（新钢优特钢带）','新余丽佳库（新钢优特钢带）')";
				}else if ("板加库".equals(ypd.getDestpcd().trim())) {
					condtion += " and irmc.destpcd = '新钢板加公司'";
				}else if ("翔威库".equals(ypd.getDestpcd().trim())) {
					condtion += " and irmc.destpcd in ('新余翔威库（新钢优特钢带）','新余佳材开平厂（新钢特钢）')";
				}else if ("广瑞库".equals(ypd.getDestpcd().trim())) {
					condtion += " and irmc.destpcd = '新余广瑞开平厂（新钢优特钢带）'";
				}else if ("丽佳库".equals(ypd.getDestpcd().trim())) {
					condtion += " and irmc.destpcd = '新余丽佳库（新钢优特钢带）'";
				}
			}
			if (StringUtils.isNotEmpty(ypd.getSx_valid_flg())) {
				condtion += " and ypd.sx_valid_flg = '"+ypd.getSx_valid_flg()+"'";
			}			
			if (StringUtils.isNotEmpty(cp.getMemo6()) && "外".equals(cp.getMemo6())) { //转外库可发资源查询
				condtion += " and ypd.judgeresult is not null and nvl(ypd.is_package,'1') <> '0' and ypd.state = '0' and (select listagg(t.coillist, ',') within group(order by coillist) from lms_passportinfo t where t.platenumber is null and t.coillist is not null) not like '%'||ypd.coilno||'%'";
			}
			if (day > 0) {												//入库天数
				condtion += " and (trunc(sysdate)-trunc(ypd.indate)) >= " + day;
			}
			if (StringUtils.isNotEmpty(ypd.getIs_kucun_new()) && "1".equals(ypd.getIs_kucun_new())) {
				condtion += " and (trunc(sysdate)-trunc(ypd.indate)) >= 30 and ypd.judgeresult in ('40740802','40740803')";
			}
			condtion += ")";
			String condition2 = "";
			if (StringUtils.isNotEmpty(ypd.getIslock()) && "true".equals(ypd.getIslock())) {
				condition2 += " or exists("
						+ " (select 1 from ( "
						+ " select t.material_no as coil_no,t.machine_code from mes_cr_slitting_m t where 1=1 and t.machine_code='M0001' and nvl(hand_over_status,'0') = '0'"
						+ " union "
						+ " select t.material_no as coil_no,t.machine_code from mes_cr_slitting_m t where 1=1 and t.machine_code='M0002' and nvl(hand_over_status,'0') = '0' "
						+ " union "
						+ " select t.material_no as coil_no,t.machine_code from mes_cr_cross_cutting_m t  where 1=1 and t.machine_code='M0003' and nvl(hand_over_status,'0') = '0' "
						+ " union "
						+ " select t.material_no as coil_no,t.machine_code from mes_cr_anneal_m t  where 1=1 and  nvl(hand_over_status,'0') = '0' "
						+ " union "
						+ " select t.material_no as coil_no,t.machine_code from mes_tpm_m t  where 1=1 and  nvl(hand_over_status,'0') = '0' ) x "
						+ " where x.coil_no = ypd.coilno "+condtion+"))";
			}
			String conditonTotal = condtion + condition2;
			List<YdmProductDetail> list = this.getMapper(YdmProductDetailMapper.class).doReportWebBySQL(conditonTotal, ypd, (pageIndex - 1) * pageSize, (pageIndex - 1) * pageSize + pageSize);
			pm.setRows(list);
			pm.setTotal(this.getMapper(YdmProductDetailMapper.class).doReportWebBySQLCount(conditonTotal, ypd));
			rm.setState("200");
			rm.setData(pm);
		}
		catch(Exception ex)
		{
			rm.setState("500");
			rm.setMsgInfo("查询失败!");
			ex.printStackTrace();
		}
		return rm;
	}
	
	
	/**
	 * @author 赵淋支
	 * @date   2018年10月31日
	 * @description (罩退接口)验证卷号对应的库存状态
	 */
	public ResultModel interfaceJudgeCoilState(CommonPage<YdmProductDetail> cp){
		ResultModel rm=new ResultModel();
		String coilno = cp.getMemo1(); //卷号
		if (coilno==null || "".equals(coilno)) {
			rm.setState("500");
			rm.setMsgInfo("请传入卷号！");
			return rm;
		}
		try
		{
			YdmProductDetail ypd = this.getMapper(YdmProductDetailMapper.class).findById(coilno);
			if (ypd == null) {
				rm.setState("500");
				rm.setMsgInfo("卷号["+coilno+"]的库存数据不存在！");
				return rm;
			}
			if (!"-1".equals(ypd.getState())) {
				rm.setState("500");
				rm.setMsgInfo("卷号["+coilno+"]不为待入库状态！");
				return rm;
			}
		}
		catch(Exception ex)
		{
			rm.setState("500");
			rm.setMsgInfo("卷号["+coilno+"]验证失败!");
			ex.printStackTrace();
		}
		rm.setState("200");
		return rm;
	}
	
	/**
	 * @author 赵淋支
	 * @date   2018年10月31日
	 * @description (罩退接口)删除卷号对应的库存信息和移库信息
	 */
	public ResultModel interfaceCancelCoil(CommonPage<YdmProductDetail> cp){
		ResultModel rm=new ResultModel();
		String coilno = cp.getMemo1(); //卷号
		if (coilno==null || "".equals(coilno)) {
			rm.setState("500");
			rm.setMsgInfo("请传入卷号！");
			return rm;
		}
		try
		{
			YdmProductDetail ypd = this.getMapper(YdmProductDetailMapper.class).findById(coilno);
			if (ypd == null) {
				rm.setState("500");
				rm.setMsgInfo("卷号["+coilno+"]的库存数据不存在！");
				return rm;
			}
			//删除卷号对应的库存信息
			this.getMapper(YdmProductDetailMapper.class).delete(coilno);			
		}
		catch(Exception ex)
		{
			rm.setState("500");
			rm.setMsgInfo("删除卷号["+coilno+"]的库存信息失败!");
			ex.printStackTrace();
		}
		rm.setState("200");
		return rm;
	} 
	
	
	/**
	 * @author 赵淋支
	 * @date   2018年10月31日
	 * @description (平整机 ：交工接口)新增数据到库存表中
	 */
	public ResultModel interfaceInsertCoil(CommonPage<MesTpmD> cp){
		ResultModel rm=new ResultModel();

		MesTpmD mtd = cp.getObject(); //平整机生产主实绩
		MesTpmM mtm=new MesTpmM();
		mtm.setSchedule_no(mtd.getSchedule_no());
		mtm.setMaterial_no(mtd.getMaterial_no());
		List<MesTpmM> list_mtm = this.getMapper(MesTpmMMapper.class).find(mtm, 0, 0);
		if(list_mtm==null || list_mtm.size()<=0){
			rm.setState("500");
			rm.setMsgInfo("不存在生产主实绩，请重新进行上料操作！");
			return rm;
		}
		mtm=list_mtm.get(0);
		String username=(String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		
		try { 
			if (mtd==null || mtd.getCoil_no()==null || "".equals(mtd.getCoil_no())) {
				rm.setState("500");
				rm.setMsgInfo("请传入卷号！");
				return rm;
			}
			//1.判断是否已经存在库存信息
			YdmProductDetail YPD = new YdmProductDetail();
			if ("0".equals(cp.getMemo5())) {//吊销卷号
				YPD.setCoilno(mtm.getRejected_coil_no());
			}
			YPD.setCoilno(mtd.getCoil_no());
			int count = this.getMapper(YdmProductDetailMapper.class).count(YPD);
			if (count > 0) {
				rm.setState("500");
				rm.setMsgInfo("卷号["+YPD.getCoilno()+"]已存在库存数据！");
				return rm;
			}
			
			YdmProductDetail ypd = new YdmProductDetail();
			//ypd.setCoilno(mtm.getCoil_no());
			//ypd.setCoiltype("?");//卷类别(0原料卷、1中间卷、2成品卷)
			//原料卷类型（1酸洗卷、2酸轧卷、3连退卷、4重卷卷、0其它卷）					
			List<YdmProductDetail> list = this.getMapper(YdmProductDetailMapper.class).findWithCondition(null, "coilno ='"+mtm.getMaterial_no()+"'", null, 0, 0);
			if(list!=null && list.size()>0){
				ypd.setMaterailtype(list.get(0).getMaterailtype());
				ypd.setHeatno(list.get(0).getHeatno());//2019-03-11 赵淋支 将原料卷的炉号带入分条退火信息
			}
			ypd.setSource("0");//原料卷来源（0新钢、1其它）
			
			if (mtm.getMachine_code()!=null && !"".equals(mtm.getMachine_code())) {
				ypd.setMachine_code(mtm.getMachine_code());//机台号代码
				ComBaseMachine machine = this.getMapper(ComBaseMachineMapper.class).findById(mtm.getMachine_code());
				if (machine!=null) {
					ypd.setMachine_name(machine.getMachine_name());//机台号名称
				}
			}
			ypd.setMaterailcoilno(mtm.getMaterial_no_src());//原始卷号
			ypd.setObject_no_vt(mtd.getCoil_no_vt());
			
			
			
			if ("0".equals(cp.getMemo5())) {
				//吊销卷号
				ypd.setCoilno(mtm.getRejected_coil_no());//吊销卷号
				ypd.setTheoryweight(mtm.getRejected_coil_thr_wgt());//吊销卷理重
				ypd.setFactweight(mtm.getRejected_coil_act_wgt());//吊销卷实重
				ypd.setLength(mtm.getRejected_coil_len());//吊销卷长度
				ypd.setDiameter_out(mtm.getRejected_coil_out_dia());//吊销卷外径
			}else{
				//产出卷号
				ypd.setCoilno(mtd.getCoil_no());
				ypd.setThick(mtd.getAct_thk());
				ypd.setWidth(mtd.getAct_wdt());
				ypd.setLength(mtd.getAct_len());
				//20191018:包括目标宽度，目标厚度到库存表
				ypd.setLz_thick(mtd.getAim_thk());//合同厚度(目标厚度)
				ypd.setLz_width(mtd.getAim_wdt());//合同宽度(目标宽度)
				ypd.setDiameter_inn(mtd.getAct_inn_dia());
				ypd.setDiameter_out(mtd.getAct_out_dia());
				ypd.setTheoryweight(mtd.getCoil_thr_wgt());
				ypd.setFactweight(mtd.getCoil_act_wgt());
				ypd.setProdorderno(mtd.getProd_order_no());
				ypd.setOrderno(mtd.getOrder_no());
				ypd.setInspection_lot(mtd.getInspection_lot());
				if(mtd.getProd_order_no()!=null && !"".equals(mtd.getProd_order_no())){
					//生产订单信息
					PlnProdorder pp=new PlnProdorder();
					pp.setProd_order_no(mtd.getProd_order_no());
					List<PlnProdorder> lpp=this.getMapper(PlnProdorderMapper.class).find(pp, 0, 1);
					if(lpp!=null&&lpp.size()>0){
						pp = lpp.get(0);
						ypd.setCustomer_no(pp.getCustomer_no());
						ypd.setCustomer_nm(pp.getCustomer_name());
						ypd.setPsc(pp.getPsc());
						ypd.setDelivery_state_code(pp.getDelivery_state_code());
						ypd.setDelivery_state_desc(pp.getDelivery_state_desc());
						ypd.setProd_code(pp.getProd_code());
						ypd.setProd_name(pp.getProd_name());
						ypd.setStd_code(pp.getStd_code());
						ypd.setStd_name(pp.getStd_name());
						ypd.setUse_code(pp.getUse_code());
						ypd.setUse_name(pp.getUser_name());
						ypd.setPlan_code(pp.getPline_code());
						ypd.setPline_name(pp.getPline_name());
						ypd.setSteelcode(pp.getSteel_code());
						ypd.setReceiv_no(pp.getReciver_unit_no());
						ypd.setReceiv_nm(pp.getReciver_unit_name()); 
					}
				}
			}
			
			ypd.setCreater(username);
			ypd.setCreatedate(new Date()); 
			
			ypd.setSteelcode(mtd.getSteel_code());
			
			// 改为获取投料卷信息里的判定牌号：20240722刘侣 改回来不动 20240815
			ypd.setJudgesteelcode(mtd.getSteel_code());	
//			ypd.setJudgesteelcode(list.get(0).getJudgesteelcode());	
			
			
			ypd.setProduceorder(mtd.getWrk_ord());//作业班次 --》生产班次
			ypd.setProduceteam(mtd.getWrk_grp());//作业班组 --》生产班组
			if (mtd.getWrk_date()!=null && !"".equals(mtd.getWrk_date())) {				
				ypd.setProducedate(new Date());
			}
			//ypd.setProducedate(mtd.getCreate_time());
			//2018-11-27  生产时间取创建时间，宋燕辉
			/*if (mtd.getWrk_date()!=null && !"".equals(mtd.getWrk_date())) {				
				ypd.setProducedate(new SimpleDateFormat("yyyy-MM-dd").parse(mtd.getWrk_date()));
			}//作业日期 --》生产时间
			 */			
			ypd.setState("-1"); 
			ypd.setStorageattr(cp.getMemo1());//仓库编码
			ypd.setCoiltype("500203".equals(ypd.getStorageattr())?"2":"1");
			ypd.setManagementno(cp.getMemo2());//部门
			
			ypd.setQuantity(1);
			ypd.setBatchno(mtd.getSchedule_no());
			ypd.setObject_no_xg("");
			ypd.setInteam((String)this.getSession().getAttribute(HomeService.SESSION_WORK_GRP));
			ypd.setInorder((String)this.getSession().getAttribute(HomeService.SESSION_WORK_ORD));
			
			if (mtd.getCoil_no().length() >= 8) { 
				//String coco = mtd.getCoil_no().substring(0,8);
				YdmProductDetail yyd=new YdmProductDetail();
				yyd = this.getMapper(YdmProductDetailMapper.class).doFind(mtm.getMaterial_no(), null);
				if(yyd!=null){
					if(yyd.getMaterailcoilno()!=null&&!"".equals(yyd.getMaterailcoilno())){
						ypd.setMaterailcoilno(yyd.getMaterailcoilno());//原料卷号
					}
					if(yyd.getObject_attr()!=null&&!"".equals(yyd.getObject_attr())){
						ypd.setObject_attr(yyd.getObject_attr());//物料属性
					}
					//2019-02-21  物料属性
					if(cp.getMemo3()!=null && !"".equals(cp.getMemo3())){
						ypd.setObject_attr(cp.getMemo3());
						ypd.setIsbl("1");
					}
					
					if(yyd.getLzorderno()!=null&&!"".equals(yyd.getLzorderno())){
						ypd.setLzorderno(yyd.getLzorderno());//冷轧销售订单号
					}
				} 
			}
			//20190801 边丝和头尾卷默认综判判废//20191025 进废副品库
			String lastChar = ypd.getCoilno().substring(ypd.getCoilno().length()-1);
			if ("B".equals(lastChar) || "H".equals(lastChar)) {
				// 边丝和头尾卷默认综判判废
				ypd.setJudgeresult("40740804");
				//20200317 净重的头尾卷及边丝入废钢库  毛重进成品库 黄涛提出
				String condition = "contract_no||contract_line_seq||order_seq = '"+ypd.getOrderno()+"' and nvl(change_flag,'0')<>'1'";
				List<SlmOrderInfo> soiList = this.getMapper(SlmOrderInfoMapper.class).findWithCondition(null, condition, null, 0, 0);
				if (soiList!=null && soiList.size()>0) {
					String weight_type_no = soiList.get(0).getWeight_type_no();
					if ("102301".equals(weight_type_no)) {//净重
						
						// 净重的头尾卷和边丝脱单后需要手动插入脱单信息：20230725刘侣
						// 前提是这个卷没有做过脱单插入：
						
						String conditiontd = " and OBJECT_NO = '"+ypd.getCoilno()+"' and OPT_TYPE = '0'";
						List<PlnMatReplaceLog> pmrList = this.getMapper(PlnMatReplaceLogMapper.class).findWithCondition(null, conditiontd, null, 0, 0);
						if (pmrList.size() == 0) {
							List<PlnMatReplaceLog> lmmrl = this.getMapper(PlnMatReplaceLogMapper.class).findWithCondition(null, "",
									" serial_no desc ", 0, 1);
							long iNo = 1;
							if (lmmrl != null && lmmrl.size() > 0) {
								iNo = lmmrl.get(0).getSerial_no() + 1;
							}
							PlnMatReplaceLog mmrl = new PlnMatReplaceLog();
							mmrl.setSerial_no(iNo++);
							mmrl.setObject_no(ypd.getCoilno());
							mmrl.setOpt_type("0");
							mmrl.setStatus("0");
							mmrl.setOrder_no(ypd.getOrderno());
							
							mmrl.setCreate_man_name(username);
							mmrl.setCreate_time(new Date());
							mmrl.setMemo("净重头尾边丝卷补脱单信息");
							this.getMapper(PlnMatReplaceLogMapper.class).insert(mmrl);
						}
						
						// end
						
						
						ypd.setStorageattr("500210");
						ypd.setOrderno("");
						ypd.setProdorderno("");
						ypd.setObject_no_vt("");
					}else if ("102302".equals(weight_type_no)) {//毛重
						ypd.setStorageattr("500203");
						// 毛重头尾边丝默认合格：20231024刘侣
						ypd.setJudgeresult("40740801");
					}
				}
			}
			//包装状态(20191016)
			ypd.setIs_package("0");
			this.getMapper(YdmProductDetailMapper.class).insert(ypd);
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("新增库存数据失败！");
			return rm;
		}
		rm.setState("200");
		return rm; 
	} 
	

	/**
	 * @author 赵淋支
	 * @date   2018年11月27日
	 * @parameter orderno：销售订单号    result1：材质结果   result2材质结果ID inspection_lot检验号
	 * @description 通过销售订单号：修改材质结果和材质结果ID
	 */
	public ResultModel doUpdateByOrderno(String orderno,String result1,String result2,String inspection_lot){
		ResultModel rm = new ResultModel();
		if (orderno==null || "".equals(orderno)) {
			rm.setData("500");
			rm.setMsgInfo("销售订单号不能为空！");
		}
		if (result1==null || "".equals(result1)) {
			rm.setData("500");
			rm.setMsgInfo("材质判定结果不能为空！");
		}
		if (result2==null || "".equals(result2)) {
			rm.setData("500");
			rm.setMsgInfo("材质判定结果ID不能为空！");
		}
		this.getMapper(YdmProductDetailMapper.class).doUpdateByOrderno(orderno,result1,result2,inspection_lot);
		rm.setState("200");
		return rm;		
	}
	
	/**
	 * @author 赵淋支
	 * @date   2019年2月20日
	 * @parameter 
	 * @description 提货单管理提货单明细 ，点击原料卷号，弹出显示物料信息
	 */
	public ResultModel doQuaryByMaterialCoilno(CommonPage<YdmProductDetail> cp){
		ResultModel rm=new ResultModel();
		String bill_no=cp.getMemo1();//提单子编号
		YdmProductDetail ypd = cp.getObject();
		try {
			String swhere="";
			if(bill_no!=null&&!"".equals(bill_no)){
				swhere+=" and slbd.line_no = '"+bill_no+"'";
			}
			if(ypd.getMaterailcoilno()!=null&&!"".equals(ypd.getMaterailcoilno())){
				swhere+=" and slbd.materailcoilno = '"+ypd.getMaterailcoilno()+"'";
			}
			if(ypd.getStorageattr()!=null&&!"".equals(ypd.getStorageattr())){
				swhere+=" and ypd.storageattr = '"+ypd.getStorageattr()+"'";
			}
			List<YdmProductDetail> list = this.getMapper(YdmProductDetailMapper.class).doQuaryByMaterialCoilno(swhere);
			rm.setData(list);
			rm.setState("200");
			rm.setMsgInfo("查询成功");		
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("查询失败，请联系管理员！");
			throw e;
		}
		return rm;
	}	
	
	/**
	 * 24点导出功能：原来的接口先注释掉：20230703刘侣
	 * @param cp
	 * @return
	 */
//	public ResultModel doQueryYpdHistory(CommonPage<YdmProductDetail> cp){
//		ResultModel rm = new ResultModel();
//		String swhere = "";
//		YdmProductDetail ypd = cp.getObject();
//		if (ypd.getStorageattr()!=null && !"".equals(ypd.getStorageattr())) {
//			swhere += " and t.storageattr = '"+ypd.getStorageattr()+"'";
//		}
//		if (cp.getStartTime()!=null && !"".equals(cp.getStartTime())) {
//			swhere += " and t.statdate like '"+cp.getStartTime()+"'";
//		}
//		List<HashMap> list = this.getMapper(YdmProductDetailMapper.class).doQueryYpdHistory(swhere);
//		rm.setData(list);
//		rm.setState("200");
//		rm.setMsgInfo("查询成功");
//		return rm;
//	}
	
	/**
	 * 24点导出功能：消除原料库24点报表中垛位会变动的问题：20230630刘侣
	 * @param cp
	 * @return
	 */
	public ResultModel doQueryYpdHistory(CommonPage<YdmProductDetail> cp){
		ResultModel rm = new ResultModel();
		String swhere = "";
		YdmProductDetail ypd = cp.getObject();
		if (ypd.getStorageattr()!=null && !"".equals(ypd.getStorageattr())) {
			swhere += " and t.storageattr = '"+ypd.getStorageattr()+"'";
		}
		if (cp.getStartTime()!=null && !"".equals(cp.getStartTime())) {
			swhere += " and t.statdate like '"+cp.getStartTime()+"'";
		}
		// 这个查询方法需要重写：20230630刘侣
		// 解决方案：用statdate查询是否有buttreesno来决定是否用ypdhistory里的buttressno：20230703 刘侣
		List<HashMap> list = new ArrayList<>();
		int num = this.getMapper(YdmProductDetailMapper.class).findIsAllNew(cp.getStartTime());
		if (num > 100) {
			// 说明是以前的数据：
			list = this.getMapper(YdmProductDetailMapper.class).doQueryYpdHistoryBefore(swhere);
		} else {
			// 现在的数据：
			list = this.getMapper(YdmProductDetailMapper.class).doQueryYpdHistory(swhere);
		}
		
		rm.setData(list);
		rm.setState("200");
		rm.setMsgInfo("查询成功");
		return rm;
	}
	
	/**
	 * 成品库和贸易库库存报表查询功能
	 * @param cp
	 * @return
	 */
	public ResultModel doQueryCNMHistory(CommonPage<YdmProductDetail> cp){
		ResultModel rm = new ResultModel();
		HashMap hm = new HashMap();
		List<HashMap> list = new ArrayList<>();
		
		// 库存总量：成品库和贸易库；
		List<HashMap> kczlList = this.getMapper(YdmProductDetailMapper.class).doQueryKCHistory(cp.getStartTime(), cp.getEndTime());
		List<HashMap> kflList = this.getMapper(YdmProductDetailMapper.class).doQueryKFLHistory(cp.getStartTime(), cp.getEndTime());
		List<HashMap> fysdList = this.getMapper(YdmProductDetailMapper.class).doQueryFYSDHistory(cp.getStartTime(), cp.getEndTime());
		List<HashMap> rcklList = this.getMapper(YdmProductDetailMapper.class).doQueryRCKLHistory(cp.getStartTime(), cp.getEndTime());
		
		hm.put("kczl", kczlList);
		hm.put("kfl", kflList);
		hm.put("fysd", fysdList);
		hm.put("rckl", rcklList);
		list.add(hm);
		
		rm.setData(list);
		rm.setState("200");
		rm.setMsgInfo("查询成功");
		return rm;
	}
	
	/**
	 * 退卷操作 20200923 黄t:
	 * 原料库和贸易库(500201  500205) : 保存退卷原因，操作时间，操作人
	 * @param cp
	 * @return
	 * @throws Exception 
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doReturnYdmProductDetail(CommonPage<YdmProductDetail> cp) throws Exception{
		ResultModel rm = new ResultModel();
		List<YdmProductDetail> ypdList = cp.getObjectList();
		Date now = new Date();
		String opr_time = new SimpleDateFormat("yyyyMMddHHmmss").format(now); //操作时间
		String rtn_opr = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME); //退卷操作人
		String rtn_reason = cp.getMemo1(); //退卷原因
		if (CollectionUtils.isEmpty(ypdList)) {
			throw new Exception("请勾选数据");
		}
		for (YdmProductDetail ypd : ypdList) {
			ypd.setRtn_date(now);//退卷时间
			ypd.setRtn_opr(rtn_opr);//退卷操作人
			ypd.setRtn_reason(rtn_reason);//退卷时间
			this.getMapper(YdmProductDetailMapper.class).update(ypd);
			
			// 查询卷信息是否存在，且是待入库状态：
			YdmProductDetail ypdQ = this.getMapper(YdmProductDetailMapper.class).findById(ypd.getCoilno());
			if (ypdQ == null) {
				throw new Exception("卷信息不存在");
			}
			if (!"-1".equals(ypdQ.getState())) {
				throw new Exception("卷状态不是 待入库");
			}
			// 先改卷板信息：insertReturnLog
			this.getMapper(YdmProductDetailMapper.class).updateJbReturn(ypdQ.getMaterailcoilno());
			this.getMapper(YdmProductDetailMapper.class).insertReturnLog(ypdQ.getMaterailcoilno(), rtn_opr);
			// 记录日志：
			MesOprLog mol = new MesOprLog("物流管理", "原料入库管理", "退卷", rtn_opr, opr_time, ypdQ.getCoilno(), null, null, null, ypdQ.getMaterailcoilno());
			this.getMapper(MesOprLogMapper.class).insert(mol);
			// 再将本地待入库卷删掉：
			this.getMapper(YdmProductDetailMapper.class).doDelete(ypdQ.getCoilno(), null);
			// 删除if中间表等待冷轧系统重新推送：20240625刘侣
			this.getMapper(YdmProductDetailMapper.class).delif(ypdQ.getMaterailcoilno());
			this.getMapper(YdmProductDetailMapper.class).deliflz(ypdQ.getMaterailcoilno());
		}
		rm.setState("200");
		rm.setMsgInfo("退卷成功");
		return rm;
	}
	
	/**
	 * 库存查询：封闭功能
	 * @param cp
	 * @return
	 * @throws Exception
	 */
	public ResultModel doCloseYdmProductDetail(CommonPage<YdmProductDetail> cp) throws Exception{
		ResultModel rm = new ResultModel();
		Date now = new Date();
		String opr_time = new SimpleDateFormat("yyyyMMddHHmmss").format(now); //操作时间
		String username = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		YdmProductDetail ypd = cp.getObject();
		YdmProductDetail y = this.getMapper(YdmProductDetailMapper.class).findById(ypd.getCoilno());
		if (y==null || !"0".equals(y.getState())) {
			throw new Exception("卷号【"+ypd.getCoilno()+"】不是在库状态,无法封闭");
		}
		ypd.setState("7");//封闭
		this.getMapper(YdmProductDetailMapper.class).update(ypd);
		//日志
		MesOprLog mol = new MesOprLog("物流管理", "库存查询", "封闭", username, opr_time, ypd.getCoilno(), null, null, null, null);
		this.getMapper(MesOprLogMapper.class).insert(mol);
		rm.setState("200");
		rm.setMsgInfo("封闭成功");
		return rm;
	}
	
	/**
	 * 库存查询：解除封闭
	 * @param cp
	 * @return
	 * @throws Exception
	 */
	public ResultModel doCancelCloseYdmProductDetail(CommonPage<YdmProductDetail> cp) throws Exception{
		ResultModel rm = new ResultModel();
		Date now = new Date();
		String opr_time = new SimpleDateFormat("yyyyMMddHHmmss").format(now); //操作时间
		String username = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		YdmProductDetail ypd = cp.getObject();
		YdmProductDetail y = this.getMapper(YdmProductDetailMapper.class).findById(ypd.getCoilno());
		if (y==null || !"7".equals(y.getState())) {
			throw new Exception("卷号【"+ypd.getCoilno()+"】不是封闭状态,无法解除封闭");
		}
		ypd.setState("0");//在库
		this.getMapper(YdmProductDetailMapper.class).update(ypd);
		//日志
		MesOprLog mol = new MesOprLog("物流管理", "库存查询", "解除封闭", username, opr_time, ypd.getCoilno(), null, null, null, null);
		this.getMapper(MesOprLogMapper.class).insert(mol);
		rm.setState("200");
		rm.setMsgInfo("解除封闭成功");
		return rm;
	}
	
	/**
	 * 库存查询：可修改重量的
	 * */
	public ResultModel doQueryWChangeAble(CommonPage<YdmProductDetail> cp) {
		ResultModel rm=new ResultModel();
		int pageIndex=cp.getPageIndex();
		int pageSize=cp.getPageSize();
		PageModel<YdmProductDetail> pm = new PageModel<YdmProductDetail>(pageIndex,pageSize);
		try {
			YdmProductDetail yp=cp.getObject();
			String sWhere="1=1 and state in ('0', '7') ";
			if(cp.getStartTime()!=null&&cp.getStartTime()!="") {
				String starttime=cp.getStartTime();
				sWhere+=" and INDATE>=to_date(concat('"+starttime+"',' 00:00:00'),'yyyy-MM-dd HH24:MI:SS') ";
			}
			if(cp.getEndTime()!=null&&cp.getEndTime()!="") {
				String endtime=cp.getEndTime();
				sWhere+=" and INDATE<=to_date(concat('"+endtime+"',' 23:59:59'),'yyyy-MM-dd HH24:MI:SS') ";
			}
			if(cp.getObject().getCoilno()!=null&&cp.getObject().getCoilno()!="")
			{
				String coilno=cp.getObject().getCoilno();
				sWhere+=" and COILNO like '%"+coilno+"%'";
			}
			if(cp.getObject().getJudgesteelcode()!=null&&cp.getObject().getJudgesteelcode()!="")
			{
				String judgesteelcodeString=cp.getObject().getJudgesteelcode();
				sWhere+=" and judgesteelcode like '%"+judgesteelcodeString+"%'";
			}

			if(cp.getObject().getMaterailcoilno()!=null&&cp.getObject().getMaterailcoilno()!="")
			{
				String MaterailcoilnoString=cp.getObject().getMaterailcoilno();
				sWhere+=" and materailcoilno like '%"+MaterailcoilnoString+"%'";
			}

			if(cp.getMemo1()!=null&&cp.getMemo1()!="")
			{
				double hmin=Double.valueOf(cp.getMemo1()).doubleValue();
				sWhere+=" and THICK>="+hmin;
			}
			if(cp.getMemo2()!=null&&cp.getMemo2()!="")
			{
				double hmax=Double.valueOf(cp.getMemo2()).doubleValue();
				sWhere+=" and THICK<="+hmax;
			}
			if(cp.getMemo3()!=null&&cp.getMemo3()!="")
			{
				double wmin=Double.valueOf(cp.getMemo3()).doubleValue();
				sWhere+=" and WIDTH>="+wmin;
			}
			if(cp.getMemo4()!=null&&cp.getMemo4()!="")
			{
				double wmax=Double.valueOf(cp.getMemo4()).doubleValue();
				sWhere+=" and WIDTH<="+wmax;
			}
			pm.setRows(this.getMapper(YdmProductDetailMapper.class).findWithCondition(yp, sWhere, "INDATE", (pageIndex-1)*pageSize, pageSize));
			pm.setTotal(this.getMapper(YdmProductDetailMapper.class).countWithCondition(yp,sWhere));
			rm.setState("200");
			rm.setData(pm);
		}
		catch(Exception ex)
		{
			rm.setState("500");
			rm.setMsgInfo("查询失败!");
			ex.printStackTrace();
		}
		return rm;
	}
	
	/**
	 * 修改库存重量：
	 * */
	public ResultModel wChange(CommonPage<YdmProductDetail> cp) {
		ResultModel rm=new ResultModel();
		String userName=(String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		String userId = (String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
		String opr_time = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()); //操作时间
		Date now = new Date();
		int pageIndex=cp.getPageIndex();
		int pageSize=cp.getPageSize();
		PageModel<YdmProductDetail> pm = new PageModel<YdmProductDetail>(pageIndex,pageSize);
		try {
			// 参数：memo1为新重量；
			// 库存
			YdmProductDetail ypd = cp.getObject();
			List<YdmProductDetail> ypdL =  this.getMapper(YdmProductDetailMapper.class).findWithCondition(null, " COILNO = '"+ypd.getCoilno()+"'", null, 0, 0);
			YdmProductDetail ypd1 = ypdL.get(0);
			
			// 检测该卷是否做过一次结算，如果做过一次结算则不允许修改重量，必须先撤回一次结算提单：20240415刘侣
			int isJsnum = this.getMapper(SlmLadingBillHeatMapper.class).isJSNum(ypd.getCoilno());
			if (isJsnum > 0) {
				throw new Exception("卷：" + ypd.getCoilno() + " 做过一次结算不允许修改重量,必须先撤回一次结算提单");
			}
			
			Double prevWeight = ypd1.getFactweight();
			Double newWeight = Double.parseDouble(cp.getMemo1());
			ypd1.setTheoryweight(newWeight);
			ypd1.setFactweight(newWeight);
			this.getMapper(YdmProductDetailMapper.class).update(ypd1);
			this.getMapper(YdmProductDetailMapper.class).kctbYdd(ypd1.getCoilno());
			
			// 入库记录：如果有的话；
			List<YdmProductInlist> yil=this.getMapper(YdmProductInlistMapper.class).findWithCondition(null, " COILNO = '"+ypd.getCoilno()+"'", null, 0, 0);
			if (yil != null && yil.size() > 0) {
				for(YdmProductInlist yi : yil) {
					// 去掉理重修改：20230404 刘侣
//					yi.setTheoryweight(newWeight);
					yi.setFactweight(newWeight);
					this.getMapper(YdmProductInlistMapper.class).update(yi);
				}
			}
			
			// 出库记录：如果有的话；
			List<YdmProductOutlist> yol=this.getMapper(YdmProductOutlistMapper.class).findWithCondition(null, " COILNO = '"+ypd.getCoilno()+"'", null, 0, 0);
			if (yol != null && yol.size() > 0) {
				for(YdmProductOutlist yo : yol) {
//					yo.setTheoryweight(newWeight);
					yo.setFactweight(newWeight);
					this.getMapper(YdmProductOutlistMapper.class).update(yo);
				}
			}
			
			// 修改各个机组的产出实绩表：
			String machine = ypd1.getCoilno().substring(ypd1.getCoilno().length() - 5, ypd1.getCoilno().length() - 4);
			if ("F".equals(machine)) {
				MesCrSlittingD d=new MesCrSlittingD();
				d.setCoil_no(ypd1.getCoilno());
				List<MesCrSlittingD> mcds=this.getMapper(MesCrSlittingDMapper.class).findWithCondition(d, null, " coil_no ", 0, 0);
				MesCrSlittingD mcd = mcds.get(0);
//				mcd.setCoil_thr_wgt(newWeight);
				mcd.setCoil_act_wgt(newWeight);
				this.getMapper(MesCrSlittingDMapper.class).update(mcd);
			} else if ("T".equals(machine)) {
//				List<MesCrAnnealM> v_rec_anl_m = this.getMapper(MesCrAnnealMMapper.class).
				MesCrAnnealM d=new MesCrAnnealM();
				d.setCoil_no(ypd1.getCoilno());
				List<MesCrAnnealM> mads=this.getMapper(MesCrAnnealMMapper.class).findWithCondition(d, null, " coil_no ", 0, 0);
				MesCrAnnealM mad = mads.get(0);
//				mad.setCoil_thr_wgt(newWeight);
				mad.setCoil_act_wgt(newWeight);
				this.getMapper(MesCrAnnealMMapper.class).update(mad);
			} else if ("P".equals(machine)) {
				// MesTpmD  MesTpmDMapper
				MesTpmD d=new MesTpmD();
				d.setCoil_no(ypd1.getCoilno());
				List<MesTpmD> mtds=this.getMapper(MesTpmDMapper.class).findWithCondition(d, null, " coil_no ", 0, 0);
				MesTpmD mtd = mtds.get(0);
//				mtd.setCoil_thr_wgt(newWeight);
				mtd.setCoil_act_wgt(newWeight);
				this.getMapper(MesTpmDMapper.class).update(mtd);
			} else if ("L".equals(machine)) {
				// MesRcmD  MesRcmDMapper
				MesRcmD d=new MesRcmD();
				d.setCoil_no(ypd1.getCoilno());
				List<MesRcmD> mrds=this.getMapper(MesRcmDMapper.class).findWithCondition(d, null, " coil_no ", 0, 0);
				MesRcmD mrd = mrds.get(0);
//				mrd.setCoil_thr_wgt(newWeight);
				mrd.setCoil_act_wgt(newWeight);
				this.getMapper(MesRcmDMapper.class).update(mrd);
			} else if ("S".equals(machine)) {
				// MesPlmD  MesPlmDMapper
				MesPlmD d=new MesPlmD();
				d.setCoil_no(ypd1.getCoilno());
				List<MesPlmD> mpds=this.getMapper(MesPlmDMapper.class).findWithCondition(d, null, " coil_no ", 0, 0);
				MesPlmD mpd = mpds.get(0);
//				mpd.setCoil_thr_wgt(newWeight);
				mpd.setCoil_act_wgt(newWeight);
				this.getMapper(MesPlmDMapper.class).update(mpd);
			}
			
			// 重量改变，需要抛送异动点：20230918刘侣
			String isOpen = this.getMapper(ComBaseInfoMapper.class).findById("9512").getMemo();
			if ("1".equals(isOpen)) {
				// 记录成本异动点：20230811刘侣
				ComBaseCbYdd cd = new ComBaseCbYdd();
				cd.setCoilStus("0"); // 综判前算半成品，综判入库后算成品
				cd.setInstDisp("01");
				cd.setDataTypeIdx("O");
				// 当前产线：
				String currentMachine = ypd1.getCoilno().contains("S") ? "YTSX" : ypd1.getCoilno().contains("L") ? "YTZZ" : ypd1.getCoilno().contains("P") ? "YTPZ" : ypd1.getCoilno().contains("T") ? "YTTH" : ypd1.getCoilno().contains("K") ? "YTHJ" : ypd1.getCoilno().contains("F") ? "YTZJ" : "YT1";
				cd.setLocNo(currentMachine); // 填当前产线：优特酸选 YTSX,优特轧制YTZZ,优特平整YTPZ,优特退火YTTH,优特横剪YTHJ,优特纵剪YTZJ
				cd.setInvQtyUnit("块");
				cd.setInvWidth(ypd1.getWidth() != null ? String.valueOf(ypd1.getWidth()) : "0");
				cd.setCreateId(userId);
				cd.setActivityId("11A");
				cd.setInvLength(ypd1.getLength() != null ? String.valueOf(ypd1.getLength()) : "0");
				cd.setManageip(currentMachine); // 填当前产线：优特酸选 YTSX,优特轧制YTZZ,优特平整YTPZ,优特退火YTTH,优特横剪YTHJ,优特纵剪YTZJ
				cd.setProdName(ypd1.getProd_name());
				cd.setInvThick(ypd1.getThick() != null ? String.valueOf(ypd1.getThick()) : "0");
				
				String prevcoil = this.getBean(YdmProductInlistService.class).doQueryTl(ypd1.getCoilno());
				// 上道产线：
				String prevMachine = prevcoil.contains("S") ? "YTSX" : prevcoil.contains("L") ? "YTZZ" : prevcoil.contains("P") ? "YTPZ" : prevcoil.contains("T") ? "YTTH" : prevcoil.contains("K") ? "YTHJ" : prevcoil.contains("F") ? "YTZJ" : "YT1";
				cd.setRefnoA("YT1".equals(prevMachine) ? ypd1.getMaterailcoilno() : prevcoil); // 投料卷号：如果是第一道工序就拿卷板号，如果不是就拿优特钢上道工序卷号；
				cd.setSysId("IA");
				cd.setOrderItem(ypd1.getOrderno());
				cd.setCrntMill(currentMachine); // 填当前产线：优特酸选 YTSX,优特轧制YTZZ,优特平整YTPZ,优特退火YTTH,优特横剪YTHJ,优特纵剪YTZJ
				cd.setBeTransUnit("t");
				cd.setExecJobFunc("N");
				ComBaseSteel cbs = this.getMapper(ComBaseSteelMapper.class).findById(ypd1.getSteelcode());
				if (cbs != null) {
					cd.setAlloyNo(cbs.getSteel_name()); // 牌号
				}
				
				cd.setAcctDate(new SimpleDateFormat("yyyyMMdd").format(now));
				cd.setLastMill(prevMachine); // 填上道工序产线：优特酸选 YTSX,优特轧制YTZZ,优特平整YTPZ,优特退火YTTH,优特横剪YTHJ,优特纵剪YTZJ
				cd.setBeTransWgt(ypd1.getFactweight() != null ? ypd1.getFactweight() / 1000 : ypd1.getFactweight());
				cd.setProdTypeNo("CTR");
				cd.setAppId("优特钢产出卷入库");
				cd.setInvId(ypd1.getCoilno());
				cd.setInvQty("1");
				cd.setSrcFac("1");
				cd.setGrade("40740601".equals(ypd1.getUp_surface_result()) ? "1" : "1"); // 正品　1　协议　3　次品　5
				cd.setProdClassNo("H");
				
				// 产出分为：1、，2、酸洗重量调整（如果有重量改了也抛送一条）
				// 存入表：1、酸洗产出（11，生产完成）
				String currentMName = ypd1.getCoilno().contains("S") ? "酸洗" : ypd1.getCoilno().contains("L") ? "冷轧" : ypd1.getCoilno().contains("P") ? "平整" : ypd1.getCoilno().contains("T") ? "退火" : ypd1.getCoilno().contains("K") ? "横剪" : ypd1.getCoilno().contains("F") ? "分条" : "原料";
				cd.setActivityId("11A");
				cd.setExecJobFunc("N");
				cd.setDataTypeIdx("O");
				cd.setProdTypeNo("CTR");
				cd.setCoilStus("0"); // 综判前算半成品0，综判入库后算成品1
				cd.setCrntMill(currentMachine); // 填上道产线：优特酸选 YTSX,优特轧制YTZZ,优特平整YTPZ,优特退火YTTH,优特横剪YTHJ,优特纵剪YTZJ
				cd.setManageip(currentMachine); // 填上道产线：优特酸选 YTSX,优特轧制YTZZ,优特平整YTPZ,优特退火YTTH,优特横剪YTHJ,优特纵剪YTZJ
				cd.setLocNo(currentMachine); // 填上道产线：优特酸选 YTSX,优特轧制YTZZ,优特平整YTPZ,优特退火YTTH,优特横剪YTHJ,优特纵剪YTZJ
//				String prevMName = prevcoil.contains("S") ? "酸洗" : prevcoil.contains("L") ? "冷轧" : prevcoil.contains("P") ? "平整" : prevcoil.contains("T") ? "退火" : prevcoil.contains("K") ? "开平" : prevcoil.contains("F") ? "分条" : "原料";
				cd.setAppId("优特" + currentMName + "产出");
//				this.getBean(SlmFundFirstDetailService.class).doCbDataIns(cd, cd.getActivityId());

				// setSrcFac根据卷的物料属性去判断：20231031刘侣
				cd.setSrcFac("507101".equals(ypd1.getObject_attr()) ? "1" : "3");
				
//				// 二抛：酸洗重量调整（如果有重量改了也抛送一条）：接收原料和上料之间不存在重量调整，所以不需要：20230913刘侣：还是需要这个节点的：20230918刘侣
				cd.setActivityId("11AA");
				cd.setExecJobFunc("N");
				cd.setDataTypeIdx("O");
				cd.setProdTypeNo("CTR");
				cd.setCoilStus("0"); // 综判前算半成品0，综判入库后算成品1
				cd.setCrntMill(currentMachine); // 填上道产线：优特酸选 YTSX,优特轧制YTZZ,优特平整YTPZ,优特退火YTTH,优特横剪YTHJ,优特纵剪YTZJ
				cd.setManageip(currentMachine); // 填上道产线：优特酸选 YTSX,优特轧制YTZZ,优特平整YTPZ,优特退火YTTH,优特横剪YTHJ,优特纵剪YTZJ
				cd.setLocNo(currentMachine); // 填上道产线：优特酸选 YTSX,优特轧制YTZZ,优特平整YTPZ,优特退火YTTH,优特横剪YTHJ,优特纵剪YTZJ
				cd.setAppId("优特" + currentMName + "重量调整");
				this.getBean(SlmFundFirstDetailService.class).doCbDataIns(cd, cd.getActivityId(), ypd1.getMaterailcoilno());
				
				// end
			}
			
			MesOprLog mol = new MesOprLog("车间管理", "自行修改重量", "修改重量", userName, opr_time, ypd.getCoilno(), null, null, null, ypd.getCoilno() + "，修改前重量" + prevWeight + "，修改后重量：" + newWeight);
			this.getMapper(MesOprLogMapper.class).insert(mol);
			
			rm.setState("200");
			rm.setMsgInfo("重量修改成功");
		}
		catch(Exception ex)
		{
			rm.setState("500");
			rm.setMsgInfo("修改重量失败!" + ex.getMessage());
			ex.printStackTrace();
		}
		return rm;
	}
	
	/**
	 * 库存查询：可修改规格的
	 * */
	public ResultModel querySpecChangeAble(CommonPage<YdmProductDetail> cp) {
		ResultModel rm=new ResultModel();
		int pageIndex=cp.getPageIndex();
		int pageSize=cp.getPageSize();
		PageModel<YdmProductDetail> pm = new PageModel<YdmProductDetail>(pageIndex,pageSize);
		try {
			YdmProductDetail yp=cp.getObject();
			String sWhere="1=1 and state not in ('2', '7') ";
			if(cp.getStartTime()!=null&&cp.getStartTime()!="") {
				String starttime=cp.getStartTime();
				sWhere+=" and INDATE>=to_date(concat('"+starttime+"',' 00:00:00'),'yyyy-MM-dd HH24:MI:SS') ";
			}
			if(cp.getEndTime()!=null&&cp.getEndTime()!="") {
				String endtime=cp.getEndTime();
				sWhere+=" and INDATE<=to_date(concat('"+endtime+"',' 23:59:59'),'yyyy-MM-dd HH24:MI:SS') ";
			}
			if(cp.getObject().getCoilno()!=null&&cp.getObject().getCoilno()!="")
			{
				String coilno=cp.getObject().getCoilno();
				sWhere+=" and COILNO like '%"+coilno+"%'";
			} else {
				throw new Exception("请传入卷号");
			}

			pm.setRows(this.getMapper(YdmProductDetailMapper.class).findWithCondition(null, sWhere, "INDATE", (pageIndex-1)*pageSize, pageSize));
			pm.setTotal(this.getMapper(YdmProductDetailMapper.class).countWithCondition(null,sWhere));
			rm.setState("200");
			rm.setData(pm);
		}
		catch(Exception ex)
		{
			rm.setState("500");
			rm.setMsgInfo("查询失败: " + ex.getMessage());
			ex.printStackTrace();
		}
		return rm;
	}

	
	/**
	 * 修改库存规格：
	 * */
	public ResultModel specChange(CommonPage<YdmProductDetail> cp) {
		ResultModel rm=new ResultModel();
		String userName=(String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		String opr_time = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()); //操作时间

		try {
			// 参数：memo1为新重量；
			// 库存
			YdmProductDetail ypd = cp.getObject();
			List<YdmProductDetail> ypdL =  this.getMapper(YdmProductDetailMapper.class).findWithCondition(null, " COILNO = '"+ypd.getCoilno()+"'", null, 0, 0);
			YdmProductDetail ypdQ = ypdL.get(0);
			// 存入旧规格：
			String prevSpec = ypdQ.getThick() + " * " + ypdQ.getWidth();
			// 写入新规格：
			ypdQ.setThick(ypd.getThick());
			ypdQ.setWidth(ypd.getWidth());
			
			String newSpec = ypd.getThick() + " * " + ypd.getWidth();
			
			this.getMapper(YdmProductDetailMapper.class).update(ypdQ);
			this.getMapper(YdmProductDetailMapper.class).kctbYdd(ypdQ.getCoilno());
			
			// 入库记录：如果有的话；
			List<YdmProductInlist> yil=this.getMapper(YdmProductInlistMapper.class).findWithCondition(null, " COILNO = '"+ypd.getCoilno()+"'", null, 0, 0);
			if (yil != null && yil.size() > 0) {
				for(YdmProductInlist yi : yil) {
					// 写入新规格：
					yi.setThick(ypd.getThick());
					yi.setWidth(ypd.getWidth());
					this.getMapper(YdmProductInlistMapper.class).update(yi);
				}
			}
			
			// 出库记录：如果有的话；
			List<YdmProductOutlist> yol=this.getMapper(YdmProductOutlistMapper.class).findWithCondition(null, " COILNO = '"+ypd.getCoilno()+"'", null, 0, 0);
			if (yol != null && yol.size() > 0) {
				for(YdmProductOutlist yo : yol) {
					// 写入新规格：
					yo.setThick(ypd.getThick());
					yo.setWidth(ypd.getWidth());
					this.getMapper(YdmProductOutlistMapper.class).update(yo);
				}
			}
			
			// 修改各个机组的产出实绩表：
			String machine = ypdQ.getCoilno().substring(ypdQ.getCoilno().length() - 5, ypdQ.getCoilno().length() - 4);
			if ("F".equals(machine)) {
				MesCrSlittingD d=new MesCrSlittingD();
				d.setCoil_no(ypdQ.getCoilno());
				List<MesCrSlittingD> mcds=this.getMapper(MesCrSlittingDMapper.class).findWithCondition(d, null, " coil_no ", 0, 0);
				MesCrSlittingD mcd = mcds.get(0);
				mcd.setAct_thk(ypd.getThick());
				mcd.setAct_wdt(ypd.getWidth());
				this.getMapper(MesCrSlittingDMapper.class).update(mcd);
			} else if ("T".equals(machine)) {

				MesCrAnnealM d=new MesCrAnnealM();
				d.setCoil_no(ypdQ.getCoilno());
				List<MesCrAnnealM> mads=this.getMapper(MesCrAnnealMMapper.class).findWithCondition(d, null, " coil_no ", 0, 0);
				MesCrAnnealM mad = mads.get(0);
				mad.setAct_thk(ypd.getThick());
				mad.setAct_wdt(ypd.getWidth());
				this.getMapper(MesCrAnnealMMapper.class).update(mad);
			} else if ("P".equals(machine)) {
				// MesTpmD  MesTpmDMapper
				MesTpmD d=new MesTpmD();
				d.setCoil_no(ypdQ.getCoilno());
				List<MesTpmD> mtds=this.getMapper(MesTpmDMapper.class).findWithCondition(d, null, " coil_no ", 0, 0);
				MesTpmD mtd = mtds.get(0);
				mtd.setAct_thk(ypd.getThick());
				mtd.setAct_wdt(ypd.getWidth());
				this.getMapper(MesTpmDMapper.class).update(mtd);
			} else if ("L".equals(machine)) {
				// MesRcmD  MesRcmDMapper
				MesRcmD d=new MesRcmD();
				d.setCoil_no(ypdQ.getCoilno());
				List<MesRcmD> mrds=this.getMapper(MesRcmDMapper.class).findWithCondition(d, null, " coil_no ", 0, 0);
				MesRcmD mrd = mrds.get(0);
				mrd.setAct_thk(ypd.getThick());
				mrd.setAct_wdt(ypd.getWidth());
				this.getMapper(MesRcmDMapper.class).update(mrd);
			} else if ("S".equals(machine)) {
				// MesPlmD  MesPlmDMapper
				MesPlmD d=new MesPlmD();
				d.setCoil_no(ypdQ.getCoilno());
				List<MesPlmD> mpds=this.getMapper(MesPlmDMapper.class).findWithCondition(d, null, " coil_no ", 0, 0);
				MesPlmD mpd = mpds.get(0);
				mpd.setAct_thk(ypd.getThick());
				mpd.setAct_wdt(ypd.getWidth());
				this.getMapper(MesPlmDMapper.class).update(mpd);
			}
			
			MesOprLog mol = new MesOprLog("车间管理", "自行修改规格", "修改规格", userName, opr_time, ypd.getCoilno(), null, null, null, ypd.getCoilno() + "，修改前规格" + prevSpec + "，修改后规格：" + newSpec);
			this.getMapper(MesOprLogMapper.class).insert(mol);
			
			rm.setState("200");
			rm.setMsgInfo("规格修改成功");
		}
		catch(Exception ex)
		{
			rm.setState("500");
			rm.setMsgInfo("修改规格失败!" + ex.getMessage());
			ex.printStackTrace();
		}
		return rm;
	}
	
	
	/**
     * 查询多个卷垛位
     * @param 20230117 刘侣
     * @return
     */
    public ResultModel doQueryCButtress(YdmProductDetail ypd){
    	ResultModel rm = new ResultModel();
    	String coilStr = ypd.getCoilno();
    	String[] split = coilStr.split(",");
    	List<String> coils = Arrays.asList(split);
    	List<HashMap> result = this.getMapper(YdmProductDetailMapper.class).doQueryCButtress(coils);
    	rm.setData(result);
		rm.setState("200");
		
    	return rm;
   }
    
    
    /**
     * 查询可发明细
     * @param 20230210 刘侣
     * @return
     */
    public ResultModel doQueryKF(){
    	ResultModel rm = new ResultModel();
    	List<HashMap> result = this.getMapper(YdmProductDetailMapper.class).doQueryKF();
    	rm.setData(result);
		rm.setState("200");
		
    	return rm;
   }
    
    
}
