package com.mandi.webservice.service.impl;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.mandi.common.JSONobj;
import com.mandi.common.Jacksonmethod;
import com.mandi.common.ParamFileter.Fileoperation;
import com.mandi.dao.common.ObjectResult;
import com.mandi.dao.common.PageResult;
import com.mandi.fendan.mapper.BusinessMapper;
import com.mandi.fendan.mapper.Fd_BsheetMapper;
import com.mandi.fendan.mapper.Fd_YKTMapper;
import com.mandi.fendan.persist.Fd_Bsheet;
import com.mandi.fendan.util.Contants;
import com.mandi.fendan.util.persist.Serviceenum;
import com.mandi.fendan.util.persist.WaitXSTD;
import com.mandi.servlet.file.impl.FileLoadImpl;
import com.mandi.webservice.service.IFdXSTDService;
import com.mandi.webservice.service.IInvoiceService;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class InvoiceService implements IInvoiceService{
	@Resource
	private IFdXSTDService fdser;
	@Resource
	private BusinessMapper bsPer;
	@Resource
	private Fd_BsheetMapper fdbPer;
	@Resource
	private Fd_YKTMapper yktmper;

	private Logger log= Logger.getLogger(FileLoadImpl.class);

	@Override
	public String action(HttpServletRequest r,String jsonParam) {
		Fileoperation.AppendContentToFile(Fileoperation.gettestFileName(), "请求ip::::"+r.getRemoteAddr());
		Fileoperation.AppendContentToFile(Fileoperation.gettestFileName(), "数据值::b::"+jsonParam);
		String action="";
		String dataArr="";
		try {
			JsonParser jp=new JsonParser();
			JsonObject jo=jp.parse(jsonParam).getAsJsonObject();
			action=JSONobj.getstring(jo, "action");
			dataArr=jo.get("data").toString();
		} catch (Exception e) {

		}
//		Fileoperation.AppendContentToFile(Fileoperation.gettestFileName(), "参数action::::"+action);
//		Fileoperation.AppendContentToFile(Fileoperation.gettestFileName(), "参数dataArr::::"+dataArr);
		Map<String, Object> mm=new HashMap<String, Object>();
		ObjectResult<Boolean> orr=null;
		if(!StringUtils.isEmpty(action)){
			try {
				if(Contants.NcactionCode_add.equals(action)){
					orr=fdser.saveFd_xstd(dataArr);
				}else if(Contants.NcactionCode_del.equals(action)){
					orr=fdser.delFd_xstd(dataArr);
				}else if(Contants.NcactionCode_open.equals(action)){
					orr=fdser.openFd_xstd(dataArr);
				}else if(Contants.NcactionCode_close.equals(action)){
					orr=fdser.closeFd_xstd(dataArr);
				}else{
					mm.put("respcode", 999);
					mm.put("respmsg", "动作参数不正确!");
				}
				if(orr!=null){
					if(orr.getCode()==0){
						mm.put("respcode", 111);
						mm.put("respmsg", orr.getErrmsg());
					}else{
						mm.put("respcode", 999);
						mm.put("respmsg", orr.getErrmsg());
					}
				}
			} catch (Exception e) {
				mm.put("respcode", 999);
				mm.put("respmsg", "错误:"+e.getMessage());
			}
		}else{
			mm.put("respcode", 999);
			mm.put("respmsg", "动作参数未找到!");
		}
		String str=Jacksonmethod.tojson(mm, false);
		return str;
	}

	@Override
	public String delLoading(HttpServletRequest r,String jsonParam) {
		Fileoperation.AppendContentToFile(Fileoperation.gettestFileName(), "请求ip::::"+r.getRemoteAddr());
		Fileoperation.AppendContentToFile(Fileoperation.gettestFileName(), "参数metaid::b::"+jsonParam);
		String cardID="";
		String yktywdh="";
		JsonArray dataArr=null;
		Map<String, Object> mm=new HashMap<String, Object>();
		try {
			JsonParser jp=new JsonParser();
			JsonObject jo=jp.parse(jsonParam).getAsJsonObject();
			cardID=JSONobj.getstring(jo, "cardID");
			yktywdh=JSONobj.getstring(jo, "yktywdh");
			dataArr=JSONobj.getJsonArray(jo, "data");
		} catch (Exception e) {
			mm.put("respcode", 999);
			mm.put("respmsg", "数据解析出错!");
			String str=Jacksonmethod.tojson(mm, false);
			return str;
		}
		ObjectResult<Boolean> prs=new ObjectResult<Boolean>();
		try {
			prs=fdser.delZCMX(cardID, yktywdh, dataArr);
			Fileoperation.AppendContentToFile(Fileoperation.gettestFileName(), "delLoading返回值::::"+Jacksonmethod.tojson(prs, false));
		} catch (Exception e) {
			mm.put("respcode", 999);
			mm.put("respmsg", e.getMessage());
			Fileoperation.AppendContentToFile(Fileoperation.gettestFileName(), "delLoading返回值::::"+Jacksonmethod.tojson(mm, false));
			String str=Jacksonmethod.tojson(mm, false);
			return str;
		}
		if(prs!=null){
			if(prs.getCode()==0){
				mm.put("respcode", 111);
				mm.put("respmsg", "操作成功!");
			}else{
				if(prs.getCode()==2){
					mm.put("respcode", 222);
					mm.put("respmsg", prs.getErrmsg());
				}else{
					mm.put("respcode", 999);
					mm.put("respmsg", prs.getErrmsg());
				}
			}
		}
		String str=Jacksonmethod.tojson(mm, false);
		return str;
	}

	@Override
	public String addLoading(HttpServletRequest r,String jsonParam) {
		Fileoperation.AppendContentToFile(Fileoperation.gettestFileName(), "请求ip::::"+r.getRemoteAddr());
		Fileoperation.AppendContentToFile(Fileoperation.gettestFileName(), "参数metaid::b::"+jsonParam);
		String cardID="";
		String yktywdh="";
		JsonArray dataArr=null;
		Map<String, Object> mm=new HashMap<String, Object>();
		try {
			JsonParser jp=new JsonParser();
			JsonObject jo=jp.parse(jsonParam).getAsJsonObject();
			cardID=JSONobj.getstring(jo, "cardID");
			yktywdh=JSONobj.getstring(jo, "yktywdh");
			dataArr=JSONobj.getJsonArray(jo, "data");
//			System.err.println("ddata::::"+dataArr);
		} catch (Exception e) {
			mm.put("respcode", 999);
			mm.put("respmsg", "数据解析出错!");
			String str=Jacksonmethod.tojson(mm, false);
			return str;
		}
		PageResult<Map<String, Object>> prs=new PageResult<Map<String, Object>>();
		try {
			prs=fdser.addZCMX(cardID, yktywdh, dataArr);
			Fileoperation.AppendContentToFile(Fileoperation.gettestFileName(), "addLoading返回值::::"+Jacksonmethod.tojson(prs, false));
		} catch (Exception e) {
			mm.put("respcode", 999);
			mm.put("respmsg", e.getMessage());
			Fileoperation.AppendContentToFile(Fileoperation.gettestFileName(), "addLoading返回值::::"+Jacksonmethod.tojson(mm, false));
			String str=Jacksonmethod.tojson(mm, false);
			return str;
		}
		if(prs!=null){
			if(prs.getCode()==0){
				mm.put("respcode", 111);
				mm.put("respmsg", "操作成功!");
				mm.put("data", prs.getList());
			}else{
//				mm.put("respcode", 999);
//				mm.put("respmsg", prs.getErrmsg());
				if(prs.getCode()==2){
					mm.put("respcode", 222);
					mm.put("respmsg", prs.getErrmsg());
				}else{
					mm.put("respcode", 999);
					mm.put("respmsg", prs.getErrmsg());
				}
			}
		}
		String str=Jacksonmethod.tojson(mm, false);
		return str;
	}

	@Override
	public String zcFinish(HttpServletRequest r, String jsonParam) {
		Fileoperation.AppendContentToFile(Fileoperation.gettestFileName(), "zcFinish参数map::::" + jsonParam);
		Map<String, Object> mm = new HashMap<String, Object>();
		String businessNo = "";
		boolean zcWc = false;
		try {
			JsonParser jp = new JsonParser();
			JsonObject jo = jp.parse(jsonParam).getAsJsonObject();
			businessNo = JSONobj.getstring(jo, "businessNo");
			zcWc = JSONobj.getbool(jo, "isWc");
		} catch (Exception e) {
			mm.put("respcode", 999);
			mm.put("respmsg", "数据解析出错!");
			String str = Jacksonmethod.tojson(mm, false);
			return str;
		}
		if (StringUtils.isBlank(businessNo)) {
			mm.put("respcode", 999);
			mm.put("respmsg", "业务单号参数为空!");
			String str = Jacksonmethod.tojson(mm, false);
			return str;
		}
		Fd_Bsheet fb = this.fdbPer.getbybsNo(businessNo);
		if (fb == null && fb.getStatus() != Serviceenum.在办) {
			mm.put("respcode", 999);
			mm.put("respmsg", "未找到在办业务单!");
			String str = Jacksonmethod.tojson(mm, false);
			return str;
		}
		Map<String, Object> xsTask = bsPer.selectTaskTime(businessNo);
		if (xsTask != null) {
			Map<String, Object> xstm = new HashMap<String, Object>();
			xstm.put("businessNo", businessNo);
			xstm.put("zcWc", zcWc);
			if (zcWc)
				xstm.put("zcwcTime", new Date());
			else
				xstm.put("zcwcTime", null);
			bsPer.updateZcWcTime(xstm);

			if (zcWc) {
				List<WaitXSTD> l = this.fdbPer.getAllKH(fb.getBsNo(), fb.getId());
				if (l != null) {
					boolean iscz = false;//收货单位在明细中是否存在
					WaitXSTD wt = null;
					for (WaitXSTD waitXSTD : l) {
						if (fb.getShdw().equals(waitXSTD.getTd_khmc())) {
							iscz = true;
							break;
						} else {
							if (wt == null && StringUtils.isNotBlank(waitXSTD.getTd_khmc())) {
								wt = waitXSTD;
							}
						}
					}
					if (!iscz && wt != null) {
						fb.setShdw(wt.getTd_khmc());
						this.yktmper.updateshcompany(fb.getBsNo(), wt.getTd_khmc(), wt.getTd_shdd());
					}
				}
			}
			mm.put("respcode", 111);
			mm.put("respmsg", "完成装车操作成功!");
		} else {
			mm.put("respcode", 999);
			mm.put("respmsg", "业务单号参数错误!");
		}
		String str = Jacksonmethod.tojson(mm, false);
		return str;
	}

	@Override
	public String getVInfo(HttpServletRequest r,String jsonParam) {

		Fileoperation.AppendContentToFile(Fileoperation.gettestFileName(), "getVInfo参数map::::"+jsonParam);
//		Fileoperation.AppendContentToFile(Fileoperation.gettestFileName(), "参数yktkh::::"+RequestParam.getString(r, "yktkh"));

		String cardID="";
		Map<String, Object> mm=new HashMap<String, Object>();
		try {
			JsonParser jp=new JsonParser();
			JsonObject jo=jp.parse(jsonParam).getAsJsonObject();
			cardID=JSONobj.getstring(jo, "yktkh");
		} catch (Exception e) {
			mm.put("respcode", 999);
			mm.put("respmsg", "数据解析出错!");
			String str=Jacksonmethod.tojson(mm, false);
			return str;
		}
		ObjectResult<Map<String, Object>> prs=fdser.getcInfo(cardID);
		if(prs.getCode() == 1) {
			log.info("卡号:" + cardID + "---- 错误:"+ prs.getErrmsg());
		}
		if(prs!=null){
			if(prs.getCode()==0){
				mm=prs.getItem();
			}else{
				mm.put("respcode", 999);
				mm.put("respmsg", prs.getErrmsg());
			}
		}
		String str=Jacksonmethod.tojson(mm, false);
		return str;
	}

	@Override
	public String getxstdInfo(HttpServletRequest r, String jsonParam) {
		String xstdNo="";
		Map<String, Object> mm=new HashMap<String, Object>();
		try {
			JsonParser jp=new JsonParser();
			JsonObject jo=jp.parse(jsonParam).getAsJsonObject();
			xstdNo=JSONobj.getstring(jo, "xstdNo");
		} catch (Exception e) {
			mm.put("respcode", 999);
			mm.put("respmsg", "数据解析出错!");
			String str=Jacksonmethod.tojson(mm, false);
			return str;
		}
		ObjectResult<Map<String, Object>> or=fdser.getxstdInfo(xstdNo);
		String str="";
		if(or.getCode()!=0){
			str=Jacksonmethod.tojson(or, false);
		}else{
			str=Jacksonmethod.tojson(or.getItem(), false);
		}
		return str;
	}

	@Override
	public String savewdControl(HttpServletRequest r, String jsonParam) {
		Fileoperation.AppendContentToFile(Fileoperation.gettestFileName(), "请求ip::::"+r.getRemoteAddr());
		Fileoperation.AppendContentToFile(Fileoperation.gettestFileName(), "参数savewdControl::b::"+jsonParam);
		JsonArray dataArr=null;
		Map<String, Object> mm=new HashMap<String, Object>();
		try {
			JsonParser jp=new JsonParser();
			JsonObject jo=jp.parse(jsonParam).getAsJsonObject();
			dataArr=JSONobj.getJsonArray(jo, "data");
			System.err.println("savewdControl ddata::::"+dataArr);
		} catch (Exception e) {
			mm.put("respcode", 999);
			mm.put("respmsg", "数据解析出错!");
			String str=Jacksonmethod.tojson(mm, false);
			return str;
		}
		ObjectResult<Boolean> prs=new ObjectResult<Boolean>();
		try {
			prs=fdser.savewdControl(dataArr);
			Fileoperation.AppendContentToFile(Fileoperation.gettestFileName(), "savewdControl返回值::::"+Jacksonmethod.tojson(prs, false));
		} catch (Exception e) {
			mm.put("respcode", 999);
			mm.put("respmsg", e.getMessage());
			Fileoperation.AppendContentToFile(Fileoperation.gettestFileName(), "savewdControl返回值::::"+Jacksonmethod.tojson(mm, false));
			String str=Jacksonmethod.tojson(mm, false);
			return str;
		}
		if(prs!=null){
			if(prs.getCode()==0){
				mm.put("respcode", 111);
				mm.put("respmsg", "操作成功!");
			}else{
				mm.put("respcode", 999);
				mm.put("respmsg", prs.getErrmsg());
			}
		}
		String str=Jacksonmethod.tojson(mm, false);
		return str;
	}

	@Override
	public String receiveCurrWeight(HttpServletRequest r, String jsonParam) { // 获取业务单号 一次称重 二次称重
		JsonParser jp=new JsonParser();
		JsonObject jo=jp.parse(jsonParam).getAsJsonObject();
		String businessNo = JSONobj.getstring(jo, "businessNo");
		boolean firstWeighing = JSONobj.getbool(jo, "firstWeighing");
		boolean secondWeighing = JSONobj.getbool(jo, "secondWeighing");
		String sdepartNo = JSONobj.getstring(jo, "sdepartNo");
		if(StringUtils.isBlank(businessNo) || businessNo.indexOf("XS")!=0 || StringUtils.isBlank(sdepartNo)
		) {
			return null;
		}
		// 刷新
		if(firstWeighing || secondWeighing) { // 称重需要刷新一下
			if ("06".equals(sdepartNo)) { // 明晟
				synchronized (FdXSTDService.refreshMsLedLock) {
					if (FdXSTDService.refreshMsLedLock) { // 系统繁忙
						return null;
					}
				}
				try {
					synchronized (FdXSTDService.refreshMsLedLock) {
						FdXSTDService.refreshMsLedLock = true;
					}
					fdser.sendToMsLed(null);
				} finally {
					synchronized (FdXSTDService.refreshMsLedLock) {
						FdXSTDService.refreshMsLedLock = false;
					}
				}
			} else if ("08".equals(sdepartNo)) { // 义瑞
				synchronized (FdXSTDService.refreshYrLedLock) {
					if (FdXSTDService.refreshYrLedLock) { // 系统繁忙
						return null;
					}
				}
				try {
					synchronized (FdXSTDService.refreshYrLedLock) {
						FdXSTDService.refreshYrLedLock = true;
					}
					fdser.sendToYrLed(null);
				}finally {
					synchronized (FdXSTDService.refreshYrLedLock) {
						FdXSTDService.refreshYrLedLock = false;
					}
				}
			}
		} else if ("04".equals(sdepartNo)) { // 科技
			synchronized (FdXSTDService.refreshYrLedLock) {
				if (FdXSTDService.refreshKjLedLock) { // 系统繁忙
					return null;
				}
			}
			try {
				synchronized (FdXSTDService.refreshKjLedLock) {
					FdXSTDService.refreshKjLedLock = true;
				}
				fdser.sendToKjLed(null);
			} finally {
				synchronized (FdXSTDService.refreshKjLedLock) {
					FdXSTDService.refreshKjLedLock = false;
				}
			}
		}
		return null;
	}


}