package nc.impl.hr.train;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
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.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.CloudTrainingContentVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.lang.UFDate;
/**
 * 所有培训课程同步到NCC后台表
 * @author chenzf
 * @date 2022-4-21 08:38:14
 *
 */
public class CloudTrainningContentPlugin implements IBackgroundWorkPlugin {

	@Override
	public PreAlertObject executeTask(BgWorkingContext arg0) throws BusinessException {
		PreAlertObject alertObj = new PreAlertObject();
		if(!PKLock.getInstance().addDynamicLock("CloudTrainningContentPlugin")) {
			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 = "1997-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());
		}
		BaseDAO dao = new BaseDAO();
		int totalPages = 0;
		String result = impl.gettrainingcontentlistpage(bodyMap, 1, 500);
		if(StringUtils.isNotBlank(result)) {
			JSONObject jsonObject = JSONObject.parseObject(result);
			String code = jsonObject.getString("code");
			String sql = "select trainingId from cloud_train_content where nvl(dr,0) = 0";
			List<String> list = (List<String>) dao.executeQuery(sql, new ColumnListProcessor());
			List<CloudTrainingContentVO> insertList = new ArrayList();
			if("0".equals(code)) {
				JSONObject paging = jsonObject.getJSONObject("paging");
				if(paging != null) {
					totalPages = paging.getInteger("totalPages");//总页数
					int totalCount = paging.getInteger("totalCount");//总条数
//					if(totalPages > 1) {
//						count = Integer.valueOf(totalCount) / 500;
//					}
				}
				JSONArray jsonArray = jsonObject.getJSONArray("data");//数据data
				if(jsonArray != null) {
					List<CloudTrainingContentVO> trainList = JSONArray.parseArray(jsonArray.toJSONString(), CloudTrainingContentVO.class);
					if(trainList != null) {
//						for(int i = 0;i < jsonArray.size(); i++) {
//							JSONObject obj = jsonArray.getJSONObject(i);
//							for(CloudTrainingContentVO train : trainList) {
//								if(train.getTrainingId() != null && train.getTrainingId().equals(obj.get("trainingId"))) {
//									train.setTrianStatus(obj.getString("status"));
//								}
//							}
//						}
						for(CloudTrainingContentVO psnVO : trainList) {
							if(!list.contains(psnVO.getTrainingId())) {
								insertList.add(psnVO);
							}
						}
					}
				}
			}
			if(totalPages > 1) {
				for(int i = 2; i < totalPages;i++) {
					result = impl.gettrainingcontentlistpage(bodyMap, i, 500);
					JSONObject ijsonObject = JSONObject.parseObject(result);
					String icode = jsonObject.getString("code");
					if("0".equals(icode)) {
						JSONArray jsonArray = ijsonObject.getJSONArray("data");//数据data
						if(jsonArray != null) {
							List<CloudTrainingContentVO> trainList = JSONArray.parseArray(jsonArray.toJSONString(), CloudTrainingContentVO.class);
							if(trainList != null) {
//								for(int j = 0;j < jsonArray.size(); j++) {
//									JSONObject obj = jsonArray.getJSONObject(j);
//									for(CloudTrainingContentVO train : trainList) {
//										if(train.getTrainingId() != null && train.getTrainingId().equals(obj.get("trainingId"))) {
//											train.setTrianStatus(obj.getString("status"));
//										}
//									}
//								}
								for(CloudTrainingContentVO psnVO : trainList) {
									if(!list.contains(psnVO.getTrainingId())) {
										insertList.add(psnVO);
									}
								}
							}
						}
					}
				}
			}
			
			//判断是否已存在中间表
			if(insertList.size() > 0) {
				String[] pks = dao.insertVOList(insertList);
				alertObj.setReturnObj(pks);
				alertObj.setMsgTitle("保存所有培训课程"+pks.length+"条数据");
			}
		}
		return alertObj;
	}
}
