package com.cpop.oam.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.cpop.common.utils.StringUtils;
import com.cpop.common.utils.bean.BeanUtils;
import com.cpop.core.base.entity.BaseEntity;
import com.cpop.core.base.enums.InitRoleEnum;
import com.cpop.core.base.enums.SourceType;
import com.cpop.core.base.table.SysUser;
import com.cpop.core.mapper.CoreMapper;
import com.cpop.core.utils.PasswordEncoder;
import com.cpop.core.utils.RsaUtils;
import com.cpop.core.utils.SpringUtils;
import com.cpop.jambox.business.entity.*;
import com.cpop.jambox.business.service.*;
import com.cpop.oam.business.bo.StaffBo;
import com.cpop.oam.business.entity.*;
import com.cpop.oam.business.service.*;
import com.cpop.oam.business.vo.StaffInfoVo;
import com.cpop.system.business.entity.Store;
import com.cpop.system.business.entity.StoreLicense;
import com.cpop.system.business.entity.StoreRenew;
import com.cpop.system.business.entity.StoreSign;
import com.cpop.system.business.service.*;
import com.mybatisflex.core.FlexGlobalConfig;
import com.mybatisflex.core.datasource.DataSourceKey;
import com.mybatisflex.core.datasource.FlexDataSource;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.row.Db;
import com.mybatisflex.core.row.DbChain;
import com.mybatisflex.core.row.Row;
import com.mybatisflex.core.row.RowUtil;
import com.zaxxer.hikari.HikariDataSource;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;

import java.io.*;
import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;

import static com.cpop.core.base.table.table.SysUserTableDef.SYS_USER;
import static com.cpop.jambox.business.entity.table.BrandExtendTableDef.BRAND_EXTEND;
import static com.cpop.jambox.business.entity.table.StoreExtendTableDef.STORE_EXTEND;
import static com.cpop.oam.business.entity.table.StaffTableDef.STAFF;
import static com.cpop.system.business.entity.table.BrandTableDef.BRAND;
import static com.cpop.system.business.entity.table.StoreTableDef.STORE;

/**
 * @author DB
 * @createTime 2023/11/27 17:24
 * @description 导入数据
 */
@Slf4j
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles(profiles = {"dev", "core"})
public class CpopImportTests {

    /**
     * @descriptions 导入品牌
     * @author DB
     * @date 2023/11/27 17:26
     * @return: void
     */
    @Test
    public void importBrandJson() throws IOException {
        /*String brandFileUrl = "C:\\Users\\Administrator\\Desktop\\brand.json";
        List<String> filterList = new ArrayList<>();
        filterList.add("b00064a760d0c4f121b0835d09b909ca");
        filterList.add("41ae62ef621ee0f909da5583399ff318");
        filterList.add("0122a5876468513f0d42569d389e8264");
        filterList.add("0ab5303b62c68da00d302cc2079b7fbe");
        filterList.add("41ae62ef6228587e0b430c3a16483a9a");
        filterList.add("ac1268b164d1d20700080aae1703ecf8");
        filterList.add("def1da45654259bc090e5054208e9b9c");
        List<JsonBrand> jsonBrands = JSONArray.parseArray(readJson(brandFileUrl), JsonBrand.class);
        //过滤已存在的品牌
        List<JsonBrand> filterBrand = jsonBrands.stream().filter(item -> !filterList.contains(item.getBrandCloudId())).collect(Collectors.toList());
        //打印
        Map<BrandExtend, Brand> brandJsonBrandMap = new HashMap<>();
        filterBrand.forEach(item -> {
            Brand brand = new Brand();
            brand.setSourceType(SourceType.JAMBOX.toString()).setBrandName(item.getBrandName());
            BrandExtend brandExtend = new BrandExtend();
            brandExtend.setBrandCloudId(item.getBrandCloudId());
            brandJsonBrandMap.put(brandExtend, brand);
        });
        SpringUtils.getBean(BrandService.class).saveBatch(brandJsonBrandMap.values());
        //批量插入拓展信息
        brandJsonBrandMap.forEach((key, value) -> {
            key.setBrandId(value.getId());
        });
        SpringUtils.getBean(BrandExtendService.class).saveBatch(brandJsonBrandMap.keySet());*/
        //更新商户号
        try {
            //查询旧表
            DataSourceKey.use("jambox");
            List<Row> rowList = DbChain.table("j_merchant_info")
                    .select("jmi.wx_mch_id as wxMchId")
                    .select("tbi.brand_id as brandCloudId")
                    .from("j_merchant_info").as("jmi")
                    .leftJoin("t_brand_info").as("tbi").on("tbi.id = jmi.main_id")
                    .where("jmi.main_type = 1")
                    .list();
            log.info(JSONArray.toJSONString(rowList));
        } finally {
            DataSourceKey.clear();
        }
    }

    /**
     * @descriptions 导入校区
     * @author DB
     * @date 2023/11/27 17:26
     * @return: void
     */
    @Test
    public void importStoreJson() throws IOException {
        String storeFileUrl = "C:\\Users\\Administrator\\Desktop\\store.json";
        List<String> filterList = Db.selectListByQuery(STORE_EXTEND.getTableName(), QueryWrapper.create().select(STORE_EXTEND.STORE_CLOUD_ID))
                .stream().map(item -> item.getString("storeCloudId")).collect(Collectors.toList());
        List<JsonStore> jsonStores = JSONArray.parseArray(readJson(storeFileUrl), JsonStore.class);
        //过滤已存在的品牌
        List<JsonStore> filterStore = jsonStores.stream()
                .filter(item -> !filterList.contains(item.getBrandCloudId()) &&
                        //品牌不为null和空
                        null != item.getBrandCloudId() && StringUtils.isNotBlank(item.getBrandCloudId())).collect(Collectors.toList());
        //打印
        Map<StoreExtend, Store> storeExtendStoreMap = new HashMap<>();
        //获取所有品牌
        List<Row> brandRowList = DbChain.table(BRAND).select(BRAND.ID, BRAND_EXTEND.BRAND_CLOUD_ID)
                .from(BRAND)
                .leftJoin(BRAND_EXTEND).on(BRAND_EXTEND.BRAND_ID.eq(BRAND.ID)).list();
        Map<String, Row> brandCloudIdMap = brandRowList.stream().collect(Collectors.toMap(item -> item.getString("brandCloudId"), item -> item));
        filterStore.forEach(item -> {
            //只插入能找到品牌的校区
            if (null != brandCloudIdMap.get(item.brandCloudId)){
                StoreExtend storeExtend = new StoreExtend();
                storeExtend.setStoreCloudId(item.getStoreCloudId());
                Store store = new Store();
                store.setSourceType(SourceType.JAMBOX.toString())
                        .setStoreName(item.getName())
                        .setBrandId(brandCloudIdMap.get(item.brandCloudId).getString("id"));
                storeExtendStoreMap.put(storeExtend,store);
            }
        });
        //插入校区
        SpringUtils.getBean(StoreService.class).saveBatch(storeExtendStoreMap.values());
        storeExtendStoreMap.forEach((key, value) -> {
            key.setStoreId(value.getId());
        });
        //插入拓展
        SpringUtils.getBean(StoreExtendService.class).saveBatch(storeExtendStoreMap.keySet());
    }

    /**
     * 更新导入的旧数据
     * @author DB
     * @since 2023/12/08
     */
    @Test
    public void updateImportData() {
        List<Row> rowList = null;
        try {
            //查询旧表
            DataSourceKey.use("jambox");
            rowList = DbChain.table("t_mechanism_info")
                    .select("tmi.store_id as storeCloudId")
                    .select("tmi.charge_name as personCharge")
                    .select("tmi.charge_phone as phone")
                    .select("tmi.address as storeAddr")
                    .select("tmi.test as isTest")
                    .select("ifnull(date_format(tsm.end_time,'%Y-%m-%d'),null) as expireDate")
                    .select("if(tmi.clue_id is null,0,1) as haveCounselor")
                    .select("if(of.activation is null,0,of.activation) as haveActive")
                    .from("t_mechanism_info").as("tmi")
                    .leftJoin("OAM_finance").as("of").on("of.clue_id LIKE tmi.clue_id")
                    .leftJoin("t_signContract_mechanism").as("tsm").on("tsm.store_id = tmi.store_id")
                    .list();
        } finally {
            DataSourceKey.clear();
        }
        //以云id换id
        Map<String, String> cloudToIdMap = SpringUtils.getBean(StoreExtendService.class).list()
                .stream().collect(Collectors.toMap(StoreExtend::getStoreCloudId, StoreExtend::getStoreId));
        //用迭代器
        Iterator<Row> iterator = rowList.iterator();
        while (iterator.hasNext()) {
            Row next = iterator.next();
            if (StringUtils.isBlank(cloudToIdMap.get(next.getString("storeCloudId")))) {
                iterator.remove();
            } else {
                next.set("id", cloudToIdMap.get(next.getString("storeCloudId")));
                if (StringUtils.isBlank(next.getString("id"))){
                    iterator.remove();
                }
            }
        }
        List<Store> entityList = RowUtil.toEntityList(rowList, Store.class);
        entityList.forEach(item->{
            if (StringUtils.isBlank(item.getId())){
                log.error(item.toString());
            }
        });
        SpringUtils.getBean(StoreService.class).updateBatch(entityList);
    }

