package com.ningdatech.kqapi.scheduler.manage;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import com.ningdatech.kqapi.zzsfw.entity.dto.NdKqZzsfwMattersDeduplicateDTO;
import com.ningdatech.kqapi.zzsfw.entity.dto.NdKqZzsfwMenuDTO;
import com.ningdatech.kqapi.zzsfw.entity.dto.NdKqZzsfwPolicyDTO;
import com.ningdatech.kqapi.zzsfw.entity.entity.NdKqZzsfwMattersDeduplicate;
import com.ningdatech.kqapi.zzsfw.entity.entity.NdKqZzsfwMenu;
import com.ningdatech.kqapi.zzsfw.entity.entity.NdKqZzsfwPolicy;
import com.ningdatech.kqapi.zzsfw.service.INdKqZzsfwMatterDeduplicateService;
import com.ningdatech.kqapi.zzsfw.service.INdKqZzsfwMenuService;
import com.ningdatech.kqapi.zzsfw.service.INdKqZzsfwPolicyService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @Classname SynManage
 * @Description
 * @Date 2023/12/20 15:18
 * @Author PoffyZhang
 */
@Component
@Slf4j
@RequiredArgsConstructor
public class SynManage {

    private final INdKqZzsfwMatterDeduplicateService deduplicateService;

    private final INdKqZzsfwMenuService menuService;

    private final INdKqZzsfwPolicyService policyService;

    private final RestTemplate restTemplate;

    public void synData() {
        log.info("同步数据开始");
        //1.去重数据
        List<NdKqZzsfwMattersDeduplicate> dups = deduplicateService.list();
        for(NdKqZzsfwMattersDeduplicate dup : dups){
            NdKqZzsfwMattersDeduplicateDTO dto = BeanUtil.copyProperties(dup,NdKqZzsfwMattersDeduplicateDTO.class);
            HttpHeaders headers = new HttpHeaders();
            MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
            headers.setContentType(type);
            headers.add("Accept", MediaType.APPLICATION_JSON.toString());
            HttpEntity<String> formEntity = new HttpEntity(JSON.toJSONString(dto), headers);
            ResponseEntity<String> stringResponseEntity = restTemplate.postForEntity(
                    "http://localhost:33061/kq/api/v1/zzsfw/save-dup", formEntity, String.class);
            log.info("结果:{}", JSON.toJSONString(stringResponseEntity));
        }

        //2.菜单数据
        List<NdKqZzsfwMenu> menus = menuService.list();
        for(NdKqZzsfwMenu menu : menus){
            NdKqZzsfwMenuDTO dto = BeanUtil.copyProperties(menu,NdKqZzsfwMenuDTO.class);
            HttpHeaders headers = new HttpHeaders();
            MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
            headers.setContentType(type);
            headers.add("Accept", MediaType.APPLICATION_JSON.toString());
            HttpEntity<String> formEntity = new HttpEntity(JSON.toJSONString(dto), headers);
            ResponseEntity<String> stringResponseEntity = restTemplate.postForEntity(
                    "http://localhost:33061/kq/api/v1/zzsfw/save", formEntity, String.class);
            log.info("结果:{}", JSON.toJSONString(stringResponseEntity));
        }

        //3.其它数据
        List<NdKqZzsfwPolicy> policies = policyService.list();
        for(NdKqZzsfwPolicy policy : policies){
            NdKqZzsfwPolicyDTO dto = BeanUtil.copyProperties(policy,NdKqZzsfwPolicyDTO.class);
            HttpHeaders headers = new HttpHeaders();
            MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
            headers.setContentType(type);
            headers.add("Accept", MediaType.APPLICATION_JSON.toString());
            HttpEntity<String> formEntity = new HttpEntity(JSON.toJSONString(dto), headers);
            ResponseEntity<String> stringResponseEntity = restTemplate.postForEntity(
                    "http://localhost:33061/kq/api/v1/poclicy/save", formEntity, String.class);
            log.info("结果:{}", JSON.toJSONString(stringResponseEntity));
        }
    }
}
