package com.ningdatech.kqapi.scheduler.manage;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.ningdatech.kqapi.zzsfw.model.dto.KqZzsfwMattersDeduplicateDTO;
import com.ningdatech.kqapi.zzsfw.model.dto.KqZzsfwMenuDTO;
import com.ningdatech.kqapi.zzsfw.model.dto.KqZzsfwPolicyDTO;
import com.ningdatech.kqapi.zzsfw.model.entity.KqZzsfwMenu;
import com.ningdatech.kqapi.zzsfw.model.entity.KqZzsfwMattersDeduplicate;
import com.ningdatech.kqapi.zzsfw.model.entity.KqZzsfwPolicy;
import com.ningdatech.kqapi.zzsfw.service.IKqZzsfwMatterDeduplicateService;
import com.ningdatech.kqapi.zzsfw.service.IKqZzsfwMenuService;
import com.ningdatech.kqapi.zzsfw.service.IKqZzsfwPolicyService;
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 IKqZzsfwMatterDeduplicateService deduplicateService;

    private final IKqZzsfwMenuService menuService;

    private final IKqZzsfwPolicyService policyService;

    private final RestTemplate restTemplate;

    public void synData() {
        log.info("同步数据开始");
        //1.去重数据
        List<KqZzsfwMattersDeduplicate> dups = deduplicateService.list();
        for(KqZzsfwMattersDeduplicate dup : dups){
            KqZzsfwMattersDeduplicateDTO dto = BeanUtil.copyProperties(dup, KqZzsfwMattersDeduplicateDTO.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<KqZzsfwMenu> menus = menuService.list();
        for(KqZzsfwMenu menu : menus){
            KqZzsfwMenuDTO dto = BeanUtil.copyProperties(menu, KqZzsfwMenuDTO.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<KqZzsfwPolicy> policies = policyService.list();
        for(KqZzsfwPolicy policy : policies){
            KqZzsfwPolicyDTO dto = BeanUtil.copyProperties(policy, KqZzsfwPolicyDTO.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));
        }
    }
}
