package ncc.bs.mdg.task;

import java.util.List;
import java.util.Map;

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

import nc.bs.dao.BaseDAO;
import nc.bs.framework.common.NCLocator;
import nc.bs.pub.pa.PreAlertObject;
import nc.bs.pub.pa.PreAlertReturnType;
import nc.bs.pub.taskcenter.BgWorkingContext;
import nc.bs.pub.taskcenter.IBackgroundWorkPlugin;
import nc.bs.uap.lock.PKLock;
import nc.itf.uapbd.mdg.IMDGSupplierInfoService;
import nc.jdbc.framework.processor.BeanListProcessor;
import nc.util.mmf.framework.base.MMValueCheck;
import nc.util.mmf.framework.base.MMValueConvert;
import nc.vo.esb.ResultEsbVO;
import nc.vo.esb.ResultVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.lang.UFDate;
import ncc.vo.mdg.supmidd.MDGSupplierVO;

public class MDGSupplierGenFromMiddPlugin implements IBackgroundWorkPlugin {

	@Override
	public PreAlertObject executeTask(BgWorkingContext context) throws BusinessException {
		PreAlertObject msg = new PreAlertObject();
		msg.setReturnType(PreAlertReturnType.RETURNMESSAGE);
		msg.setMsgTitle("生成MDG供应商");
		if(!PKLock.getInstance().addDynamicLock(this.getClass().getName())) {
			msg.setReturnObj("任务执行中，请勿重复执行！");
			context.setLogStr(msg.getReturnObj().toString());
			return msg;
		}
		BaseDAO baseDAO=new BaseDAO();
		//add by longht 2021-09-14 优化代码-执行同步 前，先删除前N天的历史数据，为空则默认为3个月 及更新中间表逻辑  start
		Map paraMap = context.getKeyMap();
		int days = 90;
		if(null!=paraMap&&null!=paraMap.get("days")) {
			days = Integer.parseInt(paraMap.get("days")+"");
		}
		String delsql = " update ncc_mdg_bd_supplier set dr = 1 where dr = 0 and ts < '"+new UFDate().getDateBefore(days).toStdString().substring(0, 10)+"'";
		baseDAO.executeUpdate(delsql);
		//add by longht 2021-09-14 优化代码-执行同步 前，先删除前N天的历史数据，为空则默认为3个月 及更新中间表逻辑  end 
		//String sql="select * from ncc_mdg_bd_supplier where to_number(flagstatus)<2  and dr =0";//modify by longht billno不应该为空
		String sql="select * from ncc_mdg_bd_supplier where to_number(flagstatus)<2  and dr =0 AND MSG IS  NULL ORDER BY TS  ";//modify by longht 20211013 优化执行逻辑，未同步且同步信息为空的才进行同步
		List<MDGSupplierVO> vos=(List<MDGSupplierVO>) baseDAO.executeQuery(sql, new BeanListProcessor(MDGSupplierVO.class));
		int total=0;
		int sucess=0;
		if(MMValueCheck.isNotEmpty(vos)) {
			IMDGSupplierInfoService service=NCLocator.getInstance().lookup(IMDGSupplierInfoService.class);
			total=vos.size();
			for(int i=0;i<vos.size();i++) {
				//add by longht 2021-09-14 优化代码-执行同步 前，先删除前N天的历史数据，为空则默认为3个月 及更新中间表逻辑  start
				//String resString=service.genSupplierFromMidd_RequiresNew(JSON.parseObject(vos.get(i).getBilldata()));
				JSONObject json = JSON.parseObject(vos.get(i).getBilldata());
				json.put("pk", vos.get(i).getPk());
				String resString=service.genSupplierFromMidd_RequiresNew(json);
				//add by longht 2021-09-14 优化代码-执行同步 前，先删除前N天的历史数据，为空则默认为3个月 及更新中间表逻辑  end
				ResultEsbVO<ResultVO> esb =JSON.parseObject(resString,new TypeReference<ResultEsbVO<ResultVO>>() {});
				if(MMValueConvert.toUFBoolean(esb.getEsbInfo().getReturnStatus()).booleanValue()) {
					sucess=sucess+1;
				}
			}
		}
		msg.setReturnObj("执行完成，执行总数量："+total+"，执行成功数量："+sucess);
		context.setLogStr(msg.getReturnObj().toString());
		return msg;
	}

}
