package com.malk.hangshi.controller;

import com.alibaba.fastjson.JSON;
import com.malk.hangshi.service.HSService;
import com.malk.server.aliwork.YDConf;
import com.malk.server.aliwork.YDParam;
import com.malk.server.common.McException;
import com.malk.server.common.McR;
import com.malk.service.aliwork.YDClient;
import com.malk.service.dingtalk.DDClient;
import com.malk.service.dingtalk.DDClient_Contacts;
import com.malk.service.dingtalk.DDService;
import com.malk.utils.UtilMap;
import com.malk.utils.UtilServlet;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 错误抛出与拦截详见CatchException
 */
@Slf4j
@RestController
@RequestMapping
public class HSController {

    @Autowired
    private DDClient ddClient;

    @Autowired
    private DDClient_Contacts ddClient_contacts;

    @Autowired
    private HSService hsService;

    /**
     * 获取员工人数
     */
    @PostMapping("user/count")
    McR queryCount() {
        return McR.success(hsService.getCorpCount());
    }

    @PostMapping("user/deptInfo")
    McR queryDeptInfo() {
        return McR.success(hsService.getDeptInfo(false));
    }

    @Autowired
    private YDClient ydClient;

    /**
     * 自愿上报动态审批
     */
    @PostMapping("user/approve")
    List<String> approveAQGL(HttpServletRequest request) {

        Map param = UtilServlet.getParamMap(request);
        log.info("自愿上报安全管理, {}", param);
        McException.assertParamException_Null(param, "corpName");
        String type = "总公司".equals(param.get("corpName")) ? "总公司安全管理部" : "子公司安全部人员";

        List<Map> list = (List<Map>) ydClient.queryData(YDParam.builder()
                .formUuid("FORM-3C866TC1RA29KJXD7XF4N4ZXW9932Z69W6CFLR")
                .searchFieldJson(JSON.toJSONString(UtilMap.map("textField_lfw8yizv, selectField_lfw82zh0", type, param.get("corpName"))))
                .build(), YDConf.FORM_QUERY.retrieve_search_form).getData();
        // 存在前缀相同的公司, 后置过滤
        list = list.stream().filter(item -> param.get("corpName").equals(((Map) item.get("formData")).get("selectField_lfw82zh0"))).collect(Collectors.toList());
        List<String> userIds = list.stream().map(item -> String.valueOf(((Map) item.get("formData")).get("textField_lfw8yizu"))).collect(Collectors.toList());
        return userIds;
    }

    /**
     * 自愿上报动态审批
     */
    @PostMapping("user/compId")
    List<String> approveZRBM(HttpServletRequest request) {

        Map param = UtilServlet.getParamMap(request);
        log.info("自愿上责任部门, {}", param);
        return Arrays.asList(String.valueOf(param.get("userId")).split(","));
    }

    @Autowired
    private DDService ddService;

    /**
     * jsApi 注册
     */
    @PostMapping("register")
    McR register(@RequestBody Map<String, String> data) {

        McException.assertParamException_Null(data, "url", "nonceStr");
        return McR.success(ddService.registerJsApi(data.get("url"), data.get("nonceStr")));
    }

    /**
     * jsApi 免登
     */
    @PostMapping("user/code")
    McR userCodeAuth(@RequestBody Map<String, String> data) {
        McException.assertParamException_Null(data, "code");
        return McR.success(ddService.getUserInfoByCode(data.get("code")));
    }

}

