package com.ensign.crm.module.crm.job.service;

import com.alibaba.fastjson.JSONObject;
import com.ensign.crm.framework.common.enums.CommonStatusEnum;
import com.ensign.crm.module.crm.exception.AllKingdeeException;
import com.ensign.crm.module.crm.service.ProxyService;
import com.ensign.crm.module.system.dal.dataobject.user.AdminUserDO;
import com.ensign.crm.module.system.dal.mysql.user.AdminUserMapper;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: TODO
 * @Date: 2024/10/15 17:02
 * @Created: by ZZSLL
 */

@Component
@Slf4j
public class TaskHandler {

    @Autowired
    private AdminUserMapper userMapper;
    @Autowired
    private ProxyService proxyService;

    public void syncUser(AdminUserDO user, List<AdminUserDO> errors) throws IOException, URISyntaxException, AllKingdeeException {
        Map<String, Object> body = convertToKingdeeObj(user);
        log.info("请求body：{}", body);
        JSONObject jsonObject = proxyService.doRequestPost("/ierp/kapi/v2/yem/yem_crmbasic/yem_crm_dealeruser/CRM_yem_crm_dealeruser", "POST", body, null);
        log.info("响应body：{}", jsonObject);
        if (jsonObject.containsKey("status") && jsonObject.containsKey("errorCode")) {
            if (jsonObject.getBoolean("status") && jsonObject.getInteger("errorCode") == 0) {
                if (jsonObject.containsKey("data")) {
                    JSONObject dataObject = jsonObject.getJSONObject("data");
                    if (dataObject.containsKey("successCount")) {
                        int successCount = dataObject.getInteger("successCount");
                        if (successCount < 1) {
                            errors.add(user);
                            log.error("异常数据");
                        }

                        if (successCount == 1) {
                            user.setIsSyncKingdee(CommonStatusEnum.ENABLE.getStatus());
                            userMapper.updateById(user);
                        }
                    }
                }
            }
        }
    }

    @NotNull
    public static Map<String, Object> convertToKingdeeObj(AdminUserDO user) {
        List<Map<String, Object>> list = new ArrayList<>();

        Map<String, Object> addUser = new HashMap<>();
        addUser.put("number", user.getUsername());//编码
        addUser.put("name", user.getNickname());//经销商销售员
        addUser.put("webuserid", user.getId());//移动端人员ID
        addUser.put("webdeptid", user.getDeptId());//经销商.移动端部门ID
        addUser.put("function", user.getCrmUserFunction());//职能 [D:经销商后台, E:经销商销售员]
        addUser.put("phone", user.getMobile());//手机号
        addUser.put("createorg", "110");//创建组织.编码
        list.add(addUser);
        Map<String, Object> body = new HashMap<>();
        body.put("data", list);
        return body;
    }
}
