package com.nbport.zgwl.controller;

import com.nbport.zgwl.common.R;
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 java.util.List;
import java.util.Map;

/**
 * 组织架构树 Controller
 *
 * 当前为 mock 数据，对应内网正式接口。
 * 后续接入统一认证后可直接替换为真实接口调用。
 */
@RestController
@RequestMapping("/admin/org")
public class AdminOrgController {

    /**
     * 获取组织树（完整层级）
     * GET /admin/org/getOrgTree
     */
    @RequestMapping("/getOrgTree")
    public R<List<OrgTreeNode>> getOrgTree(String status, String tenantName) {
        return R.success(buildOrgTree());
    }

    /**
     * 按类型查询组织列表（扁平）
     * POST /admin/org/listAllOrgNameByTypeWithId
     * 传参 {"orgCode": "AGENT"}
     */
    @PostMapping("/listAllOrgNameByTypeWithId")
    public R<List<OrgNameIdItem>> listAllOrgNameByTypeWithId(@RequestBody(required = false) Map<String, Object> body) {
        return R.success(buildAgentOrgList());
    }

    // ========== 货代公司（AGENT）列表 ==========

    private List<OrgNameIdItem> buildAgentOrgList() {
        return List.of(
                new OrgNameIdItem("江苏远洋新世纪2供应链有限公司", 384L),
                new OrgNameIdItem("嘉兴兴港", 15L),
                new OrgNameIdItem("货代测试", 79L),
                new OrgNameIdItem("堆场测试", 139L),
                new OrgNameIdItem("堆场测试-中通", 150L),
                new OrgNameIdItem("堆场测试-汇海通", 151L),
                new OrgNameIdItem("堆场测试-顺丰", 152L),
                new OrgNameIdItem("堆场测试-圆通", 153L),
                new OrgNameIdItem("堆场测试-京东", 154L),
                new OrgNameIdItem("堆场测试-申通", 155L),
                new OrgNameIdItem("宁波众兴物流有限公司", 385L),
                new OrgNameIdItem("公司入驻测试", 241L),
                new OrgNameIdItem("宁波港东南物流集团有限公司", 80L),
                new OrgNameIdItem("绍兴彬亮纺织品有限集团", 402L),
                new OrgNameIdItem("深圳市益禾国际货运代理有限公司", 361L),
                new OrgNameIdItem("四港物流", 16L),
                new OrgNameIdItem("宁波泰利物流有限公司", 17L),
                new OrgNameIdItem("二级货代测试公司", 134L),
                new OrgNameIdItem("东南货代测试", 135L),
                new OrgNameIdItem("义乌市万全进出口有限公司", 401L)
        );
    }

    // ========== 组织树（与 getOrgTree 返回一致） ==========

