package com.bonus.gs.sub.evaluate.evaluate.controller;

import com.bonus.gs.sub.evaluate.evaluate.beans.InitiateEvaluateBean;
import com.bonus.gs.sub.evaluate.evaluate.beans.NodeBean;
import com.bonus.gs.sub.evaluate.evaluate.beans.SetTemplateBean;
import com.bonus.gs.sub.evaluate.evaluate.beans.TeamEvaluateBean;
import com.bonus.gs.sub.evaluate.evaluate.dao.InitiateEvaluateDao;
import com.bonus.gs.sub.evaluate.evaluate.service.InitiateEvaluateService;
import com.bonus.gs.sub.evaluate.manager.utils.AjaxRes;
import com.bonus.gs.sub.evaluate.manager.utils.GlobalConst;
import com.bonus.gs.sub.evaluate.manager.utils.UserUtil;
import lombok.val;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @ClassName: InitiateEvaluateController
 * @author: tqzhang
 * @Date: 2024/12/19 9:11
 * @version: 1.0
 * @description: 外包商发起评价
 */
@RestController
@RequestMapping("/initiateEvaluate")
public class InitiateEvaluateController {

    @Resource
    private InitiateEvaluateService service;

    @Resource
    private InitiateEvaluateDao dao;

    /**
     * 获取外包商评价列表
     * @param o
     * @return
     */
    @GetMapping("getInitiateEvaluateList")
    @ResponseBody
    public AjaxRes getInitiateEvaluateList(InitiateEvaluateBean o){
        AjaxRes ar = new AjaxRes();
        List<InitiateEvaluateBean> list = dao.getInitiateEvaluateList(o);
        ar.setListSucceed(list);
        return ar;
    }

    @RequestMapping("deleteInitiateEvaluateById")
    @ResponseBody
    public AjaxRes deleteTemplateById(String id) {
        AjaxRes ar = new AjaxRes();
        try {
            int result = service.deleteInitiateEvaluateById(id);
            if(result > 0) {
                ar.setRes(GlobalConst.SUCCEED);
                ar.setResMsg("删除成功");
            }else {
                ar.setFailMsg(GlobalConst.DATA_FAIL);
                ar.setResMsg("删除失败,请联系系统管理员");
            }

        }catch(Exception e) {
            ar.setFailMsg(GlobalConst.DATA_FAIL);
        }
        return ar;
    }

    @RequestMapping(value = "getOrgSelect", method = RequestMethod.POST)
    @ResponseBody
    public AjaxRes getOrgSelect(InitiateEvaluateBean bean) {
        AjaxRes ar = new AjaxRes();
        try {
            List<InitiateEvaluateBean> list = dao.getOrgSelect(bean);
            ar.setSucceed(list);
        } catch (Exception e) {
            ar.setFailMsg(GlobalConst.DATA_FAIL);
        }
        return ar;
    }

    @RequestMapping(value = "getTemplateSelect", method = RequestMethod.POST)
    @ResponseBody
    public AjaxRes getTemplateSelect(InitiateEvaluateBean bean) {
        AjaxRes ar = new AjaxRes();
        try {
            List<InitiateEvaluateBean> list = dao.getTemplateSelect(bean);
            ar.setSucceed(list);
        } catch (Exception e) {
            ar.setFailMsg(GlobalConst.DATA_FAIL);
        }
        return ar;
    }

    @RequestMapping("getOrgTree")
    @ResponseBody
    public AjaxRes getOrgTree(InitiateEvaluateBean o) {
        AjaxRes ar = new AjaxRes();
        try {
            List<NodeBean> list = dao.getOrgTree(o);
            List<NodeBean> nodeBeans = convertToTree(list,o.getOrgId());
            //将数据转换为树形结构
            ar.setSucceed(nodeBeans);
        } catch (Exception e) {
            ar.setFailMsg(GlobalConst.DATA_FAIL);
        }
        return ar;
    }
    @GetMapping("getSubListByOrgId")
    @ResponseBody
    public AjaxRes getSubListByOrgId(InitiateEvaluateBean o){
        AjaxRes ar = new AjaxRes();
        String[] proIds = o.getProId().split(",");
        List<InitiateEvaluateBean> list = dao.getSubListByOrgId(proIds);
        ar.setListSucceed(list);
        return ar;
    }
    private List<NodeBean> convertToTree(List<NodeBean> nodes,String orgId) {
        Map<String, NodeBean> nodeMap = new HashMap<>();
        List<NodeBean> tree = new ArrayList<>();

        for (NodeBean node : nodes) {
            nodeMap.put(node.getId(), node);
        }
        for (NodeBean node : nodes) {
            node.setSpread(true);
            if (Objects.equals(node.getParentId(), "0")) {
                tree.add(node);
            } else {
                NodeBean parent = nodeMap.get(node.getParentId());
                if (parent != null) {
                    if (parent.getChildren() == null) {
                        parent.setChildren(new ArrayList<>());
                    }
                    parent.getChildren().add(node);
                }
            }
        }
        for (NodeBean node : tree) {
            if (node.getChildren() != null) {
                node.setChildren(node.getChildren().stream()
                        .filter(child -> orgId.equals(child.getId()))
                        .collect(Collectors.toList()));
            }
        }
        return tree;
    }

    /**
     * 新增外包商评价
     * @param o
     * @return
     */
    @RequestMapping("submitInitiateEvaluate")
    @ResponseBody
    public AjaxRes submitInitiateEvaluate(InitiateEvaluateBean o) {
        AjaxRes ar = new AjaxRes();
        try {
            ar = service.submitInitiateEvaluate(o);
        }catch(Exception e) {
            ar.setFailMsg(GlobalConst.DATA_FAIL);
        }
        return ar;
    }

    @RequestMapping(value = "getInitiateEvaluateDetails", method = RequestMethod.GET)
    @ResponseBody
    public AjaxRes getInitiateEvaluateDetails(InitiateEvaluateBean bean) {
        return service.getInitiateEvaluateDetails(bean);
    }
}
