package nc.impl.hr.train;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

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

import nc.bs.dao.BaseDAO;
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.jdbc.framework.processor.ColumnListProcessor;
import nc.vo.hi.cloud.train.CloudKngcatalogsVO;
import nc.vo.pub.BusinessException;
/**
 * 获取知识目录列表到NCC后台表
 * @author chenzf
 * @date 2022-4-20 20:30:06
 * 
 */
public class CloudKngcatalogsPlugin implements IBackgroundWorkPlugin {

	@Override
	public PreAlertObject executeTask(BgWorkingContext arg0) throws BusinessException {
		PreAlertObject alertObj = new PreAlertObject();
		if(!PKLock.getInstance().addDynamicLock("CloudKngcatalogsPlugin")) {
			alertObj.setReturnObj("任务执行中，请勿重复执行！");
			return alertObj;
	    }
		TrainCloudSchoolImpl impl = new TrainCloudSchoolImpl();
		Map<String,Object> bodyMap = impl.getBodyMap();
		Map map = arg0.getKeyMap();
		String result = impl.getKngcatalogs(bodyMap, 0, 0);
		if(StringUtils.isNotBlank(result)) {
			BaseDAO dao = new BaseDAO();
			JSONObject jsonObject = JSONObject.parseObject(result);
			String code = jsonObject.getString("code");
			if("0".equals(code)) {
				JSONArray jsonArray = jsonObject.getJSONArray("data");//数据data
				if(jsonArray != null && jsonArray.size() > 0) {
					List<CloudKngcatalogsVO> vosList = JSONArray.parseArray(jsonArray.toJSONString().replaceAll("status", "cloudStatus"), CloudKngcatalogsVO.class);
					if(vosList != null) {
						String sql = "select id,pk_kngcatalogs from cloud_kngcatalogs where nvl(dr,0) = 0";
						List<Map<String,String>> listMap = (List<Map<String,String>>) dao.executeQuery(sql, new ColumnListProcessor());
						List<String> list = new ArrayList();//存id的list
						Map<String,String> idMap = new HashMap();//id为key，pk_kngcatalogs为value
						if(list != null && list.size() >0) {
							for(Map<String,String> m : listMap) {
								list.add(m.get("id"));
								idMap.put(m.get("id"), m.get("pk_kngcatalogs"));
							}
						}
						
						List<CloudKngcatalogsVO> insertList = new ArrayList();
						List<CloudKngcatalogsVO> updList = new ArrayList();
						//判断是否已存在中间表
						for(CloudKngcatalogsVO vo : vosList) {
							if(!list.contains(vo.getId())) {
								insertList.add(vo);
							} else {
								vo.setPk_kngcatalogs(idMap.get(vo.getId()));
								updList.add(vo);
							}
						}
						alertObj.setMsgTitle("");
						if(insertList.size() > 0) {
							String[] pks = dao.insertVOList(insertList);
							alertObj.setReturnObj(pks);
							alertObj.setMsgTitle("新增知识目录"+pks.length+"条数据!");
						}
						if(updList.size() > 0) {
							dao.updateVOList(updList);
							alertObj.setMsgTitle(alertObj.getMsgTitle()+"修改知识目录"+updList.size()+"条数据!");
						}
					}
				}
			} else {
				throw new BusinessException(jsonObject.getString("message"));
			}
		} else {
			throw new BusinessException("查询结果result为空");
		}
		alertObj.setReturnType(PreAlertReturnType.RETURNMESSAGE);
		return alertObj;
	}

}
