package com.steerinfo.ems.ifmesemsproductorder.service.impl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

import com.steerinfo.ems.ifmesemsproductorder.mapper.IfMesEmsProductorderMapper;
import com.steerinfo.ems.ifmesemsproductorder.model.IfMesEmsProductorder;
import com.steerinfo.ems.ifmesemsproductorder.service.IIfMesEmsProductorderService;
import com.steerinfo.ems.ifmesemsswapfile.model.IfMesEmsSwapfile;
import com.steerinfo.ems.Utils.HttpRequestMes;
import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service(value = "ifMesEmsProductorderService")
public class IfMesEmsProductorderServiceImpl extends BaseServiceImpl<IfMesEmsProductorder, String> implements IIfMesEmsProductorderService {

	private static final Logger logger = LoggerFactory.getLogger(IfMesEmsProductorderServiceImpl.class);

    @Autowired
    private IfMesEmsProductorderMapper ifmesemsproductordermapper;

    @Override
    protected IBaseMapper<IfMesEmsProductorder, String> getMapper() {
        return ifmesemsproductordermapper;
    }

    public String synchronousMesAnEmsData() {
		int addSize = 0;
		int updateSize = 0;
		int errorSize = 0;
		String url = "http://172.16.100.91:8093/mes_interface/mesplan"; // 生产计划数据
		while (true) {
			String res = HttpRequestMes.httpRequestMes(url, "get", null);
			JSONArray ja = JSONArray.parseArray(res);
			if (ja != null && !ja.isEmpty()) {
				logger.info("获取生产计划数据条数：" + ja.size());
				for (int i = 0; i < ja.size(); i++) {
					JSONObject jo = (JSONObject) ja.get(i);
					if(!jo.containsKey("fid")){
						errorSize += 1;
						continue;
					}
					IfMesEmsProductorder model = new IfMesEmsProductorder();
					model.jsonobjectToIfMesEmsProductorder(jo);
					if (ifmesemsproductordermapper.updateByPrimaryKey(model) == 0){
						addSize += ifmesemsproductordermapper.insert(model);
					}else{
						updateSize +=1;
					}
					HttpRequestMes.httpRequestMes(url, "post", "[{\"id\":\"" + jo.getString("fid") + "\",\"status\":\"1\"}]");
				}
			} else {
				break;
			}
		}
		logger.info("本次总共新增生产计划数据条数：" + addSize);
		logger.info("本次总共更新生产计划数据条数：" + updateSize);
		logger.info("本次总共错误生产计划数据条数：" + errorSize);
        return "本次新增" + addSize + "条,更新" + updateSize + "条,失败" + errorSize + "条。";
    }

	public List<IfMesEmsProductorder> selectNewMaterial(HashMap<String, Object> model) {
		List<IfMesEmsProductorder> list = ifmesemsproductordermapper.selectNewMaterial(model);
		return list;
	};

	public int updateStatus(HashMap<String, Object> m) {
		return ifmesemsproductordermapper.updateStatus(m);
	}

	@Override
	public String synchronousMesFromProd(Date d) {
		String clock = new SimpleDateFormat("yyyy-MM").format(d);
		String url = "http://ems.sxzygc.com.cn/zhongsteel.pass.web/pass/ems/v1/ifmesemsproductorder/?pageNum=1&pageSize=2000&eprodate=" + clock; // 生产计划数据
		String res = HttpRequestMes.httpRequestMes(url, "get", null);
		JSONObject jo = JSONObject.parseObject(res);
		if (jo != null && !jo.isEmpty() && jo.getString("code").equals("0")){
			JSONObject jo2 = (JSONObject)jo.get("data");
			JSONArray ja = (JSONArray)jo2.get("list");
			if (ja != null && !ja.isEmpty()) {
				for (int i = 0; i < ja.size(); i++) {
					JSONObject jo3 = (JSONObject) ja.get(i);
					if (ifmesemsproductordermapper.selectByPrimaryKey(jo3.getString("fid")) == null){
						IfMesEmsProductorder model = new IfMesEmsProductorder();
						model.setFid(jo3.getString("fid"));
						model.setId(jo3.getString("fid"));
						model.setCreatetime(jo3.getString("createtime")!=null?jo3.getString("createtime").substring(0, 19):"");
						model.setWorks(jo3.getString("works"));
						model.setMaterialcode(jo3.getString("materialcode"));
						model.setMaterialname(jo3.getString("materialname"));
						model.setOrderno(jo3.getString("orderno"));
						model.setEprodate(jo3.getString("eprodate")!=null?jo3.getString("eprodate").substring(0, 19):"");
						model.setProdline(jo3.getString("prodline"));
						model.setQty(jo3.getBigDecimal("qty"));
						model.setSendtime(jo3.getString("sendtime")!=null?jo3.getString("sendtime").substring(0, 19):"");
						model.setStatus(jo3.getString("status"));
						model.setSprodate(jo3.getString("sprodate")!=null?jo3.getString("sprodate").substring(0, 19):"");
						model.setUnit(jo3.getString("unit"));
						ifmesemsproductordermapper.insert(model);
					}
				}
			}
		}
		logger.info("从正式环境同步MES生产计划数据--执行完毕--");
		return null;
	}

}
