//package org.dromara.job.snailjob;
//
//import cn.dev33.satoken.secure.BCrypt;
//import cn.hutool.core.date.DateField;
//import cn.hutool.core.date.DateUtil;
//import cn.hutool.core.util.ObjectUtil;
//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.SysUser;
//import org.dromara.system.mapper.SysUserMapper;
//import org.dromara.web.domain.TDeviceInfo;
//import org.dromara.web.mapper.TDeviceInfoMapper;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//import java.util.Date;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//
///**
// * 用户抽取（合肥）-增量
// * @author Luo.J
// */
//@Component
//@JobExecutor(name = "userExtractionJobExecutor-increment")
//public class UserExtractionJobExecutor2 {
//
//    @Autowired
//    private SysUserMapper sysUserMapper;
//
//    @Autowired
//    private TDeviceInfoMapper deviceInfoMapper;
//
//    public ExecuteResult jobExecute(JobArgs jobArgs) throws JSONException {
//        SnailJobLog.LOCAL.info("用户抽取任务参数. JobParams:{}", jobArgs.getJobParams());
//        String url = "http://53.1.230.5:8159/api/UUDB/user/updateList";
//        String sysCode = "f156334885cc4fbc853a07a5bdcd8907";
//        String page;
//        String limit;
//        String startTime;
//        String endTime;
//        Object jobParams = jobArgs.getJobParams();
//        if (ObjectUtil.isNotEmpty(jobParams)){
//            String[] split = jobParams.toString().split(",");
//            page = split[0];
//            limit = split[1];
//            startTime = split[2];
//            endTime = split[3];
//        }else {
//            page = "1";
//            limit = "1000";
//            startTime = DateUtil.format(DateUtil.offset(new Date(), DateField.HOUR_OF_DAY,-6),"yyyy-MM-dd HH:mm:ss");
//            endTime = DateUtil.format(new Date(),"yyyy-MM-dd HH:mm:ss");
//        }
//        Map<String,String> paramMap = new HashMap<>();
//        paramMap.put("orgId","340100000000");
//        paramMap.put("page",page);
//        paramMap.put("limit",limit);
//        paramMap.put("startTime",startTime);
//        paramMap.put("endTime",endTime);
//        JSONObject json = new JSONObject(paramMap);
//        String body = json.toString();
//        SnailJobLog.LOCAL.info("用户抽取调用第三方参数. body:{}", body);
//        SnailJobLog.REMOTE.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> datum : data) {
//            SysUser sysUser = setSysUser(datum);
//            SysUser user = sysUserMapper.selectByUserName(sysUser.getUserName());
//            if (user == null){
//                sysUserMapper.insert(sysUser);
//                SnailJobLog.LOCAL.info("用户. userName:{}抽取成功！", sysUser.getUserName());
//                SnailJobLog.REMOTE.info("用户. userName:{}抽取成功！", sysUser.getUserName());
//            }else {
//                // 用户信息异常，修改设备表中对应设备的是否异常字段
//                if ("0".equals(user.getStatus()) && "1".equals(sysUser.getStatus())){
//                    if (ObjectUtil.isNotEmpty(sysUser.getIdCard())){
//                        TDeviceInfo deviceInfo = deviceInfoMapper.selectByIdCard(sysUser.getIdCard());
//                        if (deviceInfo != null){
//                            deviceInfoMapper.updateSfyc(deviceInfo.getId());
//                        }
//                    }
//                }
//                sysUser.setUserId(user.getUserId());
//                sysUserMapper.updateById(sysUser);
//                SnailJobLog.LOCAL.info("用户. userName:{}更新成功！", sysUser.getUserName());
//                SnailJobLog.REMOTE.info("用户. userName:{}更新成功！", sysUser.getUserName());
//            }
//        }
//        return ExecuteResult.success("增量用户抽取程序执行成功！");
//    }
//
//    private SysUser setSysUser(Map<String, Object> datum) {
//        Map<String, Object> user = (Map<String, Object>) datum.get("user");
//        Object userId = user.get("id"); // userId
//        Object nickName = user.get("name"); // 姓名
//        Object userType = user.get("type");
//        Object sex = user.get("sex"); // 性别
//        Object userName = user.get("userNo"); // 警号
//        Object email = user.get("workEmail");
//        Object phoneNumber = user.get("personalPhone");
//        Object workPhone = user.get("workPhone");
//        Object cardNo = user.get("cardNo");
//        Object policeType = user.get("policeType");
//        Object avatar = user.get("head");
//        Object status = user.get("status");
//        Object createTime = user.get("createTime");
//        Object updateTime = user.get("modiTime");
//        Object post = user.get("post");
//        SysUser sysUser = new SysUser();
//        sysUser.setPassword(BCrypt.hashpw(userName.toString()));
//        if (ObjectUtil.isNotEmpty(userId)) sysUser.setRemark(userId.toString());
//        if (!ObjectUtil.isNull(nickName)) sysUser.setNickName(nickName.toString());
//        if (!ObjectUtil.isNull(userType)) sysUser.setUserType(userType.toString());
//        if (!ObjectUtil.isNull(sex)) sysUser.setSex(sex.toString());
//        if (!ObjectUtil.isNull(userName)) sysUser.setUserName(userName.toString());
//        if (!ObjectUtil.isNull(email)) sysUser.setEmail(email.toString());
//        if (!ObjectUtil.isNull(phoneNumber)) sysUser.setPhonenumber(phoneNumber.toString());
//        if (!ObjectUtil.isNull(avatar)) sysUser.setAvatar(avatar.toString());
//        if (!ObjectUtil.isNull(createTime)) sysUser.setCreateTime(DateUtil.parse(createTime.toString(), "yyyy-MM-dd HH:mm:ss"));
//        if (!ObjectUtil.isNull(updateTime)) sysUser.setUpdateTime(DateUtil.parse(updateTime.toString(), "yyyy-MM-dd HH:mm:ss"));
//        if (!ObjectUtil.isNull(workPhone)) sysUser.setWorkPhone(workPhone.toString());
//        if (!ObjectUtil.isNull(cardNo)) sysUser.setIdCard(cardNo.toString());
//        if (!ObjectUtil.isNull(policeType)) sysUser.setPoliceType(policeType.toString());
//        if (!ObjectUtil.isNull(post)) sysUser.setPost(post.toString());
//        if (!ObjectUtil.isNull(status)) {
//            if ("1".equals(status)){
//                sysUser.setStatus("0");
//            }else if ("0".equals(status)){
//                sysUser.setStatus("1");
//            }else {
//                sysUser.setStatus("1");
//                sysUser.setDelFlag("2");
//            }
//        }
//        List<Map> userOrgList = (List<Map>) datum.get("userOrg");
//        for (Map map1 : userOrgList) {
//            if ("RELATIONSHIP_TYPE_1".equals(map1.get("type"))) { // 正式
//                String orgId1 = (String) map1.get("orgId");
//                sysUser.setDeptId(Long.valueOf(orgId1));
//            }
//        }
//        return sysUser;
//    }
//
//}
