/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2019-03-15
 * 描述            ：平整机出口卷线上称重实绩
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
***********************************************************/
package market.Mes.Tpm.impl;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import market.Com.pb.DoubleUtil;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Mes.Pickle.Performance.mapper.MesPlmWgtMapper;
import market.Mes.Tpm.mapper.IfTpm01L2PdoMapper;
import market.Mes.Tpm.mapper.MesTpmDMapper;
import market.Mes.Tpm.mapper.MesTpmMMapper;
import market.Mes.Tpm.mapper.MesTpmWgtMapper;
import market.Mes.Tpm.service.MesTpmWgtService;
import market.Mes.Tpm.vo.IfTpm01L2Pdo;
import market.Mes.Tpm.vo.MesTpmD;
import market.Mes.Tpm.vo.MesTpmM;
import market.Mes.Tpm.vo.MesTpmWgt;

import org.apache.commons.collections.CollectionUtils;

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

@RemoteService(path="MesTpmWgtService")
public class MesTpmWgtServiceImpl extends BaseServiceImpl<MesTpmWgt,Object> implements MesTpmWgtService {
	
	/**
	 * 平整机重量的自动获取
	 * @param cp
	 * @return
	 */
	public ResultModel doUpdateMesTpmDInfo(CommonPage<MesTpmD> cp) {
		ResultModel rm = new ResultModel();
		Date now = new Date();
		MesTpmD send  = cp.getObject();
		MesTpmD mtd1 = this.getMapper(MesTpmDMapper.class).findById(send.getCoil_no());
        if (mtd1==null) {
			rm.setMsgInfo("子卷信息未找到");
			return rm;
		}
        //1. 判断是否手动保存过重量
        String condition = "coil_no = '"+mtd1.getCoil_no()+"'and wgt_source = '2'";
        int count = this.getMapper(MesTpmWgtMapper.class).countWithCondition(null, condition);
        if (count > 0) {
			rm.setState("500");
			rm.setMsgInfo("卷号【"+mtd1.getCoil_no()+"】存在手动保存的重量记录，无法自动更新重量信息，请在【平整称重作业】界面修改重量");
			return rm;
		}
        //2.判断物料是否交工（20200727 出现过自动改了重量，但是数据已经进库存的问题）
        condition = "schedule_no = '"+mtd1.getSchedule_no()+"' and material_no = '"+mtd1.getMaterial_no()+"' and hand_over_status = '1'";
        int count1 = this.getMapper(MesTpmMMapper.class).countWithCondition(null, condition);
        if (count1 > 0) {
			rm.setState("500");
			return rm;
		}
        
        //3.判断订单的计重方式
        String sql = "select count(1) from mes_tpm_d t "
        		+ " left join slm_order_info soi on t.order_no = soi.contract_no||soi.contract_line_seq||soi.order_seq and nvl(soi.change_flag,'0')<>'1' "
        		+ " where t.coil_no = '"+mtd1.getCoil_no()+"' and soi.weight_type_no= '102302'";
		int typeCount = this.getMapper(MesPlmWgtMapper.class).doQueryCoilWeightType(sql);
		//获取子卷最后一位
		String endExit = mtd1.getCoil_no().substring(mtd1.getCoil_no().length()-1);
		
        //4.获取平整二级的重量信息
        condition = "entry_coil_no = '"+mtd1.getMaterial_no()+"' and substr(exit_coil_no,-1,1) in ('0','"+endExit+"')";
        List<IfTpm01L2Pdo> ifTList = this.getMapper(IfTpm01L2PdoMapper.class).findWithCondition(null, condition, "msg_time desc", 0, 1);
        if (CollectionUtils.isNotEmpty(ifTList)) {
			for (IfTpm01L2Pdo ifT : ifTList) {
				mtd1.setCoil_act_wgt(ifT.getExit_weight().doubleValue());//出口卷实际重量
				//******条件1:毛重交货的大卷，条件2:不存在分卷。条件3: 20Kg以内：重量直接进行母卷重量覆盖，否则颜色警告******
				if (typeCount > 0) {//条件1：毛重交货
					//判断是否分卷
					condition = "schedule_no = '"+mtd1.getSchedule_no()+"' and material_no = '"+mtd1.getMaterial_no()+"'";
					int divideCount = this.getMapper(MesTpmDMapper.class).countWithCondition(null, condition);
					if (divideCount == 1) {//条件2：不分卷
						//和母卷重量在20Kg以内
						condition = "schedule_no = '"+mtd1.getSchedule_no()+"' and material_no = '"+mtd1.getMaterial_no()+"'";
						MesTpmM mtm = this.getMapper(MesTpmMMapper.class).findById(mtd1.getSchedule_no(),mtd1.getMaterial_no());
						if (mtm!=null && mtm.getMtr_act_wgt() !=null) {
							if (Math.abs(mtm.getMtr_act_wgt() - ifT.getExit_weight().doubleValue()) <= 20) {
								mtd1.setCoil_act_wgt(mtm.getMtr_act_wgt());//重量取母卷的重量
							}
						}
					}
				}
				mtd1.setCoil_wgt_time(ifT.getMsg_time());//出口卷消息时间
				this.getMapper(MesTpmDMapper.class).update(mtd1);  				
				//新增称重保存记录：需判断是否是在重复发送的数据
				condition = "coil_no = '"+mtd1.getCoil_no()+"' and wgt_value = "+ifT.getExit_weight()+".000"+" and wgt_source = '1'";
				int wgtCount = this.getMapper(MesTpmWgtMapper.class).countWithCondition(null, condition);
				if (wgtCount == 0) { //称重数据未被记录过					
					int icount=0;
					String dt=new SimpleDateFormat("yyMMdd").format(now);
					List<MesTpmWgt> mrwList=this.getMapper(MesTpmWgtMapper.class).findWithCondition(null, " substr(serial_no,1,6)='"+dt+"'", " serial_no desc", 0, 1);
					if(CollectionUtils.isEmpty(mrwList)){
						icount=1;
					}else{
						icount=Integer.parseInt(mrwList.get(0).getSerial_no().substring(6,11))+1;
					}
					MesTpmWgt mtw = new MesTpmWgt();
					mtw.setSerial_no(dt+String.format("%05d", icount++));
					mtw.setSchedule_no(mtd1.getSchedule_no());
					mtw.setMaterial_no(mtd1.getMaterial_no());
					mtw.setWgt_source("1");//1采集   2人工
					mtw.setWgt_value(ifT.getExit_weight().doubleValue());
					mtw.setWgt_time(ifT.getMsg_time());
					mtw.setDivide_flag("1");
					mtw.setDivide_opr("sys");
					mtw.setDivide_time(now);
					mtw.setCoil_no(mtd1.getCoil_no());//称重卷号
					this.getMapper(MesTpmWgtMapper.class).insert(mtw);
					rm.setState("200");
					rm.setMsgInfo("获取2级数据成功");
				}
			}
		}
        return  rm;
	}
}
