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


import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.framework.model.OrderCol;

import com.mortals.framework.web.BaseCRUDJsonBodyMappingController;
import com.mortals.xhx.base.system.menu.model.MenuQuery;
import com.mortals.xhx.base.system.menu.model.MenuTreeSelect;
import com.mortals.xhx.common.code.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import com.mortals.framework.web.BaseCRUDJsonMappingController;
import com.mortals.xhx.base.system.menu.model.MenuEntity;
import com.mortals.xhx.base.system.menu.service.MenuService;

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

import static com.mortals.framework.ap.SysConstains.RESULT_KEY;

/**
 * 菜单信息
 *
 * @author: zxfei
 * @date: 2024/9/6 16:23
 */
@RestController
@RequestMapping("menu")
public class MenuController extends BaseCRUDJsonBodyMappingController<MenuService, MenuEntity, Long> {

    public MenuController() {
        super.setModuleDesc("菜单信息");
    }

    @Override
    protected void doListBefore(MenuEntity query, Map<String, Object> model, Context context) throws AppException {
        List<OrderCol> orderColList = new ArrayList<>();
        orderColList.add(new OrderCol("orderId"));
        query.setOrderColList(orderColList);
    }

    @Override
    protected void init(Map<String, Object> model, Context context) {
        this.addDict(model, "linkType", LinkTypeEnum.getEnumMap());
        this.addDict(model, "commMenu", CommMenuEnum.getEnumMap());
        this.addDict(model, "menuType", MenuTypeEnum.getEnumMap());
        this.addDict(model, "authType", AuthTypeEnum.getEnumMap());
        this.addDict(model, "visible", VisibleEnum.getEnumMap());
        this.addDict(model, "status", StatusEnum.getEnumMap());
    }

    /**
     * 改变状态
     */
    @ApiOperation(value = "改变状态", notes = "改变状态")
    @RequestMapping(value = "change/status")
    public String changeStatus(@RequestBody MenuEntity query) {
        JSONObject ret = new JSONObject();
        Context context = getContext();
        try {
            MenuEntity entity = this.service.get(query.getId(), context);//.doSubmitAudit(form.getEntity(), context);
            if (null == entity) {
                throw new AppException("菜单不存在!");
            }
            if (null == entity.getStatus()) {
                throw new AppException("菜单状态不能为空!");
            }
            if (entity.getStatus() != DataSatus.ENABLE.getValue() && entity.getStatus() != DataSatus.DISENABLE.getValue()) {
                throw new AppException("非法菜单状态!");
            }
            String busiDesc = DataSatus.getByValue(entity.getStatus()).getDesc();
            entity.setStatus(entity.getStatus());
            this.service.update(entity, context);
            ret.put(KEY_RESULT_CODE, VALUE_RESULT_SUCCESS);
            ret.put(KEY_RESULT_MSG, busiDesc + "成功");
            recordSysLog(request, busiDesc + " 【成功】 [id:" + entity.getId() + "]");

        } catch (Exception e) {
            ret.put(KEY_RESULT_CODE, VALUE_RESULT_FAILURE);
            ret.put(KEY_RESULT_MSG, super.convertException(e));
        }
        return ret.toJSONString();
    }


    /**
     * 更换排序
     */
    @ApiOperation(value = "更换排序")
    @PostMapping(value = "upOrDown")
    public String upOrDownTopicList(@RequestBody MenuEntity query) {
        JSONObject ret = new JSONObject();
        ret.put(KEY_RESULT_CODE, VALUE_RESULT_SUCCESS);
        try {
            this.service.upOrDown(query.getId(), query.getType());
        } catch (Exception e) {
            log.error("更新错误", e);
            ret.put(KEY_RESULT_CODE, VALUE_RESULT_FAILURE);
            ret.put(KEY_RESULT_MSG, e.getMessage());
        }
        return ret.toJSONString();
    }


    /**
     * 获取站点下拉树列表
     */
    @ApiOperation(value = "菜单树列表")
    @PostMapping("treeselect")
    public String treeselect() {
        Map<String, Object> model = new HashMap<>();
        JSONObject ret = new JSONObject();
        String busiDesc = "查询" + this.getModuleDesc();
        int code = VALUE_RESULT_SUCCESS;
        try {
            List<MenuEntity> list = this.service.find(new MenuQuery());
            List<MenuTreeSelect> treeSelects = this.service.buildMenuTreeSelect(list);
            model.put("result", treeSelects);
            recordSysLog(request, busiDesc + " 【成功】");
        } catch (Exception e) {
            code = VALUE_RESULT_FAILURE;
            this.doException(request, busiDesc, model, e);
        }
        ret.put(KEY_RESULT_DATA, model);
        ret.put(KEY_RESULT_CODE, code);
        return ret.toJSONString();
    }


    /**
     * 根据parentId查询子信息
     */
    @ApiOperation(value = "根据parentId查询子信息")
    @GetMapping(value = "getListByParentId")
    public String getListByParentId(Long parentId) {
        JSONObject ret = new JSONObject();
        Map<String, Object> model = new HashMap<>();
        String busiDesc = "查询" + this.getModuleDesc() + "子节点";
        try {
            List<MenuTreeSelect> treeList = this.service.getListByParentId(parentId, getContext());
            model.put(KEY_RESULT_DATA, treeList);
            ret.put(KEY_RESULT_CODE, VALUE_RESULT_SUCCESS);
            ret.put(KEY_RESULT_DATA, model);
            recordSysLog(request, busiDesc + "【成功】");
        } catch (Exception e) {
            log.error("根据parentId查询子信息错误", e);
            this.doException(request, busiDesc, model, e);
            ret.put(KEY_RESULT_CODE, VALUE_RESULT_FAILURE);
            ret.put(KEY_RESULT_MSG, e.getMessage());
        }
        return ret.toJSONString();
    }
}