package com.njcn.web.utils;

import cn.hutool.http.HttpStatus;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.njcn.common.pojo.enums.auth.ClientEnum;
import com.njcn.common.pojo.response.HttpResult;
import com.njcn.common.utils.HttpResultUtil;
import org.springframework.http.MediaType;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author hongawen
 * @version 1.0.0
 * @date  2021年05月24日 16:56
 */
public class WebUtil {

    private final static ObjectMapper OBJECT_MAPPER = new ObjectMapper();

    /**
     * @param code    响应码
     * @param message 响应信息
     */
    public static void responseInfo( HttpServletResponse response, String code, String message) throws IOException {
        response.setStatus(HttpStatus.HTTP_OK);
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Cache-Control", "no-cache");
        //可以使用封装类简写Content-Type，使用该方法则无需使用setCharacterEncoding
        response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
        HttpResult<String> result = HttpResultUtil.assembleResult(code, null, message);
        response.getWriter().print(OBJECT_MAPPER.writeValueAsString(result));
        response.getWriter().flush();
    }

    /**
     * @return 对应的部门类型
     */
    public static List<Integer> filterDeptType() {
        List<Integer> deptType = new ArrayList<>();
        String clientId = RequestUtil.getClientId();
        String clientType = ClientEnum.getClientType(clientId);
        switch (clientType) {
            case "app":
                deptType.add(2);
                break;
            case "screen":
                deptType.add(0);
                break;
            default:
                deptType.add(0);
                deptType.add(1);
                deptType.add(3);
                break;
        }
        return deptType;
    }


}
