package com.ningdatech.pmapi.ding.task;

import cn.hutool.core.collection.CollUtil;
import com.ningdatech.basic.model.GenericResult;
import com.ningdatech.pmapi.common.config.GovDingProperties;
import com.ningdatech.pmapi.organization.entity.DingOrganization;
import com.ningdatech.pmapi.organization.service.IDingOrganizationService;
import com.ningdatech.zwdd.client.ZwddClient;
import com.ningdatech.zwdd.model.Page;
import com.ningdatech.zwdd.model.query.PageOrganizationEmployeePositionsQuery;
import com.ningdatech.zwdd.model.response.OrganizationEmployeePosition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liuxinxin
 * @date 2023/2/10 上午9:52
 */

@Component
public class EmployeeBatchGetTask {

    private final static Integer PAGE_SIZE = 20;

    @Autowired
    private ZwddClient zwddClient;
    private IDingOrganizationService iDingOrganizationService;

    @Transactional(rollbackFor = Exception.class)
    public void batchGetEmployeeTask() {
        List<DingOrganization> dingOrganizationList = iDingOrganizationService.list();
        if (CollUtil.isNotEmpty(dingOrganizationList)) {
            List<OrganizationEmployeePosition> allOrganizationEmployeePositionList = new ArrayList<>();

            for (DingOrganization dingOrganization : dingOrganizationList) {
                String organizationCode = dingOrganization.getOrganizationCode();
                PageOrganizationEmployeePositionsQuery query = new PageOrganizationEmployeePositionsQuery();
                query.setEmployeeStatus("A");
                query.setOrganizationCode(organizationCode);
                query.setReturnTotalSize(true);
                query.setTenantId(GovDingProperties.tenantId);

                int pageNo = 1;
                query.setPageNo(pageNo);
                query.setPageSize(PAGE_SIZE);

                GenericResult<Page<OrganizationEmployeePosition>> firstPageGenericResult = zwddClient.pageOrganizationEmployeePositions(query);
                Page<OrganizationEmployeePosition> data = firstPageGenericResult.getData();
                if (CollUtil.isNotEmpty(data.getData())) {
                    allOrganizationEmployeePositionList.addAll(data.getData());
                }
                Long totalSize = data.getTotalSize();
                if (totalSize > PAGE_SIZE) {
                    int maxPageNo = (int) Math.ceil(totalSize / PAGE_SIZE);
                    for (pageNo = 2; pageNo <= maxPageNo; pageNo++) {
                        query.setPageNo(maxPageNo);
                        GenericResult<Page<OrganizationEmployeePosition>> pageGenericResult = zwddClient.pageOrganizationEmployeePositions(query);
                        if (CollUtil.isNotEmpty(pageGenericResult.getData().getData())) {
                            allOrganizationEmployeePositionList.addAll(pageGenericResult.getData().getData());
                        }
                    }
                }



            }

//            zwddClient.pageOrganizationEmployeePositions(query);

        }


    }
}
