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


import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.annotation.UnAuth;
import com.mortals.framework.common.IBaseEnum;
import com.mortals.framework.common.Rest;
import com.mortals.framework.common.code.UserType;
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.MenuTreeSelect;
import com.mortals.xhx.base.system.resource.model.ResourceEntity;
import com.mortals.xhx.base.system.resource.model.ResourceQuery;
import com.mortals.xhx.base.system.resource.service.ResourceService;
import com.mortals.xhx.base.system.role.model.RoleAuthEntity;
import com.mortals.xhx.base.system.role.model.RoleAuthQuery;
import com.mortals.xhx.base.system.role.service.RoleAuthService;
import com.mortals.xhx.common.code.AuthType;
import com.mortals.xhx.common.code.SourceType;
import com.mortals.xhx.common.code.YesNoEnum;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

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

/**
 * 资源信息
 *
 * @author: zxfei
 * @date: 2022/5/7 15:27
 */
@Slf4j
@RestController
@RequestMapping("resource")
public class ResourceController extends BaseCRUDJsonBodyMappingController<ResourceService, ResourceEntity, Long> {

    @Autowired
    private RoleAuthService roleAuthService;

    public ResourceController() {
        super.setModuleDesc("资源信息");
    }

    @Override
    protected void init(Map<String, Object> model, Context context) {
        Map<String, Object> statsus = new HashMap<String, Object>();
        statsus.put("authType", AuthType.getEnumMap());
        statsus.put("sourceType", SourceType.getEnumMap());
        if (getCurUser().isAdmin()) {
            statsus.put("userType", IBaseEnum.getEnumMap(UserType.class));
        } else {
            statsus.put("userType", UserType.findByValue(getCurUser().getUserType()));
        }
        model.put(KEY_RESULT_DICT, statsus);
    }


    @Override
    protected void doListBefore(ResourceEntity query, Map<String, Object> model, Context context) throws AppException {
        query.setOrderColList(Arrays.asList(new OrderCol("sourceType")));
        super.doListBefore(query, model, context);
    }

    /**
     * 资源路径刷新
     */
    @PostMapping(value = "refreshUrl")
    @UnAuth
    public Rest<String> refreshUrl(@RequestParam(name = "packageName", defaultValue = "com.mortals.xhx") String packageName) {
        log.info("刷新资源路径，packageName", packageName);
        String busiDesc = this.getModuleDesc() + "资源路径刷新";
        Rest<String> rest = Rest.ok(busiDesc + " 【成功】");
        try {
            this.service.refreshResourceUrl(packageName, getContext());
            recordSysLog(request, busiDesc + " 【成功】");
        } catch (Exception e) {
            log.error(busiDesc, e);
            rest = Rest.fail(super.convertException(e));
        }
        return rest;
    }


    /**
     * 分组显示
     */
    @ApiOperation(value = "分组列表")
    @PostMapping(value = "group")
    @UnAuth
    public String group(@RequestBody ResourceQuery query) {
        JSONObject ret = new JSONObject();
        Map<String, Object> model = new HashMap<>();
        String busiDesc = "查询" + this.getModuleDesc() + "子节点";
        try {
            Map<String, List<ResourceEntity>> group = this.service.group(query.getRoleId(), getContext());
            model.put(KEY_RESULT_DATA, group);
            ret.put(KEY_RESULT_CODE, VALUE_RESULT_SUCCESS);
            ret.put(KEY_RESULT_DATA, model);
            recordSysLog(request, busiDesc + "【成功】");
        } catch (Exception e) {
            log.error("分组列表错误", 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();
    }

}