package com.qmth.distributed.print;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qmth.teachcloud.common.contant.SystemConstant;
import com.qmth.teachcloud.common.entity.SysPrivilege;
import com.qmth.teachcloud.common.service.SysPrivilegeService;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.*;

/**
 * @Date: 2021/8/9.
 */
@SpringBootTest
@RunWith(SpringRunner.class)
public class CreateSchoolPrivilegeTest {

    private static final Long SCHOOL_ID = 2l;

    @Autowired
    private SysPrivilegeService sysPrivilegeService;

    @Test
    public void test(){
        QueryWrapper<SysPrivilege> queryWrapper = new QueryWrapper<>();
//        queryWrapper.lambda().eq(SysPrivilege::getSchoolId, SCHOOL_ID);
        List<SysPrivilege> sysPrivileges = sysPrivilegeService.list(queryWrapper);
        Map<Long, Long> map = new HashMap<>();
        List<SysPrivilege> newList = new ArrayList<>();
        for (SysPrivilege sysPrivilege : sysPrivileges) {
            Long id = SystemConstant.getDbUuid();
            map.put(sysPrivilege.getId(), id);
            sysPrivilege.setId(id);
//            sysPrivilege.setSchoolId(137l);
            newList.add(sysPrivilege);
        }
        for (SysPrivilege sysPrivilege : newList) {
            if(Objects.nonNull(sysPrivilege.getParentId())){
                sysPrivilege.setParentId(map.get(sysPrivilege.getParentId()));
            }
            String relateId = sysPrivilege.getRelated();
            if(StringUtils.isNotBlank(relateId)){
                String[] relateIds = relateId.split(",");
                List<String> newRelateIds = new ArrayList<>();
                for (String id : relateIds) {
                    Long lid = Long.valueOf(id.trim());
                    newRelateIds.add(String.valueOf(map.get(lid)));
                }
                String newRelateId = String.join(",", newRelateIds);
                sysPrivilege.setRelated(newRelateId);
            }
        }
        sysPrivilegeService.saveBatch(newList);
    }

}
