package com.tzy.common.dto;

import lombok.Data;
import lombok.ToString;

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

/**
 * 输出参数
 */
@Data
@ToString
public class OutDTO implements Serializable {
    private static final long serialVersionUID = 1L;
    private boolean success = true;

    private Integer code = 0;

    private String msg = "成功";

    private Map<String ,Object> data;
    //时间戳
    private long timestamp = System.currentTimeMillis();
    //数据总量
    private Integer total;
    //每页条数
    private Integer pageSize = 10;
    //页码
    private Integer pageNo = 1;
    //总页数
    private Integer pages;
    //
    private String roleCode;

    public OutDTO(){}

    public OutDTO(boolean success,Integer code,String msg){
        this.success =success;
        this.code = code;
        this.msg = msg;
    }

    /**
     *
     * @param pageSize 每页条数
     * @param pageNo 页码
     * @param total 总量
     * @return
     */
    public OutDTO setCount(Integer pageSize ,Integer pageNo ,Integer total){
        this.total = total;
        this.pageSize = pageSize;
        this.pageNo = pageNo;
        if(total % pageSize > 0){
            this.pages = total / pageSize + 1;
        }else{
            this.pages = total / pageSize;
        }
        return this;
    }

    /**
     * @param inDto
     * @param total
     * 设置分页
     * @return
     */
    public OutDTO setCount(InDto inDto,Integer total){
        this.total = total;
        this.pageSize = inDto.getPageSize();
        this.pageNo = inDto.getPageNo();
        if(total % this.pageSize > 0){
            this.pages = total / this.pageSize + 1;
        }else{
            this.pages = total / this.pageSize;
        }
        return this;
    }

    public OutDTO page(InDto inDto,Integer total , String key, List datas){
        OutDTO outDTO = this.setCount(inDto, total);
        if(null != datas && datas.size() > 0 ){
            if(datas.size() >= inDto.getPageSize() * inDto.getPageNo()){
                datas = datas.subList(inDto.getPageSize() *(inDto.getPageNo()-1),inDto.getPageSize() *inDto.getPageNo() );
            }else{
                datas = datas.subList(inDto.getPageSize() *(inDto.getPageNo()-1),datas.size());
            }
        }
        outDTO.put(key,datas);
        return outDTO;
    }

    public OutDTO buildPage(InDto inDto,Integer total , String key, List datas){
        OutDTO outDTO = this.setCount(inDto, total);
        outDTO.put(key,datas);
        return outDTO;
    }

    /**
     *
     * @param key
     * @param value
     * @return
     */
    public OutDTO put(String key,Object value){
        if(null == this.data) this.data = new HashMap<>();
        this.data.put(key,value);
        return this;
    }

    /**
     *
     * @param key
     * @param value
     * @return
     */
    public OutDTO putString(String key,String value){
        if(null == this.data) this.data = new HashMap<>();
        this.data.put(key,value);
        return this;
    }

    /**
     *
     * @param key
     * @return
     */
    public Object get(String key){
        if(null != this.data) return this.data.get(key);
        return null;
    }

    /**
     * get String
     * @param key
     * @return
     */
    public String getString(String key){
        if(null != this.data)  return (String) this.data.get(key);
        return null;
    }

    public static OutDTO ok(){
        OutDTO outDTO = new OutDTO();
        outDTO.put("ok","");
        return outDTO;
    }

    public static OutDTO ok(String msg) {
        return new OutDTO(true,0,msg);
    }

    public static OutDTO ok(Integer code ,String msg){
        return new OutDTO(true,code,msg);
    }

    public static OutDTO ok(Map data) {
        OutDTO r = new OutDTO();
        r.setData(data);
        return r;
    }

    /**
     * 失败
     * @param code
     * @param msg
     * @return
     */
    public static OutDTO error(Integer code,String msg){
        return new OutDTO(false,code,msg);
    }

    /**
     *
     * @param message
     * @return
     */
    public static OutDTO error500(String message) {
        return new OutDTO(false,500,message);
    }

    /**
     * 无权限访问返回结果
     */
    public static OutDTO noauth(String msg) {
        return error(510, msg);
    }



}