    /**
     * 导入品牌管理员
     * @author DB
     * @since 2023/12/08
     */
    @Test
    public void importBrandManage() throws IOException {
        String brandManagerFileUrl = "C:\\Users\\Administrator\\Desktop\\brandManager.json";
        List<JsonBrandManager> jsonBrands = JSONArray.parseArray(readJson(brandManagerFileUrl), JsonBrandManager.class);
        List<BrandManager> brandManagers = BeanUtils.mapToList(jsonBrands, BrandManager.class);
        SpringUtils.getBean(BrandManagerService.class).saveBatch(brandManagers);
        Map<String, String> brandManagerMap = brandManagers.stream().collect(Collectors.toMap(BrandManager::getBrandManagerCloudId, BrandManager::getId));
        //中间表数据
        Map<String, String> brandMap = SpringUtils.getBean(BrandExtendService.class).list()
                .stream().collect(Collectors.toMap(BrandExtend::getBrandCloudId, BrandExtend::getBrandId));
        Map<String, String> storMap = SpringUtils.getBean(StoreExtendService.class).list()
                .stream().collect(Collectors.toMap(StoreExtend::getStoreCloudId, StoreExtend::getStoreId));
        List<BrandManagerStore> brandManagerStores = new ArrayList<>();
        jsonBrands.forEach(item->{
            if (item.getStoreCloudIds() != null && !item.getStoreCloudIds().isEmpty()) {
                item.getStoreCloudIds().forEach(inner -> {
                    BrandManagerStore brandManagerStore = new BrandManagerStore();
                    brandManagerStore.setBrandId(brandMap.get(item.getBrandCloudId()))
                            .setBrandManagerId(brandManagerMap.get(item.getBrandManagerCloudId()))
                            .setStoreId(storMap.get(inner));
                    brandManagerStores.add(brandManagerStore);
                });
            } else {
                if (StringUtils.isNotBlank(item.getBrandCloudId())) {
                    BrandManagerStore brandManagerStore = new BrandManagerStore();
                    brandManagerStore.setBrandId(brandMap.get(item.getBrandCloudId())).setBrandManagerId(brandManagerMap.get(item.getBrandManagerCloudId()));
                    brandManagerStores.add(brandManagerStore);
                }
            }
        });
        SpringUtils.getBean(BrandManagerStoreService.class).saveBatch(brandManagerStores);
    }

    /**
     * 批量修改校区是否是测试校区
     * @author DB
     * @since 2023/12/12
     */
    @Test
    public void batchUpdateStoreIsTest() {
        List<String> storeCloudIds;
        try {
            DataSourceKey.use("jambox");
            List<Row> rowList = DbChain.table("t_mechanism_info").select("store_id").where("test = 0").and("deleted = 1").list();
            storeCloudIds = RowUtil.toEntityList(rowList, String.class);
            //批量修改
        } finally {
            DataSourceKey.clear();
        }
        if (storeCloudIds != null) {
            Set<String> storeSet = SpringUtils.getBean(StoreExtendService.class).queryChain().where(STORE_EXTEND.STORE_CLOUD_ID.in(storeCloudIds)).list()
                    .stream().map(StoreExtend::getId).collect(Collectors.toSet());
            SpringUtils.getBean(StoreService.class).updateChain().set(STORE.IS_TEST,false).where(STORE.ID.in(storeSet)).update();
        }
    }

    /**
     * 批量导入校区营业执照
     * @author DB
     * @since 2023/12/12
     */
    @Test
    public void batchImportStoreLicense() {
        List<Row> rowList;
        try {
            DataSourceKey.use("jambox");
            rowList = DbChain.table("t_mechanism_info").select("store_id as storeCloudId", "reg_addr as licenseAddr", "str_to_date(reg_date,'%Y年%m月%d') as licenseDate", "reg_Name as licenseName",
                            "ness_license_id as licenseCode", "corp_name as licenseUserName", "reg_pic as licensePicUrl")
                    .where("reg_addr is not null")
                    .list();
        } finally {
            DataSourceKey.clear();
        }
        //获取校区信息
        Map<String, String> storeMap = SpringUtils.getBean(StoreExtendService.class).queryChain()
                .where(STORE_EXTEND.STORE_CLOUD_ID.in(rowList.stream().map(item -> item.getString("storeCloudId")).collect(Collectors.toSet())))
                .list()
                .stream().collect(Collectors.toMap(StoreExtend::getStoreCloudId, StoreExtend::getStoreId));
        rowList.forEach(item -> {
            item.set("storeId", storeMap.get(item.getString("storeCloudId")));
            System.out.println(item.toString());
        });
        List<StoreLicense> licenseList = RowUtil.toEntityList(rowList, StoreLicense.class).stream().filter(item->item.getStoreId() != null).collect(Collectors.toList());
        SpringUtils.getBean(StoreLicenseService.class).saveBatch(licenseList);
    }

    /**
     * 批量修改校区创建时间
     * @author DB
     * @since 2023/12/13
     */
    @Test
    public void batchUpdateStoreCreateTime() {
        List<Row> rowList;
        try {
            DataSourceKey.use("jambox");
            rowList = DbChain.table("t_mechanism_info").select("store_id as storeCloudId", "creation_time as createTime")
                    .and("deleted = 1")
                    .list();
        } finally {
            DataSourceKey.clear();
        }
        //获取所有校区数据
        StoreExtendService storeExtendService = SpringUtils.getBean(StoreExtendService.class);
        Map<String, String> storeExtendMap = storeExtendService.list().stream().collect(Collectors.toMap(StoreExtend::getStoreCloudId, StoreExtend::getStoreId));
        rowList.forEach(item -> {
            item.set("id", storeExtendMap.get(item.getString("storeCloudId")));
        });
        List<Store> storeList = RowUtil.toEntityList(rowList, Store.class).stream().filter(item -> item.getId() != null).collect(Collectors.toList());
        SpringUtils.getBean(StoreService.class).updateBatch(storeList);
    }

    /**
     *  导入校区签约数据
     * @author DB
     * @since 2023/12/13
     */
    @Test
    public void importStoreSignData() {
        List<Row> rowList;
        try {
            DataSourceKey.use("jambox");
            rowList = DbChain.table("t_mechanism_info")
                    .select("tmi.store_id as storeCloudId","tmi.clue_id")
                    .select("date_format(tsm.end_time,'%Y-%m-%d') as expireDate")
                    .from("t_mechanism_info").as("tmi")
                    .leftJoin("t_signContract_mechanism").as("tsm").on("tsm.store_id = tmi.store_id")
                    .and("tmi.deleted = 1")
                    .list();
        } finally {
            DataSourceKey.clear();
        }
        //获取校区
        Map<String, String> storeMap = SpringUtils.getBean(StoreExtendService.class).list().stream().collect(Collectors.toMap(StoreExtend::getStoreCloudId, StoreExtend::getStoreId));
        rowList.forEach(item->{
            item.set("storeId",storeMap.get(item.getString("storeCloudId")));
        });
        List<StoreSign> entityList = RowUtil.toEntityList(rowList, StoreSign.class);
        Set<StoreSign> filterStoreList = entityList.stream().filter(item -> item.getStoreId() != null && item.getExpireDate() != null).collect(Collectors.toSet());
        SpringUtils.getBean(StoreSignService.class).saveBatch(filterStoreList);
    }

    /**
     * 导入校区续费
     * @author DB
     * @since 2023/12/14
     */
    @Test
    public void importStoreRenew() {
        List<Row> rowList;
        try {
            DataSourceKey.use("jambox");
            rowList = DbChain.table("OAM_clue_renew")
                    .select("tmi.store_id as storeCloudId")
                    .select("ocr.remarks as renewDesc","ocr.price as renewAmount","date_format(ocr.creation_time,'%Y-%m-%d') as renewDate")
                    .from("OAM_clue_renew").as("ocr")
                    .leftJoin("t_mechanism_info").as("tmi").on("tmi.mechanism_id = ocr.mechanism_id")
                    .list();
        } finally {
            DataSourceKey.clear();
        }

        //获取校区
        Map<String, String> storeMap = SpringUtils.getBean(StoreExtendService.class).list().stream().collect(Collectors.toMap(StoreExtend::getStoreCloudId, StoreExtend::getStoreId));
        rowList.forEach(item->{
            item.set("storeId",storeMap.get(item.getString("storeCloudId")));
        });
        List<StoreRenew> entityList = RowUtil.toEntityList(rowList, StoreRenew.class);
        Set<StoreRenew> filterStoreList = entityList.stream().filter(item -> item.getStoreId() != null ).collect(Collectors.toSet());
        SpringUtils.getBean(StoreRenewService.class).saveBatch(filterStoreList);

        /*getTestDynamicDataSource();
        try {
            DataSourceKey.use("dev");
            //获取校区
            Map<String, String> storeMap = SpringUtils.getBean(StoreExtendService.class).list().stream().collect(Collectors.toMap(StoreExtend::getStoreCloudId, StoreExtend::getStoreId));
            rowList.forEach(item->{
                item.set("storeId",storeMap.get(item.getString("storeCloudId")));
            });
            List<StoreRenew> entityList = RowUtil.toEntityList(rowList, StoreRenew.class);
            Set<StoreRenew> filterStoreList = entityList.stream().filter(item -> item.getStoreId() != null ).collect(Collectors.toSet());
            SpringUtils.getBean(StoreRenewService.class).saveBatch(filterStoreList);
        } finally {
            DataSourceKey.clear();
        }*/
    }

