//package org.dromara.job.snailjob;
//
//import cn.hutool.core.date.DateUtil;
//import cn.hutool.http.HttpUtil;
//import cn.hutool.json.JSONException;
//import cn.hutool.json.JSONObject;
//import com.aizuda.snailjob.client.job.core.annotation.JobExecutor;
//import com.aizuda.snailjob.client.job.core.dto.JobArgs;
//import com.aizuda.snailjob.client.model.ExecuteResult;
//import com.aizuda.snailjob.common.log.SnailJobLog;
//import org.dromara.system.domain.SysDept;
//import org.dromara.system.mapper.SysDeptMapper;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//
///**
// * 机构抽取（合肥）-全量
// * @author Luo.J
// */
//@Component
//@JobExecutor(name = "deptExtractionJobExecutor-full")
//public class DeptExtractionJobExecutor1 {
//
//    @Autowired
//    private SysDeptMapper sysDeptMapper;
//
//    public ExecuteResult jobExecute(JobArgs jobArgs) throws JSONException {
//        SnailJobLog.LOCAL.info("组织机构抽取任务参数. JobParams:{}", jobArgs.getJobParams());
//        String url = "http://53.1.230.5:8159/api/UUDB/org/list";
//        String sysCode = "f156334885cc4fbc853a07a5bdcd8907";
//        String page = jobArgs.getJobParams().toString();
//        Map<String,String> paramMap = new HashMap<>();
//        paramMap.put("orgId","340100000000");
//        paramMap.put("page",page);
//        paramMap.put("limit","1000");
//        JSONObject json = new JSONObject(paramMap);
//        String body = json.toString();
//        SnailJobLog.LOCAL.info("组织机构抽取调用第三方参数. body:{}", body);
//
//        String result = HttpUtil.createPost(url)
//            .header("syscode", sysCode)
//            .body(body)
//            .execute().body();
//        JSONObject resultObject = new JSONObject(result);
//        SnailJobLog.LOCAL.info("组织机构抽取调用第三方返回结果. resultObject:{}", resultObject);
//        List<Map<String,Object>> data = (List<Map<String,Object>>) resultObject.get("data");
//        if (data.isEmpty()){
//            return ExecuteResult.failure("返回值为空！");
//        }
//        // data里面存放若干个组织机构的信息
//        for (Map<String,Object> orgInfo : data) {
//            String deptId = (String) orgInfo.get("id");
//            String parentId = (String) orgInfo.get("pid");
//            String path = (String) orgInfo.get("path"); // /340000000000/
//            String replace = path.replaceAll("/", ","); // ,340000000000,
//            String newStr = "0" + replace; // 0,340000000000,
//            String ancestors = newStr.substring(0, newStr.length() - 1); // 去掉最后一个逗号 -> 0,340100000000
//            String newAncestors = ancestors.replace("," + deptId,"");
//            String deptName = (String) orgInfo.get("orgName");
//            String shortName = (String) orgInfo.get("shortName");
//            String status = (String) orgInfo.get("status");
//            Integer orderNum = (Integer) orgInfo.get("orderIdInt");
//            String createTime = (String) orgInfo.get("createTime");
//            String updateTime = (String) orgInfo.get("modiTime");
//            String type = (String) orgInfo.get("type");
//            SysDept sysDept = new SysDept();
//            // 抽取机构数据的时候过滤掉测试数据()
//            if (deptName.contains("测试") || !deptId.startsWith("34") || deptId.length() < 12 || deptId.length() > 14){continue;}
//            // 2. 将组织机构名称格式统一（有的为合肥市公安局，有的为安徽省宿州市公安局）
//            if (deptName.startsWith("安徽省") && !deptId.equals("340000000000")){deptName = deptName.substring(3);}
//            // 将抽取到的组织机构信息放入集合中
//            sysDept.setDeptId(Long.valueOf(deptId));
//            sysDept.setParentId(Long.valueOf(parentId));
//            sysDept.setAncestors(newAncestors);
//            sysDept.setDeptName(deptName);
//            sysDept.setShortName(shortName);
//            if ("1".equals(status)){
//                sysDept.setStatus("0");
//            }else if ("0".equals(status)){
//                sysDept.setStatus("1");
//            }else {
//                sysDept.setStatus("1");
//                sysDept.setDelFlag("2");
//            }
//            sysDept.setOrderNum(orderNum);
//            sysDept.setRemark(type);
//            sysDept.setCreateTime(DateUtil.parse(createTime,"yyyy-MM-dd HH:mm:ss"));
//            sysDept.setUpdateTime(DateUtil.parse(updateTime,"yyyy-MM-dd HH:mm:ss"));
//            sysDeptMapper.insert(sysDept);
//            SnailJobLog.LOCAL.info("组织机构. deptId:{}抽取成功！", deptId);
//        }
//        return ExecuteResult.success("全量组织机构抽取程序执行成功！");
//    }
//}
