package com.tzy.common.dto;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.tzy.common.exception.ServiceException;
import com.tzy.common.utils.UserUtils;
import com.tzy.common.utils.bean.DozerUtils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

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

/**
 * 接口入参
 */
@Data
@Slf4j
@JsonIgnoreProperties(ignoreUnknown = true)
public class InDto implements Serializable {
    private static final long serialVersionUID = 1L;
    //appid
    private String appid;
    //openid
    private String openid;
    //unionid
    private String unionid;
    //用户类型: WX_WEB (微信公众号) WX_APPLET (微信小程序) THIRD (第三方) WX_AUTH(微信授权登陆)
    private String userType;
    //userid
    private Integer userId;
    //账号
    private String username;
    //每页数据条数
    private Integer pageSize =10;
    //页码数
    private Integer pageNo=1;
    //其他数据
    private Map<String,Object> data;
    //部门id
    private Integer departId;

    private Integer offset;

    private long timestamp;

    private String version;
    // 极光id
    private String smsRegisterId;
    //签名
    private String sign;

    public Object get(String key){
        if(null != data && !data.isEmpty()){
            return this.data.get(key);
        }
        return null;
    }
    public Object getDefault(String key,Object value){
        if(null != data && !data.isEmpty()){
            try{
                return null == this.data.get(key) ? value : this.data.get(key);
            }catch (Exception e){
            }
        }
        return value;
    }
    public String getString(String key){
        if(null != data && !data.isEmpty()&&this.data.containsKey(key)){
            return null != this.data.get(key) ? String.valueOf(this.data.get(key)) : null;
        }
        return null;
    }

    public Boolean existKeyString(String key){
        if(null != data && !data.isEmpty()&&this.data.containsKey(key)){
            return true;
        }
        return false;
    }

    public Integer getIntegerParam(String key) {
        if (CollectionUtils.isEmpty(data)) {
            return null;
        }
        return (Integer) this.data.get(key);
    }

    public boolean getBooleanParam(String key) {
        if (CollectionUtils.isEmpty(data)) {
            return false;
        }
        Boolean b = (Boolean) this.data.get(key);
        return b != null && b;
    }

    public String getStringDefault(String key,String value){
        if(null != data && !data.isEmpty()){
            try {
                String ret = String.valueOf(this.data.get(key));
                return StringUtils.isEmpty(ret) ? value : ("null".equals(ret) ? value : ret);
            }catch (Exception e){}
        }
        return value;
    }
    public Integer getIntegerDefault(String key,Integer value){
        if(null != data && !data.isEmpty()){
            try {
                Integer ret = (Integer) this.data.get(key);
                return StringUtils.isEmpty(ret) ? value :  ret;
            }catch (Exception e){
            }
        }
        return value;
    }
    public InDto put(String key ,Object value){
        if(null == this.data) this.data = new HashMap<>();
        this.data.put(key, value);
        return this;
    }
    public InDto remove(String key){
        if(null != this.data && !this.data.isEmpty()){
            this.data.remove(key);
        }
        return this;
    }

    public Integer getPageSize() {
        if(this.pageSize == null){
            return pageSize;
        }
        return this.pageSize>0? this.pageSize:10;
    }

    public Integer getPageNo() {
        if(this.pageNo == null){
            return pageNo;
        }
        return this.pageNo>0? this.pageNo:1;
    }

    public Integer getOffset(){
        if(this.pageNo==null||this.pageSize==null){
            return 0;
        }
        return((pageNo) - 1 ) * pageSize;
    }

    public Integer getUserId() {
        UserInfo userInfo = UserUtils.getSimpleUserInfo();
        if(userInfo!=null){
            return userInfo.getId();
        }
        return userId;
    }

    /**
     * 转换indto中data为指定参数
     *
     * @param param
     * @param <T>
     * @return
     */
    public <T> T buildParam( T param) {
        try {
            DozerUtils.copy(this.data,param);
            return param;
        } catch (Exception e) {
            log.error("参数转换失败:", e);
        }
        throw new ServiceException(500, "参数获取失败!");
    }
}
