package market.Qms.qcm.service.impl;
import java.util.Date;
import java.util.List;

import market.Api.Lms.impl.LmsApiServiceImpl;
import market.Com.mapper.ComBaseChemMapper;
import market.Com.vo.ComBaseChem;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Lms.Warehouse.vo.YdmProductDetail;
import market.Qms.common.DataHelper;
import market.Qms.qcm.mapper.QcmJhyElementsMapper;
import market.Qms.qcm.mapper.QcmJhySampleConsignDMapper;
import market.Qms.qcm.mapper.QcmJhySampleROrdMapper;
import market.Qms.qcm.mapper.QcmJudgeChemicalMapper;
import market.Qms.qcm.service.QcmJudgeChemicalService;
import market.Qms.qcm.vo.QcmJhyElements;
import market.Qms.qcm.vo.QcmJhySampleConsignD;
import market.Qms.qcm.vo.QcmJhySampleROrd;
import market.Qms.qcm.vo.QcmJudgeChemical;

import org.springframework.transaction.annotation.Transactional;

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

@RemoteService(path="QcmJudgeChemicalService")
public class QcmJudgeChemicalServiceImpl extends BaseServiceImpl<QcmJudgeChemical,String> implements QcmJudgeChemicalService { 
	
	/**
	 * 保存成分判定信息
	 * @param chemical
	 * @param listResult
	 * @return
	 */
	@Transactional(value="tm",rollbackFor=Exception.class)
	public ResultModel SaveJudgeChem(List<QcmJudgeChemical> listChemical){
		ResultModel rm=new ResultModel("200","判定成功");
		//==此处需要判断是否已经综判，与设计确认结果为（优特钢调物流模块提供的接口进行验证，传入参数：检验号、销售订单号）
		for (QcmJudgeChemical chemical : listChemical) {
			QcmJudgeChemicalMapper chemMapper=this.getMapper(QcmJudgeChemicalMapper.class);
			//成分判定记录号(C+2位年/月/日+4位流水号）
			String head="C"+DataHelper.GetDateFormatString(new Date(), "yyMMdd");
			String maxId=chemMapper.GetMaxId(head);
			if (maxId==null) {
				maxId="C"+DataHelper.GetDateFormatString(new Date(), "yyMMdd")+"0001";
			}else{
				maxId=DataHelper.InitSerial(maxId, "C", 11);
			}
			chemical.setCic_id(maxId);
			
			String username = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
			if(!DataHelper.IsNullOrSpace(username)){
					chemical.setJudge_name(username);
			}
			chemical.setJudge_ttime(new Date());
			chemMapper.insert(chemical);
			//更新库存表中成分判定信息
			YdmProductDetail ydm=new YdmProductDetail();
			ydm.setChemresult(chemical.getCic_result_code());
			ydm.setChemresult_id(chemical.getCic_id());
			ydm.setCoilno(chemical.getMaterial_no());
			CommonPage<YdmProductDetail> com=new CommonPage<>();
			com.setObject(ydm);
			try {
				this.getBean(LmsApiServiceImpl.class).doUpdateResultByCoilno(com);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			//修改委托为已判定
			QcmJhySampleROrd ord=new QcmJhySampleROrd();
			ord.setSmp_no(chemical.getSmp_no());
			ord.setContract_line_no(chemical.getContract_line_no());
			ord.setPsc(chemical.getPsc());
			ord.setJudge_status("1");
			ord.setJudge_memo(chemical.getCic_memo());
			ord.setJudge_name(username);
			ord.setJudge_time(chemical.getJudge_ttime());
			ord.setJudge_result_code(chemical.getCic_result_code());
			ord.setJudge_result_desc(chemical.getCic_result_desc());
			this.getMapper(QcmJhySampleROrdMapper.class).update(ord);
		}
		return rm;
	}
	
	/**
	 * 获取成分判定记录
	 * @param page
	 * @return
	 */
	public ResultModel GetJudgeChemRecord(CommonPage<QcmJudgeChemical> page){
		ResultModel rm=new ResultModel("200","获取数据成功");
		try{
			QcmJudgeChemicalMapper resultMapper=this.getMapper(QcmJudgeChemicalMapper.class);
			String condition=null;
			if(!DataHelper.IsNullOrSpace(page.getStartTime())){
				condition=DataHelper.CreateSqlConditionDate("judge_ttime",DataHelper.GetDate1(page.getStartTime()),DataHelper.GetDate1(page.getEndTime()));
			}
			List<QcmJudgeChemical> listChemical=resultMapper.findWithCondition(page.getObject(), condition, "judge_ttime desc", page.getSkipResults(), page.getPageSize());
			rm.setData(listChemical);
		}catch(Exception ex){
			rm.setErrorInfo(ex.getMessage());
			rm.setState("500");
			rm.setMsgInfo("获取成分判定记录出错");
		}
		return rm;
	}
	/**
	 * 接口概述：原料卷入库时，从热轧中间表获取成分实绩通过调用质量模块提供的接口写入成分实绩业务表
			传入参数：
			ASSAY_NO（成分化验号）
			HEAT_NO（炉号）
			MATERIAL_NO（原料卷号）
			ASSAY_TYPE（类型L:熔炼成分,P:成品成分）
			CHEM_CODE（化学元素代码，物流模块传入时化学元素代码为空，
			需要通过化学元素描述查询COM_BASE_CHEM表获取CHEM_CODE_2LG对应的CHEM_CODE）
			CHEM_NAME（物流模块传入的CHEM_CODE）
			CHEM_VALUE（成分值）
			SOURCE（数据来源（0:新钢,1:外购），物流模块传入）
	 * @param sum
	 * @return
	 */
	public ResultModel insertComponentInfo(List<QcmJhyElements> sum){
		ResultModel rm=new ResultModel("200","操作成功");
		List<QcmJhyElements> listqje = sum;
		try {
			for (QcmJhyElements qje : listqje) {
				QcmJhyElements q=qje;
				List<ComBaseChem> listcbc = this.getMapper(ComBaseChemMapper.class).findWithCondition(null, "CHEM_CODE_2LG='"+qje.getChem_code()+"'", null, 0, 0);
			    for (ComBaseChem cbc : listcbc) {
			    	qje.setChem_code(cbc.getChem_code());
					this.getMapper(QcmJhyElementsMapper.class).insert(q);
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		return rm;
	}
	  public ResultModel verificationIsJudge(String smp_no){
	    	ResultModel rm=new ResultModel();
	    	try {
	    		List<QcmJhySampleConsignD> listd = this.getMapper(QcmJhySampleConsignDMapper.class).findWithCondition(null, "smp_no='"+smp_no+"'", null, 0, 0);
	    		for (QcmJhySampleConsignD d : listd) {
	    			if (!d.getStatus().equals("3")) {
	    				rm.setState("403");
	    				rm.setMsgInfo("此试样号对应的取样编号"+d.getSpecimen_no()+"不是已完成状态，无法进行判定");
	    				return rm;
	    			}
	    		}
	    		rm.setState("200");
			} catch (Exception e) {
				// TODO: handle exception
				rm.setState("500");
			}
	    	return rm;
	    }
	}
	
	
	