    /**
     * 导入旧数币订单数据
     * @author DB
     * @since 2023/12/14
     */
    @Test
    public void importOldSudaOrderData() {
        List<Row> rowList;
        try {
            //数币订单
            DataSourceKey.use("jambox");
            rowList = DbChain.table("j_commodity_ticket")
                    .select("tbi.brand_id as brandCloudId","jct.store_id as storeCloudId","jct.wx_order_id as outOrderNo")
                    .select("jct.name as customerName","jct.phone as customerPhone")
                    .select("jct.price as totalAmount","jct.price as totalPayAmount")
                    .from("j_commodity_ticket").as("jct")
                    .leftJoin("t_brand_info").as("tbi").on("tbi.id = jct.brand_id")
                    .where("jct.deleted = 1")
                    .and("jct.pay_status = 'PAY'")
                    .and("jct.is_suda = 1")
                    .list();
        } finally {
            DataSourceKey.clear();
        }
        //获取校区
        Map<String, String> storeMap = SpringUtils.getBean(StoreExtendService.class).list().stream().collect(Collectors.toMap(StoreExtend::getStoreCloudId, StoreExtend::getStoreId));
        Map<String, String> brandMap = SpringUtils.getBean(BrandExtendService.class).list().stream().collect(Collectors.toMap(BrandExtend::getBrandCloudId, BrandExtend::getBrandId));
        rowList.forEach(item->{
            item.set("orderType",3);
            item.set("brandId",brandMap.get(item.getString("brandCloudId")));
            item.set("storeId",storeMap.get(item.getString("storeCloudId")));
        });

        List<EasyLearnOrder> entityList = RowUtil.toEntityList(rowList, EasyLearnOrder.class);
        SpringUtils.getBean(EasyLearnOrderService.class).saveBatch(entityList);

        /*getTestDynamicDataSource();
        try {
            DataSourceKey.use("dev");
            //获取校区
            Map<String, String> storeMap = SpringUtils.getBean(StoreExtendService.class).list().stream().collect(Collectors.toMap(StoreExtend::getStoreCloudId, StoreExtend::getStoreId));
            Map<String, String> brandMap = SpringUtils.getBean(BrandExtendService.class).list().stream().collect(Collectors.toMap(BrandExtend::getBrandCloudId, BrandExtend::getBrandId));
            rowList.forEach(item->{
                item.set("orderType",3);
                item.set("brandId",brandMap.get(item.getString("brandCloudId")));
                item.set("storeId",storeMap.get(item.getString("storeCloudId")));
            });
            DataSourceKey.use("dev");
            List<EasyLearnOrder> entityList = RowUtil.toEntityList(rowList, EasyLearnOrder.class);
            SpringUtils.getBean(EasyLearnOrderService.class).saveBatch(entityList);
        }finally {
            DataSourceKey.clear();
        }*/
    }

    /**
     * 导入旧微信支付数据
     * @author DB
     * @since 2023/12/14
     */
    @Test
    public void importOldWxPayOrderData() {
        List<Row> rowList;
        try {
            //数币订单
            DataSourceKey.use("jambox");
            rowList = DbChain.table("j_commodity_ticket")
                    .select("tbi.brand_id as brandCloudId","jct.store_id as storeCloudId","jct.wx_order_id as outOrderNo")
                    .select("jct.name as customerName","jct.phone as customerPhone")
                    .select("jct.price as totalAmount","jct.price as totalPayAmount")
                    .select("jwpp.is_month as isMonth","jwpp.class_hour as periodNumber","jwpp.sub_merch_id as subMchId")
                    .from("j_commodity_ticket").as("jct")
                    .leftJoin("t_brand_info").as("tbi").on("tbi.id = jct.brand_id")
                    .leftJoin("j_wechat_pay_plan").as("jwpp").on("jwpp.template_id = jct.commodity_id")
                    .where("jct.deleted = 1")
                    .and("jct.pay_status = 'PAY'")
                    .and("jct.ticket_type = 1")
                    .list();
        } finally {
            DataSourceKey.clear();
        }
        System.out.println(JSON.toJSONString(rowList));
        /*getTestDynamicDataSource();
        try {
            //获取校区
            Map<String, String> storeMap = SpringUtils.getBean(StoreExtendService.class).list().stream().collect(Collectors.toMap(StoreExtend::getStoreCloudId, StoreExtend::getStoreId));
            Map<String, String> brandMap = SpringUtils.getBean(BrandExtendService.class).list().stream().collect(Collectors.toMap(BrandExtend::getBrandCloudId, BrandExtend::getBrandId));
            rowList.forEach(item->{
                item.set("orderType",3);
                item.set("brandId",brandMap.get(item.getString("brandCloudId")));
                item.set("storeId",storeMap.get(item.getString("storeCloudId")));
            });
            DataSourceKey.use("dev");
            List<EasyLearnOrdre> entityList = RowUtil.toEntityList(rowList, EasyLearnOrdre.class);
            SpringUtils.getBean(EasyLearnOrdreService.class).saveBatch(entityList);
        }finally {
            DataSourceKey.clear();
        }*/
    }

    /**
     * 导入校区插件
     * @author DB
     * @since 2023/12/29
     */
    @Test
    public void importStorePlug(){
        List<Row> rowList;
        try {
            //数币订单
            DataSourceKey.use("jambox");
            rowList = DbChain.table("t_application_mechanism")
                    .select("store_id as storeCloudId", "char_tag as plugTag", "remarks","creation_time as createTime","deleted as isOpen")
                    .list();
        } finally {
            DataSourceKey.clear();
        }
        //获取所有校区
        Map<String, String> storeCloudToStore = SpringUtils.getBean(StoreExtendService.class).list().stream().collect(Collectors.toMap(StoreExtend::getStoreCloudId, StoreExtend::getStoreId));
        rowList.forEach(item -> {
            item.put("storeId", storeCloudToStore.get(item.getString("storeCloudId")));
            item.set("isOpen", !item.getBoolean("isOpen"));
        });
        List<Row> filterRow = rowList.stream().filter(item -> item.getString("storeId") != null).collect(Collectors.toList());
        List<StorePlug> entityList = RowUtil.toEntityList(filterRow, StorePlug.class);
        SpringUtils.getBean(StorePlugService.class).saveBatch(entityList);
    }

    private static final String URL = "jdbc:mysql://localhost:3306/cpop_dev?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8";
    /**
     * 数据库用户名
     */
    private static final String USERNAME = "root";
    /**
     * 数据库密码
     */
    private static final String PASSWORD = "root";

    private void getTestDynamicDataSource() {
        FlexDataSource flexDataSource = FlexGlobalConfig.getDefaultConfig().getDataSource();
        //新的数据源
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setJdbcUrl(URL);
        dataSource.setUsername(USERNAME);
        dataSource.setPassword(PASSWORD);
        flexDataSource.addDataSource("dev", dataSource);
    }

    @Data
    private class JsonBrandManager implements Serializable {

        /**
         * 云品牌id
         */
        @JSONField(name = "_brandid")
        private String brandCloudId;

        /**
         * 云校区id集合
         */
        @JSONField(name = "store")
        private List<String> storeCloudIds;

        /**
         * 手机号
         */
        private String phone;

        /**
         * 姓名
         */
        private String name;

        /**
         * 云id
         */
        @JSONField(name = "_id")
        private String brandManagerCloudId;

    }

    /**
     * json格式品牌
     */
    @Data
    private class JsonBrand implements Serializable{

        /**
         * 品牌名
         */
        @JSONField(name = "name")
        private String brandName;

        /**
         * 云品牌id
         */
        @JSONField(name = "_id")
        private String brandCloudId;

    }

    /**
     * json格式店铺/校区
     */
    @Data
    private class JsonStore implements Serializable {

        /**
         * 校区名
         */
        @JSONField(name = "name")
        private String name;

        /**
         * 云校区id
         */
        @JSONField(name = "_id")
        private String storeCloudId;

        /**
         * 云品牌id
         */
        @JSONField(name = "_brandid")
        private String brandCloudId;
    }

    @Data
    private class UpdateStore implements Serializable {

        /**
         * 云校区id
         */
        private String storeCloudId;

        /**
         * 店铺/校区地址
         */
        private String storeAddr;

        /**
         * 负责人
         */
        private String personCharge;

        /**
         * 手机号
         */
        private String phone;

        /**
         * 到期日期
         */
        private LocalDate expireDate;

        /**
         * 是否有顾问
         */
        private Boolean haveCounselor;

        /**
         * 是否激活
         */
        private Boolean haveActive;
    }

    /**
     * 读取json文件数据
     *
     * @param jsonPath json文件路径
     * @return 字符串
     */
    public String readJson(String jsonPath) throws IOException {
        File jsonFile = new File(jsonPath);
        FileReader fileReader = new FileReader(jsonFile);
        BufferedReader reader = new BufferedReader(fileReader);
        StringBuilder sb = new StringBuilder();
        while (true) {
            int ch = reader.read();
            if (ch != -1) {
                sb.append((char) ch);
            } else {
                break;
            }
        }
        fileReader.close();
        reader.close();
        return sb.toString();
    }

