package com.ningdatech.pmapi.org;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ningdatech.pmapi.AppTests;
import com.ningdatech.pmapi.organization.model.entity.DingOrganization;
import com.ningdatech.pmapi.organization.service.IDingOrganizationService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;

/**
 * @Classname RegionTest
 * @Description
 * @Date 2023/6/8 14:44
 * @Author PoffyZhang
 */
public class OrgTest extends AppTests {

    @Autowired
    private IDingOrganizationService organizationService;

    @Test
    public void test(){
        List<DingOrganization> orgs = organizationService.list(Wrappers.lambdaQuery(DingOrganization.class)
                .eq(DingOrganization::getDivisionCode, "331199"));

        for(DingOrganization org  : orgs){
            List<DingOrganization> inners = organizationService.list(Wrappers.lambdaQuery(DingOrganization.class)
                    .eq(DingOrganization::getParentCode, org.getOrganizationCode()));
            for(DingOrganization inner : inners){
                mergeRegionCode(inner);
            }
        }
    }

    private void mergeRegionCode(DingOrganization inner) {
        inner.setDivisionCode("331199");
        organizationService.updateById(inner);

        List<DingOrganization> is = organizationService.list(Wrappers.lambdaQuery(DingOrganization.class)
                .eq(DingOrganization::getParentCode, inner.getOrganizationCode()));
        if(CollUtil.isEmpty(is)){
            return;
        }
        for(DingOrganization i : is){
            mergeRegionCode(i);
        }
    }

}
