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


import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.annotation.RepeatSubmit;
import com.mortals.framework.annotation.UnAuth;
import com.mortals.framework.common.code.PageDisplayType;
import com.mortals.framework.model.Context;

import com.mortals.framework.util.FileUtil;
import com.mortals.framework.web.BaseCRUDJsonBodyMappingController;
import com.mortals.xhx.common.pdu.WeightPdu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import com.mortals.framework.web.BaseCRUDJsonMappingController;
import com.mortals.xhx.base.system.param.model.ParamEntity;
import com.mortals.xhx.base.system.param.service.ParamService;
import com.mortals.xhx.common.code.DataSatusEnum;
import com.mortals.xhx.common.code.ModStatusEnum;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

import static com.mortals.xhx.common.key.ParamKey.SYS_PARAM_WEIGHT;

/**
 * 参数信息
 *
 * @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);
    }


    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;
    }


    @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);
            if (ObjectUtils.isEmpty(value) && key.equals(SYS_PARAM_WEIGHT)) {
                WeightPdu weightPdu = new WeightPdu();
                value = JSONObject.toJSONString(weightPdu);
            }
            Long id = this.service.getIdByKey(key);
            jsonObject.put(KEY_RESULT_DATA, value);
            jsonObject.put("id", id);
            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();
    }

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

}