package com.persagy.dmp.common.utils;


import cn.hutool.core.collection.CollUtil;
import com.persagy.dmp.common.exception.BusinessException;
import com.persagy.dmp.common.model.response.CommonResult;

import java.util.List;

/**
 * 响应结果包装助手
 * @author Charlie Yu
 * @date 2021-03-25
 */
public class ResultHelper {

    /** 成功 */
    private static final String SUCCESS = "success";
    /** 失败 */
    private static final String FAIL = "fail";

    /**
     * 成功消息
     * @return
     */
    public static <T> CommonResult<T> success(){
//        return new CommonResult(ResponseCode.A00000.getCode(), ResponseCode.A00000.getDesc());
        return success(null);
    }

    /**
     * 成功消息
     * @return
     */
    public static <T> CommonResult<T> success(String respMsg){
//        return new CommonResult(ResponseCode.A00000.getCode(), respMsg);
        return new CommonResult<>(SUCCESS, respMsg);
    }

    /**
     * 失败消息
     * @param respMsg
     * @return
     */
    public static CommonResult<Void> failure(String respMsg) {
        return failure(FAIL, respMsg);
    }

    /**
     * 失败消息
     * @param respCode
     * @param respMsg
     * @return
     */
    public static CommonResult<Void> failure(String respCode, String respMsg) {
        return new CommonResult<>(respCode, respMsg);
    }

    /**
     * 单个数据
     * @param content
     * @param <T>
     * @return
     */
    public static <T> CommonResult<T> single(T content) {
        return single(content, null);
    }

    /**
     * 单个数据
     * @param content
     * @param <T>
     * @return
     */
    public static <T> CommonResult<T> single(T content, String desc) {
//        return new CommonResult(ResponseCode.A00000.getCode(), desc, content);
        return new CommonResult<>(SUCCESS, desc, content);
    }

    /**
     * 多个数据 - 列表
     * @param records
     * @param <T>
     * @return
     */
    public static <T> CommonResult<List<T>> multi(List<T> records) {
        return multi(records, CollUtil.size(records));
    }

    /**
     * 多个数据 - 分页
     * @param page
     * @param <T>
     * @return
     */
//    public static <T> CommonResult<PageList<T>> multi(IPage<T> page) {
//        List<T> records = page == null ? null : page.getRecords();
//        long total = page == null ? 0 : page.getTotal();
//        return multi(records, total);
//    }

    /**
     * 多个数据
     * @param records
     * @param total
     * @param <T>
     * @return
     */
    public static <T> CommonResult<List<T>> multi(List<T> records, long total) {
        // 容错处理，出现总数比实际数少时，以实际数为准（page查询全部时，total为0）
        int size = CollUtil.size(records);
        if(total < size) {
            total = size;
        }
        CommonResult<List<T>> result = success();
        result.setData(records);
        result.setCount(total);
        return result;
//        PageList<T> pageList = new PageList<>(records, total);
//        return new CommonResult(ResponseCode.A00000.getCode(), ResponseCode.A00000.getDesc(), pageList);
    }

    /**
     * 读取结果
     * @param result
     * @param <T>
     * @return 对应的结果
     */
    public static <T> T getContent(CommonResult<T> result) {
//        if(!ResponseCode.A00000.getCode().equals(result.getRespCode())) {
//            throw new BusinessException(result.getRespCode(), result.getRespMsg());
//        }
//        return result.getContent();
        if(!SUCCESS.equals(result.getResult())) {
            throw new BusinessException(result.getResult(), result.getMessage());
        }
        return result.getData();
    }
}