    /**
     * 导入旧员工数据
     * @author DB
     * @since 2024/01/02
     */
    @Test
    public void importOldStaffData(){
        //获取已有的员工数据
        List<Row> rows = Db.selectListByQuery(QueryWrapper.create()
                .select(STAFF.ID)
                .select(SYS_USER.PHONE_NUMBER)
                .from(STAFF)
                .leftJoin(SYS_USER).on(SYS_USER.ID.eq(STAFF.USER_ID)));
        System.out.println(rows);
        List<Row> rowList;
        try {
            DataSourceKey.use("jambox");
            rowList = DbChain.table("OAM_staff")
                    .select("qy_id as wxCpUserId", "name", "phone as phoneNumber", "type", "avatar_url as avatar", "plaintext as password")
                    .from("OAM_staff")
                    .where("deleted = 1")
                    .notIn("phone", rows.stream().map(item -> item.getString("phoneNumber")).collect(Collectors.toList()))
                    .list();
        } finally {
            DataSourceKey.clear();
        }
        StaffService staffService = SpringUtils.getBean(StaffService.class);
        RsaUtils rsaUtils = SpringUtils.getBean(RsaUtils.class);
        rowList.forEach(item->{
            String encrypt = rsaUtils.encrypt(item.getString("password"));
            // 创建员工
            StaffBo staffBo = new StaffBo();
            // 读取用户通用信息
            staffBo.setUserName(item.getString("phoneNumber"))
                    .setName(item.getString("name"))
                    .setPhoneNumber(item.getString("phoneNumber"))
                    .setSex(true)
                    .setStatus(true)
                    .setStaffType(1)
                    .setWxCpUserId(item.getString("wxCpUserId"))
                    .setPassword(encrypt)
                    // 设置部门
                    .setDeptId("82682906627141632")
                    // 区分技术员工和运营员工
                    .setRoleId(InitRoleEnum.WX_CP_INIT_OPERATION_ROLE.getId());
            // 保存员工
            staffService.insertStaff(staffBo);
        });
    }

    /**
     * 导入旧值班数据
     * @author DB
     * @since 2024/01/02
     */
    @Test
    public void importOldDutyData() {
        List<Row> rowList;
        //获取旧员工数据
        Map<String, String> oldStaffPhone;
        try {
            DataSourceKey.use("jambox");
            rowList = DbChain.table("OAM_staff")
                    .select("after_sales", "iteration", "duty_time")
                    .from("OAM_duty")
                    .where("deleted = 1")
                    .list();
            //获取旧员工数据
            oldStaffPhone = DbChain.table("OAM_staff")
                    .select("staff_id", "phone")
                    .from("OAM_staff")
                    .where("deleted = 1")
                    .list().stream().collect(Collectors.toMap(item -> item.getString("staffId"), item -> item.getString("phone")));
        } finally {
            DataSourceKey.clear();
        }
        //获取现有所有员工
        Map<String, StaffInfoVo> staffMap = SpringUtils.getBean(StaffService.class).listAs(QueryWrapper.create()
                        .select(STAFF.ID, STAFF.NAME)
                        .select(SYS_USER.PHONE_NUMBER.as(StaffInfoVo::getPhoneNumber))
                        .from(STAFF)
                        .leftJoin(SYS_USER).on(SYS_USER.ID.eq(STAFF.USER_ID)),
                StaffInfoVo.class).stream().collect(Collectors.toMap(StaffInfoVo::getPhoneNumber, item -> item));
        rowList.forEach(item -> {
            StaffInfoVo afterSales = staffMap.get(oldStaffPhone.get(item.getString("afterSales")));
            StaffInfoVo iteration = staffMap.get(oldStaffPhone.get(item.getString("iteration")));
            if (afterSales != null && iteration != null){
                item.put("serviceStaffId",afterSales.getId());
                item.put("technologyStaffId",iteration.getId());
            } else {
                item.put("serviceStaffId","97819551034630144");
                item.put("technologyStaffId","97819551034630144");
            }
            //时间戳转日期
            item.put("dutyDate", item.getLocalDateTime("dutyTime").toLocalDate());
        });
        List<Duty> entityList = RowUtil.toEntityList(rowList, Duty.class);
        SpringUtils.getBean(DutyService.class).saveBatch(entityList.stream().filter(item->item.getServiceStaffId()!=null).collect(Collectors.toList()));
    }

    /**
     * 导入旧数据记录
     * @author DB
     * @since 2024/01/02
     */
    @Test
    public void importOldDataImport(){
        List<Row> rowList;
        try {
            DataSourceKey.use("jambox");
            rowList = DbChain.table("OAM_upload")
                    .select("ou.file_url", "ou.empty_tag as isClear", "ou.status as importStatus", "ou.remarks", "tmi.store_id as storeCloudId","ou.creation_time as createTime")
                    .from("OAM_upload").as("ou")
                    .where("ou.deleted = 1")
                    .leftJoin("t_mechanism_info").as("tmi").on("tmi.mechanism_id = ou.mechanism_id")
                    .list();
        } finally {
            DataSourceKey.clear();
        }
        //查询校区
        Map<String, String> cloudToStore = SpringUtils.getBean(StoreExtendService.class).list().stream().collect(Collectors.toMap(StoreExtend::getStoreCloudId, StoreExtend::getStoreId));
        Map<String, Store> storeMap = SpringUtils.getBean(StoreService.class).queryChain().list().stream().collect(Collectors.toMap(Store::getId, item -> item));
        rowList.forEach(item -> {
            String storeId = cloudToStore.get(item.getString("storeCloudId"));
            if (StringUtils.isNotBlank(storeId)) {
                item.put("storeId", storeId);
                item.put("brandId", storeMap.get(storeId).getBrandId());
                item.put("sourceType", SourceType.JAMBOX.toString());
            }
        });
        List<Row> filterList = rowList.stream().filter(item -> StringUtils.isNotBlank(item.getString("storeId"))).collect(Collectors.toList());
        List<DataImport> entityList = RowUtil.toEntityList(filterList, DataImport.class);
        SpringUtils.getBean(DataImportService.class).saveBatch(entityList);
    }

    /**
     * 财务报销旧数据导入
     * @author DB
     * @since 2024/01/03
     */
    @Test
    public void financeReimburseOldDataImport(){
        //报销类型
        //List<Row> typeRowList;
        List<Row> recordRowList;
        List<Row> stageRowList;
        try {
            DataSourceKey.use("jambox");

            //报销记录
            recordRowList = DbChain.table("OAM_finance_reimbursement")
                    .select("ofr.id as updateUserId", "os.phone", "ofr.status", "ofr.type_id", "ofr.report_date", "ofr.remarks", "ofr.price", "ofr.annex as attachmentUrl")
                    .from("OAM_finance_reimbursement").as("ofr")
                    .leftJoin("OAM_staff").as("os").on("os.staff_id = ofr.staff_id")
                    .where("ofr.deleted = 1")
                    .list();
            //报销阶段
            stageRowList = DbChain.table("OAM_finance_reimbursement_stage")
                    .select("finance_reimbursement_id", "stage_amount", "payment_time")
                    .from("OAM_finance_reimbursement_stage")
                    .where("deleted = 1")
                    .list();
        } finally {
            DataSourceKey.clear();
        }
        //获取现有所有员工
        Map<String, StaffInfoVo> staffMap = SpringUtils.getBean(StaffService.class).listAs(QueryWrapper.create()
                        .select(STAFF.ID, STAFF.NAME, STAFF.USER_ID)
                        .select(SYS_USER.PHONE_NUMBER.as(StaffInfoVo::getPhoneNumber))
                        .from(STAFF)
                        .leftJoin(SYS_USER).on(SYS_USER.ID.eq(STAFF.USER_ID)),
                StaffInfoVo.class).stream().collect(Collectors.toMap(StaffInfoVo::getPhoneNumber, item -> item));
        List<Row> filterRecordRowList = recordRowList.stream()
                .filter(item -> StringUtils.isNotBlank(item.getString("phone")) && staffMap.get(item.getString("phone")) != null)
                .collect(Collectors.toList());
        filterRecordRowList.forEach(item->{
            //文件上传地址格式调整
            if (StringUtils.isNotBlank(item.getString("attachmentUrl"))) {
                Map<String, String> map = new HashMap<>();
                map.put("name", "旧数据无名字");
                map.put("url", item.getString("attachmentUrl"));
                List<Map<String, String>> maps = new ArrayList<>();
                maps.add(map);
                item.set("attachmentUrl", JSONArray.toJSONString(maps));
            }
            StaffInfoVo staffInfoVo = staffMap.get(item.getString("phone"));
            item.set("staffId", staffInfoVo.getId());
        });
        List<FinanceReimburse> financeReimburses = RowUtil.toEntityList(filterRecordRowList, FinanceReimburse.class);
        SpringUtils.getBean(FinanceReimburseService.class).saveBatch(financeReimburses);
        //获取所有员工
        Map<String, FinanceReimburse> oldReimburseIdMap = SpringUtils.getBean(FinanceReimburseService.class).list().stream().collect(Collectors.toMap(BaseEntity::getUpdateUserId, item -> item));
        stageRowList.forEach(item -> {
            if (oldReimburseIdMap.get(item.getString("financeReimbursementId")) != null){
                item.set("financeReimburseId", oldReimburseIdMap.get(item.getString("financeReimbursementId")).getId());
            }
        });
        List<FinanceReimburseStage> financeReimburseStages = RowUtil.toEntityList(stageRowList, FinanceReimburseStage.class);
        SpringUtils.getBean(FinanceReimburseStageService.class).saveBatch(financeReimburseStages);
    }

