/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2019-11-11
 * 描述            ：包装实绩管理表
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
***********************************************************/
package market.Mes.Packing.Result.impl;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Lms.Warehouse.mapper.YdmProductDetailMapper;
import market.Lms.Warehouse.vo.YdmProductDetail;
import market.Mes.Packing.Result.mapper.MesPackResultMapper;
import market.Mes.Packing.Result.service.MesPackResultService;
import market.Mes.Packing.Result.vo.MesPackResult;

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;

@RemoteService(path="MesPackResultService")
public class MesPackResultServiceImpl extends BaseServiceImpl<MesPackResult,Object> implements MesPackResultService {

	/**
	 * 查询待包装的信息
	 * @param cp
	 * @return
	 * @throws Exception
	 */
	public ResultModel doQueryMesPackResultNo(CommonPage<MesPackResult> cp) throws Exception{
		ResultModel rm  = new ResultModel();
		MesPackResult mpr = cp.getObject();
		int pageIndex=cp.getPageIndex();
		int pageSize=cp.getPageSize();
		PageModel<MesPackResult> pm = new PageModel<MesPackResult>(pageIndex,pageSize);
		try {
			String swhere = "";
			if (mpr.getMat_coil_no()!=null && !"".equals(mpr.getMat_coil_no())) {
				swhere += " and ypd.materailcoilno like '%"+mpr.getMat_coil_no()+"%'";//原料卷号
			}
			if (mpr.getMat_coil_no()!=null && !"".equals(mpr.getMat_coil_no())) {
				swhere += " and ypd.materailcoilno like '%"+mpr.getMat_coil_no()+"%'";
			}
			if (cp.getStartTime()!=null && !"".equals(cp.getStartTime())) {
				swhere += " and ypd.producedate >= to_date('"+cp.getStartTime()+" 00:00:00','yyyy-MM-dd HH24:mi:ss')";
			}
			if (cp.getEndTime()!=null && !"".equals(cp.getEndTime())) {
				swhere += " and ypd.producedate <= to_date('"+cp.getEndTime()+" 23:59:59','yyyy-MM-dd HH24:mi:ss')";
			}
			if (mpr.getMachine_code()!=null && !"".equals(mpr.getMachine_code())) {
				swhere += " and ypd.machine_code = '"+mpr.getMachine_code()+"'";
			}
			if (mpr.getCoil_no()!=null && !"".equals(mpr.getCoil_no())) {
				swhere += " and ypd.coilno like '%"+mpr.getCoil_no()+"%'";//子卷号
			}
			
			
			if (cp.getMemo1()!=null && "1".equals(cp.getMemo1())) {
				// 酸洗--冷轧 --退火 --平整  --轧机 --分条 --开平
				//
			}else {
				swhere += " and (ypd.is_package is null or ypd.is_package = 0)"
						+ " and substr(ypd.coilno,-1,1) not in ('B','H','F')"
//						+ " and ((ypd.machine_code = 'M0010' and (soi.process like '%2分条%' or soi.process like '%2退火%' or soi.process like '%酸洗'))"
//						+ " or (ypd.machine_code = 'M0007')"
//						+ " or (ypd.machine_code in ('M0001','M0002') and soi.pk_ask_code != '201101' and soi.process like '%分条')"
//						+ " or (ypd.machine_code in ('M0003') and soi.pk_ask_code != '201101' and soi.process like '%开平')"
//						+ " or (ypd.machine_code in ('M0008') and soi.pk_ask_code != '201101' and soi.process like '%冷轧')"
//						+ " or (ypd.machine_name like '%退火%' and soi.process not like '%分条'))"
						;				
			}
			List<MesPackResult> list = this.getMapper(MesPackResultMapper.class).doQueryMesPackResultNo(swhere,(pageIndex-1)*pageSize, pageSize*pageIndex);
			pm.setRows(list);
			int count = this.getMapper(MesPackResultMapper.class).doQueryMesPackResultNoCount(swhere);
			pm.setTotal(count);
			rm.setData(pm);
			rm.setState("200");
			rm.setMsgInfo("查询待包装信息成功");
		} catch (Exception e) {
			throw new Exception("查询失败，请联系管理员");
		}
		return rm;
	}
	
