package com.zcloud.controller.openApi;

import com.zcloud.controller.base.BaseController;
import com.zcloud.entity.PageData;
import com.zcloud.entity.system.Department;
import com.zcloud.service.corp.CorpInfoService;
import com.zcloud.service.system.DepartmentService;
import com.zcloud.service.system.UsersService;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author fangjiakai
 * @date 2023/05/09 15:07
 */
@Controller
@RequestMapping("/openApi")
public class OpenApiController extends BaseController {

    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private UsersService usersService;
    @Autowired
    private CorpInfoService corpInfoService;
    /**
     * 显示列表ztree
     * @return
     */
    @RequestMapping(value="/department/listTree")
    @ResponseBody
    public Object listTree()throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        List<Department> zdepartmentPdList = new ArrayList<Department>();
        PageData dept = new PageData();
        dept.put("CORPINFO_ID", pd.get("CORPINFO_ID"));
        dept=this.departmentService.findByCorpId(dept);

        Department depar = new Department();
        depar.setDEPARTMENT_ID(dept.getString("DEPARTMENT_ID"));
        depar.setPARENT_ID(dept.getString("PARENT_ID"));
        depar.setBIANMA(dept.getString("LEVEL"));
        depar.setNAME(dept.getString("NAME"));
        depar.setSubDepartment(this.departmentService.listAllDepartment(depar.getDEPARTMENT_ID(),""));
        depar.setTarget("treeFrame");
        depar.setIcon("../../../assets/images/user.gif");
        zdepartmentPdList.add(depar);
        JSONArray arr = JSONArray.fromObject(zdepartmentPdList);
        String json = arr.toString();
        json = json.replaceAll("DEPARTMENT_ID", "id").replaceAll("PARENT_ID", "pId").replaceAll("NAME", "name").replaceAll("subDepartment", "nodes").replaceAll("hasDepartment", "checked").replaceAll("treeurl", "url").replaceAll("BIANMA", "LEVEL");

        map.put("zTreeNodes", json);
        map.put("result", errInfo);
        return map;
    }

    /**列表
     * @throws Exception
     */
    @RequestMapping(value="/user/listAll")
    @ResponseBody
    public Object listAll() throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        List<PageData> userList = usersService.listAllUser(pd);	//列出Post列表
        map.put("userList", userList);
        map.put("result", errInfo);
        return map;
    }

    /**列表
     * @throws Exception
     */
    @RequestMapping(value="/corp/listAll")
    @ResponseBody
    public Object corpListAll() throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        List<PageData> list = corpInfoService.listAll(pd);	//列出Post列表
        map.put("list", list);
        map.put("result", errInfo);
        return map;
    }
}
