package com.mortals.xhx.base.system.param.web;


import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.common.utils.IpUtils;
import com.mortals.framework.annotation.UnAuth;
import com.mortals.framework.common.code.PageDisplayType;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.framework.util.FileUtil;
import com.mortals.framework.web.BaseCRUDJsonBodyMappingController;
import com.mortals.xhx.base.system.param.model.ParamEntity;
import com.mortals.xhx.base.system.param.model.ParamQuery;
import com.mortals.xhx.base.system.param.service.ParamService;
import com.mortals.xhx.common.code.DataSatusEnum;
import com.mortals.xhx.common.code.ModStatusEnum;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

import static com.mortals.xhx.common.key.Constant.PARAM_SERVER_HTTP_URL;

/**
 * 参数信息
 *
 * @author: zxfei
 * @date: 2022/5/7 15:38
 */
@RestController
@RequestMapping("param")
public class ParamController extends BaseCRUDJsonBodyMappingController<ParamService, ParamEntity, Long> {

    public ParamController() {
        super.setFormClass(ParamForm.class);
        super.setModuleDesc("参数信息");
    }

    @Override
    protected void init(Map<String, Object> model, Context context) {
        Map<String, Object> status = new HashMap<String, Object>();
        status.put("validStatus", DataSatusEnum.getEnumMap(DataSatusEnum.CLOSE.getValue(),
                DataSatusEnum.DELETE.getValue(), DataSatusEnum.OVERDUE.getValue(), DataSatusEnum.USEOUT.getValue()));
        status.put("modStatus", ModStatusEnum.getEnumMap());
        status.put("displayType", getPageDisplayType());
        model.put(KEY_RESULT_DICT, status);
    }

    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, value = "key")
    @UnAuth
    public String getValueByKey(@RequestParam(name = "key") String key) {
        JSONObject jsonObject = new JSONObject();
        String busiDesc = "通过key查询参数值" + this.getModuleDesc();
        try {
            String value = this.service.getValueByKey(key);
            jsonObject.put(KEY_RESULT_DATA, value);
            recordSysLog(request, busiDesc + " 【成功】");
            jsonObject.put(KEY_RESULT_CODE, VALUE_RESULT_SUCCESS);
            jsonObject.put(KEY_RESULT_MSG, busiDesc + "成功！");
        } catch (Exception e) {
            log.error("获取异常", e);
            jsonObject.put(KEY_RESULT_CODE, VALUE_RESULT_FAILURE);
            jsonObject.put(KEY_RESULT_MSG, super.convertException(e));
        }
        return jsonObject.toJSONString();
    }


    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, value = "dict")
    @UnAuth
    public String getDict(@RequestParam(name = "first") String first, @RequestParam(name = "second") String second) {
        JSONObject jsonObject = new JSONObject();
        String busiDesc = "通过key查询字典参数值" + this.getModuleDesc();
        try {
            Map<String, String> dict = this.service.getParamBySecondOrganize(first, second);
            // String value = this.service.getValueByKey(key);
            jsonObject.put(KEY_RESULT_DATA, dict);
            recordSysLog(request, busiDesc + " 【成功】");
            jsonObject.put(KEY_RESULT_CODE, VALUE_RESULT_SUCCESS);
            jsonObject.put(KEY_RESULT_MSG, busiDesc + "成功！");
        } catch (Exception e) {
            log.error("获取异常", e);
            jsonObject.put(KEY_RESULT_CODE, VALUE_RESULT_FAILURE);
            jsonObject.put(KEY_RESULT_MSG, super.convertException(e));
        }
        return jsonObject.toJSONString();
    }


    /**
     * 更新终端访问服务器应用ip地址
     */
    @GetMapping(value = "updateServerIp")
    @UnAuth
    public String updateServerip(String serverIp) {

        JSONObject jsonObject = new JSONObject();
        try {
            //校验ip地址
            boolean ipv4 = IpUtils.isIpv4(serverIp);
            if (!ipv4) {
                throw new AppException("请输入正确的ip地址");
            }

            ParamQuery paramQuery = new ParamQuery();
            paramQuery.setParamKey(PARAM_SERVER_HTTP_URL);

            ParamEntity paramEntity = this.service.selectOne(paramQuery);

            if (!ObjectUtils.isEmpty(paramEntity)) {
                String value = "http://" + serverIp + ":11078";
                paramEntity.setParamValue(value);
                this.service.update(paramEntity);
            }
            jsonObject.put(KEY_RESULT_CODE, VALUE_RESULT_SUCCESS);
            jsonObject.put(KEY_RESULT_MSG, "更新终端访问服务端ip成功！");
        } catch (Exception e) {
            log.error("获取异常", e);
            jsonObject.put(KEY_RESULT_CODE, VALUE_RESULT_FAILURE);
            jsonObject.put(KEY_RESULT_MSG, super.convertException(e));
        }
        return jsonObject.toJSONString();
    }


    private Map<String, Object> getPageDisplayType() {
        PageDisplayType[] pageDisplayTypes = PageDisplayType.values();
        Map<String, Object> result = new HashMap<>(pageDisplayTypes.length);
        for (PageDisplayType pageDisplayType : pageDisplayTypes) {
            result.put(pageDisplayType.getValue() + "", pageDisplayType.getDesc());
        }
        return result;
    }

    public static void main(String[] args) {
        FileUtil.delete("E:\\pic\\1.png");
    }

}