	/**
	 * 查询已包装的数据
	 * @param cp
	 * @return
	 * @throws Exception
	 */
	public ResultModel doQueryMesPackResultYes(CommonPage<MesPackResult> cp) throws Exception{
		ResultModel rm  = new ResultModel();
		MesPackResult mpr = cp.getObject();
		int pageIndex=cp.getPageIndex();
		int pageSize=cp.getPageSize();
		PageModel<MesPackResult> pm = new PageModel<MesPackResult>(pageIndex,pageSize);
		try {
			String swhere = "1=1";
			if (mpr.getMat_coil_no()!=null && !"".equals(mpr.getMat_coil_no())) {
				swhere += " and t.mat_coil_no like '%"+mpr.getMat_coil_no()+"%'";
			}
			if (mpr.getCoil_no()!=null && !"".equals(mpr.getCoil_no())) {
				swhere += " and t.coil_no like '%"+mpr.getCoil_no()+"%'";
			}
			if (cp.getStartTime()!=null && !"".equals(cp.getStartTime())) {
				swhere += " and t.wrk_date >= '"+cp.getStartTime()+"'";
			}
			if (cp.getEndTime()!=null && !"".equals(cp.getEndTime())) {
				swhere += " and t.wrk_date <= '"+cp.getEndTime()+"'";
			}
			if (mpr.getMachine_code()!=null && !"".equals(mpr.getMachine_code())) {
				swhere += " and t.machine_code = '"+mpr.getMachine_code()+"'";
			}		
			
			String swhere2 = "1=1";
			if (mpr.getMat_coil_no()!=null && !"".equals(mpr.getMat_coil_no())) {
				swhere2 += " and mat_coil_no like '%"+mpr.getMat_coil_no()+"%'";
			}
			if (mpr.getCoil_no()!=null && !"".equals(mpr.getCoil_no())) {
				swhere2 += " and coil_no like '%"+mpr.getCoil_no()+"%'";
			}
			if (cp.getStartTime()!=null && !"".equals(cp.getStartTime())) {
				swhere2 += " and wrk_date >= '"+cp.getStartTime()+"'";
			}
			if (cp.getEndTime()!=null && !"".equals(cp.getEndTime())) {
				swhere2 += " and wrk_date <= '"+cp.getEndTime()+"'";
			}
			if (mpr.getMachine_code()!=null && !"".equals(mpr.getMachine_code())) {
				swhere2 += " and machine_code = '"+mpr.getMachine_code()+"'";
			}	
			
//			List<MesPackResult> list = this.getMapper(MesPackResultMapper.class).findWithCondition(null, swhere, "", (pageIndex-1)*pageSize, pageSize);
//			doQueryMesPacked
			// 新增已包装查询（带垛位位置）：20230130刘侣
			// 修复第二页起没有数据的bug：20240711刘侣
			List<MesPackResult> list = this.getMapper(MesPackResultMapper.class).doQueryMesPacked(swhere, (pageIndex-1)*pageSize, pageIndex*pageSize);
			pm.setRows(list);
			int count = this.getMapper(MesPackResultMapper.class).countWithCondition(null, swhere2);
			pm.setTotal(count);
			
			rm.setData(pm);
			rm.setState("200");
			rm.setMsgInfo("查询已包装信息成功");
		} catch (Exception e) {
			throw new Exception("查询失败，请联系管理员");
		}
		return rm;
	}
	
