package com.sw.mes.sap.task;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sw.mes.base.factory.material.entity.MaterialModel;
import com.sw.mes.base.factory.material.entity.MaterialModelCharts;
import com.sw.mes.base.factory.material.entity.MaterialModelMakt;
import com.sw.mes.base.factory.material.entity.MaterialModelMarc;
import com.sw.mes.base.factory.material.entity.MaterialType;
import com.sw.mes.base.factory.material.entity.Unit;
import com.sw.mes.base.factory.material.service.MaterialModelChartsService;
import com.sw.mes.base.factory.material.service.MaterialModelMaktService;
import com.sw.mes.base.factory.material.service.MaterialModelMarcService;
import com.sw.mes.base.factory.material.service.MaterialModelService;
import com.sw.mes.base.factory.material.service.MaterialTypeService;
import com.sw.mes.base.factory.material.service.UnitService;
import com.sw.mes.core.utils.BeanUtil;
import com.sw.mes.utils.StringUtils;

import cc.cczn.matrix.commons.tasks.queue.Task;
import cc.cczn.matrix.commons.tasks.queue.TaskProcessor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class MaterialDataHandlerTask implements TaskProcessor<JSONArray> {
	
	@Autowired
	private MaterialTypeService materialTypeService;
	
	@Autowired
	private MaterialModelService materialModelService;
	
	@Autowired
	private UnitService unitService;
	
	@Autowired
	private MaterialModelMaktService materialModelMaktService;
	
	@Autowired
	private MaterialModelMarcService materialModelMarcService;
	
	@Autowired
	private MaterialModelChartsService materialModelChartsService;
	
	
    @Override
    public void process(Task<JSONArray> task) {
    	log.info("=======开始执行同步物料主数据:"+task.getKey()+"页========");
        handlerData(task.getData());
        log.info("=======同步完成物料主数据:"+task.getKey()+"页========");
    }
    
    public void handlerData(JSONArray jsonArray) {
		// 获取数组长度
		int length = jsonArray.size();
		// 使用for循环遍历每个物料组信息数据
		for (int i = 0; i < length; i++) {
		    JSONObject element = jsonArray.getJSONObject(i);
		    String MATNR = element.getString("MATNR");
		    String MATKL = element.getString("MATKL");
		    String WGBEZ = element.getString("WGBEZ");
		    String ZMAKLTX = element.getString("ZMAKLTX");
		    String MEINS = element.getString("MEINS");
		    String MTART = element.getString("MTART");
		    String MTBEZ = element.getString("MTBEZ");
		    String MSTAE = element.getString("MSTAE");
		    String LVORM = element.getString("LVORM");
		    String BRGEW = element.getString("BRGEW");
		    String NTGEW = element.getString("NTGEW");
		    String VOLUM = element.getString("VOLUM");
		    String LAENG = element.getString("LAENG");
		    String BREIT = element.getString("BREIT");
		    String HOEHE = element.getString("HOEHE");
		    String BISMT = element.getString("BISMT");
		    String SPART = element.getString("SPART");
		    String VTEXT = element.getString("VTEXT");
		    String GEWEI = element.getString("GEWEI");
		    String VOLEH = element.getString("VOLEH");
		    String MEABM = element.getString("MEABM");
		    String MEINH = element.getString("MEINH");
		    String UMREN = element.getString("UMREN");
		    String CLASS = element.getString("CLASS");
		    String MHDHB = element.getString("MHDHB");
		    String NORMT = element.getString("NORMT");
		    String EXTWG = element.getString("EXTWG");
		    String MSEHL = element.getString("MSEHL");
		    String SATNR = element.getString("SATNR");
		    String SMAKTX = element.getString("SMAKTX");
		    String ZQT1 = element.getString("ZQT1");
		    String ZQT2 = element.getString("ZQT2");
		    String ZQT3 = element.getString("ZQT3");
		    String ZQT4 = element.getString("ZQT4");
		    String ZQT5 = element.getString("ZQT5");
		    String ZQT6 = element.getString("ZQT6");
		    String ZQT7 = element.getString("ZQT7");
		    String MSEHT_J = element.getString("MSEHT_J");
		    String MSEHT_F = element.getString("MSEHT_F");
		    String MSEHT_Z = element.getString("MSEHT_Z");



		    MaterialModel materialModel = materialModelService.findByCode(MATNR);
		    MaterialType materialType = materialTypeService.getByCode(MATKL);
		    if(materialModel==null) {
		        materialModel = new MaterialModel();
		        materialModel.setCode(MATNR);//物料编码 物料编码
		        materialModel.setCreateDate(new Date());
		        materialModel.setCreateUser("SAP");
		    }
		    
		    materialModel.setStatus(0);//默认启用
		    materialModel.setMaterialType(materialType);

		    materialModel.setDel((LVORM!=null && LVORM.equals("X"))?true:false);//LVORM 删除标识
		    //materialModel.setName(MTBEZ);//MAKTX 物料描述
		    materialModel.setMtbez(MTBEZ);
		    materialModel.setMstae(MSTAE);
		    materialModel.setMeinh(MEINH);
		    materialModel.setUmren(UMREN);
		    materialModel.setBismt(BISMT);
		    materialModel.setMtart(MTART);
		    materialModel.setMaktx(SMAKTX);
		    materialModel.setMatkl(MATKL);
		    materialModel.setMeins(MEINS);
		    materialModel.setWgbez(WGBEZ);
		    materialModel.setZmakltx(ZMAKLTX);
		    materialModel.setBrgew(BRGEW);
		    materialModel.setNtgew(NTGEW);
		    materialModel.setLenRange(LAENG);
		    materialModel.setVolum(VOLUM);
		    materialModel.setLaeng(LAENG);
		    materialModel.setMeinh(MEINH);
		    materialModel.setExtwg(EXTWG);
		    materialModel.setBreit(BREIT);
		    materialModel.setHoehe(HOEHE);
		    materialModel.setSpart(SPART);
		    materialModel.setVtext(VTEXT);
		    materialModel.setGewei(GEWEI);
		    materialModel.setVoleh(VOLEH);
		    materialModel.setMeabm(MEABM);
		    materialModel.setClassCode(CLASS);
		    materialModel.setMhdhb(MHDHB);
		    materialModel.setNormt(NORMT);
		    materialModel.setMsehl(MSEHL);
		    materialModel.setSatnr(SATNR);
		    materialModel.setZqt1(ZQT1);
		    materialModel.setZqt2(ZQT2);
		    materialModel.setZqt3(ZQT3);
		    materialModel.setZqt4(ZQT4);
		    materialModel.setZqt5(ZQT5);
		    materialModel.setZqt6(ZQT6);
		    materialModel.setZqt7(ZQT7);
		    materialModel.setMsehtj(MSEHT_J);
		    materialModel.setMsehtf(MSEHT_F);
		    materialModel.setMsehtz(MSEHT_Z);
		    
//		    if(MaterialEnum.MAINMATERIALTYPES.contains(MTART))
//		    	materialModel.setType(MaterialTypeEnumValues.主产品物料.getValue());
//		    else
//		    	materialModel.setType(MaterialTypeEnumValues.副产品物料.getValue());
		    


		    //单位   //MEINS
		    if(!StringUtils.isBlank(MEINS)) {
		        Unit unit = unitService.getCode(MEINS);
		        if(unit==null) {
		            unit = new Unit();
		            unit.setCode(MEINS);
		            unit.setName(MEINS);
		            unit.setCreateDate(new Date());
		            unit.setCreateUser("SAP");
		            unit.setDel(false);
		            unitService.saveOrUpdate(unit);
		        }
		        //设置物料主数据单位
		        materialModel.setUnit(unit);
		    }
		    
		    
		    //描述数据
		    JSONArray maktArray = element.getJSONArray("MAKT");
		    for(int j=0;j<maktArray.size();j++){
		        JSONObject maktObj = maktArray.getJSONObject(j);
		        MaterialModelMakt makt = maktObj.toJavaObject(MaterialModelMakt.class);
		        if(makt.getMaktx()==null)
		            continue;
		        if(makt.getSpras().equalsIgnoreCase("ZH")) {
		        	materialModel.setName(makt.getMaktx());
		        }
		    }
		    
		    

		    //添加更新物料主数据
		    materialModelService.saveOrUpdate(materialModel);


		    //描述数据
		    for(int j=0;j<maktArray.size();j++){
		        JSONObject maktObj = maktArray.getJSONObject(j);
		        MaterialModelMakt makt = maktObj.toJavaObject(MaterialModelMakt.class);
		        makt.setMaterialModelId(materialModel.getId());

		        if(makt.getMaktx()==null)
		            continue;

		        MaterialModelMakt haveObj = materialModelMaktService.findByMaktxAndMaterialModelId(makt.getMaktx(),materialModel.getId());
		        if(haveObj!=null)
		            BeanUtil.copyProperties(haveObj,makt,"id");
		        else
		            haveObj = makt;
		        
		        if(haveObj.getSpras().equalsIgnoreCase("ZH")) {
		        	materialModel.setName(haveObj.getMaktx());
		        }
		        
		        materialModelMaktService.saveOrUpdate(haveObj);
		    }


		    //工厂信息
		    JSONArray marcArray = element.getJSONArray("MARC");
		    for(int j=0;j<marcArray.size();j++){
		        JSONObject marcObj = marcArray.getJSONObject(j);
		        MaterialModelMarc marc = marcObj.toJavaObject(MaterialModelMarc.class);
		        marc.setMaterialModelId(materialModel.getId());

		        if(marc.getName1()==null){
		            continue;
		        }

		        MaterialModelMarc haveObj = materialModelMarcService.findByName1AndMaterialModelId(marc.getName1(),materialModel.getId());
		        if(haveObj!=null)
		            BeanUtil.copyProperties(haveObj,marc,"id");
		        else
		            haveObj = marc;
		        materialModelMarcService.saveOrUpdate(haveObj);
		    }


		    //工厂信息
		    JSONArray chartsArray = element.getJSONArray("CHARTS");
		    for(int j=0;j<chartsArray.size();j++){
		        JSONObject chartsObj = chartsArray.getJSONObject(j);
		        MaterialModelCharts charts = chartsObj.toJavaObject(MaterialModelCharts.class);
		        charts.setMaterialModelId(materialModel.getId());

		        if(charts.getAtnam()==null)
		            continue;

		        MaterialModelCharts haveObj = materialModelChartsService.findByAtnamAndMaterialModelId(charts.getAtnam(),materialModel.getId());
		        if(haveObj!=null)
		            BeanUtil.copyProperties(charts,haveObj,"id");
		        else {
		            haveObj = charts;
		        }
		        materialModelChartsService.saveOrUpdate(haveObj);
		        
		        if(haveObj.getAtnam().contentEquals("S07")) {
		        	materialModel.setDirectionalSign(haveObj.getAtwrt());
		        }
		        
		        if(haveObj.getAtnam().contentEquals("S11")) {
		        	materialModel.setDopant(haveObj.getAtwrt());
		        	materialModel.setType(haveObj.getAtwrt());
		        }
		        
		        if(haveObj.getAtnam().contentEquals("S15")) {
		        	materialModel.setProductName(haveObj.getAtwrt());
		        }
		        
		        if(haveObj.getAtnam().contentEquals("S16")) {
		        	materialModel.setSpecification(haveObj.getAtwtb());
		        }
		        
		        if(haveObj.getAtnam().contentEquals("S08")) { //基地代码
		        	materialModel.setWerks(haveObj.getAtwrt());
		        }
		        
		        if(haveObj.getAtnam().contentEquals("S11")) {
		        	materialModel.setModelNumber(haveObj.getAtwrt());
		        }
		        
		        
		        if(haveObj.getAtnam().contentEquals("S04")) {
		        	materialModel.setProductLevel(haveObj.getAtwrt());
		        }
		        
		        materialModelService.saveOrUpdate(materialModel);
		        materialModelService.updateExtra(materialModel.getId(), haveObj.getAtnam(), haveObj.getAtwrt(),haveObj.getAtbez(),haveObj.getAtwtb(), true);
		    }
		}
	}
    
}