    /**
     * 迭代旧数据
     * @author DB
     * @since 2024/01/03
     */
    @Test
    public void iterationOldDataImport(){
        List<Row> rowList;
        List<Row> staffRowList;
        try {
            DataSourceKey.use("jambox");
            rowList = DbChain.table("OAM_iteration")
                    .select("iteration_id as updateUserId", "system_name as subject", "staff_id", "enclosure as attachmentUrl", "content as taskContent",
                            "DATE_FORMAT(estimate_time,'%Y-%m-%d') as expectedCompletionDate", "status", "grade as taskRating", "remarks as remark")
                    .select("creation_time as createTime", "last_modification_date as updateTime", "creation_by")
                    .from("OAM_iteration")
                    .where("deleted = 1")
                    .list();
            staffRowList = DbChain.table("OAM_staff")
                    .select("staff_id", "phone")
                    .from("OAM_staff")
                    .where("deleted = 1")
                    .list();
        } finally {
            DataSourceKey.clear();
        }
        //获取旧员工id
        Map<String, String> oldStaffMap = staffRowList.stream().collect(Collectors.toMap(item -> item.getString("staffId"), item -> item.getString("phone")));
        //获取现有所有员工
        Map<String, StaffInfoVo> staffMap = SpringUtils.getBean(StaffService.class).listAs(QueryWrapper.create()
                        .select(STAFF.ID, STAFF.NAME, STAFF.USER_ID)
                        .select(SYS_USER.PHONE_NUMBER.as(StaffInfoVo::getPhoneNumber))
                        .from(STAFF)
                        .leftJoin(SYS_USER).on(SYS_USER.ID.eq(STAFF.USER_ID)),
                StaffInfoVo.class).stream().collect(Collectors.toMap(StaffInfoVo::getPhoneNumber, item -> item));
        rowList.forEach(item -> {
            //所有任务预设为迭代任务
            item.put("taskType", 0);
            //负责员工不为空
            if (StringUtils.isNotBlank(item.getString("staffId")) && oldStaffMap.get(item.getString("staffId")) != null) {
                if (staffMap.get(oldStaffMap.get(item.getString("staffId"))) != null) {
                    item.put("responsibleStaffId", staffMap.get(oldStaffMap.get(item.getString("staffId"))).getId());
                } else {
                    item.put("responsibleStaffId", "97819551034630144");
                }
            } else {
                item.put("responsibleStaffId", "97819551034630144");
            }
            //记录员工为创建人
            if (StringUtils.isNotBlank(item.getString("creationBy")) && oldStaffMap.get(item.getString("creationBy")) != null) {
                String createStaffPhone = oldStaffMap.get(item.getString("creationBy"));
                if (staffMap.get(createStaffPhone) != null) {
                    item.put("recordStaffId", staffMap.get(createStaffPhone).getId());
                } else {
                    item.put("recordStaffId", "97819551034630144");
                }
            } else {
                item.put("recordStaffId", "97819551034630144");
            }
            if (StringUtils.isNotBlank(item.getString("creationBy")) && oldStaffMap.get(item.getString("creationBy")) != null) {
                String createStaffPhone = oldStaffMap.get(item.getString("creationBy"));
                if (staffMap.get(createStaffPhone) != null) {
                    item.put("createUserId", staffMap.get(createStaffPhone).getUserId());
                } else {
                    item.put("createUserId", "fb8bc1dcee4040b7b27feaf2789aeef1");
                }
            } else {
                item.put("createUserId", "fb8bc1dcee4040b7b27feaf2789aeef1");
            }
            //文件上传地址格式调整
            if (StringUtils.isNotBlank(item.getString("attachmentUrl"))) {
                Map<String, String> map = new HashMap<>();
                map.put("name", "旧数据无名字");
                map.put("url", item.getString("attachmentUrl"));
                List<Map<String, String>> maps = new ArrayList<>();
                maps.add(map);
                item.set("attachmentUrl", JSONArray.toJSONString(maps));
            }
            //获取任务状态
            getTaskStatus(item.getInt("status"), item);
            //评级
            if (StringUtils.isNotBlank(item.getString("taskRating"))){
                //获取任务权重
                getTaskWeight(item);
            }
        });
        List<Task> entityList = RowUtil.toEntityList(rowList, Task.class);
        SpringUtils.getBean(TaskService.class).saveBatch(entityList);
    }

    /**
     * 绩点
     * @author DB
     * @since 2024/01/03
     */
    @Test
    public void taskStaffOldDataImport() {
        List<Row> rowList;
        List<Row> staffRowList;
        try {
            DataSourceKey.use("jambox");
            rowList = DbChain.table("OAM_iteration_scale")
                    .select("iteration_id", "scale as gradePoint", "staff_id", "remarks as remark")
                    .select("creation_time as createTime", "last_modification_date as updateTime", "creation_by", "last_modified")
                    .from("OAM_iteration_scale")
                    .where("deleted = 1")
                    .list();
            staffRowList = DbChain.table("OAM_staff")
                    .select("staff_id", "phone")
                    .from("OAM_staff")
                    .where("deleted = 1")
                    .list();
        } finally {
            DataSourceKey.clear();
        }
        //获取旧员工id
        Map<String, String> oldStaffMap = staffRowList.stream().collect(Collectors.toMap(item -> item.getString("staffId"), item -> item.getString("phone")));
        //获取所有任务
        Map<String, String> taskMap = SpringUtils.getBean(TaskService.class).list().stream().collect(Collectors.toMap(BaseEntity::getUpdateUserId, Task::getId));
        //获取现有所有员工
        Map<String, StaffInfoVo> staffMap = SpringUtils.getBean(StaffService.class).listAs(QueryWrapper.create()
                        .select(STAFF.ID, STAFF.NAME, STAFF.USER_ID)
                        .select(SYS_USER.PHONE_NUMBER.as(StaffInfoVo::getPhoneNumber))
                        .from(STAFF)
                        .leftJoin(SYS_USER).on(SYS_USER.ID.eq(STAFF.USER_ID)),
                StaffInfoVo.class).stream().collect(Collectors.toMap(StaffInfoVo::getPhoneNumber, item -> item));
        rowList.forEach(item -> {
            //员工
            if (StringUtils.isNotBlank(item.getString("staffId")) && oldStaffMap.get(item.getString("staffId")) != null) {
                if (staffMap.get(oldStaffMap.get(item.getString("staffId"))) != null) {
                    item.set("staffId", staffMap.get(oldStaffMap.get(item.getString("staffId"))).getId());
                } else {
                    item.put("staffId", "97819551034630144");
                }
            } else {
                item.put("staffId", "97819551034630144");
            }
            item.put("taskId",taskMap.get(item.getString("iterationId")));
            if (StringUtils.isNotBlank(item.getString("creationBy")) && oldStaffMap.get(item.getString("creationBy")) != null) {
                String createStaffPhone = oldStaffMap.get(item.getString("creationBy"));
                if (staffMap.get(createStaffPhone) != null) {
                    item.put("createUserId", staffMap.get(createStaffPhone).getUserId());
                }else {
                    item.put("createUserId", "fb8bc1dcee4040b7b27feaf2789aeef1");
                }
            } else {
                item.put("createUserId", "fb8bc1dcee4040b7b27feaf2789aeef1");
            }
            if (StringUtils.isNotBlank(item.getString("creationBy")) && oldStaffMap.get(item.getString("creationBy")) != null) {
                String createStaffPhone = oldStaffMap.get(item.getString("creationBy"));
                if (staffMap.get(createStaffPhone) != null) {
                    item.put("createUserId", staffMap.get(createStaffPhone).getUserId());
                }else {
                    item.put("createUserId", "fb8bc1dcee4040b7b27feaf2789aeef1");
                }
            } else {
                item.put("createUserId", "fb8bc1dcee4040b7b27feaf2789aeef1");
            }
            if (StringUtils.isNotBlank(item.getString("lastModified")) && oldStaffMap.get(item.getString("lastModified")) != null) {
                String updateStaffPhone = oldStaffMap.get(item.getString("lastModified"));
                if (staffMap.get(updateStaffPhone) != null) {
                    item.put("updateUserId", staffMap.get(updateStaffPhone).getUserId());
                } else {
                    item.put("updateUserId", "fb8bc1dcee4040b7b27feaf2789aeef1");
                }
            } else {
                item.put("updateUserId", "fb8bc1dcee4040b7b27feaf2789aeef1");
            }
        });
        List<TaskStaffGroup> entityList = RowUtil.toEntityList(rowList, TaskStaffGroup.class);
        SpringUtils.getBean(TaskStaffGroupService.class).saveBatch(entityList);
    }

    /**
     * 获取任务状态
     * @author DB
     * @since 2024/1/4
     * @param status 旧状态值
     * @param row 插入
     */
    private void getTaskStatus(Integer status, Row row) {
        switch (status) {
            //待领取
            case 0:
                //有评级
                if (StringUtils.isNotBlank(row.getString("taskRating"))){
                    row.put("taskStatus",1);
                } else {
                    row.put("taskStatus",0);
                }
                row.put("taskItem",0);
                break;
            //进行中
            case 1:
            case 2:
                row.put("taskStatus",2);
                row.put("taskItem",1);
                row.put("taskProgress","0,1,2,3,4");
                row.put("taskReceiptTime",row.getLocalDateTime("createTime"));
                break;
            //带部署
            case 3:
                row.put("taskStatus", 8);
                row.put("taskItem", 2);
                row.put("taskProgress", "0,1,2,3,4");
                row.put("testToFinishUrl",row.getString("attachmentUrl"));
                row.put("toTestTime",row.getLocalDateTime("updateTime"));
                row.put("taskReceiptTime",row.getLocalDateTime("createTime"));
                row.put("completion_date",row.getLocalDateTime("updateTime").toLocalDate());
                break;
            //已归档
            default:
                row.put("taskStatus", 9);
                row.put("taskItem", 3);
                row.put("taskProgress", "0,1,2,3,4");
                row.put("testToFinishUrl",row.getString("attachmentUrl"));
                row.put("toDeployTime",row.getLocalDateTime("updateTime"));
                row.put("toTestTime",row.getLocalDateTime("updateTime"));
                row.put("taskReceiptTime",row.getLocalDateTime("createTime"));
                row.put("completion_date",row.getLocalDateTime("updateTime").toLocalDate());
                break;
        }
    }

