package nc.bs.uapbd.mdg.service;

import java.util.ArrayList;
import java.util.List;

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

import nc.bs.dao.BaseDAO;
import nc.bs.resa.sql.SqlUtil;
import nc.bs.uapbd.mdg.process.MDGSupplierSynNCPorcess;
import nc.itf.uapbd.mdg.IMDGSupplierInfoService;
import nc.net.pu.utils.HttpReqUtil;
import nc.net.servlet.IReturnStatus;
import nc.util.mmf.framework.base.MMValueCheck;
import nc.vo.esb.ResultEsbVO;
import nc.vo.esb.ResultVO;
import nc.vo.pub.BusinessException;
import ncc.vo.mdg.supmidd.MDGSupplierVO;

public class MDGSupplierInfoServiceImpl implements IMDGSupplierInfoService {

	@Override
	public String mdgSynSupToNCC(JSONObject jsonobj) throws BusinessException {
		JSONObject reqJsonObject =jsonobj.getJSONObject("requestInfo");
		JSONArray  datas=reqJsonObject.getJSONArray("data");
		if(MMValueCheck.isNotEmpty(datas)) {
			JSONObject datajson=null;
			MDGSupplierVO mdgSupplierVO=null;
			BaseDAO baseDAO=new BaseDAO();
			List<MDGSupplierVO> suppliers=new ArrayList<MDGSupplierVO>();
			List<String> codes=new ArrayList<String>();
			for(int i=0;i<datas.size();i++) {
				mdgSupplierVO=new MDGSupplierVO();
				datajson=datas.getJSONObject(i);
				String code=datajson.getString("code");
				mdgSupplierVO.setBillno(code);
				mdgSupplierVO.setBilldata(JSON.toJSONString(datajson));
				mdgSupplierVO.setFlagstatus("0");//0为初始状态
				codes.add(code);
				suppliers.add(mdgSupplierVO);
			}
//			baseDAO.deleteByClause(MDGSupplierVO.class, SqlUtil.buildInSql("billno", codes.toArray(new String[0]), true));//modify by longht 处理银行账号只有一条进来的问题
			//新增
			baseDAO.insertVOList(suppliers);
		}
		ResultEsbVO<ResultVO> esb = new ResultEsbVO<ResultVO>();
		ResultVO result = new ResultVO();
		result.setCode(IReturnStatus.SUCCEED);// 成功
		com.alibaba.fastjson.JSONObject resJsonObject=new com.alibaba.fastjson.JSONObject();
		result.setResult(resJsonObject);
		result.setMessage("同步供应商成功");
		HttpReqUtil.createResultEsbVO(esb);
		esb.setResultInfo(result);
		return com.alibaba.fastjson.JSONObject.toJSONString(esb);
	}

	@Override
	public String genSupplierFromMidd_RequiresNew(JSONObject jsonobj) throws BusinessException {
		return MDGSupplierSynNCPorcess.process(jsonobj);
	}

}
