/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
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.domain.Log;
import me.zhengjie.modules.dm.user.domain.DmUser;
import me.zhengjie.modules.dm.user.repository.DmUserRepository;
import me.zhengjie.modules.system.domain.Dept;
import me.zhengjie.modules.system.domain.Job;
import me.zhengjie.modules.system.domain.Role;
import me.zhengjie.modules.system.domain.User;
import me.zhengjie.modules.system.repository.DeptRepository;
import me.zhengjie.modules.system.repository.UserRepository;
import me.zhengjie.repository.LogRepository;
import me.zhengjie.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * ERP 数据同步
 * @author Sky
 */
@Slf4j
@RequiredArgsConstructor
@Component
public class ZkDataSyncTask {

    @Value("${key}")
    private String key;
    @Value("${zkApiUrl}")
    private String zkApiUrl;
    @Value("${headerKey}")
    private String header_key;
    @Value("${deptApi}")
    private String deptApi;
    @Value("${userApi}")
    private String userApi;

    private final UserRepository userRepository;
    private final DmUserRepository dmUserRepository;
    private final DeptRepository deptRepository;
    private final PasswordEncoder passwordEncoder;
    private final LogRepository logRepository;

    public void run(){
        log.info("deptsync 执行开始");
//        deptSync();
        deptSyncAll();
        log.info("deptsync 执行结束");

        log.info("usersync 执行开始");
//        userSync();
        userSyncAll();
        log.info("usersync 执行结束");
    }

    public void runDept(){
        log.info("deptsync 执行开始");
        deptSyncAll();
        log.info("deptsync 执行结束");
    }

    public void runUser(){
        log.info("usersync 执行开始");
        userSyncAll();
        log.info("usersync 执行结束");
    }

    private JSONObject getDatas(String api,JSONArray time, int pageNum, int pageSize,int type) {
        String res = "";
        JSONObject json = null;
        try {
            Map<String,Object> tokenData = SecurityUtils.getToken(key);
            JSONObject body = new JSONObject();
            body.put("timestamp",tokenData.get("timestamp"));
            body.put("nonce",tokenData.get("nonce"));
            JSONObject query = new JSONObject();
            if(type == 1){
                query.put("updateTime",time);
            }
            body.put("query",query);

            res = HttpRequest.get(zkApiUrl + api+"?page="+pageNum+"&size="+pageSize)
                    .header(header_key, tokenData.get("token").toString())
                    .body(body.toString()).execute().body();
            json = JSONObject.parseObject(res);
            log.info("json:"+json);
        } catch (Exception ex){
            ex.printStackTrace();
            Log log = new Log();
            log.setDescription(ex.toString());
            log.setLogType("ERROR");
            log.setMethod(zkApiUrl + api);
            log.setParams(res);
            log.setUsername("中控同步数据问题");
            logRepository.save(log);
        }
        return json;
    }

    /**
     * 部门数据同步
     */
    private void deptSync() {
//        String api = "/api/thirdparty/v1/user/getzkdeps";
        JSONArray createTme = new JSONArray();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY,-24);
        String yesterdayDate = dateFormat.format(calendar.getTime());
        String todayDate = dateFormat.format(new Date());
        createTme.add(yesterdayDate);
        createTme.add(todayDate);

        //计算总页数
        int total = getDatas(deptApi,createTme,0,100,1).getJSONObject("data").getInteger("totalElements");
        int pageSize = 100;
        int pageTotal = (total / pageSize);
        if (total % pageSize != 0) {
            pageTotal++;
        }
        log.info("pageTotal:"+pageTotal);