    private Integer getTaskWeight(Row row){
        switch (row.getString("taskRating")){
            case "A":
                row.put("taskWeight",200);
                return 200;
            case "B":
                row.put("taskWeight",100);
                return 100;
            case "C":
                row.put("taskWeight",80);
                return 80;
            case "D":
                row.put("taskWeight",40);
                return 40;
            case "E":
                row.put("taskWeight",20);
                return 20;
            default:
                row.put("taskWeight",10);
                return 10;
        }
    }

    private LocalDate taskRatingToDate(String rating,LocalDate startDate){
        switch (rating){
            case "A":
                return startDate.plusDays(30);
            case "B":
                return startDate.plusDays(21);
            case "C":
                return startDate.plusDays(15);
            case "D":
                return startDate.plusDays(7);
            case "E":
                return startDate.plusDays(3);
            default:
                return startDate.plusDays(1);
        }
    }

    @Test
    public void demandOldDataImport(){
        List<Row> rowList;
        List<Row> staffRowList;
        try {
            DataSourceKey.use("jambox");
            //报销阶段
            rowList = DbChain.table("OAM_demand")
                    .select("tmi.store_id as storeCloudId", "od.iteration_id", "od.type", "od.content as taskContent", "od.file_url as attachmentUrl", "od.status",
                            "od.remarks", "od.grade as taskRating", "od.sign_pic", "od.sign_url as payQrCode", "od.payment as prepayment", "od.price as payable", "od.wx_order_id", "od.sign_agree")
                    .select("od.creation_time as createTime", "od.last_modification_date as updateTime", "od.creation_by", "od.last_modified")
                    .from("OAM_demand").as("od")
                    .leftJoin("t_mechanism_info").as("tmi").on("tmi.mechanism_id = od.mechanism_id")
                    .leftJoin("OAM_iteration").as("oi").on("oi.iteration_id = od.iteration_id")
                    .where("od.deleted = 1")
                    .list();
            staffRowList = DbChain.table("OAM_staff")
                    .select("staff_id", "phone")
                    .from("OAM_staff")
                    .where("deleted = 1")
                    .list();
        } finally {
            DataSourceKey.clear();
        }
        //获取旧员工id
        Map<String, String> oldStaffMap = staffRowList.stream().collect(Collectors.toMap(item -> item.getString("staffId"), item -> item.getString("phone")));
        //获取现有所有员工
        Map<String, StaffInfoVo> staffMap = SpringUtils.getBean(StaffService.class).listAs(QueryWrapper.create()
                        .select(STAFF.ID, STAFF.NAME,STAFF.USER_ID)
                        .select(SYS_USER.PHONE_NUMBER.as(StaffInfoVo::getPhoneNumber))
                        .from(STAFF)
                        .leftJoin(SYS_USER).on(SYS_USER.ID.eq(STAFF.USER_ID)),
                StaffInfoVo.class).stream().collect(Collectors.toMap(StaffInfoVo::getPhoneNumber, item -> item));
        //查询校区
        Map<String, String> cloudToStore = SpringUtils.getBean(StoreExtendService.class).list().stream().collect(Collectors.toMap(StoreExtend::getStoreCloudId, StoreExtend::getStoreId));
        Map<String, Store> storeMap = SpringUtils.getBean(StoreService.class).queryChain().list().stream().collect(Collectors.toMap(Store::getId, item -> item));
        TaskService taskService = SpringUtils.getBean(TaskService.class);
        //获取所有任务
        Map<String, Task> taskMap = taskService.list().stream().collect(Collectors.toMap(BaseEntity::getUpdateUserId, item->item));
        //是否有任务
        List<Row> notHaveTask = rowList.stream().filter(item -> StringUtils.isBlank(item.getString("iteration_id"))).collect(Collectors.toList());
        //更新
        notHaveTask.forEach(item -> {
            //是否有评级
            Task task = new Task();
            task.setCreateTime(item.getLocalDateTime("createTime"));
            task.setUpdateTime(item.getLocalDateTime("updateTime"));
            if (StringUtils.isNotBlank(item.getString("creationBy")) && oldStaffMap.get(item.getString("creationBy")) != null) {
                String createStaffPhone = oldStaffMap.get(item.getString("creationBy"));
                if (staffMap.get(createStaffPhone) != null) {
                    item.put("createUserId", staffMap.get(createStaffPhone).getUserId());
                    task.setCreateUserId(staffMap.get(createStaffPhone).getUserId());
                    task.setUpdateUserId(staffMap.get(createStaffPhone).getUserId());
                } else {
                    item.put("createUserId", "fb8bc1dcee4040b7b27feaf2789aeef1");
                    task.setCreateUserId("fb8bc1dcee4040b7b27feaf2789aeef1");
                    task.setUpdateUserId("fb8bc1dcee4040b7b27feaf2789aeef1");
                }
            } else {
                item.put("createUserId", "fb8bc1dcee4040b7b27feaf2789aeef1");
                task.setCreateUserId("fb8bc1dcee4040b7b27feaf2789aeef1");
                task.setUpdateUserId("fb8bc1dcee4040b7b27feaf2789aeef1");
            }
            if (StringUtils.isNotBlank(item.getString("lastModified")) && oldStaffMap.get(item.getString("lastModified")) != null) {
                String updateStaffPhone = oldStaffMap.get(item.getString("lastModified"));
                if (staffMap.get(updateStaffPhone) != null) {
                    item.put("updateUserId", staffMap.get(updateStaffPhone).getUserId());
                }else {
                    item.put("updateUserId", "fb8bc1dcee4040b7b27feaf2789aeef1");
                }
            } else {
                item.put("updateUserId", "fb8bc1dcee4040b7b27feaf2789aeef1");
            }
            //文件上传地址格式调整
            if (StringUtils.isNotBlank(item.getString("attachmentUrl"))) {
                Map<String, String> map = new HashMap<>();
                map.put("name", "旧数据无名字");
                map.put("url", item.getString("attachmentUrl"));
                List<Map<String, String>> maps = new ArrayList<>();
                maps.add(map);
                item.set("attachmentUrl", JSONArray.toJSONString(maps));
            }
            //记录员工为创建人
            if (StringUtils.isNotBlank(item.getString("creationBy")) && oldStaffMap.get(item.getString("creationBy")) != null) {
                String createStaffPhone = oldStaffMap.get(item.getString("creationBy"));
                if (staffMap.get(createStaffPhone) != null) {
                    task.setRecordStaffId(staffMap.get(createStaffPhone).getId());
                }else {
                    task.setRecordStaffId("97819551034630144");
                }
            } else {
                task.setRecordStaffId("97819551034630144");
            }
            if (StringUtils.isBlank(item.getString("taskRating"))) {
                // 默认待审核
                task.setTaskStatus(0).setTaskItem(0).setTaskType(1).setSubject("果酱盒子").setTaskContent(item.getString("taskContent"));
            } else if (item.getBoolean("status")) {
                //已完成
                task.setTaskStatus(9).setTaskItem(3).setTaskType(1).setSubject("果酱盒子").setTaskContent(item.getString("taskContent"));
                task.setTaskRating(item.getString("taskRating")).setTaskWeight(getTaskWeight(item));
                //通用
                task.setToDeployTime(item.getLocalDateTime("updateTime")).setToTestTime(item.getLocalDateTime("updateTime"))
                        .setTaskReceiptTime(item.getLocalDateTime("createTime")).setCompletionDate(item.getLocalDateTime("updateTime").toLocalDate())
                        .setAttachmentUrl(item.getString("attachmentUrl")).setTestToFinishUrl(item.getString("attachmentUrl"))
                        .setExpectedCompletionDate(taskRatingToDate(item.getString("taskRating"), item.getLocalDateTime("createTime").toLocalDate()));
            } else {
                //未完成
                task.setTaskStatus(1).setTaskItem(0).setTaskType(1).setSubject("果酱盒子").setTaskContent(item.getString("taskContent"));
                task.setTaskRating(item.getString("taskRating")).setTaskWeight(getTaskWeight(item)).setTaskReceiptTime(item.getLocalDateTime("createTime"))
                        .setAttachmentUrl(item.getString("attachmentUrl"));
            }
            taskService.save(task);
            item.set("taskId", task.getId());
            if (storeMap.get(cloudToStore.get(item.getString("storeCloudId"))) != null){
                item.set("storeId", storeMap.get(cloudToStore.get(item.getString("storeCloudId"))).getId());
                item.set("brandId", storeMap.get(cloudToStore.get(item.getString("storeCloudId"))).getBrandId());
            }
        });
        List<Task> updateTasks = new ArrayList<>();
        List<Row> haveTask = rowList.stream().filter(item -> StringUtils.isNotBlank(item.getString("iteration_id"))).collect(Collectors.toList());
        haveTask.forEach(item->{
            //是否有评级
            Task task = new Task();
            task.setCreateTime(item.getLocalDateTime("createTime"));
            task.setUpdateTime(item.getLocalDateTime("updateTime"));
            if (StringUtils.isNotBlank(item.getString("creationBy")) && oldStaffMap.get(item.getString("creationBy")) != null) {
                String createStaffPhone = oldStaffMap.get(item.getString("creationBy"));
                if (staffMap.get(createStaffPhone) != null) {
                    item.put("createUserId", staffMap.get(createStaffPhone).getUserId());
                    task.setCreateUserId(staffMap.get(createStaffPhone).getUserId());
                    task.setUpdateUserId(staffMap.get(createStaffPhone).getUserId());
                } else {
                    item.put("createUserId", "fb8bc1dcee4040b7b27feaf2789aeef1");
                    task.setCreateUserId("fb8bc1dcee4040b7b27feaf2789aeef1");
                    task.setUpdateUserId("fb8bc1dcee4040b7b27feaf2789aeef1");
                }
            } else {
                item.put("createUserId", "fb8bc1dcee4040b7b27feaf2789aeef1");
                task.setCreateUserId("fb8bc1dcee4040b7b27feaf2789aeef1");
                task.setUpdateUserId("fb8bc1dcee4040b7b27feaf2789aeef1");
            }
            if (StringUtils.isNotBlank(item.getString("lastModified")) && oldStaffMap.get(item.getString("lastModified")) != null) {
                String updateStaffPhone = oldStaffMap.get(item.getString("lastModified"));
                if (staffMap.get(updateStaffPhone) != null) {
                    item.put("updateUserId", staffMap.get(updateStaffPhone).getUserId());
                } else {
                    item.put("updateUserId", "fb8bc1dcee4040b7b27feaf2789aeef1");
                }
            } else {
                item.put("updateUserId", "fb8bc1dcee4040b7b27feaf2789aeef1");
            }
            //文件上传地址格式调整
            if (StringUtils.isNotBlank(item.getString("attachmentUrl"))) {
                Map<String, String> map = new HashMap<>();
                map.put("name", "旧数据无名字");
                map.put("url", item.getString("attachmentUrl"));
                List<Map<String, String>> maps = new ArrayList<>();
                maps.add(map);
                item.set("attachmentUrl", JSONArray.toJSONString(maps));
            }
            //记录员工为创建人
            if (StringUtils.isNotBlank(item.getString("creationBy")) && oldStaffMap.get(item.getString("creationBy")) != null) {
                String createStaffPhone = oldStaffMap.get(item.getString("creationBy"));
                if (staffMap.get(createStaffPhone) != null) {
                    task.setRecordStaffId(staffMap.get(createStaffPhone).getId());
                }else {
                    task.setRecordStaffId("97819551034630144");
                }
            } else {
                task.setRecordStaffId("97819551034630144");
            }
            if (taskMap.get(item.getString("iterationId")) != null) {
                //是否有评级
                if (StringUtils.isBlank(item.getString("taskRating"))) {
                    // 默认待审核
                    task.setId(taskMap.get(item.getString("iterationId")).getId()).setTaskStatus(0).setTaskItem(0).setTaskType(1).setSubject("果酱盒子").setTaskContent(item.getString("taskContent"));
                } else if (item.getBoolean("status")) {
                    //已完成
                    task.setId(taskMap.get(item.getString("iterationId")).getId()).setTaskStatus(9).setTaskItem(3).setTaskType(1)
                            .setSubject("果酱盒子").setTaskContent(item.getString("taskContent"));
                    task.setTaskRating(item.getString("taskRating")).setTaskWeight(getTaskWeight(item));
                    //通用
                    task.setToDeployTime(item.getLocalDateTime("updateTime")).setToTestTime(item.getLocalDateTime("updateTime"))
                            .setTaskReceiptTime(item.getLocalDateTime("createTime")).setCompletionDate(item.getLocalDateTime("updateTime").toLocalDate())
                            .setAttachmentUrl(item.getString("attachmentUrl")).setTestToFinishUrl(item.getString("attachmentUrl"))
                            .setExpectedCompletionDate(taskRatingToDate(item.getString("taskRating"), item.getLocalDateTime("createTime").toLocalDate()));
                } else {
                    //未完成
                    task.setId(taskMap.get(item.getString("iterationId")).getId()).setTaskStatus(1).setTaskItem(0).setTaskType(1).setSubject("果酱盒子").setTaskContent(item.getString("taskContent"));
                    task.setTaskRating(item.getString("taskRating")).setTaskWeight(getTaskWeight(item))
                            .setAttachmentUrl(item.getString("attachmentUrl"));
                }
                updateTasks.add(task);
            } else {
                //是否有评级
                if (StringUtils.isBlank(item.getString("taskRating"))) {
                    // 默认待审核
                    task.setTaskStatus(0).setTaskItem(0).setTaskType(1).setSubject("果酱盒子").setTaskContent(item.getString("taskContent"));
                } else if (item.getBoolean("status")) {
                    //已完成
                    task.setTaskStatus(9).setTaskItem(3).setTaskType(1).setSubject("果酱盒子").setTaskContent(item.getString("taskContent"));
                    task.setTaskRating(item.getString("taskRating")).setTaskWeight(getTaskWeight(item));
                    //通用
                    task.setToDeployTime(item.getLocalDateTime("updateTime")).setToTestTime(item.getLocalDateTime("updateTime"))
                            .setTaskReceiptTime(item.getLocalDateTime("createTime")).setCompletionDate(item.getLocalDateTime("updateTime").toLocalDate())
                            .setAttachmentUrl(item.getString("attachmentUrl")).setTestToFinishUrl(item.getString("attachmentUrl"))
                            .setExpectedCompletionDate(taskRatingToDate(item.getString("taskRating"), item.getLocalDateTime("createTime").toLocalDate()));
                } else {
                    //未完成
                    task.setTaskStatus(1).setTaskItem(0).setTaskType(1).setSubject("果酱盒子").setTaskContent(item.getString("taskContent"));
                    task.setTaskRating(item.getString("taskRating")).setTaskWeight(getTaskWeight(item)).setTaskReceiptTime(item.getLocalDateTime("createTime"))
                            .setAttachmentUrl(item.getString("attachmentUrl"));
                }
                taskService.save(task);
            } item.set("taskId", task.getId());
            if (storeMap.get(cloudToStore.get(item.getString("storeCloudId"))) != null){
                item.set("storeId", storeMap.get(cloudToStore.get(item.getString("storeCloudId"))).getId());
                item.set("brandId", storeMap.get(cloudToStore.get(item.getString("storeCloudId"))).getBrandId());
            }
        });
        taskService.updateBatch(updateTasks);
        SpringUtils.getBean(TaskDemandService.class).saveBatch(RowUtil.toEntityList(rowList, TaskDemand.class));
    }

