package com.qmth.teachcloud.exchange.common.entity;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.qmth.teachcloud.exchange.common.base.BaseEntity;
import com.qmth.teachcloud.exchange.common.bean.dto.syssetting.SettingObject;
import com.qmth.teachcloud.exchange.common.bean.dto.syssetting.SimpleObject;
import com.qmth.teachcloud.exchange.common.contant.SysSettingConstant;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 系统设置表
 * </p>
 */
@TableName("sys_setting")
public class SysSetting extends BaseEntity implements Serializable {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "学校id")
    @JsonSerialize(using = ToStringSerializer.class)
    @TableField("school_id")
    private Long schoolId;


    /**
     * 参数配置详情
     */
    @TableField("parameter_object")
    private String parameterObject;

    public Long getSchoolId() {
        return schoolId;
    }

    public void setSchoolId(Long schoolId) {
        this.schoolId = schoolId;
    }

    public String getParameterObject() {
        return parameterObject;
    }

    public void setParameterObject(String parameterObject) {
        this.parameterObject = parameterObject;
    }

    /**
     * 初始化数据
     */
    public static SysSetting init(BasicSchool basicSchool) {
        List<SettingObject> settingObjects = new ArrayList<>();

        List<SimpleObject> basicSimpleObjects = new ArrayList<>();
        basicSimpleObjects.add(new SimpleObject(SysSettingConstant.FILE_HOST_URL, "文件访问地址", "", "text", 1, true));
        settingObjects.add(new SettingObject("访问参数设置", 1, basicSimpleObjects));

        String schoolCode = basicSchool.getCode();
        if ("whdx".equals(schoolCode) || "test-school-1".equals(schoolCode)) {
            List<SimpleObject> simpleObjects1 = new ArrayList<>();
            simpleObjects1.add(new SimpleObject(SysSettingConstant.WHU_APP_KEY, "App Key", "", "text", 1, true));
            simpleObjects1.add(new SimpleObject(SysSettingConstant.WHU_APP_SECRET, "App Secret", "", "text", 2, true));
            simpleObjects1.add(new SimpleObject(SysSettingConstant.WHU_TOKEN_URL, "Token接口", "", "text", 3, true));
            simpleObjects1.add(new SimpleObject(SysSettingConstant.WHU_KW_URL, "考务数据接口", "", "text", 4, true));
            settingObjects.add(new SettingObject("对接参数设置", 2, simpleObjects1));
        } else if ("cdut".equals(schoolCode)) {
            List<SimpleObject> simpleObjects1 = new ArrayList<>();
            simpleObjects1.add(new SimpleObject(SysSettingConstant.CDUT_APP_KEY, "App Key", "", "text", 1, true));
            simpleObjects1.add(new SimpleObject(SysSettingConstant.CDUT_APP_SECRET, "App Secret", "", "text", 2, true));
            simpleObjects1.add(new SimpleObject(SysSettingConstant.CDUT_TOKEN_URL, "Token接口", "", "text", 3, true));
            simpleObjects1.add(new SimpleObject(SysSettingConstant.CDUT_KW_URL, "考务数据接口", "", "text", 4, true));
            settingObjects.add(new SettingObject("对接参数设置", 2, simpleObjects1));
        }

        List<SimpleObject> cloudMarkingSimpleObjects = new ArrayList<>();
        cloudMarkingSimpleObjects.add(new SimpleObject(SysSettingConstant.CLOUD_MARKING_ACCESS_KEY, "Access Key", basicSchool.getAccessKey(), "text", 1, true));
        cloudMarkingSimpleObjects.add(new SimpleObject(SysSettingConstant.CLOUD_MARKING_ACCESS_SECRET, "Access Secret", basicSchool.getAccessSecret(), "text", 2, true));
        cloudMarkingSimpleObjects.add(new SimpleObject(SysSettingConstant.CLOUD_MARKING_ROOT_URL, "云阅卷地址", "", "text", 3, true));
        settingObjects.add(new SettingObject("云阅卷参数设置", 3, cloudMarkingSimpleObjects));

        SysSetting sysSetting = new SysSetting();
        sysSetting.setSchoolId(basicSchool.getId());
        sysSetting.setParameterObject(JSON.toJSONString(settingObjects));
        return sysSetting;
    }
}