        //按页获取
        for (int k = 0; k < pageTotal; k++) {
            JSONObject json = getDatas(deptApi, createTme,k,pageSize,1);
            JSONObject data = json.getJSONObject("data");
            JSONArray content = data.getJSONArray("content");
            for (int i = 0; i < content.size(); i++) {
                Dept dept = JSONObject.parseObject(content.getString(i), Dept.class);
                deptRepository.save(dept);
            }
        }
    }

    /**
     * 部门数据同步(全量)
     */
    private void deptSyncAll() {
//        String api = "/api/thirdparty/v1/user/getzkdeps";

        //计算总页数
        int total = getDatas(deptApi,null,0,100,2).getJSONObject("data").getInteger("totalElements");
        int pageSize = 100;
        int pageTotal = (total / pageSize);
        if (total % pageSize != 0) {
            pageTotal++;
        }
        log.info("pageTotal:"+pageTotal);

        //按页获取
        for (int k = 0; k < pageTotal; k++) {
            JSONObject json = getDatas(deptApi,null,k,pageSize,2);
            JSONObject data = json.getJSONObject("data");
            JSONArray content = data.getJSONArray("content");
            for (int i = 0; i < content.size(); i++) {
                Dept dept = JSONObject.parseObject(content.getString(i), Dept.class);
                deptRepository.save(dept);
            }
        }
    }

    /**
     * 人员数据同步
     */
    private void userSync() {
//        String api = "/api/thirdparty/v1/user/getZkSysUsers";

        JSONArray createTme = new JSONArray();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY,-24);
        String yesterdayDate = dateFormat.format(calendar.getTime());

        String todayDate = dateFormat.format(new Date());

        createTme.add(yesterdayDate);
        createTme.add(todayDate);

        //计算总页数
        int total = getDatas(userApi,createTme,0,1,1).getJSONObject("data").getInteger("totalElements");
        int pageSize = 10;
        int pageTotal = (total / pageSize);
        if (total % pageSize != 0) {
            pageTotal++;
        }
        log.info("pageTotal:"+pageTotal);

        //按页获取
        for (int k = 0; k < pageTotal; k++) {
            JSONObject json = getDatas(userApi, createTme,k,pageSize,1);
            JSONObject data = json.getJSONObject("data");
            JSONArray content = data.getJSONArray("content");
            for (int i = 0; i < content.size(); i++) {
                User user = new User();
                JSONObject userData = content.getJSONObject(i);

                user.setId(userData.getString("id"));
                if(userData.containsKey("username")) {
                    user.setUsername(userData.getString("username"));
                }
                user.setPassword(userData.getString("password"));
                user.setOrgPassword("12345678");
                if(userData.containsKey("jobs")) {
                    Set<Job> jobs = new HashSet<>(JSONArray.parseArray(userData.getString("jobs"), Job.class));
                    user.setJobs(jobs);
                }
                if(userData.containsKey("roles")) {
                    Set<Role> roles = new HashSet<>(JSONArray.parseArray(userData.getString("roles"), Role.class));
                    user.setRoles(roles);
                }
                if(userData.containsKey("gender")) {
                    user.setGender(userData.getString("gender"));
                }

                if(userData.containsKey("dept")) {
                    user.setDept(JSONObject.parseObject(userData.getString("dept"), Dept.class));
                }

                if(userData.containsKey("email")) {
                    user.setEmail(userData.getString("email"));
                }

                if(userData.containsKey("nickName")) {
                    user.setNickName(userData.getString("nickName"));
                }

                if(userData.containsKey("phone")) {
                    user.setPhone(userData.getString("phone"));
                }

                if(userData.containsKey("enabled")) {
                    user.setEnabled(userData.getBoolean("enabled"));
                }

                DmUser dmUser = new DmUser();
                dmUser.setDept(user.getDept());
                dmUser.setId(user.getId());
                dmUser.setEmail(user.getEmail());
                dmUser.setContacts(user.getPhone());
                dmUser.setName(user.getNickName());
                dmUser.setSex(user.getGender());

                if(!ObjectUtils.isEmpty(userData.getJSONObject("dmUser"))){
                    JSONObject dmUserObject = userData.getJSONObject("dmUser");
                    if(dmUserObject.containsKey("cardid")) {
                        dmUser.setCardId(dmUserObject.getString("cardid"));
                    }
                    if(dmUserObject.containsKey("userNumber")) {
                        dmUser.setUserNumber(dmUserObject.getString("userNumber"));
                    }
                    if(dmUserObject.containsKey("faceid")) {
                        dmUser.setFaceid(dmUserObject.getString("faceid"));
                    }
                }
                DmUser newDmUser = dmUserRepository.save(dmUser);
                user.setDmUser(newDmUser);
                userRepository.save(user);
            }
        }
    }


    /**
     * 人员数据同步（全量）
     */
    private void userSyncAll() {

//        String api = "/api/thirdparty/v1/user/getZkSysUsers";

        //计算总页数
        int total = getDatas(userApi,null,0,10,2).getJSONObject("data").getInteger("totalElements");
        int pageSize = 10;
        int pageTotal = (total / pageSize);
        if (total % pageSize != 0) {
            pageTotal++;
        }
        log.info("pageTotal:"+pageTotal);

        //按页获取
        for (int k = 0; k < pageTotal; k++) {
            JSONObject json = getDatas(userApi, null,k,pageSize,1);
            JSONObject data = json.getJSONObject("data");
            JSONArray content = data.getJSONArray("content");
            for (int i = 0; i < content.size(); i++) {
                User user = new User();
                JSONObject userData = content.getJSONObject(i);

                user.setId(userData.getString("id"));
                if(userData.containsKey("username")) {
                    user.setUsername(userData.getString("username"));
                }
                user.setPassword(userData.getString("password"));
                user.setOrgPassword("12345678");
                if(userData.containsKey("jobs")) {
                    Set<Job> jobs = new HashSet<>(JSONArray.parseArray(userData.getString("jobs"), Job.class));
                    user.setJobs(jobs);
                }
                if(userData.containsKey("roles")) {
                    Set<Role> roles = new HashSet<>(JSONArray.parseArray(userData.getString("roles"), Role.class));
                    user.setRoles(roles);
                }
                if(userData.containsKey("gender")) {
                    user.setGender(userData.getString("gender"));
                }

                if(userData.containsKey("dept")) {
                    user.setDept(JSONObject.parseObject(userData.getString("dept"), Dept.class));
                }

                if(userData.containsKey("email")) {
                    user.setEmail(userData.getString("email"));
                }

                if(userData.containsKey("nickName")) {
                    user.setNickName(userData.getString("nickName"));
                }

                if(userData.containsKey("phone")) {
                    user.setPhone(userData.getString("phone"));
                }

                if(userData.containsKey("enabled")) {
                    user.setEnabled(userData.getBoolean("enabled"));
                }

                DmUser dmUser = new DmUser();
                dmUser.setDept(user.getDept());
                dmUser.setId(user.getId());
                dmUser.setEmail(user.getEmail());
                dmUser.setContacts(user.getPhone());
                dmUser.setName(user.getNickName());
                dmUser.setSex(user.getGender());

                if(!ObjectUtils.isEmpty(userData.getJSONObject("dmUser"))){
                    JSONObject dmUserObject = userData.getJSONObject("dmUser");
                    if(dmUserObject.containsKey("cardid")) {
                        dmUser.setCardId(dmUserObject.getString("cardid"));
                    }
                    if(dmUserObject.containsKey("userNumber")) {
                        dmUser.setUserNumber(dmUserObject.getString("userNumber"));
                    }
                    if(dmUserObject.containsKey("faceid")) {
                        dmUser.setFaceid(dmUserObject.getString("faceid"));
                    }
                }
                DmUser newDmUser = dmUserRepository.save(dmUser);
                user.setDmUser(newDmUser);
                userRepository.save(user);
            }
        }
    }
}