    /**
     * 工单旧数据导入
     * @author DB
     * @since 2024/01/03
     */
    @Test
    public void workOrderOldDataImport(){
        List<Row> rowList;
        List<Row> staffRowList;
        try {
            DataSourceKey.use("jambox");
            rowList = DbChain.table("OAM_work")
                    .select("tmi.store_id as storeCloudId","ow.work_id as updateUserId", "ow.suspend_time", "ow.system_name", "ow.content as taskContent", "ow.phone as phoneNumber", "ow.file_url as attachmentUrl",
                            "ow.status", "ow.receive", "ow.receive_time", "ow.remarks as remark","TIMESTAMPDIFF(MINUTE, ow.creation_time, receive_time) as receivingTime",
                            "ow.last_modification_date as finishTime")
                    .select("ow.creation_time as createTime", "ow.last_modification_date as updateTime", "ow.creation_by", "ow.last_modified")
                    .from("OAM_work").as("ow")
                    .leftJoin("t_mechanism_info").as("tmi").on("tmi.mechanism_id = ow.mechanism_id")
                    .where("ow.deleted = 1")
                    .list();
            staffRowList = DbChain.table("OAM_staff")
                    .select("staff_id", "phone")
                    .from("OAM_staff")
                    .where("deleted = 1")
                    .list();
        } finally {
            DataSourceKey.clear();
        }
        //获取旧员工id
        Map<String, String> oldStaffMap = staffRowList.stream().collect(Collectors.toMap(item -> item.getString("staffId"), item -> item.getString("phone")));
        //查询校区
        Map<String, String> cloudToStore = SpringUtils.getBean(StoreExtendService.class).list().stream().collect(Collectors.toMap(StoreExtend::getStoreCloudId, StoreExtend::getStoreId));
        Map<String, Store> storeMap = SpringUtils.getBean(StoreService.class).queryChain().list().stream().collect(Collectors.toMap(Store::getId, item -> item));
        //获取现有所有员工
        Map<String, StaffInfoVo> staffMap = SpringUtils.getBean(StaffService.class).listAs(QueryWrapper.create()
                        .select(STAFF.ID, STAFF.NAME,STAFF.USER_ID)
                        .select(SYS_USER.PHONE_NUMBER.as(StaffInfoVo::getPhoneNumber))
                        .from(STAFF)
                        .leftJoin(SYS_USER).on(SYS_USER.ID.eq(STAFF.USER_ID)),
                StaffInfoVo.class).stream().collect(Collectors.toMap(StaffInfoVo::getPhoneNumber, item -> item));
        TaskService taskService = SpringUtils.getBean(TaskService.class);
        List<TaskWorkOrder> taskWorkOrders = new ArrayList<>();
        rowList.forEach(item->{
            Task task = new Task();
            //负责员工不为空
            if (StringUtils.isNotBlank(item.getString("receive")) && oldStaffMap.get(item.getString("receive")) != null) {
                if (staffMap.get(oldStaffMap.get(item.getString("receive"))) != null) {
                    task.setResponsibleStaffId(staffMap.get(oldStaffMap.get(item.getString("receive"))).getId());
                } else {
                    task.setResponsibleStaffId("97819551034630144");
                }
            } else {
                task.setResponsibleStaffId("97819551034630144");
            }
            //记录员工为创建人
            if (StringUtils.isNotBlank(item.getString("creationBy")) && oldStaffMap.get(item.getString("creationBy")) != null) {
                String createStaffPhone = oldStaffMap.get(item.getString("creationBy"));
                if (staffMap.get(createStaffPhone) != null) {
                    task.setRecordStaffId(staffMap.get(createStaffPhone).getId());
                    task.setCreateUserId(staffMap.get(createStaffPhone).getUserId());
                    task.setUpdateUserId(staffMap.get(createStaffPhone).getUserId());
                }else {
                    task.setRecordStaffId("97819551034630144");
                    task.setCreateUserId("fb8bc1dcee4040b7b27feaf2789aeef1");
                    task.setUpdateUserId("fb8bc1dcee4040b7b27feaf2789aeef1");
                }
            } else {
                task.setRecordStaffId("97819551034630144");
                task.setCreateUserId("fb8bc1dcee4040b7b27feaf2789aeef1");
                task.setUpdateUserId("fb8bc1dcee4040b7b27feaf2789aeef1");
            }
            task.setCreateTime(item.getLocalDateTime("createTime"));
            task.setUpdateTime(item.getLocalDateTime("updateTime"));
            task.setTaskItem(3).setTaskStatus(9).setTaskReceiptTime(item.getLocalDateTime("receiveTime"))
                    .setTaskType(2).setTaskRating("N").setTaskContent(item.getString("taskContent")).setCreateUserId("1");
            taskService.save(task);
            TaskWorkOrder workOrder = new TaskWorkOrder();
            String storeId = cloudToStore.get(item.getString("storeCloudId"));
            if (StringUtils.isNotBlank(storeId)) {
                workOrder.setStoreId(storeId).setBrandId(storeMap.get(storeId).getBrandId());
            }
            workOrder.setTaskId(task.getId()).setPhoneNumber(item.getString("phoneNumber")).setReceivingTime(item.getInt("receivingTime"))
                    .setFinishTime(item.getLocalDateTime("finishTime"));
            //记录员工为创建人
            if (StringUtils.isNotBlank(item.getString("creationBy")) && oldStaffMap.get(item.getString("creationBy")) != null) {
                String createStaffPhone = oldStaffMap.get(item.getString("creationBy"));
                if (staffMap.get(createStaffPhone) != null) {
                    workOrder.setCreateUserId(staffMap.get(createStaffPhone).getUserId());
                    workOrder.setFinishStaffId(staffMap.get(createStaffPhone).getId());
                }else {
                    workOrder.setCreateUserId("fb8bc1dcee4040b7b27feaf2789aeef1");
                    workOrder.setFinishStaffId("97819551034630144");
                }
            } else {
                workOrder.setCreateUserId("fb8bc1dcee4040b7b27feaf2789aeef1");
                workOrder.setFinishStaffId("97819551034630144");
            }
            workOrder.setCreateTime(item.getLocalDateTime("createTime"));
            workOrder.setUpdateTime(item.getLocalDateTime("updateTime"));
            workOrder.setUpdateUserId(item.getString("updateUserId"));
            taskWorkOrders.add(workOrder);
        });
        SpringUtils.getBean(TaskWorkOrderService.class).saveBatch(taskWorkOrders);
    }

