package com.reyun.saas.mob.openapi.dto;

import com.reyun.saas.common.exception.CustomException;
import com.reyun.saas.common.model.Constant;
import com.reyun.saas.common.restResult.ResultCode;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.util.StringUtils;

import java.io.Serializable;

/**
 * @author: hanzepeng
 * @Date: 2022/02/11/13:40
 * @Description: app输入DTO
 */
@Data
@ApiModel(value = "AppInputDTO", description = "OpenApi APP入参对象")
public class AppInputDTO implements Serializable {

    @ApiModelProperty(value = "应用包名")
    private String bid;

    @ApiModelProperty(value = "应用名称")
    private String appName;

    @ApiModelProperty(value = "系统类型 1为iOS 2位为Android")
    private String os;

    @ApiModelProperty(value = "用户唯一标识")
    private String oid;

    public String getStrOS(){
        //校验系统类型
        if(!(Constant.OS_IOS_CODE.equals(this.os)
                || Constant.OS_ANDROID_CODE.equals(this.os))){
            throw new CustomException(ResultCode.OS_TYPE_ERROR);
        }
       return Constant.OS_IOS_CODE.equals(this.os)?"iOS":"Android";
    }

    /**
     * 校验参数
     */
    public void checkParam(){
        //去空格
        this.paramTrim();

        //非空校验
        if(StringUtils.isEmpty(bid)
                || StringUtils.isEmpty(appName)
                || StringUtils.isEmpty(oid)){
            throw new CustomException(ResultCode.REQUEST_PARAM_ERROR);
        }

        //校验系统类型
        try {
            //校验浮点数参数
            Integer.parseInt(this.os);
        } catch (NumberFormatException e) {
            throw new CustomException(ResultCode.REQUEST_PARAM_ERROR);
        }
        if(!(Constant.OS_ANDROID_CODE.equals(this.os)
                || Constant.OS_IOS_CODE.equals(this.os))){
            throw new CustomException(ResultCode.OS_TYPE_ERROR);
        }

        //校验bid长度
        if(bid.length() > 128){
            throw new CustomException(ResultCode.BID_FORMAT_ERROR);
        }

    }

    /**
     * 参数去空格
     */
    public void paramTrim(){
        if(!StringUtils.isEmpty(this.bid)){
            this.bid = this.bid.trim();
        }

        if(!StringUtils.isEmpty(this.appName)){
            this.appName = this.appName.trim();
        }

        if(!StringUtils.isEmpty(this.oid)){
            this.oid = this.oid.trim();
        }

        if(!StringUtils.isEmpty(this.os)){
            this.os = this.os.trim();
        }
    }

}
