package com.ruoyi.system.domain.cnooc;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.annotation.JSONField;
import lombok.Data;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

@Data
public class Dept {
    public static void main(String[] args) {
        String getJson = "[\n" +
                "    {\n" +
                "        \"orgName\": \"惠州石化\",\n" +
                "        \"orgList\": [\n" +
                "            {\n" +
                "                \"orgid\": \"18\",\n" +
                "                \"orgname\": \"公司领导\",\n" +
                "                \"orgorder\": 1,\n" +
                "                \"gsid\": \"17\",\n" +
                "                \"zhaoshang\": 1,\n" +
                "                \"orgtype\": 0,\n" +
                "                \"orgcode\": \"2000\",\n" +
                "                \"upper_uid\": null,\n" +
                "                \"organtype\": \"0\"\n" +
                "            }\n" +
                "        ],\n" +
                "        \"orgid\": 17\n" +
                "    }\n" +
                "]";
//        List<Dept> list = JSON.parseObject(getJson, (Type) Dept.class);
        List<Dept> deptList = JSON.parseArray(getJson, Dept.class);
        System.out.println("list = " + deptList);
        System.out.println("list = " + JSON.toJSONString(deptList));
    }

    /**
     * 上级组织机构编码
     */
    @JSONField(name = "orgid")
    private Long parentId;
    /**
     * 上级组织机构名称
     */
    @JSONField(name = "orgName")
    private String parentName;


    @JSONField(name = "orgList")
    private List<ChildrenDept> children = new ArrayList<>();

    @Data
    public static class ChildrenDept {
        /**
         * 组织机构ID
         */
        @JSONField(name = "orgid")
        private Long deptId;

        /**
         * 上级组织机构编号
         */
        @JSONField(name = "gsid")
        private Long parentId;

        /**
         * 组织机构名称
         */
        @JSONField(name = "orgname")
        private String deptName;
        /**
         * 排序
         */
        @JSONField(name = "orgorder")
        private Integer orderNum;


        /**
         * 部门类型（1生产部门）
         */
//        @JSONField(name = "orgtype")
//        private Integer type;
        /**
         * 部门编码
         */
        @JSONField(name = "orgcode")
        private String deptCode;

        /**
         * 统计职能
         */
        // @JSONField(name = "zhaoshang")
        // private Long zhaoshang;
        /**
         * 配置主管
         */
        //@JSONField(name = "upper_uid")
        //private String upper_uid;

        /**
         * 机构类型（0内部机构 1外部机构）
         */
        //@JSONField(name = "organtype")
        //private String organtype;
        public void setDeptName(String deptName) {
            //可以替换大部分空白字符， 不限于空格 ； \s 可以匹配空格、制表符、换页符等空白字符的其中任意一个。
            this.deptName = deptName == null ? null : deptName.replaceAll("\\s*", "");
        }

        public void setDeptCode(String deptCode) {
            this.deptCode = deptCode == null ? null : deptCode.replaceAll("\\s*", "");
        }
    }

    /*
    [
    {
        "orgName": "惠州石化",
        "orgList": [
            {
                "orgid": "18",
                "orgname": "公司领导",
                "orgorder": 1,
                "gsid": "17",
                "zhaoshang": 1,
                "orgtype": 0,
                "orgcode": "2000",
                "upper_uid": null,
                "organtype": "0"
            },
            {
                "orgid": "19",
                "orgname": "运行一部",
                "orgorder": 2,
                "gsid": "17",
                "zhaoshang": 1,
                "orgtype": 1,
                "orgcode": "1001",
                "upper_uid": null,
                "organtype": "0"
            }
        ],
        "orgid": 17
    }
]
     */
}
