package com.persagy.proxy.adm.request;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSONArray;
import com.persagy.proxy.adm.utils.AdmEntityTransferUtil;
import com.persagy.proxy.common.entity.DmpResult;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;

/**
 * ADM 通用返回结果
 * @author Charlie Yu
 * @date 2021-08-16
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class AdmResponse {
    private final static String SUCCESS = "success";
    private final static String FAILURE = "failure";
    /** 结果 */
    private String result;
    /** 消息 */
    private String message;
    /** 符合条件的总记录数 */
    private Long total;
    private Long count;
    /** 页码 */
    private Integer pageNumber;
    /** 每页条数 */
    private Integer pageSize;
    /** 查询到的数据列表 */
    private List<?> content;
    /**楼层id*/
    private String floorId;
    /**空间功能类型*/
    private String zoneType;

    public static AdmResponse success() {
        return new AdmResponse(SUCCESS, "", 0L,null, 1, 500, new ArrayList<>(1),null,null);
    }

    public static AdmResponse failure(String message) {
        return new AdmResponse(FAILURE, message, null, null, null, null, new ArrayList<>(1),null,null);
    }

    public static AdmResponse success(List<?> content) {
        return new AdmResponse(SUCCESS, "", CollUtil.isEmpty(content) ? 0L :content.size()*1L,null, 1, 50, CollUtil.isEmpty(content) ? new ArrayList<>(1): content,null,null);
    }
    public static AdmResponse success(List<?> content, AdmQueryCriteria request) {
        int pagesize = 50;
        if(request!= null && request.getPageNumber() != null && request.getPageNumber() > 0)
            pagesize = request.getPageNumber();
        int pageNum = 1;
        if(request!= null && request.getPageSize() != null && request.getPageSize() > 0){
            pageNum = request.getPageSize();
        }
        return new AdmResponse(SUCCESS, "", CollUtil.isEmpty(content) ? 0L :content.size()*1L,CollUtil.isEmpty(content) ? 0L :content.size()*1L, pageNum, pagesize, CollUtil.isEmpty(content) ? new ArrayList<>(1): content,null,null);
    }
    public static AdmResponse success(String message) {
        return new AdmResponse(SUCCESS, message, null,null, null,null,null,null,null);
    }

    /**
     * 从数据中台结果转换
     * @param admRequest
     * @param dmpResult
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> AdmResponse fromDmp(AdmQueryCriteria admRequest, DmpResult<JSONArray> dmpResult, Class<T> clazz) {
        if(!DmpResult.SUCCESS.equals(dmpResult.getResult())) {
            return failure("调用中台查询接口出错：" + dmpResult.getMessage());
        }
        // 转换为结果
        List<T> admVOs = AdmEntityTransferUtil.toAdmMultiEntity(dmpResult.getData(), admRequest.getProjection(), clazz);
        Long total = dmpResult.getCount() == null ? null : dmpResult.getCount().longValue();
        Integer pageNo = admRequest == null ? null : admRequest.getPageNumber();
        Integer pageSize = admRequest == null ? null : admRequest.getPageSize();
        if(admRequest.isOnlyCount()){
            return new AdmResponse(SUCCESS, null, null,total, pageNo, pageSize, CollUtil.isEmpty(admVOs) ? new ArrayList<>(1) : admVOs,null,null);
        }else {
            return new AdmResponse(SUCCESS, null, total,null, pageNo, pageSize, CollUtil.isEmpty(admVOs) ? new ArrayList<>(1) : admVOs,null,null);
        }
    }
}
