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 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 com.mortals.xhx.common.code.DataSatus;
import com.mortals.xhx.common.code.MenuAuthType;
import com.mortals.xhx.common.code.MenuComm;
import com.mortals.xhx.common.code.MenuLinkType;
import com.mortals.xhx.common.code.MenuType;

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;


/**
 * 菜单信息
 *
 * @author: zxfei
 * @date: 2021/11/30 10:02
 */
@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<OrderCol>();
        orderColList.add(new OrderCol("parentId"));
        orderColList.add(new OrderCol("orderId"));
        query.setOrderColList(orderColList);
    }

    @Override
    protected void init(Map<String, Object> model, Context context) {
        Map<String, Object> status = new HashMap<String, Object>();
        status.put("status", DataSatus.getEnumMap(DataSatus.CLOSE.getValue(), DataSatus.DELETE.getValue(), DataSatus.OVERDUE.getValue(), DataSatus.USEOUT.getValue()));
        status.put("linkType", MenuLinkType.getEnumMap());
        status.put("commMenu", MenuComm.getEnumMap());
        status.put("menuType", MenuType.getEnumMap());
        status.put("authType", MenuAuthType.getEnumMap());
        model.put(KEY_RESULT_DICT, status);
    }

    /**
     * 改变状态
     */
    @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();
    }


    /**
     * 更换排序
     */
    @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();
    }
}