	/**
	 * 包装功能
	 * @param cp
	 * @return
	 * @throws Exception
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doInsertMesPackResult(CommonPage<MesPackResult> cp) throws Exception{
		ResultModel rm  = new ResultModel();
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		String work_grp = (String) this.getSession().getAttribute(HomeService.SESSION_WORK_GRP);
		String work_ord = (String) this.getSession().getAttribute(HomeService.SESSION_WORK_ORD);
		
		Date now = new Date();
		List<MesPackResult> list = cp.getObjectList();	
		String pk_rlt_code = cp.getMemo1();//实际包装
		String pk_rlt_desc = cp.getMemo2();
		if (list==null || list.size()<=0) {
			throw new Exception("请选择待包装数据");
		}
		for (MesPackResult m : list) {
			// 如果订单包装要求为裸包：则不允许包装：20230328 刘侣，有时候会存在实际包装了，所以需要放开：2023052刘侣
//			if ("201101".equals(m.getPk_ask_code())) {
//				throw new Exception("卷：" + m.getCoil_no() + " 订单包装要求为裸包，不需要包装");
//			}
			// end
			
			//判断是否已经存在数据
			MesPackResult isOk = this.getMapper(MesPackResultMapper.class).findById(m.getCoil_no());
			if (isOk!=null) {
				isOk.setPk_ask_code(m.getPk_ask_code());//包装要求
				isOk.setPk_ask_desc(m.getPk_ask_desc());
				isOk.setPk_rlt_code(pk_rlt_code);//实际包装
				isOk.setPk_rlt_desc(pk_rlt_desc);
				isOk.setMemo(m.getMemo());//备注
				isOk.setWrk_grp(work_grp);
				isOk.setWrk_ord(work_ord);
				isOk.setWrk_date(new SimpleDateFormat("yyyy-MM-dd").format(now));
				this.getMapper(MesPackResultMapper.class).update(isOk);
				//2.修改库存的包装状态
				YdmProductDetail newYpd = new YdmProductDetail();
				newYpd.setCoilno(m.getCoil_no());
				if ("201102".equals(pk_rlt_code) || "201103".equals(pk_rlt_code) || "201104".equals(pk_rlt_code)
						|| "201105".equals(pk_rlt_code)) {
					newYpd.setIs_package("1");
				}else {
					newYpd.setIs_package("0");
				}				
				//newYpd.setMemo((m.getMemo()==null?"":m.getMemo())+";包装作业调整");
				this.getMapper(YdmProductDetailMapper.class).update(newYpd);
				continue;
			}else{				
				String condition = " and ypd.coilno = '"+m.getCoil_no()+"'";
				List<MesPackResult> lists = this.getMapper(MesPackResultMapper.class).doQueryMesPackResultNo(condition, 0, 1);
				if (lists!=null && lists.size()>0) {
					MesPackResult MPR = lists.get(0);
					MesPackResult mpr = new MesPackResult();
					mpr.setCoil_no(MPR.getCoil_no());
					mpr.setMat_coil_no(MPR.getMat_coil_no());
					mpr.setOrder_no(MPR.getOrder_no());
					mpr.setWrk_date(MPR.getWrk_date());
					mpr.setWrk_grp(work_grp);
					mpr.setWrk_ord(work_ord);
					mpr.setSteel_code(MPR.getSteel_code());
					mpr.setSteel_name(MPR.getSteel_name());
					mpr.setAct_thk(MPR.getAct_thk());
					mpr.setAct_wdt(MPR.getAct_wdt());
					mpr.setAct_len(MPR.getAct_len());
					mpr.setAct_inn_dia(MPR.getAct_inn_dia());
					mpr.setAct_out_dia(MPR.getAct_out_dia());
					mpr.setCoil_act_wgt(MPR.getCoil_act_wgt());
					mpr.setCoil_thr_wgt(MPR.getCoil_thr_wgt());
					mpr.setPk_ask_code(MPR.getPk_ask_code());//包装要求
					mpr.setPk_ask_desc(MPR.getPk_ask_desc());
					mpr.setPk_rlt_code(pk_rlt_code);//实际包装
					mpr.setPk_rlt_desc(pk_rlt_desc);
					mpr.setMemo_xs(MPR.getMemo_xs());
					mpr.setMemo(m.getMemo());//备注
					mpr.setCreate_man(userName);
					mpr.setCreate_time(now);
					mpr.setMachine_code(MPR.getMachine_code());
					mpr.setMachine_name(MPR.getMachine_name());
					this.getMapper(MesPackResultMapper.class).insert(mpr);
					//2.修改库存的包装状态
					YdmProductDetail newYpd = new YdmProductDetail();
					newYpd.setCoilno(m.getCoil_no());
					if ("201102".equals(pk_rlt_code) || "201103".equals(pk_rlt_code) || "201104".equals(pk_rlt_code)
							|| "201105".equals(pk_rlt_code)) {
						newYpd.setIs_package("1");
					}else {
						newYpd.setIs_package("0");
					}
					this.getMapper(YdmProductDetailMapper.class).update(newYpd);
				}
			}
		}
		rm.setState("200");
		rm.setMsgInfo("包装调整成功");
		return rm;
	}
	
	/**
	 * 删除
	 * @param cp
	 * @return
	 * @throws Exception
	 */
	@Transactional(value = "tm", rollbackFor = Exception.class)
	public ResultModel doDeleteMesPackResult(CommonPage<MesPackResult> cp) throws Exception{
		ResultModel rm  = new ResultModel();
		List<MesPackResult> list = cp.getObjectList();		
		if (list==null || list.size()<=0) {
			throw new Exception("请选择待包装数据");
		}
		for (MesPackResult MPR : list) {
			//1.删除包装记录
			MesPackResult mpr = this.getMapper(MesPackResultMapper.class).findById(MPR.getCoil_no());
			if (mpr==null) {
				throw new Exception("子卷号【"+MPR.getCoil_no()+"】的包装实绩不存在");
			}			
			this.getMapper(MesPackResultMapper.class).delete(MPR.getCoil_no());
			//2.调整库存记录
			YdmProductDetail ypd = this.getMapper(YdmProductDetailMapper.class).doFind(MPR.getCoil_no(), null);
			if (ypd!=null) {
				if (!"0".equals(ypd.getCoilno())) {
					//rm.setState("500");
					//rm.setMsgInfo("子卷号【"+ypd.getCoilno()+"】的状态不是在库，无法删除包装");
					//return rm;
				}
				this.getMapper(MesPackResultMapper.class).doRemoveYpdState(ypd);
			}
		}
		rm.setState("200");
		rm.setMsgInfo("包装调整成功");
		return rm;
	}
	
	
	
}
