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

import com.bonus.gs.sub.evaluate.evaluate.beans.NodeBean;
import com.bonus.gs.sub.evaluate.evaluate.beans.PersonBean;
import com.bonus.gs.sub.evaluate.evaluate.beans.SetTemplateBean;
import com.bonus.gs.sub.evaluate.evaluate.dao.PersonDao;
import com.bonus.gs.sub.evaluate.evaluate.service.PersonService;
import com.bonus.gs.sub.evaluate.manager.model.Role;
import com.bonus.gs.sub.evaluate.manager.utils.AjaxRes;
import com.bonus.gs.sub.evaluate.manager.utils.GlobalConst;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

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

/**
 * @ClassName: PersonController
 * @author: tqzhang
 * @Date: 2024/12/20 9:26
 * @version: 1.0
 * @description: 评价人员管理
 */
@RestController
@RequestMapping("/evaluatePerson")
@Slf4j
public class PersonController {

    @Resource
    private PersonService service;

    @Resource
    private PersonDao dao;

    @RequestMapping("getDeptTree")
    @ResponseBody
    public AjaxRes getDeptTree(PersonBean o) {
        AjaxRes ar = new AjaxRes();
        try {
            List<NodeBean> list = dao.getDeptTree(o);
            List<NodeBean> nodeBeans = convertToTree(list,o.getOrgId());
            //将数据转换为树形结构
            ar.setSucceed(nodeBeans);
        } catch (Exception e) {
            ar.setFailMsg(GlobalConst.DATA_FAIL);
        }
        return ar;
    }

    @GetMapping("getUserList")
    @ResponseBody
    public AjaxRes getInitiateEvaluateList(PersonBean o){
        AjaxRes ar = new AjaxRes();
        String[] id;
        if (o.getId() == null || o.getId().isEmpty()) {
            id = new String[]{};
        } else {
            id = o.getId().split(",");
        }
        List<PersonBean> list = dao.getUserList(id,o);
        ar.setListSucceed(list);
        return ar;
    }

    @PostMapping("/getRoleSelect")
    @ApiOperation(value = "获取角色下拉选")
    public List<Role> getRoleSelect() {
        return dao.getRoleSelect();
    }
    @PostMapping("/getDeptSelect")
    @ApiOperation(value = "获取部门下拉选")
    public List<Role> getDeptSelect() {
        return dao.getDeptSelect();
    }


    @RequestMapping("addPersonInfo")
    @ResponseBody
    public AjaxRes addPersonInfo(PersonBean o) {
        AjaxRes ar = new AjaxRes();
        try {
            ar = service.addPersonInfo(o);
        }catch(Exception e) {
            ar.setFailMsg(GlobalConst.DATA_FAIL);
        }
        return ar;
    }

    @GetMapping("getPersonList")
    @ResponseBody
    public AjaxRes getPersonList(PersonBean o){
        AjaxRes ar = new AjaxRes();
        List<PersonBean> list = dao.getPersonList(o);
        ar.setListSucceed(list);
        return ar;
    }

    @RequestMapping("updatePerson")
    @ResponseBody
    public AjaxRes updatePerson(PersonBean o) {
        AjaxRes ar = new AjaxRes();
        try {
            ar = service.updatePerson(o);
        }catch(Exception e) {
            ar.setFailMsg(GlobalConst.DATA_FAIL);
        }
        return ar;
    }
    @RequestMapping("deletePersonById")
    @ResponseBody
    public AjaxRes deletePersonById(String id) {
        AjaxRes ar = new AjaxRes();
        try {
            dao.deletePersonRole(id);
            int result = dao.deletePersonById(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;
    }
    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);
                }
            }
        }
        return tree;
    }

}
