package com.rrtx.services.common.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rrtx.fap.starter.genid.service.IdService;
import com.rrtx.services.common.constant.ContextParam;
import com.rrtx.services.common.entity.ServicesConstant;
import com.rrtx.services.common.redis.IRedisService;
import org.springframework.beans.factory.annotation.Autowired;

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

/**
 * ClassName: BaseController<br>
 * Description: BaseController <br>
 * Company: rrtx
 *
 * @author rrtx
 * @version v1.0.0    2019/7/24 4:44 PM  xugz    文件初始创建
 */
public class BaseController {

	/** 超时时间 */
	private final long TIME_OUT = 30 * 60 * 1000;

	@Autowired
    IdService superIdService; // 获取流水号


	@Autowired
    IRedisService superRedisService;


	protected void setTempVal(String key, String val) {
		superRedisService.set(key, val, TIME_OUT / 2);
	}

	protected String getTempVal(String key) {
		return superRedisService.get(key);
	}

	protected void removeTempVal(String key) {
		superRedisService.del(key);
	}

//	protected void setSessionVal(String key, String val) {
//		User user = getCurrentUser();
//		key = user.getToken() + key;
//		superRedisService.set(key, val, TIME_OUT / 2);
//	}
//	protected String getSessionVal(String key) {
//		User user = getCurrentUser();
//		key = user.getToken() + key;
//		return superRedisService.get(key);
//	}
//
//	protected void removeSessionVal(String key) {
//		User user = getCurrentUser();
//		key = user.getToken() + key;
//		superRedisService.del(key);
//	}
//	protected void insertLog(String info, String result) {
//		insertLog(getCurrentUser().getId(), info, result);
//	}
	protected void insertLog(String userId, String info, String result, String bsnCode) {
//		PbLog log = new PbLog();
//		log.setLogno("L".concat(CommonUtil.objToString(superIdService.genId())));
//		log.setDatetime(DateUtil.getCurrentTime());
//		log.setUserid(userId);
//		log.setBsncode(ContextParam.BSN_CODE.get());
//		log.setResult(result);
//		log.setOs(ContextParam.OS.get());
//		log.setOsName(ContextParam.OS_NAME.get());
//		log.setOsVersion(ContextParam.OS_VERSION.get());
//		log.setDevNo(ContextParam.DEV_NO.get());
//		log.setImsi(ContextParam.DEV_IMSI.get());
//		log.setInfo(info);
//		log.setIp(IPUtil.getIpAddr(HttpContextUtil.getHttpServletRequest()));
//		logService.insertSelective(log);
	}
	protected void insertLog(String userId, String info, String result) {
		insertLog(userId, info, result, ContextParam.BSN_CODE.get());
	}

//	protected void insertLog(String info) {
//		insertLog(info, "00000000");
//	}
//	protected User getCurrentUser() throws BusinessCodeException {
//			User user = (User)superRedisService.getObject(ContextParam.TOKEN.get() + "PERSON_INF", User.class);
//
////		User user = (User)superRedisService.getObject("CHECKpkpe632ia3lqjwv7vcz6m7gda6l1uyguxxlfgp9rf65xwas4r6" + "PERSON_INF", User.class);
//			if(user == null || StringUtils.isEmpty(user.getId())) {
//				throw new BusinessCodeException("PUB10005");
//			}
//			else {
//				user.setLanguage(ContextParam.PRO_LANGUAGE.get());
//				superRedisService.set(ContextParam.TOKEN.get() + "PERSON_INF", user);
//			}
//			return user;
//	}

//	protected void login(User user) {
//		System.out.println("token："+user.getToken());
//		superRedisService.set(user.getToken() + "PERSON_INF", user, TIME_OUT);
//	}

	protected Map<String, Object> getDataTable(IPage<?> pageInfo) {
		return getDataTable(pageInfo, ServicesConstant.DATA_MAP_INITIAL_CAPACITY);
	}

	protected Map<String, Object> getDataTable(IPage<?> pageInfo, int dataMapInitialCapacity) {
		Map<String, Object> data = new HashMap<>(dataMapInitialCapacity);
		data.put("rows", pageInfo.getRecords());
		data.put("total", pageInfo.getTotal());
		return data;
	}

	protected <T,S> IPage<T>  page4Vo( IPage<S> source, List<T> voList){
		Page<T> target = new Page<>(source.getCurrent(), source.getSize(), source.getTotal());
		target.setRecords(voList);
		return target;
	}

}
