package me.zhengjie.modules.quartz.task;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.modules.dm.user.service.DmUserService;
import me.zhengjie.modules.dm.user.service.dto.DmUserDto;
import me.zhengjie.modules.dm.user.service.dto.DmUserQueryCriteria;
import me.zhengjie.modules.system.service.DeptService;
import me.zhengjie.modules.system.service.dto.DeptDto;
import me.zhengjie.modules.system.service.dto.DeptQueryNoAuthCriteria;
import me.zhengjie.utils.ImageUtil;
import me.zhengjie.utils.SecurityUtils;
import me.zhengjie.utils.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Slf4j
@RequiredArgsConstructor
@Component
public class ZkDataPushTask {

    private final String key = "DMERPYT!@#$QWER2021+{:>";
    private final String fpApi = "http://10.208.128.14:8069/";

    private final DmUserService dmUserService;
    private final DeptService deptService;

    public void run() {
        log.info("pushAttendanceData 执行开始");
        pushAttendanceData();
        log.info("pushAttendanceData 执行结束");
    }

    private void pushAttendanceData(){
        String yktAttendanceApi = "http://192.168.1.25:8000/api/thirdparty/v2/user/getAttendance";
        String erpAttendanceApi = "http://10.22.2.65:16000/attendance-api/device/identify/callback";
        Map<String,Object> newToken = SecurityUtils.getToken(key);
        JSONObject param = new JSONObject();
        param.put("timestamp",newToken.get("timestamp"));
        param.put("nonce",newToken.get("nonce"));
        String res = HttpRequest.get(yktAttendanceApi)
                .header("XYTACCESSTOKEN",newToken.get("token").toString() )
                .body(param.toJSONString()).execute().body();
        if(StringUtils.isNotEmpty(res)) {
            log.info("res:"+res);
            JSONArray attendanceArray = JSONArray.parseArray(res);
            for(int i = 0;i<attendanceArray.size();i++){
                JSONObject attendance = attendanceArray.getJSONObject(i);
                //组装返回数据
                JSONObject ertAttendanceJSON = new JSONObject();
                ertAttendanceJSON.put("deviceKey",attendance.getString("roomDevice"));
                ertAttendanceJSON.put("personId",attendance.getString("userNumber"));
                ertAttendanceJSON.put("time",attendance.getString("attendanceTime"));
                ertAttendanceJSON.put("type","face_0");
                HttpRequest.get(erpAttendanceApi).body(ertAttendanceJSON.toJSONString()).execute().body();
            }
        } else {
            log.info("res is null.");
        }
    }


    private void pushFeiPuDeptData() throws Exception {
        String api = "openapi/deptOperate";
        List<DeptDto> deptDtoList = deptService.queryAll(new DeptQueryNoAuthCriteria());
        if(!ObjectUtils.isEmpty(deptDtoList)){
            for(DeptDto deptDto: deptDtoList){
                JSONObject param = new JSONObject();
                param.put("deptId",deptDto.getId());
                param.put("deptName",deptDto.getName());
                if(!ObjectUtils.isEmpty(deptDto.getPid())){
                    param.put("pDeptId",deptDto.getPid());
                } else {
                    param.put("pDeptId","@");
                }
                param.put("sort",deptDto.getDeptSort());
                param.put("operateType",1);
                String res = HttpRequest.post(fpApi+api)
                        .form("dept",param.toJSONString()).execute().body();
                log.info("res:"+res);
            }
        }
    }

    private void pushFeiPuUserData() throws Exception {
        String api = "openapi/personOperate";
        List<Map<String,String>> dmUserDtoList = dmUserService.queryAll();
        if(!ObjectUtils.isEmpty(dmUserDtoList)){
            for(Map<String,String> map: dmUserDtoList){
                JSONObject param = new JSONObject();
                param.put("userId",map.get("userCode"));
                param.put("userName",map.get("name"));
                param.put("deptId",map.get("deptId"));
                Integer sex = null;
                if(StringUtils.isNotBlank(map.get("sex"))){
                    if(StringUtils.equals("男",map.get("sex"))){
                        sex = 2;
                    } else if(StringUtils.equals("女",map.get("sex"))) {
                        sex = 1;
                    }
                }
                param.put("sex",sex);
                param.put("cardId",map.get("cardId"));
                param.put("operateType",1);
//                if(map.containsKey("faceId")){
//                    if(!ObjectUtils.isEmpty(map.get("faceId"))){
//                        param.put("facePicture", ImageUtil.encodeImageToBase64(new URL(map.get("faceId"))));
//                    }
//                }
                String res = HttpRequest.post(fpApi+api)
                        .form("person",param.toJSONString()).execute().body();
                log.info("res:"+res);
            }
        }
    }
}