    private List<OrgTreeNode> buildOrgTree() {
        OrgTreeNode root = new OrgTreeNode();
        root.setId(1L);
        root.setOrgName("浙江海港物流集团有限公司X");
        root.setParentId(0L);
        root.setOrgType("1");
        root.setRemark("管理后台");
        root.setOrgCode("MANAGE");
        root.setStatus("1");
        root.setTenantId(1L);

        OrgTreeNode yongkang = createNode(47L, "永康陆港国际物流有限公司", 1L, "2", null, "YongKangLuGangGuoJiWuLiuYouXianGongSi", "1", 1L);
        yongkang.setChildren(List.of(createNode(138L, "永康物流分公司", 47L, "2", null, "c85c9da591e04d55b3d01dd0f5ae35aa", "1", 1L)));

        OrgTreeNode zhuji = createNode(48L, "宁波港国际物流有限公司诸暨业务点", 1L, "2", null, "NingBoGangGuoJiWuLiuYouXianGongSiZhuJiYeWuDian", "1", 1L);
        zhuji.setChildren(List.of(createNode(49L, "宁波港国际物流有限公司兰溪业务点", 48L, "2", null, "NingBoGangGuoJiWuLiuYouXianGongSiLanXiYeWuDian", "1", 1L)));

        OrgTreeNode zhongbei = createNode(58L, "浙港物流浙中北大区", 1L, "2", null, "ZheGangWuLiuZheZhongBeiDaQu", "1", 1L);
        zhongbei.setChildren(List.of(
                createNode(59L, "浙江兴港国际货运代理有限公司嘉兴分公司", 58L, "2", null, "ZheJiangXingGangGuoJiHuoYunDaiLiYouXianGongSiJiaXingFenGongSi", "1", 1L),
                createNode(60L, "宁波港铁路有限公司绍兴分公司", 58L, "2", null, "NingBoGangTieLuYouXianGongSiShaoXingFenGongSi", "1", 1L)
        ));

        OrgTreeNode jiangsu = createNode(68L, "浙港物流江苏大区", 1L, "2", null, "ZheGangWuLiuJiangSuDaQu", "1", 1L);
        OrgTreeNode jiangsuChild = createNode(69L, "浙港（江苏）国际供应链管理有限公司", 68L, "2", null, "ZheGang（JiangSu）GuoJiGongYingLianGuanLiYouXianGongSi", "1", 1L);
        jiangsuChild.setChildren(List.of(createNode(71L, "盐城海铁", 69L, "2", null, "YanChengHaiTie", "1", 1L)));
        jiangsu.setChildren(List.of(jiangsuChild, createNode(72L, "浙港物流江苏其他", 68L, "2", null, "ZheGangWuLiuJiangSuQiTa", "1", 1L)));

        OrgTreeNode xianggan = createNode(76L, "浙港物流湘赣大区", 1L, "2", null, "ZheGangWuLiuXiangGanDaQu", "1", 1L);
        xianggan.setChildren(List.of(
                createNode(77L, "宁波港铁路有限公司--向塘/贵溪/赣州/宜春西/鹰潭南/萍乡/景德镇东/新余/樟树/湘潭/株洲北/长沙北/岳阳", 76L, "2", null, "NingBoGangTieLuYouXianGongSi--XiangTang/GuiXi/GanZhou/YiChunXi/YingTanNan/PingXiang/JingDeZhenDong/XinYu/ZhangShu/XiangTan/ZhuZhouBei/ChangShaBei/YueYang", "1", 1L),
                createNode(78L, "江西上饶海港物流有限公司", 76L, "2", null, "JiangXiShangRaoHaiGangWuLiuYouXianGongSi", "1", 1L)
        ));

        OrgTreeNode anhui = createNode(23L, "浙港物流安徽大区2", 1L, "2", null, "ErJiGongSi2", "1", 1L);
        anhui.setChildren(List.of(
                createNode(43L, "宁波港铁路有限公司---蚌埠/洛阳/青龙山/萧山", 23L, "2", null, "NingBoGangTieLuYouXianGongSi---BengBu/LuoYang/QingLongShan/XiaoShan", "1", 1L),
                createNode(44L, "宁波港铁路有限公司---芜湖西/铜城闸/马鞍山", 23L, "2", null, "NingBoGangTieLuYouXianGongSi---WuHuXi/TongChengZha/MaAnShan", "1", 1L),
                createNode(45L, "宣城市浙皖国际陆港供应链管理有限公司", 23L, "2", null, "XuanChengShiZheWanGuoJiLuGangGongYingLianGuanLiYouXianGongSi", "1", 1L)
        ));

        OrgTreeNode huazhong = createNode(53L, "浙港物流华中大区", 1L, "2", null, "ZheGangWuLiuHuaZhongDaQu", "1", 1L);
        OrgTreeNode xiangyang = createNode(56L, "宁波港国际物流有限公司襄阳分公司", 53L, "2", null, "NingBoGangGuoJiWuLiuYouXianGongSiXiangYangFenGongSi", "1", 1L);
        xiangyang.setChildren(List.of(createNode(57L, "襄阳业务点", 56L, "2", null, "XiangYangYeWuDian", "1", 1L)));
        huazhong.setChildren(List.of(xiangyang,
                createNode(54L, "湖北浙港供应链管理有限公司", 53L, "2", null, "HuBeiZheGangGongYingLianGuanLiYouXianGongSi", "1", 1L),
                createNode(55L, "武铁浙港多式联运（河南）有限公司", 53L, "2", null, "WuTieZheGangDuoShiLianYun（HeNan）YouXianGongSi", "1", 1L)
        ));

        OrgTreeNode zhexi = createNode(61L, "浙港物流浙西南大区", 1L, "2", null, "ZheGangWuLiuZheXiNanDaQu", "1", 1L);
        OrgTreeNode taizhounan = createNode(63L, "浙港物流台州南", 61L, "2", null, "ZheGangWuLiuTaiZhouNan", "1", 1L);
        taizhounan.setChildren(List.of(createNode(64L, "宁波佰菲特国际货运代理有限公司", 63L, "2", null, "NingBoBaiFeiTeGuoJiHuoYunDaiLiYouXianGongSi", "1", 1L)));
        zhexi.setChildren(List.of(createNode(62L, "浙港物流金华南", 61L, "2", null, "ZheGangWuLiuJinHuaNan", "1", 1L), taizhounan));

        OrgTreeNode hefei = createNode(65L, "宁波港国际物流有限公司合肥分公司", 1L, "2", null, "NingBoGangGuoJiWuLiuYouXianGongSiHeFeiFenGongSi", "1", 1L);
        hefei.setChildren(List.of(createNode(66L, "六安安庆北业务点", 65L, "2", null, "LuAnAnQingBeiYeWuDian", "1", 1L)));

        OrgTreeNode xibu = createNode(73L, "浙港物流西部大区", 1L, "2", null, "ZheGangWuLiuXiBuDaQu", "1", 1L);
        xibu.setChildren(List.of(createNode(74L, "宁波港铁路有限公司---重庆/城厢/白银市/西安/宜兴", 73L, "2", null, "NingBoGangTieLuYouXianGongSi---ChongQing/ChengXiang/BaiYinShi/XiAn/YiXing", "1", 1L)));

        root.setChildren(List.of(
                yongkang, zhuji,
                createNode(51L, "丽水陆港国际物流有限责任公司", 1L, "2", null, "LiShuiLuGangGuoJiWuLiuYouXianZeRenGongSi", "1", 1L),
                createNode(52L, "衢州通港国际物流有限公司", 1L, "2", null, "QuZhouTongGangGuoJiWuLiuYouXianGongSi", "1", 1L),
                zhongbei, jiangsu,
                createNode(70L, "海安海铁", 1L, "2", null, "HaiAnHaiTie", "1", 1L),
                createNode(75L, "宁波港国际物流有限公司徐州分公司", 1L, "2", null, "NingBoGangGuoJiWuLiuYouXianGongSiXuZhouFenGongSi", "1", 1L),
                xianggan, anhui,
                createNode(321L, "汇海通-华峙", 1L, "2", "共享堆场测试", "21c350f7d5e54d0091ece7a47703bff8", "1", 1L),
                createNode(382L, "11", 1L, "2", "sss", "e53d1047b5bc4415a98719a0f6911d83", "1", 1L),
                createNode(383L, "测试2", 1L, "2", null, "86d33726220c452d9dfd266c01214803", "1", 1L),
                createNode(181L, "东华", 1L, "2", "共享堆场测试", "ad3fc4e7e6644b78a245dbef5c70a0f1", "1", 1L),
                createNode(182L, "港联捷", 1L, "2", "共享堆场测试", "edd2fd12073a42bca05c7682ef507b95", "1", 1L),
                createNode(46L, "浙江义乌国际公铁联运有限公司", 1L, "2", null, "ZheJiangYiWuGuoJiGongTieLianYunYouXianGongSi", "1", 1L),
                createNode(50L, "新安江业务点", 1L, "2", null, "XinAnJiangYeWuDian", "1", 1L),
                huazhong, zhexi, hefei,
                createNode(67L, "江山无水港国际物流有限公司", 1L, "2", null, "JiangShanWuShuiGangGuoJiWuLiuYouXianGongSi", "1", 1L),
                xibu
        ));
        return List.of(root);
    }

