package com.ningdatech.pmapi.ding.task;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.ningdatech.basic.model.GenericResult;
import com.ningdatech.pmapi.organization.entity.GovBusinessStrip;
import com.ningdatech.pmapi.organization.service.IGovBusinessStripService;
import com.ningdatech.zwdd.client.ZwddClient;
import com.ningdatech.zwdd.model.Page;
import com.ningdatech.zwdd.model.dto.SubGovBusinessStripsDTO;
import com.ningdatech.zwdd.model.query.PageSubGovBusinessStripsQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author liuxinxin
 * @date 2023/3/8 下午2:55
 */

@Component
@Slf4j
public class GovBusinessStripsTask {

    private final static Integer PAGE_SIZE = 20;

    private final static Integer GROUP_SIZE = 100;

    @Autowired
    private ZwddClient zwddClient;

    @Autowired
    private IGovBusinessStripService iGovBusinessStripService;

    @Transactional(rollbackFor = Exception.class)
//    @Scheduled(cron = "0 0 1 * * ?")
    public void batchGetGovBusinessStripsTask() {
        PageSubGovBusinessStripsQuery pageSubGovBusinessStripsQuery = new PageSubGovBusinessStripsQuery();
        pageSubGovBusinessStripsQuery.setPageNo(1);
        pageSubGovBusinessStripsQuery.setPageSize(100);
        GenericResult<Page<SubGovBusinessStripsDTO>> pageGenericResult = zwddClient.pageSubGovBusinessStrips(pageSubGovBusinessStripsQuery);

        log.info("pageGenericResult :{}", JSON.toJSONString(pageGenericResult));

        if(!pageGenericResult.isSuccess()){
            return;
        }

        Page<SubGovBusinessStripsDTO> data = pageGenericResult.getData();
        List<SubGovBusinessStripsDTO> govBusinessStripsDTOList = data.getData();

        List<GovBusinessStrip> govBusinessStripList = govBusinessStripsDTOList.stream().map(r -> {
            GovBusinessStrip govBusinessStrip = new GovBusinessStrip();
            govBusinessStrip.setBusinessStripCode(r.getCode());
            govBusinessStrip.setBusinessStripName(r.getName());
            return govBusinessStrip;
        }).collect(Collectors.toList());

        saveBatch(govBusinessStripList);

        for (SubGovBusinessStripsDTO subGovBusinessStripsDTO : govBusinessStripsDTOList) {
            saveSub(subGovBusinessStripsDTO);
        }

    }


    public void saveSub(SubGovBusinessStripsDTO parentGovBusinessStripsDTO) {
        String parentCode = parentGovBusinessStripsDTO.getCode();
        String parentName = parentGovBusinessStripsDTO.getName();

        PageSubGovBusinessStripsQuery pageSubGovBusinessStripsQuery = new PageSubGovBusinessStripsQuery();
        pageSubGovBusinessStripsQuery.setPageNo(1);
        pageSubGovBusinessStripsQuery.setPageSize(100);
        pageSubGovBusinessStripsQuery.setBusinessStripCode(parentCode);
        GenericResult<Page<SubGovBusinessStripsDTO>> pageGenericResult = zwddClient.pageSubGovBusinessStrips(pageSubGovBusinessStripsQuery);
        Page<SubGovBusinessStripsDTO> data = pageGenericResult.getData();
        List<SubGovBusinessStripsDTO> govBusinessStripsDTOList = data.getData();

        if (CollectionUtil.isNotEmpty(govBusinessStripsDTOList)) {
            List<GovBusinessStrip> govBusinessStripList = govBusinessStripsDTOList.stream().map(r -> {
                GovBusinessStrip govBusinessStrip = new GovBusinessStrip();
                govBusinessStrip.setBusinessStripCode(r.getCode());
                govBusinessStrip.setBusinessStripName(r.getName());
                govBusinessStrip.setParentCode(parentCode);
                govBusinessStrip.setParentName(parentName);
                return govBusinessStrip;
            }).collect(Collectors.toList());
            saveBatch(govBusinessStripList);
        }
    }


    public void saveBatch(List<GovBusinessStrip> govBusinessStripList) {
        if (CollectionUtil.isNotEmpty(govBusinessStripList)) {
            iGovBusinessStripService.saveBatch(govBusinessStripList);
        }
    }
}