    /**
     * 工单旧数据导入
     */
    @Test
    public void workOrderRecordOldDataImport() {
        List<Row> rowList;
        List<Row> staffRowList;
        try {
            DataSourceKey.use("jambox");
            rowList = DbChain.table("OAM_work_record")
                    .select("work_id", "content as recordText", "file_url as attachmentUrl")
                    .select("creation_time as createTime", "creation_time as updateTime", "creation_by")
                    .from("OAM_work_record")
                    .where("deleted = 1")
                    .list();
            staffRowList = DbChain.table("OAM_staff")
                    .select("staff_id", "phone")
                    .from("OAM_staff")
                    .where("deleted = 1")
                    .list();
        } finally {
            DataSourceKey.clear();
        }
        //获取旧员工id
        Map<String, String> oldStaffMap = staffRowList.stream().collect(Collectors.toMap(item -> item.getString("staffId"), item -> item.getString("phone")));
        //获取现有所有员工
        Map<String, StaffInfoVo> staffMap = SpringUtils.getBean(StaffService.class).listAs(QueryWrapper.create()
                        .select(STAFF.ID, STAFF.NAME,STAFF.USER_ID)
                        .select(SYS_USER.PHONE_NUMBER.as(StaffInfoVo::getPhoneNumber))
                        .from(STAFF)
                        .leftJoin(SYS_USER).on(SYS_USER.ID.eq(STAFF.USER_ID)),
                StaffInfoVo.class).stream().collect(Collectors.toMap(StaffInfoVo::getPhoneNumber, item -> item));
        //获取所有工单
        Map<String, String> workOrderMap = SpringUtils.getBean(TaskWorkOrderService.class).list().stream().filter(item->StringUtils.isNotBlank(item.getUpdateUserId()))
                .collect(Collectors.toMap(BaseEntity::getUpdateUserId, TaskWorkOrder::getId));
        rowList.forEach(item->{
            //记录员工为创建人
            if (StringUtils.isNotBlank(item.getString("creationBy")) && oldStaffMap.get(item.getString("creationBy")) != null) {
                String createStaffPhone = oldStaffMap.get(item.getString("creationBy"));
                if (staffMap.get(createStaffPhone) != null) {
                    item.put("recordStaffId", staffMap.get(createStaffPhone).getId());
                    item.put("createUserId", staffMap.get(createStaffPhone).getUserId());
                    item.put("updateUserid", staffMap.get(createStaffPhone).getUserId());
                }else {
                    item.put("recordStaffId", "97819551034630144");
                    item.put("createUserId", "fb8bc1dcee4040b7b27feaf2789aeef1");
                    item.put("updateUserid", "fb8bc1dcee4040b7b27feaf2789aeef1");
                }
            } else {
                item.put("recordStaffId", "97819551034630144");
                item.put("createUserId", "fb8bc1dcee4040b7b27feaf2789aeef1");
                item.put("updateUserid", "fb8bc1dcee4040b7b27feaf2789aeef1");
            }
            if (StringUtils.isNotBlank(item.getString("attachmentUrl"))){
                Map<String, String> map = new HashMap<>();
                map.put("name", "旧数据无名字");
                map.put("url", item.getString("attachmentUrl"));
                List<Map<String, String>> maps = new ArrayList<>();
                maps.add(map);
                item.set("attachmentUrl", JSONArray.toJSONString(maps));
            }
            item.set("taskWorkOrderId",workOrderMap.get(item.getString("workId")));
        });
        SpringUtils.getBean(TaskWorkOrderRecordService.class).saveBatch(RowUtil.toEntityList(rowList,TaskWorkOrderRecord.class));
    }

    /**
     * 导入插件数据
     * @author DB
     * @since 2024/1/5
     */
    @Test
    public void importPlugData(){
        List<Row> rowList;
        try {
            DataSourceKey.use("jambox");
            rowList = DbChain.table("t_application_info")
                    .select("application as name", "char_tag as plugTag", "pic as picUrl")
                    .select("top as orderNo", "tag as remark", "if(status = '即将上线',0,1) as status")
                    .from("t_application_info")
                    .where("deleted = 1")
                    .list();
        } finally {
            DataSourceKey.clear();
        }
        List<Plug> entityList = RowUtil.toEntityList(rowList, Plug.class);
        SpringUtils.getBean(PlugService.class).saveBatch(entityList);
    }

    @Test
    public void batchUpdateStaffPassword(){
        List<Row> cpSysUser = DbChain.table("cp_sys_user")
                .select("id","phone_number")
                .where("user_type = 'OAM_USER'")
                .and("is_delete = 0")
                .list();
        List<SysUser> entityList = RowUtil.toEntityList(cpSysUser, SysUser.class);
        //加密
        RsaUtils rsaUtils = SpringUtils.getBean(RsaUtils.class);
        entityList.forEach(item -> {
            if (!StringUtils.equals("1", item.getId())) {
                String lastFourChars = "jambox" + item.getPhoneNumber().substring(item.getPhoneNumber().length() - 4);
                item.setRsaPassword(rsaUtils.encrypt(lastFourChars));
            }
        });
        Db.executeBatch(entityList, CoreMapper.class, CoreMapper::updateSysUser);
    }

    /**
     * 同步品牌与校区数据
     * @author DB
     * @since 2024/1/8
     */
    @Test
    public void syncBrandAndStoreData() {
        BrandService brandService = SpringUtils.getBean(BrandService.class);
        for (int i = 2207; i < 2317; i++) {
            brandService.importJamboxBrand(String.valueOf(i));
            //导入营业执照相关信息
        }
    }

    /**
     * 更新报销上传文件
     * @author DB
     * @since 2024/1/8
     */
    @Test
    public void updateReimburseUploadFileData() {
        FinanceReimburseService financeReimburseService = SpringUtils.getBean(FinanceReimburseService.class);
        List<FinanceReimburse> list = financeReimburseService.list();
        List<FinanceReimburse> updateEnetyList = new ArrayList<>();
        list.forEach(item -> {
            if (StringUtils.isNotBlank(item.getAttachmentUrl())){
                JSONArray jsonArray = JSONArray.parseArray(item.getAttachmentUrl());
                JSONObject jsonObject = JSONObject.parseObject(jsonArray.get(0).toString());
                String[] split = jsonObject.getString("url").split(",");
                if (split.length > 1) {
                    List<Map<String, String>> urls = new ArrayList<>();
                    for (int i = 0; i < split.length; i++) {
                        Map<String, String> map = new HashMap<>();
                        map.put("name", "旧数据无名字");
                        map.put("url", split[i]);
                        urls.add(map);
                    }
                    item.setAttachmentUrl(JSONArray.toJSONString(urls));
                    updateEnetyList.add(item);
                }
            }
        });
        financeReimburseService.updateBatch(updateEnetyList);
    }

}