    private OrgTreeNode createNode(Long id, String orgName, Long parentId, String orgType,
                                   String remark, String orgCode, String status, Long tenantId) {
        OrgTreeNode node = new OrgTreeNode();
        node.setId(id);
        node.setOrgName(orgName);
        node.setParentId(parentId);
        node.setOrgType(orgType);
        node.setRemark(remark);
        node.setOrgCode(orgCode);
        node.setStatus(status);
        node.setTenantId(tenantId);
        return node;
    }

    // ========== DTOs ==========

    public static class OrgTreeNode {
        private Long id;
        private String orgName;
        private Long parentId;
        private String orgType;
        private String remark;
        private String orgCode;
        private String status;
        private Long tenantId;
        private List<OrgTreeNode> children;

        public Long getId() { return id; }
        public void setId(Long id) { this.id = id; }
        public String getOrgName() { return orgName; }
        public void setOrgName(String orgName) { this.orgName = orgName; }
        public Long getParentId() { return parentId; }
        public void setParentId(Long parentId) { this.parentId = parentId; }
        public String getOrgType() { return orgType; }
        public void setOrgType(String orgType) { this.orgType = orgType; }
        public String getRemark() { return remark; }
        public void setRemark(String remark) { this.remark = remark; }
        public String getOrgCode() { return orgCode; }
        public void setOrgCode(String orgCode) { this.orgCode = orgCode; }
        public String getStatus() { return status; }
        public void setStatus(String status) { this.status = status; }
        public Long getTenantId() { return tenantId; }
        public void setTenantId(Long tenantId) { this.tenantId = tenantId; }
        public List<OrgTreeNode> getChildren() { return children; }
        public void setChildren(List<OrgTreeNode> children) { this.children = children; }
    }

    public static class OrgNameIdItem {
        private String name;
        private Long id;

        public OrgNameIdItem() {}
        public OrgNameIdItem(String name, Long id) { this.name = name; this.id = id; }

        public String getName() { return name; }
        public void setName(String name) { this.name = name; }
        public Long getId() { return id; }
        public void setId(Long id) { this.id = id; }
    }
}
