package nc.impl.hr.train;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
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.hi.cloud.train.CloudKnowledgesVO;
import nc.vo.pub.BusinessException;

/**
 * 知识列表到NCC后台表
 * @author chenzf
 * @date 2022-5-7 17:21:17
 */
public class CloudKnowledgesPlugin 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 updateDate = "";//"yyyy-MM-dd"
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date today = new Date();//获取今天的日期
		Calendar c = Calendar.getInstance();
		c.setTime(today);
		if(map.get("nums") != null && !"".equals(map.get("nums"))) {
			int nums = Integer.valueOf(arg0.getKeyMap().get("nums").toString());
			if(nums == 0) {
				updateDate = "2019-01-01";
			} else {
				c.add(Calendar.DAY_OF_MONTH, nums);
				updateDate = sdf.format(c.getTime());
			}
		} else {
			c.add(Calendar.DAY_OF_MONTH, 1);
			updateDate = sdf.format(c.getTime());
		}
		bodyMap.put("updateDate", updateDate);
		String result = impl.getKnowledges(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<CloudKnowledgesVO> vosList = JSONArray.parseArray(jsonArray.toJSONString().replaceAll("status", "cloudStatus"), CloudKnowledgesVO.class);
					if(vosList != null) {
						String sql = "select id,pk_knowledges from cloud_knowledges 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_knowledges"));
							}
						}
						
						List<CloudKnowledgesVO> insertList = new ArrayList();
						List<CloudKnowledgesVO> updList = new ArrayList();
						//判断是否已存在中间表
						for(CloudKnowledgesVO vo : vosList) {
							if(!list.contains(vo.getId())) {
								insertList.add(vo);
							} else {
								vo.setPk_knowledges(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;
	}

}
