/***文档注释***********************************************
 * 作者               :                   龙文航
 * 创建日期      :                   2017.11.22
 * 描述               :                   仓库管理-原料库在制品库成品库管理-出库查询
 * 注意事项      :                   无
 * 遗留BUG :                   无
 * 修改日期      :                   
 * 修改人员      :                   
 * 修改内容      :                   
***********************************************************/
package market.Lms.Warehouse.impl;

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

import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Lms.Warehouse.mapper.YdmProductOutlistMapper;
import market.Lms.Warehouse.service.YdmProductOutlistService;
import market.Lms.Warehouse.vo.YdmProductFind;
import market.Lms.Warehouse.vo.YdmProductOutlist;
import market.Mes.Opr.mapper.MesOprLogMapper;
import market.Mes.Opr.vo.MesOprLog;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
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")
@RemoteService(path="YdmProductOutlistService")
public class YdmProductOutlistServiceImpl extends BaseServiceImpl<YdmProductOutlist,Object> implements YdmProductOutlistService {

	/**
	 * 出库查询
	 * memo1-4分别传厚与宽的范围
	 * */
	public ResultModel doQueryWeb(CommonPage<YdmProductOutlist> cp) {
		ResultModel rm=new ResultModel();
		int pageIndex=cp.getPageIndex();
		int pageSize=cp.getPageSize();
		PageModel<YdmProductOutlist> pm = new PageModel<YdmProductOutlist>(pageIndex,pageSize);
		try {
			YdmProductOutlist yp=cp.getObject();
			String sWhere="1=1";
			if(cp.getStartTime()!=null&&cp.getStartTime()!="") {
				String starttime=cp.getStartTime();
				sWhere=" and OUTDATE>=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 OUTDATE<=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.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(YdmProductOutlistMapper.class).findWithCondition(yp, sWhere, "OUTBILLNO", (pageIndex-1)*pageSize, pageSize));
			pm.setTotal(this.getMapper(YdmProductOutlistMapper.class).countWithCondition(yp,sWhere));
			rm.setState("200");
			rm.setData(pm);
		}
		catch(Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("查询失败!");
			ex.printStackTrace();
		}
		return rm;
	}
	
	/**
	 * 出库查询
	 * SQL实现
	 * 条件+汇总+分页
	 * */
	public ResultModel doQueryWebBySQL(CommonPage<YdmProductOutlist> cp) {
		ResultModel rm=new ResultModel();
		int pageSize=cp.getPageSize();
		int pageIndex=cp.getPageIndex();
		PageModel<YdmProductOutlist> pm = new PageModel<YdmProductOutlist>(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(YdmProductOutlistMapper.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().getMaterailcoilno(),
							cp.getObject().getCustomer_no(),cp.getObject().getOrderno(),cp.getObject().getObject_attr(),cp.getObject().getMaterailtype(),cp.getObject()));
			pm.setTotal(this.getMapper(YdmProductOutlistMapper.class).doQueryWebBySQLCount
					(cp.getStartTime(), cp.getEndTime(), cp.getObject().getCoilno(), cp.getObject().getJudgesteelcode(),hmin,hmax,wmin,wmax,cp.getObject().getStorageattr(),cp.getObject().getMaterailcoilno(),
							cp.getObject().getCustomer_no(),cp.getObject().getOrderno(),cp.getObject().getObject_attr(),cp.getObject().getMaterailtype(),cp.getObject()));
			rm.setData(pm);
			rm.setState("200");
		}
		catch(Exception ex)
		{
			rm.setState("500");
			rm.setMsgInfo("查询失败!");
			ex.printStackTrace();
		}
		return rm;
	}
	
	/**
	 * 生成主键
	 * @return
	 * @throws Exception 
	 */
	public String createOUTBILLNO() throws Exception {
		try {
			String outbillno="";
			List<HashMap> list = this.getMapper(YdmProductOutlistMapper.class).getNo("OT"+(new SimpleDateFormat("yyyyMMdd").format(new Date())));
			outbillno = (String) list.get(0).get("MAXNO");
			return outbillno;
		} 
		catch (Exception ex) {
			throw (ex);
		}
	}
	
	
	/**
	 * 經銷存查询
	 * SQL实现
	 * 条件+汇总+分页
	 * */
	public ResultModel doQueryWebBySQLJXC(CommonPage<YdmProductOutlist> cp) {
		ResultModel rm=new ResultModel();
		int pageSize=cp.getPageSize();
		int pageIndex=cp.getPageIndex();
		PageModel<YdmProductFind> pm = new PageModel<YdmProductFind>(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(YdmProductOutlistMapper.class).doQueryWebBySQLJXC
					((pageIndex-1)*pageSize, (pageIndex-1)*pageSize+pageSize, cp.getStartTime(), cp.getEndTime(), cp.getObject().getCoilno(), cp.getObject().getJudgesteelcode(),hmin,hmax,wmin,wmax,cp.getObject().getStorageattr()));
			pm.setTotal(this.getMapper(YdmProductOutlistMapper.class).doQueryWebBySQLJXC
					(1, 99999, cp.getStartTime(), cp.getEndTime(), cp.getObject().getCoilno(), cp.getObject().getJudgesteelcode(),hmin,hmax,wmin,wmax,cp.getObject().getStorageattr()).size()+1);
			rm.setData(pm);
			rm.setState("200");
		}
		catch(Exception ex) {
			rm.setState("500");
			rm.setMsgInfo("查询失败!");
			ex.printStackTrace();
		}
		return rm;
	}
	
	/**
	 * 调整出库时间
	 * @param cp  [{"objectList":[{"outbillno":"出库单号"},{"outbillno":"OT2020040800021"}],"memo1":"2020-06-29 13:45:47"}]
	 * @return
	 * @throws Exception
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doChangeOutDate(CommonPage<YdmProductOutlist> 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);
		List<YdmProductOutlist> ydoList = cp.getObjectList();
		if (CollectionUtils.isEmpty(ydoList)) {
			throw new Exception("请传入参数");
		}
		if (StringUtils.isEmpty(cp.getMemo1())) {
			throw new Exception("请传入出库时间");
		}
		Date outdate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(cp.getMemo1());
		for (YdmProductOutlist ypo : ydoList) {
			YdmProductOutlist y = new YdmProductOutlist();
			y.setOutbillno(ypo.getOutbillno());
			y.setOutdate(outdate);
			this.getMapper(YdmProductOutlistMapper.class).update(y);	
			
			MesOprLog mol = new MesOprLog("物流管理", "出库查询", "调整出库时间", username, opr_time, null, null, null, null, "出库单号："+ypo.getOutbillno());
			this.getMapper(MesOprLogMapper.class).insert(mol);
		}
		rm.setState("200");
		rm.setMsgInfo("调整入库时间成功");
		return rm;
	}
	
}
