package com.tofly.qualitycontrol.util;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.tofly.pojo.message.Messagepush;
import com.tofly.qualitycontrol.qcenum.QualityControlMessageTopicEnum;
import com.tofly.qualitycontrol.qcenum.QualityControlMessageTypeEnum;
import org.springframework.validation.BindingResult;

import java.util.List;

import static com.alibaba.fastjson.JSON.parseObject;
import static com.alibaba.fastjson.JSON.toJSONString;
import static org.apache.commons.lang.StringUtils.*;

/**
 * @author B
 */
public class QualityControlUtil {

	public final static Boolean isNotNull(String string) {
		return isNotBlank(string) && isNotEmpty(string);
	}

	public final static Boolean isNull(String string) {
		return isBlank(string) && isEmpty(string);
	}

	public final static <T> T getVO(Object object,Class<T> clazz) {
		return parseObject(toJSONString(object),clazz);
	}

	public final static Messagepush getMessagePush(Long id,String message,QualityControlMessageTopicEnum topicEnum,QualityControlMessageTypeEnum typeEnum) {
		Messagepush messagepush = new Messagepush();
		messagepush.setTableId(id);
		messagepush.setMessage(message);
		messagepush.setTopic(topicEnum.getTopic());
		messagepush.setTableName(topicEnum.getTableName());
		messagepush.setType(typeEnum.getCode());
		return messagepush;
	}

	public final static List<String> getErrorStrings(BindingResult bindingResult) {
		List<String> strings = Lists.newArrayList();
		bindingResult.getAllErrors().forEach(error -> strings.add(error.getDefaultMessage()));
		return strings;
	}

	public final static <T,V> IPage<V> getIPage(Page<T> page,List<V> voList,long total) {
		IPage<V> voPage = new Page<>(page.getCurrent(),page.getSize(),total);
		voPage.setRecords(voList);
		return voPage;
	}

}
