package test;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kiisoo.ic.app.IcApplication;
import com.kiisoo.ic.common.utils.MD5FileUtil;
import com.kiisoo.ic.config.WxCpConfiguration;
import com.kiisoo.ic.domain.entity.PrivilageDomainDO;
import com.kiisoo.ic.domain.entity.PrivilageDomainEntityDO;
import com.kiisoo.ic.domain.mapper.PrivilageDomainDOMapper;
import com.kiisoo.ic.domain.mapper.PrivilageDomainEntityDOMapper;
import com.kiisoo.ic.employee.entity.PrivilageCpUserDO;
import com.kiisoo.ic.employee.entity.QrCodeDO;
import com.kiisoo.ic.employee.mapper.PrivilageCpUserDOMapper;
import com.kiisoo.ic.generalize.entity.CompanyStoreDO;
import com.kiisoo.ic.generalize.entity.PrivilageOrganizational;
import com.kiisoo.ic.generalize.entity.PrivilageOrganizationalDomain;
import com.kiisoo.ic.generalize.entity.PrivilageOrganizationalMember;
import com.kiisoo.ic.generalize.mapper.PrivilageOrganizationalDomainMapper;
import com.kiisoo.ic.generalize.mapper.RetailCompanyMapper;
import com.kiisoo.ic.store.entity.PoiStore;
import com.kiisoo.ic.store.entity.PoiStoreStaff;
import com.kiisoo.ic.store.entity.PoiStoreStaffVO;
import com.kiisoo.ic.store.entity.PrivilageCpUserStoreDO;
import com.kiisoo.ic.store.mapper.PoiStoreDOMapper;
import com.kiisoo.ic.store.mapper.PoiStoreStaffDOMapper;
import com.kiisoo.ic.store.mapper.PrivilageCpUserStoreDOMapper;
import com.kiisoo.ic.store.service.PoiStoreStaffService;
import com.kiisoo.ic.store.service.StoreEmployeeService;
import com.kiisoo.ic.system.entity.PrivilageAccountDO;
import com.kiisoo.ic.system.entity.PrivilageUserDO;
import com.kiisoo.ic.system.entity.PrivilageUserRoleDO;
import com.kiisoo.ic.system.mapper.PrivilageAccountDOMapper;
import com.kiisoo.ic.system.mapper.PrivilageUserDOMapper;
import com.kiisoo.ic.system.mapper.PrivilageUserRoleDOMapper;
import init.ShopInitDO;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl;
import me.chanjar.weixin.cp.bean.WxCpDepart;
import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.io.IOException;
import java.util.*;

import static com.kiisoo.ic.config.WxCpConfiguration.APPLICATIONID;
import static com.kiisoo.ic.store.constant.Constants.BSD_REQ_QRCODE_TYPE_SELLER;
import static com.kiisoo.ic.store.constant.Constants.BSD_REQ_QRCODE_TYPE_STORE;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = IcApplication.class)
public class InitDataTest {

    /**
     * 店铺
     */
    @Autowired
    private PoiStoreDOMapper poiStoreDOMapper;

    /**
     * 用户
     */
    @Autowired
    private PrivilageUserDOMapper privilageUserDOMapper;

    /**
     * 人员表
     */
    @Autowired
    private PoiStoreStaffDOMapper poiStoreStaffDOMapper;

    /**
     * 店长信息表
     */
    @Autowired
    private PrivilageCpUserDOMapper privilageCpUserDOMapper;

    /**
     * 店长店铺关系表
     */
    @Autowired
    private PrivilageCpUserStoreDOMapper privilageCpUserStoreDOMapper;

    /**
     * 门店号管理service
     */
    @Autowired
    private StoreEmployeeService storeEmployeeService;

    /**
     * 组织
     */
    @Autowired
    private RetailCompanyMapper retailCompanyMapper;

    /**
     * 域
     */
    @Autowired
    private PrivilageDomainDOMapper privilageDomainDOMapper;

    /**
     * 域实体
     */
    @Autowired
    private PrivilageDomainEntityDOMapper privilageDomainEntityDOMapper;

    /**
     * 组织和域的关系表
     */
    @Autowired
    private PrivilageOrganizationalDomainMapper privilageOrganizationalDomainMapper;

    /**
     * 店铺人员表service
     */
    @Autowired
    private PoiStoreStaffService poiStoreStaffService;

    /**
     * 账号
     */
    @Autowired
    private PrivilageAccountDOMapper privilageAccountDOMapper;

    @Autowired
    private PrivilageUserRoleDOMapper privilageUserRoleDOMapper;

    private static final String authorName = "蒋老师";

    /**
     * 添加零售公司
     * @throws IOException
     */
    @Test
    public void addCompany() throws IOException {
        List<String> list = FileUtils.readLines(new File("file/file_company_1.txt"));

        for (int i = 0; i < list.size(); i++) {
            String item = list.get(i);
            String[] arr = item.split(",");

            String regionName = arr[0].trim();
            // 零售公司编码
            String companyCode = arr[1].trim();
            // 零售公司名称
            String companyName = arr[2].trim();

            //查看是否有上级区域
            String parentName = regionName;
            long parentId = 0;
            if(parentName != null){
                //查区域
                PrivilageOrganizational p = new PrivilageOrganizational();
                p.setName(parentName);
                p.setLevel(1L);
                PrivilageOrganizational parent = retailCompanyMapper.selectOne(new QueryWrapper<>(p));
                if(null == parent){
                    parent = new PrivilageOrganizational();
                    parent.setName(regionName);
                    parent.setLevel(1L);
                    parent.setParentId(0L);
                    parent.setType(2L);
                    parent.setStatus(1L);
                    parent.setCreateTime(new Date());
                    parent.setUpdateTime(new Date());
                    parent.setCreateBy(authorName);
                    parent.setUpdateBy(authorName);
                    retailCompanyMapper.insert(parent);
//                    return;
                }
                parentId = parent.getId();
            }

            Map<String, Object> map = new HashMap<>();
            map.put("name", companyName);
            map.put("level", 2L);
            map.put("parent_id", parentId);
            map.put("type", 2L);
            map.put("status", 1L);
            List<PrivilageOrganizational> listOZ = retailCompanyMapper.selectByMap(map);

            if(listOZ.size() > 0){
                System.out.println("零售公司多了" + regionName + companyName + companyCode);
                continue;
            }

            PrivilageOrganizational param = new PrivilageOrganizational();
            param.setName(companyName);
            param.setCode(companyCode);
            param.setLevel(2L);
            param.setParentId(parentId);
            param.setType(2L);
            param.setStatus(1L);
            param.setCreateTime(new Date());
            param.setUpdateTime(new Date());
            param.setCreateBy(authorName);
            param.setUpdateBy(authorName);
            retailCompanyMapper.insert(param);
        }
    }

    /**
     * 更新店铺poi_store
     *
     * @throws Exception
     */
    @Test
    public void updateStoreInfo() throws Exception {
        // Excel的店铺
        List<String> list = FileUtils.readLines(new File("file/file1.txt"));
        // 系统店铺
        List<String> storeList = FileUtils.readLines(new File("file/check_store_1.txt"));

        Map<String, String> storesMap = new HashMap<>();

        List<String> newList = new ArrayList<>();
        for(int i=0;i<list.size();i++){
            String item = list.get(i);
            String[] arr = item.split(",");

            if(!storesMap.containsKey(arr[1])){
                storesMap.put(arr[1], item);
                newList.add(item);
            }else{
                System.out.println("重复店铺" + item);

            }
        }

        File file = new File("file/new_store5.txt");
//        Map<String, String> stringMap = new HashMap<>();
        for(int i=0;i<storeList.size();i++){
            String item = storeList.get(i);
            String[] arr = item.split(",");
//            stringMap.put(arr[2], item);
            String storeCode = arr[2];

            if(!storesMap.containsKey(storeCode)){
                String companyCode = arr[3];
                if(companyCode.equals("129")){
                    continue;
                }
                FileUtils.write(file, item + "\n", true);
            }
        }


        for (int i = 0; i < newList.size(); i++) {
            String item = newList.get(i);
            String[] arr = item.split(",");
            String storeName = arr[0];
            String storeCode = arr[1];
            String account = arr[2];
            String accountName = arr[3];
            String abName = arr[4];
            String phone = arr[5];


//            update(arr[0], arr[3], arr[4], arr[5]);
        }

    }

    public void update(String shopCode, String phone, String newAccount, String newPhone) throws Exception {
        Long shopId = 0L;
        String sellerCode = phone;
        String newSellerCode = newPhone;
        Long newCustomerUserId = Long.parseLong(newAccount);
        //查看店铺
        if (shopCode != null) {
            PoiStore p = new PoiStore();
            p.setCode(shopCode);
            PoiStore shop = poiStoreDOMapper.selectOne(new QueryWrapper<>(p));
            if (null != shop) {
                shopId = shop.getId();
            }

            //查看用户
            PrivilageUserDO userdo = new PrivilageUserDO();
            userdo.setMobil(phone);
            // 用户手机号码更新
            PrivilageUserDO us = privilageUserDOMapper.selectOne(new QueryWrapper<>(userdo));
            if (null != us) {
                us.setMobil(newPhone);
                privilageUserDOMapper.updateById(us);
            } else {
                return;
            }

            //店涨，导购需要加staff
            Map<String, Object> staffMap = new HashMap<>();
            staffMap.put("store_id", shopId);
            staffMap.put("user_id", us.getId());
            staffMap.put("store_code", shopCode);
            staffMap.put("staff_code", sellerCode);
            staffMap.put("type", 1);
            staffMap.put("status", 1);
            List<PoiStoreStaff> staffList = poiStoreStaffDOMapper.selectByMap(staffMap);
            if (staffList.size() > 0) {
                PoiStoreStaff poiStoreDO = staffList.get(0);
                poiStoreDO.setStaffCode(newSellerCode);
                poiStoreDO.setCustomerUserId(newCustomerUserId);
                poiStoreStaffDOMapper.updateById(poiStoreDO);
            }

            //如果是店长，需要绑定店铺cp
            //排重---根据企业微信账号查询是否存在用户
            PrivilageCpUserDO privilageCpUserDO = new PrivilageCpUserDO();
            privilageCpUserDO.setCpUserId(sellerCode);
            privilageCpUserDO.setStatus(1);

            Map<String, Object> map = new HashMap<>();
            // 客户ID
            map.put("cp_user_id", sellerCode);
            map.put("status", privilageCpUserDO.getStatus());
            List<PrivilageCpUserDO> list = privilageCpUserDOMapper.selectByMap(map);
            if (list.size() > 0) {
                PrivilageCpUserDO cpuserdo = list.get(0);
                cpuserdo.setCpUserId(newSellerCode);
                cpuserdo.setStatus(privilageCpUserDO.getStatus());
                // 导购 先作废二维码，调用BSD提供的接口
//                storeEmployeeService.cancelQrCodeFromBSD(sellerCode, "");

                // 门店不用
                String qrCodeUrl = storeEmployeeService.getQrCodeFromBSD(newSellerCode, "0", "0", shopCode);
                if (StringUtils.isNotBlank(qrCodeUrl)) {
                    // 二维码也需要修改
                    cpuserdo.setQrCode(qrCodeUrl);
                    privilageCpUserDOMapper.updateById(cpuserdo);
                } else {
                    System.out.println("获取二维码失败");
                }
            }
        }
    }

    /**
     * 更新客户组织
     *
     * @throws IOException 异常
     */
    @Test
    public void updateCustomer() throws IOException {
//        List<String> list = FileUtils.readLines(new File("file/file_customer_1.txt"));
//        List<String> list = FileUtils.readLines(new File("file/file_customer_2.txt"));
        List<String> list = FileUtils.readLines(new File("file/file_customer_3.txt"));
        for (int i = 0; i < list.size(); i++) {
            String item = list.get(i);
            String[] arr = item.split(",");
            updateData(arr);
        }
    }

    /**
     * 校验组织客户的数量
     */
    @Test
    public void checkCustomer() throws IOException {
        List<String> list = FileUtils.readLines(new File("file/t1.txt"));
        Map<String, Object> listMap = new HashMap<>();

        for (int i = 0; i < list.size(); i++) {
            String item = list.get(i);
            String[] arr = item.split(",");
            String name = arr[1].trim();
            String code = arr[0].trim();

            if (listMap.containsKey(name + code)) {
                System.out.println(item);
            } else {
                listMap.put(name + code, item);
            }

            Map<String, Object> map = new HashMap<>();
            map.put("code", code);
            map.put("level", "3");
            List<PrivilageOrganizational> res = retailCompanyMapper.selectByMap(map);
            if (res.size() == 0) {
                System.out.println("code:" + item);
            }
            Map<String, Object> map2 = new HashMap<>();
            map2.put("name", name);
            map2.put("level", "3");
            List<PrivilageOrganizational> res2 = retailCompanyMapper.selectByMap(map2);
            if (res2.size() == 0) {
                System.out.println("name" + item);
            }


        }

        System.out.println("name");

    }

    /**
     * 更新客户组织
     *
     * @param arr 字段数组
     */
    private void updateData(String[] arr) {

        String regionName = arr[0].trim();
        // 零售公司编码
        String companyCode = arr[1].trim();
        // 零售公司名称
        String companyName = arr[2].trim();
        // 客户组织编码
        String customerCode = arr[3].trim();
        // 客户组织名称
        String customerName = arr[4].trim();
        Map<String, Object> map = new HashMap<>();
        map.put("name", customerName);
        map.put("level", 3);
        List<PrivilageOrganizational> customerDO = retailCompanyMapper.selectByMap(map);
//        List<Map<String, Object>> customerDO = retailCompanyMapper.selectCustomerDOByStoreCode(storeCode);
        if (customerDO.size() > 0) {
            System.out.println("存在" + regionName + customerCode + customerName);
            PrivilageOrganizational item = customerDO.get(0);
            return;
            // 更新信息
//            PrivilageDomainDO ddd = new PrivilageDomainDO();
//            ddd.setName(customerName);
//            PrivilageDomainDO domainDO = privilageDomainDOMapper.selectOne(new QueryWrapper<>(ddd));
//            if (null == domainDO) {
//                domainDO = new PrivilageDomainDO();
//                domainDO.setName(customerName);
//                domainDO.setStatus(1);
//                domainDO.setCreateTime(new Date());
//                domainDO.setUpdateTime(new Date());
//                domainDO.setCreateBy(authorName);
//                domainDO.setUpdateBy(authorName);
//                privilageDomainDOMapper.insert(domainDO);
//            }
//
//            Map<String, Object> deMap = new HashMap<>();
//            deMap.put("domain_id", domainDO.getId());
//            List<PrivilageDomainEntityDO> domainEnityDOList = privilageDomainEntityDOMapper.selectByMap(deMap);
//            if(CollectionUtils.isEmpty(domainEnityDOList)){
//                PrivilageDomainEntityDO domainEnityDO = new PrivilageDomainEntityDO();
//                domainEnityDO.setDomainId(domainDO.getId());
//                domainEnityDO.setType(3);
//                domainEnityDO.setCreateTime(new Date());
//                domainEnityDO.setUpdateTime(new Date());
//                domainEnityDO.setUpdateBy(authorName);
//                domainEnityDO.setUpdateBy(authorName);
//                privilageDomainEntityDOMapper.insert(domainEnityDO);
//            }else{
//                System.out.println("有多个PrivilageDomainEntityDO关系");
//            }

//            // 通过域 和域实体关系 找到客户组织
//            PoiStore pp = new PoiStore();
//            pp.setCode(storeCode);
//            PoiStore poiStore = poiStoreDOMapper.selectOne(new QueryWrapper<>(pp));
//            if (null != poiStore) {
//                PrivilageDomainEntityDO dq = new PrivilageDomainEntityDO();
//                dq.setEntityId(poiStore.getId());
//                PrivilageDomainEntityDO domainEntity = privilageDomainEntityDOMapper.selectOne(new QueryWrapper<>(dq));
//                if (null != domainEntity) {
//                    PrivilageDomainDO ddd = new PrivilageDomainDO();
//                    ddd.setId(domainEntity.getDomainId());
//                    PrivilageDomainDO domainDO = privilageDomainDOMapper.selectOne(new QueryWrapper<>(ddd));
//                    if (null != domainDO && !domainDO.getName().equals(customerName)) {
//                        // 修改到新的组织里
//                        PrivilageDomainDO newDDD = new PrivilageDomainDO();
//                        newDDD.setName(customerName);
//                        PrivilageDomainDO res = privilageDomainDOMapper.selectOne(new QueryWrapper<>(newDDD));
//                        if(null != res){
//                            domainEntity.setDomainId(res.getId());
//                            domainEntity.setUpdateTime(new Date());
//                            domainEntity.setUpdateBy("蒋老师");
//                            privilageDomainEntityDOMapper.updateById(domainEntity);
//                        }
//
//                    } else if (null == domainDO) {
//                        domainDO = new PrivilageDomainDO();
//                        domainDO.setName(customerName);
//                        domainDO.setStatus(1);
//                        domainDO.setCreateTime(new Date());
//                        domainDO.setCreateBy("蒋老师");
//                        privilageDomainDOMapper.insert(domainDO);
//
//                        domainEntity.setDomainId(domainDO.getId());
//                        domainEntity.setUpdateTime(new Date());
//                        domainEntity.setUpdateBy("蒋老师");
//                        privilageDomainEntityDOMapper.updateById(domainEntity);
//                    }
//                } else {
//                    System.out.println("没有对应域关系" + ArrayUtils.toString(arr));
//                }
//            } else {
//                System.out.println("没有对应店铺" + ArrayUtils.toString(arr));
//            }
//            item.setName(customerName);
//            item.setCode(customerCode);
//            item.setUpdateTime(new Date());
//            item.setUpdateBy("蒋老师");
//            retailCompanyMapper.updateById(item);


        }
        //查看是否有零售公司
        long parentId = 0;
        //查零售公司
        PrivilageOrganizational organizational = new PrivilageOrganizational();
        organizational.setName(companyName);
        organizational.setLevel(2L);
        PrivilageOrganizational parent = retailCompanyMapper.selectOne(new QueryWrapper<>(organizational));
        if (null == parent) {
            System.out.println("父区域异常");
            return;
        } else if (parent.getCode() == null) {
//                    parent.setCode(parent.getCode().replace(".0",""));
//                    retailCompanyMapper.updateById(parent);
            System.out.println("customerCode零售公司编码没有");
            return;
        }
        parentId = parent.getId();
        //创建客户
        PrivilageOrganizational param = new PrivilageOrganizational();
        param.setName(customerName);
        param.setCode(customerCode);
        param.setLevel(3L);
        param.setParentId(parentId);
        param.setType(2L);
        param.setStatus(1L);
        param.setCreateTime(new Date());
        param.setUpdateTime(new Date());
        param.setCreateBy(authorName);
        param.setUpdateBy(authorName);
        retailCompanyMapper.insert(param);

        //创建域
        PrivilageDomainDO p = new PrivilageDomainDO();
        p.setName(customerName);
        p.setStatus(1);
        p.setCreateTime(new Date());
        p.setUpdateTime(new Date());
        p.setCreateBy(authorName);
        p.setUpdateBy(authorName);
        privilageDomainDOMapper.insert(p);

        // 创建客户和域实体关系
        PrivilageDomainEntityDO entityDO = new PrivilageDomainEntityDO();
        entityDO.setDomainId(p.getId());
        entityDO.setEntityId(param.getId());
        entityDO.setType(1);
        entityDO.setCreateTime(new Date());
        entityDO.setUpdateTime(new Date());
        entityDO.setCreateBy(authorName);
        entityDO.setUpdateBy(authorName);
        privilageDomainEntityDOMapper.insert(entityDO);

        //创建区域和域关联关系
        retailCompanyMapper.insertDomainOrgRelation(p.getId(), param.getId());
    }

    /**
     * 更新店铺
     */
    @Test
    public void updateStore() throws IOException {
//        List<String> list = FileUtils.readLines(new File("file/file_store_1.txt"));
        List<String> list = FileUtils.readLines(new File("file/file_store_5.txt"));

//        List<String> list = FileUtils.readLines(new File("file/file_store_122.txt"));
        for (int i = 0; i < list.size(); i++) {
            String item = list.get(i);
            String[] arr = item.split(",");
            updateStoreByFile2(arr);
        }
    }

    @Test
    public void checkStore() throws IOException {
        List<String> list = FileUtils.readLines(new File("file/poi_store_file_1.txt"));
        List<String> storeList = FileUtils.readLines(new File("file/file_store_1.txt"));

        Map<String, String> map = new HashMap<>();
        for (int i = 0; i < storeList.size(); i++) {
            String item = storeList.get(i);
            String[] arr = item.split(",");
            if (map.containsKey(arr[6].trim())) {
//                System.out.println(item);
            } else {
                map.put(arr[6].trim(), item);
            }
        }

        for (int i = 0; i < list.size(); i++) {
            String item = list.get(i);
            String[] arr = item.split(",");
            String name = arr[0].trim();
            String code = arr[1].trim();
            String store = map.get(code);
            if (StringUtils.isBlank(store)) {
                System.out.println(item);
                continue;
            }

//            String[] ditem = store.split(",");
//
//            if(!name.equals(ditem[5].trim()) || !code.equals(ditem[6].trim())){
//                System.out.println(item + " | "+store);
//
//                //查看是否有此店铺
////                QueryWrapper<PoiStore> qw = new QueryWrapper<>();
////                qw.eq("code", store);
////                PoiStore poiStore = poiStoreDOMapper.selectOne(qw);
//
//            }


        }
    }


    private void updateStoreByFile2(String[] arr) {
        String regionName = arr[0].trim();
        // 零售公司编码
        String companyCode = arr[1].trim();
        // 零售公司名称
        String companyName = arr[2].trim();
        // 客户组织编码
        String customerCode = arr[3].trim();
        // 客户组织名称
        String customerName = arr[4].trim();
        // 店铺编码
        String storeName = arr[5].trim();
        // 店铺名称
        String storeCode = arr[6].trim();
        // 别名
        String abName = arr[9];

        //查看是否有此店铺
        QueryWrapper<PoiStore> qw = new QueryWrapper<>();
        qw.eq("code", storeCode);
        PoiStore poiStore = poiStoreDOMapper.selectOne(qw);
        if (null == poiStore) {
            poiStore = new PoiStore();
            poiStore.setType(1L);
            poiStore.setLevel("");
            poiStore.setName(storeName);
            poiStore.setCode(storeCode);
            poiStore.setAddress("");
            poiStore.setStatus(1L);
            poiStore.setCreateTime(new Date());
            poiStore.setUpdateTime(new Date());
            poiStore.setAbbreviation(abName);
            poiStore.setCreateBy("蒋老师");
            poiStoreDOMapper.insert(poiStore);
        } else {
            // 修改更新操作
            poiStore.setName(storeName);
            poiStore.setAbbreviation(abName);
            poiStore.setUpdateTime(new Date());
            poiStore.setUpdateBy("蒋老师");
            poiStore.setCode(poiStore.getCode().trim());
            poiStoreDOMapper.updateById(poiStore);
        }

        //查看是否有客户
        String parentName = customerName;
        long parentId = 0;
        //查客户
        PrivilageOrganizational p = new PrivilageOrganizational();
        p.setName(parentName);
        p.setLevel(3L);
        PrivilageOrganizational parent = retailCompanyMapper.selectOne(new QueryWrapper<>(p));
        if (null == parent) {
            System.out.println("不存在对应客户关系");
            return;
        }
        parentId = parent.getId();

        Map<String, Object> domainMap = new HashMap<>();
        domainMap.put("name", customerName);
        List<PrivilageDomainDO> domainList = privilageDomainDOMapper.selectByMap(domainMap);

        if(CollectionUtils.isEmpty(domainList)){
            System.out.println("找不到对应domain" + ArrayUtils.toString(arr));

            return;
        }

        if(domainList.size() > 1){
            System.out.println("多了 domainList" + customerName);
            return;
        }

        PrivilageDomainDO domain = domainList.get(0);

        //找到区域域
        Map<String, Object> map = new HashMap<>();
        map.put("entity_id", poiStore.getId());
        List<PrivilageDomainEntityDO> domainEntityList = privilageDomainEntityDOMapper.selectByMap(map);
        if (domainEntityList.size() == 0) {
//            System.out.println("找不到对应域实体" + ArrayUtils.toString(arr));
            PrivilageDomainEntityDO privilageDomainEntityDO = new PrivilageDomainEntityDO();
            privilageDomainEntityDO.setDomainId(domain.getId());
            privilageDomainEntityDO.setEntityId(poiStore.getId());
            privilageDomainEntityDO.setType(3);
            privilageDomainEntityDO.setCreateTime(new Date());
            privilageDomainEntityDO.setCreateBy("蒋老师");
            privilageDomainEntityDOMapper.insert(privilageDomainEntityDO);
        } else {
            PrivilageDomainEntityDO domainEnity = domainEntityList.get(0);

            if(!domainEnity.getEntityId().equals(poiStore.getId())){
                System.out.println("该对应域实体关系错误" + ArrayUtils.toString(arr));
//                privilageDomainEntityDOMapper.updateById();
            }
        }
    }

    /**
     * 更新店铺
     *
     * @param arr
     */
    private void updateStoreByFile(String[] arr) {
        String regionName = arr[0].trim();
        // 零售公司编码
        String companyCode = arr[1].trim();
        // 零售公司名称
        String companyName = arr[2].trim();
        // 客户组织编码
        String customerCode = arr[3].trim();
        // 客户组织名称
        String customerName = arr[4].trim();
        // 店铺编码
        String storeName = arr[5].trim();
        // 店铺名称
        String storeCode = arr[6].trim();
        // 别名
        String abName = arr[9];

        //查看是否有此店铺
        QueryWrapper<PoiStore> qw = new QueryWrapper<>();
        qw.eq("code", storeCode);
        PoiStore poiStore = poiStoreDOMapper.selectOne(qw);
        if (null == poiStore) {
            poiStore = new PoiStore();
            poiStore.setType(1L);
            poiStore.setLevel("");
            poiStore.setName(storeName);
            poiStore.setCode(storeCode);
            poiStore.setAddress("");
            poiStore.setStatus(1L);
            poiStore.setCreateTime(new Date());
            poiStore.setUpdateTime(new Date());
            poiStore.setAbbreviation(abName);
            poiStore.setCreateBy("蒋老师");
            poiStoreDOMapper.insert(poiStore);
        } else {
            // 修改更新操作
            poiStore.setName(storeName);
            poiStore.setAbbreviation(abName);
            poiStore.setUpdateTime(new Date());
            poiStore.setUpdateBy("蒋老师");
            poiStore.setCode(poiStore.getCode().trim());
            poiStoreDOMapper.updateById(poiStore);
        }

        //查看是否有上级区域
        String parentName = customerName;
        long parentId = 0;
        //查区域
        PrivilageOrganizational p = new PrivilageOrganizational();
        p.setName(parentName);
        p.setLevel(3L);
        PrivilageOrganizational parent = retailCompanyMapper.selectOne(new QueryWrapper<>(p));
        if (null == parent) {
            System.out.println("父区域异常");
            return;
        }
        parentId = parent.getId();

        //找到区域域
        Map<String, Object> map = new HashMap<>();
        map.put("entity_id", poiStore.getId());
        List<PrivilageDomainEntityDO> domainEntityList = privilageDomainEntityDOMapper.selectByMap(map);
        if (domainEntityList.size() == 0) {
            System.out.println("PrivilageDomainEntityDO不存在" + ArrayUtils.toString(arr));
            PrivilageDomainEntityDO privilageDomainEntityDO = new PrivilageDomainEntityDO();
            privilageDomainEntityDO.setDomainId(parent.getId());
            privilageDomainEntityDO.setEntityId(poiStore.getId());
            privilageDomainEntityDO.setType(3);
            privilageDomainEntityDO.setCreateTime(new Date());
            privilageDomainEntityDO.setCreateBy("蒋老师");
            privilageDomainEntityDOMapper.insert(privilageDomainEntityDO);
        } else {

            PrivilageDomainEntityDO domainEnity = domainEntityList.get(0);
            Map<String, Object> map2 = new HashMap<>();
            map2.put("domain_id", domainEnity.getId());
            List<PrivilageOrganizationalDomain> orgDomain = privilageOrganizationalDomainMapper.selectByMap(map2);

            if (orgDomain.size() > 0) {
                PrivilageOrganizationalDomain od = orgDomain.get(0);
                //找到区域店铺关系
                od.setDomainId(domainEnity.getId());
                od.setUpdateTime(new Date());
                od.setUpdateBy("蒋老师");
                privilageOrganizationalDomainMapper.updateById(od);
            } else {
                System.out.println("PrivilageOrganizationalDomain不存在" + ArrayUtils.toString(arr));
                PrivilageOrganizationalDomain odomainEntity = new PrivilageOrganizationalDomain();
                odomainEntity.setOrgId(parent.getId());
                odomainEntity.setDomainId(parent.getId());
                odomainEntity.setCreateTime(new Date());
                odomainEntity.setCreateBy("蒋老师");
                privilageOrganizationalDomainMapper.insert(odomainEntity);
            }
        }

    }

    /**
     * 添加店长账号
     */
    @Test
    public void addStoreStaff() throws Exception {
//        List<String> list = FileUtils.readLines(new File("file/file_store_1.txt"));
//        List<String> list = FileUtils.readLines(new File("file/file_store_2.txt"));
//        List<String> list = FileUtils.readLines(new File("file/file_store_4.txt"));
        List<String> list = FileUtils.readLines(new File("file/file_store_6.txt"));
        for (int i = 0; i < list.size(); i++) {
            String item = list.get(i);
            String[] arr = item.split(",");
            addStoreStaffItem2(arr);
        }
    }

    private void addStoreStaffItem2(String[] arr) {

        String regionName = arr[0].trim();
        // 零售公司编码
        String companyCode = arr[1].trim();
        // 零售公司名称
        String companyName = arr[2].trim();
        // 客户组织编码
        String customerCode = arr[3].trim();
        // 客户组织名称
        String customerName = arr[4].trim();
        // 店铺编码
        String storeName = arr[5].trim();
        // 店铺名称
        String storeCode = arr[6].trim();
        // 企业微信账号
        String account = arr[7].trim();
        // 企业微信名称
        String name = arr[8].trim();
        // 别名
        String abName = arr[9].trim();
        // 手机号码
        String phone = arr[10].trim();

        String sellerCode = account;
        String shopCode = storeCode;
        long shopId = 0;
        if (sellerCode == null || phone == null) {
            System.out.println("无法找到唯一标示异常");
            return;
        }
        //查看店铺
        if (shopCode != null) {
            PoiStore p = new PoiStore();
            p.setCode(shopCode);
            PoiStore shop = poiStoreDOMapper.selectOne(new QueryWrapper<>(p));
            if (null != shop) {
                shopId = shop.getId();
            }
        }

        //查看用户
        PrivilageUserDO userdo = new PrivilageUserDO();
        userdo.setMobil(phone);
        PrivilageUserDO us = privilageUserDOMapper.selectOne(new QueryWrapper<>(userdo));
        if (null == us) {
            //如果没有用户就增加
            us = new PrivilageUserDO();
            us.setName(name);
            us.setMobil(phone);
            us.setEmail("");
            us.setAddress("");
            us.setStatus(1);
            us.setCreateTime(new Date());
            us.setUpdateTime(new Date());
            us.setAbbreviation(abName);
            try {
                privilageUserDOMapper.insert(us);
            } catch (Exception e) {
                us.setAbbreviation(null);
                privilageUserDOMapper.insert(us);
            }
        } else {
            us.setName(name);
            us.setUpdateTime(new Date());
            us.setUpdateBy("蒋老师");
            privilageUserDOMapper.updateById(us);
        }

        //店涨需要加staff
        PoiStoreStaff p = new PoiStoreStaff();
        p.setStoreId(shopId);
        p.setUserId(us.getId());
        p.setStoreCode(storeCode);
        p.setStaffCode(sellerCode);
        p.setType(1L);// 1、店长类型
        p.setStatus(1L);
        p.setCreateTime(new Date());
        p.setUpdateTime(new Date());
        p.setCustomerUserId(Long.parseLong(account));

        Map<String, Object> staffMap = new HashMap<>();
//        staffMap.put("store_id", p.getStoreId());
//        staffMap.put("user_id", p.getUserId());
        staffMap.put("store_code", p.getStoreCode());
                staffMap.put("staff_code", p.getStaffCode());
        staffMap.put("type", p.getType());
        staffMap.put("status", p.getStatus());
        List<PoiStoreStaff> staffList = poiStoreStaffDOMapper.selectByMap(staffMap);
        if (staffList.size() == 0) {
            System.out.println("添加店铺人员表" + ArrayUtils.toString(arr));
            poiStoreStaffDOMapper.insert(p);
        } else {
            staffList.get(0).setStoreId(p.getStoreId());
            staffList.get(0).setStaffCode(p.getStaffCode());
            staffList.get(0).setCustomerUserId(p.getCustomerUserId());
            staffList.get(0).setUpdateTime(new Date());
            staffList.get(0).setUpdateBy("蒋老师");
            poiStoreStaffDOMapper.updateById(staffList.get(0));
        }

        //如果是店长，需要绑定店铺cp
        //排重---根据企业微信账号查询是否存在用户
        PrivilageCpUserDO privilageCpUserDO = new PrivilageCpUserDO();
        privilageCpUserDO.setCpUserId(account);
        privilageCpUserDO.setStatus(1);
        privilageCpUserDO.setMobile(phone);
        privilageCpUserDO.setName(name);
        privilageCpUserDO.setAlias(abName);

        Map<String, Object> map = new HashMap<>();
        // 客户ID
        map.put("cp_user_id", p.getCustomerUserId());
        map.put("status", privilageCpUserDO.getStatus());
        List<PrivilageCpUserDO> list = privilageCpUserDOMapper.selectByMap(map);
        if (list.size() == 0) {
            privilageCpUserDOMapper.insert(privilageCpUserDO);

            PrivilageCpUserStoreDO cpUserStore = new PrivilageCpUserStoreDO();
            cpUserStore.setStoreId(shopId);
            cpUserStore.setCpUserId(privilageCpUserDO.getId());

            Map<String, Object> cpUserStoreMap = new HashMap<>();
            cpUserStoreMap.put("store_id", cpUserStore.getStoreId());
//                        cpUserStoreMap.put("cp_user_id", cpUserStore.getCpUserId());
            List<PrivilageCpUserStoreDO> cpUserStoreList = privilageCpUserStoreDOMapper.selectByMap(cpUserStoreMap);
            if (cpUserStoreList.size() == 0) {
//                System.out.println("找不到店长与店铺的关系表" + ArrayUtils.toString(arr));
                privilageCpUserStoreDOMapper.insert(cpUserStore);
            } else {
//                System.out.println("店长与店铺的关系表:修改" + ArrayUtils.toString(arr));
                PrivilageCpUserStoreDO cpuserstore = cpUserStoreList.get(0);
                cpuserstore.setStoreId(cpUserStore.getStoreId());
                cpuserstore.setCpUserId(cpUserStore.getCpUserId());
                privilageCpUserStoreDOMapper.updateById(cpuserstore);
            }
        } else {
            PrivilageCpUserDO cpuserdo = list.get(0);
            cpuserdo.setCpUserId(privilageCpUserDO.getCpUserId());
            cpuserdo.setStatus(privilageCpUserDO.getStatus());
            cpuserdo.setName(name);
            cpuserdo.setAlias(abName);
            cpuserdo.setMobile(phone);
            privilageCpUserDOMapper.updateById(cpuserdo);

            PrivilageCpUserStoreDO cpUserStore = new PrivilageCpUserStoreDO();
            cpUserStore.setStoreId(shopId);
            cpUserStore.setCpUserId(list.get(0).getId());
            Map<String, Object> cpUserStoreMap = new HashMap<>();
            cpUserStoreMap.put("store_id", cpUserStore.getStoreId());
//                        cpUserStoreMap.put("cp_user_id", cpUserStore.getCpUserId());
            List<PrivilageCpUserStoreDO> cpUserStoreList = privilageCpUserStoreDOMapper.selectByMap(cpUserStoreMap);
            if (cpUserStoreList.size() == 0) {
                System.out.println("找不到店长店铺表" + ArrayUtils.toString(arr));
                privilageCpUserStoreDOMapper.insert(cpUserStore);
            } else {
                PrivilageCpUserStoreDO cpuserstore = cpUserStoreList.get(0);
                cpuserstore.setStoreId(cpUserStore.getStoreId());
                cpuserstore.setCpUserId(cpUserStore.getCpUserId());
                privilageCpUserStoreDOMapper.updateById(cpuserstore);
            }
        }

        //不管什么类型都要绑定角色
        PrivilageUserRoleDO privilageUserRoleDO = new PrivilageUserRoleDO();
        privilageUserRoleDO.setUserId(us.getId());
        privilageUserRoleDO.setRoleId(4L); // 店长角色
        privilageUserRoleDO.setCreateTime(new Date());
        privilageUserRoleDO.setUpdateTime(new Date());


        Map<String, Object> userRoleDOEntity = new HashMap<>();
        userRoleDOEntity.put("user_id", privilageUserRoleDO.getUserId());
        userRoleDOEntity.put("role_id", privilageUserRoleDO.getRoleId());
        List<PrivilageUserRoleDO> userRoleDOList = privilageUserRoleDOMapper.selectByMap(userRoleDOEntity);
        if (userRoleDOList.size() == 0) {
//            System.out.println("找不到用户角色" + ArrayUtils.toString(arr));
            privilageUserRoleDOMapper.insert(privilageUserRoleDO);
        } else {
//            PrivilageUserRoleDO role = userRoleDOList.get(0);
//            role.setUserId(privilageUserRoleDO.getUserId());
//            role.setRoleId(privilageUserRoleDO.getRoleId());
//            role.setUpdateTime(new Date());
//            role.setUpdateBy("蒋老师");
//            privilageUserRoleDOMapper.updateById(role);

        }

        addAccount(arr, phone, us.getId());

    }

    private void addAccount(String[] arr, String phone, Long userId) {
        //创建账号,密码为账号加123
        PrivilageAccountDO acc = new PrivilageAccountDO();
        acc.setLogin(phone);
        acc.setPassword(MD5FileUtil.getMD5String(phone + "123"));
        acc.setUserId(userId);
        acc.setType(1);
        acc.setStatus(1);
        acc.setCreateTime(new Date());
        acc.setUpdateTime(new Date());
        acc.setUpdateBy("蒋老师");

        Map<String, Object> accountMap = new HashMap<>();
        accountMap.put("login", acc.getLogin());
//        accountMap.put("user_id", acc.getUserId());
        accountMap.put("type", acc.getType());
        List<PrivilageAccountDO> accountList = privilageAccountDOMapper.selectByMap(accountMap);
        if (accountList.size() == 0) {
            System.out.println("找不到账号" + ArrayUtils.toString(arr));
            privilageAccountDOMapper.insert(acc);
        } else {
            PrivilageAccountDO accountEntity = accountList.get(0);
            accountEntity.setLogin(acc.getLogin());
            accountEntity.setUserId(acc.getUserId());
            accountEntity.setType(acc.getType());
            accountEntity.setUpdateTime(new Date());
            accountEntity.setUpdateBy("蒋老师");
            privilageAccountDOMapper.updateById(accountEntity);
        }
    }

    private void addStoreStaffItem(String[] arr) throws Exception {
        String regionName = arr[0];
        // 零售公司编码
        String companyCode = arr[1];
        // 零售公司名称
        String companyName = arr[2];
        // 客户组织编码
        String customerCode = arr[3];
        // 客户组织名称
        String customerName = arr[4];
        // 店铺编码
        String storeName = arr[5];
        // 店铺名称
        String storeCode = arr[6];
        // 企业微信账号
        String account = arr[7];
        // 企业微信名称
        String name = arr[8];
        // 别名
        String abName = arr[9];
        // 手机号码
        String phone = arr[10];

        Map<String, Object> storeMap = new HashMap<>();
        storeMap.put("code", storeCode);
        List<PoiStore> storeDOList = poiStoreDOMapper.selectByMap(storeMap);
        if (storeDOList.size() > 0) {
            PoiStore storeDO = storeDOList.get(0);
            Map<String, Object> storeStaffMap = new HashMap<>();
            storeStaffMap.put("store_code", storeDO.getCode());


//            storeStaffMap.put("staff_code", phone);
            List<PoiStoreStaff> storeStaffDO = poiStoreStaffDOMapper.selectByMap(storeStaffMap);
            if (storeStaffDO.size() > 0) {
                PoiStoreStaff poiStoreStaff = storeStaffDO.get(0);
            } else {
//                System.out.println("没有对应店铺人员表" + ArrayUtils.toString(arr));
                PoiStoreStaffVO poiStoreStaffVO = new PoiStoreStaffVO();
                poiStoreStaffVO.setName(name);
                poiStoreStaffVO.setMobil(phone);
                poiStoreStaffVO.setStaffCode(account);
                poiStoreStaffVO.setStoreId(storeDO.getId());
                poiStoreStaffVO.setStoreCode(storeDO.getCode());
                poiStoreStaffVO.setRole("DZ-DIANZHANG");
                poiStoreStaffVO.setUserId(0L);

                QueryWrapper<PrivilageUserDO> mobileUserQw = new QueryWrapper<>();
                mobileUserQw.eq("mobil", phone).eq("status", "1");
                List<PrivilageUserDO> mobileUsers = privilageUserDOMapper.selectList(mobileUserQw);

                PrivilageUserDO privilageUserDO;
                if (mobileUsers.size() > 0) {
                    privilageUserDO = mobileUsers.get(0);
                } else {
//                    System.out.println("没有对应用户" + ArrayUtils.toString(arr));

                    PrivilageUserDO userDOEntity = new PrivilageUserDO();
                    userDOEntity.setMobil(phone);
                    userDOEntity.setName(name);
                    userDOEntity.setAbbreviation(abName);
                    userDOEntity.setStatus(1);
                    userDOEntity.setCreateTime(new Date());
                    userDOEntity.setCreateBy("蒋老师");
                    privilageUserDOMapper.insert(userDOEntity);

                    privilageUserDO = userDOEntity;
                }

                poiStoreStaffVO.setUserId(privilageUserDO.getId());
                QueryWrapper<PrivilageAccountDO> loginUserQw = new QueryWrapper<>();
                loginUserQw.eq("login", phone).eq("type", "1").eq("status", "1");
                List<PrivilageAccountDO> loginUsers = privilageAccountDOMapper.selectList(loginUserQw);
                if (CollectionUtils.isEmpty(loginUsers)) {
//                        System.out.println("没有对应账号" + ArrayUtils.toString(arr));

                    PrivilageAccountDO loginDOEntity = new PrivilageAccountDO();
                    loginDOEntity.setLogin(phone);
                    loginDOEntity.setType(1);
                    loginDOEntity.setStatus(1);
                    loginDOEntity.setPassword(MD5FileUtil.getMD5String(phone + "123"));
                    loginDOEntity.setUserId(privilageUserDO.getId());
                    loginDOEntity.setCreateTime(new Date());
                    loginDOEntity.setUpdateTime(new Date());
                    privilageAccountDOMapper.insert(loginDOEntity);

                    //不管什么类型都要绑定角色
                    PrivilageUserRoleDO privilageUserRoleDO = new PrivilageUserRoleDO();
                    privilageUserRoleDO.setUserId(privilageUserDO.getId());
                    // 店长
                    privilageUserRoleDO.setRoleId(4L);
                    privilageUserRoleDO.setCreateTime(new Date());
                    privilageUserRoleDO.setUpdateTime(new Date());


                    Map<String, Object> map = new HashMap<>();
                    map.put("user_id", privilageUserRoleDO.getUserId());
                    map.put("role_id", privilageUserRoleDO.getRoleId());
                    List<PrivilageUserRoleDO> list = privilageUserRoleDOMapper.selectByMap(map);
                    if (CollectionUtils.isEmpty(list)) {
//                            System.out.println("没有对应用户角色关系" + ArrayUtils.toString(arr));

                        PrivilageUserRoleDO userRoleDOEntity = new PrivilageUserRoleDO();
                        userRoleDOEntity.setUserId(privilageUserRoleDO.getUserId());
                        userRoleDOEntity.setRoleId(privilageUserRoleDO.getRoleId());
                        userRoleDOEntity.setCreateTime(new Date());
                        userRoleDOEntity.setCreateBy("蒋老师");
                        privilageUserRoleDOMapper.insert(userRoleDOEntity);
                    } else {
                    }
                }


                Map<String, Object> cpUserMap = new HashMap<>();
                cpUserMap.put("cp_user_id", phone);
                List<PrivilageCpUserDO> cpUserDOList = privilageCpUserDOMapper.selectByMap(cpUserMap);
                if (cpUserDOList.size() > 0) {
                    Map<String, Object> cpUserStoreMap = new HashMap<>();
                    cpUserStoreMap.put("cp_user_id", cpUserDOList.get(0).getId());
                    cpUserStoreMap.put("store_id", storeDO.getId());
                    List<PrivilageCpUserStoreDO> cpUserStoreDOList = privilageCpUserStoreDOMapper.selectByMap(cpUserStoreMap);
                    if (cpUserStoreDOList.size() > 0) {
                        PrivilageCpUserStoreDO cpUserStoreDO = cpUserStoreDOList.get(0);
                    } else {
//                            System.out.println("没有对应店铺店长关系" + ArrayUtils.toString(arr));
                        PrivilageCpUserStoreDO cpUserStoreDOEntity = new PrivilageCpUserStoreDO();
                        cpUserStoreDOEntity.setStoreId(storeDO.getId());
                        cpUserStoreDOEntity.setCpUserId(cpUserDOList.get(0).getId());
                        privilageCpUserStoreDOMapper.insert(cpUserStoreDOEntity);
                    }
                } else {
//                        System.out.println("没有对应店铺店长" + ArrayUtils.toString(arr));

                    PrivilageCpUserDO cpUserDOEntity = new PrivilageCpUserDO();
                    cpUserDOEntity.setName(name);
                    cpUserDOEntity.setAlias(abName);
                    cpUserDOEntity.setMobile(phone);
                    cpUserDOEntity.setStatus(1);
                    // 门店号
                    String qrCode = storeEmployeeService.getQrCodeFromBSD("mayifei", BSD_REQ_QRCODE_TYPE_STORE, phone, poiStoreStaffVO.getStoreCode());
                    if (StringUtils.isNotBlank(qrCode)) {
                        cpUserDOEntity.setQrCode(qrCode);
                        privilageCpUserDOMapper.insert(cpUserDOEntity);

                        Map<String, Object> cpUserStoreMap = new HashMap<>();
                        cpUserStoreMap.put("cp_user_id", cpUserDOEntity.getId());
                        cpUserStoreMap.put("store_id", storeDO.getId());
                        List<PrivilageCpUserStoreDO> cpUserStoreDOList = privilageCpUserStoreDOMapper.selectByMap(cpUserStoreMap);
                        if (cpUserStoreDOList.size() > 0) {
                            PrivilageCpUserStoreDO cpUserStoreDO = cpUserStoreDOList.get(0);
                        } else {
//                                System.out.println("没有对应店铺与店长的关系" + ArrayUtils.toString(arr));
                            PrivilageCpUserStoreDO cpUserStoreDOEntity = new PrivilageCpUserStoreDO();
                            cpUserStoreDOEntity.setStoreId(storeDO.getId());
                            cpUserStoreDOEntity.setCpUserId(cpUserDOEntity.getId());
                            privilageCpUserStoreDOMapper.insert(cpUserStoreDOEntity);
                        }
                    } else {
                        System.out.println("店长获取二维码错误" + ArrayUtils.toString(arr));
                    }
                }

            }
        } else {
            System.out.println("没有对应店铺" + ArrayUtils.toString(arr));
        }
    }

    @Test
    public void deleteStoreByFile() throws IOException {
        List<String> storeList = FileUtils.readLines(new File("file/new_store5.txt"));

        for (int i = 0; i < storeList.size(); i++) {
            String item = storeList.get(i);
            String[] arr = item.split(",");

            String storeId = arr[0];
            String storeName = arr[1];
            String storeCode = arr[2];

            // 店长账号

            PoiStore pq = new PoiStore();
            pq.setId(Long.parseLong(storeId));
            pq = poiStoreDOMapper.selectOne(new QueryWrapper<>(pq));
            if(null != pq){

                // 查询出门店人员
                Map<String, Object> poiStoreStaffDOMap = new HashMap<>();
                poiStoreStaffDOMap.put("store_id", pq.getId());
                List<PoiStoreStaff> staffList = poiStoreStaffDOMapper.selectByMap(poiStoreStaffDOMap);

                // 删除门店
                int re = poiStoreDOMapper.deleteById(Long.parseLong(storeId));
                if(re == 0){
                    System.out.println("失败");
                }
                if(staffList.size() > 1){
                    System.out.println("门店人员有多个" + pq.getId() + " | " + pq.getCode() + "|" + pq.getName());
                    continue;
                }

                if(staffList.size() == 0){
                    continue;
                }

                PoiStoreStaff staffDO = staffList.get(0);

                // 删除店长号
                Map<String, Object> cpUserDOMap = new HashMap<>();
                cpUserDOMap.put("cp_user_id", staffDO.getStaffCode());
                privilageCpUserDOMapper.deleteByMap(cpUserDOMap);

                // 删除用户
                privilageUserDOMapper.deleteById(staffDO.getUserId());

                // 删除用户角色关系
                Map<String, Object> userRoleDOMap = new HashMap<>();
                userRoleDOMap.put("user_id", staffDO.getUserId());
                privilageUserRoleDOMapper.deleteByMap(userRoleDOMap);

                // 删除账号
                privilageAccountDOMapper.deleteByUserId(staffDO.getUserId());

            }

            // 删除门店店长
            Map<String, Object> poiStoreStaffDOMap = new HashMap<>();
            poiStoreStaffDOMap.put("store_id", storeId);
            poiStoreStaffDOMapper.deleteByMap(poiStoreStaffDOMap);

            // 删除店长号和门店的关系
            Map<String, Object> cpUserStoreDOMap = new HashMap<>();
            cpUserStoreDOMap.put("store_id", storeId);
            privilageCpUserStoreDOMapper.deleteByMap(cpUserStoreDOMap);


            // 删除门店和域的关系
            Map<String, Object> domainEntityDOMap = new HashMap<>();
            domainEntityDOMap.put("entity_id", storeId);
            domainEntityDOMap.put("type", 3);
            privilageDomainEntityDOMapper.deleteByMap(domainEntityDOMap);

        }
    }

    private void deleteStore(String[] arr) {
        String regionName = arr[0];
        // 零售公司编码
        String companyCode = arr[1];
        // 零售公司名称
        String companyName = arr[2];
        // 客户组织编码
        String customerCode = arr[3];
        // 客户组织名称
        String customerName = arr[4];
        // 店铺编码
        String storeName = arr[5];
        // 店铺名称
        String storeCode = arr[6];

        String account = arr[7];
        String phone = arr[10];

        // 删除店铺
        Map<String, Object> storeMap = new HashMap<>();
        storeMap.put("code", storeCode);
        List<PoiStore> storeDOList = poiStoreDOMapper.selectByMap(storeMap);
        if (storeDOList.size() > 0) {
            PoiStore storeDO = storeDOList.get(0);
//            poiStoreDOMapper.deleteById(storeDO.getId());
            // 删除店铺下得人员
            Map<String, Object> cpUserMap = new HashMap<>();
            cpUserMap.put("cp_user_id", phone);
            List<PrivilageCpUserDO> cpUserDOList = privilageCpUserDOMapper.selectByMap(cpUserMap);
            if (cpUserDOList.size() > 0) {

//                privilageCpUserDOMapper.deleteById(cpUserDOList.get(0).getId());
                Map<String, Object> cpUserStoreMap = new HashMap<>();
                cpUserStoreMap.put("cp_user_id", cpUserDOList.get(0).getId());
                cpUserStoreMap.put("store_id", storeDO.getId());
                List<PrivilageCpUserStoreDO> cpUserStoreDOList = privilageCpUserStoreDOMapper.selectByMap(cpUserStoreMap);
                if (cpUserStoreDOList.size() > 0) {
                    PrivilageCpUserStoreDO cpUserStoreDO = cpUserStoreDOList.get(0);
//                    privilageCpUserStoreDOMapper.deleteById(cpUserStoreDO.getId());
                } else {
                    System.out.println("没有对应店铺店长关系" + ArrayUtils.toString(arr));
                }
            } else {
                System.out.println("没有对应店铺店长" + ArrayUtils.toString(arr));
            }
        } else {
            System.out.println("没有对应店铺" + ArrayUtils.toString(arr));
        }
    }


    /**
     * 修改店长名称和别名
     * @throws IOException
     */
    @Test
    public void modifyStoreDOByName() throws IOException {

        List<String> list = FileUtils.readLines(new File("check/file_1.txt"));

        for (int i = 0; i < list.size(); i++) {
            String item = list.get(i);
            String[] arr = item.split(",");

            String regionName = arr[0].trim();
            // 零售公司编码
            String companyCode = arr[1].trim();
            // 零售公司名称
            String companyName = arr[2].trim();
            // 客户组织编码
            String customerCode = arr[3].trim();
            // 客户组织名称
            String customerName = arr[4].trim();
            // 店铺编码
            String storeName = arr[5].trim();
            // 店铺名称
            String storeCode = arr[6].trim();
            // 企业微信账号
            String account = arr[7].trim();
            // 企业微信名称
            String name = arr[8].trim();
            // 别名
            String abName = arr[9].trim();
            // 手机号码
            String phone = arr[10].trim();
            //如果是店长，需要绑定店铺cp
            //排重---根据企业微信账号查询是否存在用户
            PrivilageCpUserDO privilageCpUserDO = new PrivilageCpUserDO();
            privilageCpUserDO.setCpUserId(account);
            privilageCpUserDO.setStatus(1);
            privilageCpUserDO.setMobile(phone);
            privilageCpUserDO.setName(name);
            privilageCpUserDO.setAlias(abName);

            Map<String, Object> map = new HashMap<>();
            //  根据原来的 客户ID 找到
            map.put("cp_user_id", phone);
            map.put("status", privilageCpUserDO.getStatus());
            List<PrivilageCpUserDO> cpUserDOList = privilageCpUserDOMapper.selectByMap(map);
            if (cpUserDOList.size() == 0) {
                System.out.println("找不到店长表" + ArrayUtils.toString(arr));
                continue;
            }

            PrivilageCpUserDO cpuserdo = cpUserDOList.get(0);
            cpuserdo.setCpUserId(privilageCpUserDO.getCpUserId());
            cpuserdo.setStatus(privilageCpUserDO.getStatus());
            cpuserdo.setName(name);
            cpuserdo.setAlias(abName);
            cpuserdo.setMobile(phone);
            privilageCpUserDOMapper.updateById(cpuserdo);

            // 根据原来的手机号码查询
//            Map<String, Object> userDOMap = new HashMap<>();
//            userDOMap.put("mobil", phone);
//            List<PrivilageUserDO> userDOlist = privilageUserDOMapper.selectByMap(userDOMap);
//
//            if(userDOlist.size() == 0){
//                System.out.println("找不到用户表" + ArrayUtils.toString(arr));
//                continue;
//            }
//
//            PrivilageUserDO userDO = userDOlist.get(0);
//            userDO.setMobil(phone);
//            privilageUserDOMapper.updateById(userDO);

        }



    }

    @Test
    public void check1() throws IOException {
        // 系统
        List<String> list = FileUtils.readLines(new File("check/cc_1.txt"));
        // excel
        List<String> list2 = FileUtils.readLines(new File("check/check_2.txt"));

        Map<String,String> list2Map = new HashMap<>();
        for (int i = 0; i < list2.size(); i++) {
            String item = list2.get(i);
            String[] arr = item.split(",");
            String key = arr[2] + "_" + arr[1];
            if(!list2Map.containsKey(key)){
                list2Map.put(key, item);
            }
        }

        for (int i = 0; i < list.size(); i++) {
            String item = list.get(i);
            String[] arr = item.split(",");
            String key = arr[4] + "_" + arr[2];
            String itemDO = list2Map.get(key);
            if(StringUtils.isNotBlank(itemDO)){

            }
        }
    }

    @Test
    public void addStoreStaff2() throws Exception {
        List<String> list2 = FileUtils.readLines(new File("check/check_2.txt"));

        Map<String,String> stringMap = new HashMap<>();
        for (int i = 0; i < list2.size(); i++) {
            String item = list2.get(i);
            String[] arr = item.split(",");
            String key = arr[1];
            if(!stringMap.containsKey(key)){
                stringMap.put(key, item);
            }
        }

        List<PoiStore> list = poiStoreDOMapper.selectList(null);

        File file = new File("check/bb.txt");

        for (int i = 0; i < list.size(); i++) {
            PoiStore item = list.get(i);

            Map<String, Object> staffDOMap = new HashMap<>();
            staffDOMap.put("store_id", item.getId());
            List<PoiStoreStaff> staffDOList = poiStoreStaffDOMapper.selectByMap(staffDOMap);
            if(CollectionUtils.isEmpty(staffDOList)){

                String key = item.getCode();
                String itemMap = stringMap.get(key);
                if(StringUtils.isBlank(itemMap)){
                    System.out.println("bb" + item.toString());
                    FileUtils.write(file, itemMap+"\n", true);
                    continue;
                }

                String[] itemArr = itemMap.split(",");


//                System.out.println(item.toString());

            }
        }

    }


    @Test
    public void update1(){

        String city = "武汉";
        Map<String, Object> map = new HashMap<>();
        map.put("name", city);//西安 南昌 乌鲁木齐 武汉
        map.put("level", 2);
        List<PrivilageOrganizational> companyList = retailCompanyMapper.selectByMap(map);
        if(CollectionUtils.isEmpty(companyList)){
            return;
        }

        PrivilageOrganizational oldCompany = companyList.get(0);

        Map<String, Object> map2 = new HashMap<>();
        map2.put("name", city + "波司登贸易有限公司");//西安 南昌 乌鲁木齐 武汉
        map2.put("level", 2);
        List<PrivilageOrganizational> companyList2 = retailCompanyMapper.selectByMap(map2);
        PrivilageOrganizational companyDO = companyList2.get(0);

        Map<String, Object> map3 = new HashMap<>();
        map3.put("parent_id", oldCompany.getId());//西安 南昌 乌鲁木齐 武汉
        map3.put("level", 3);
        List<PrivilageOrganizational> customerList = retailCompanyMapper.selectByMap(map3);
        if(CollectionUtils.isEmpty(customerList)){
            return;
        }

        for (int i = 0; i < customerList.size(); i++) {
            PrivilageOrganizational item = customerList.get(i);
            item.setParentId(companyDO.getId());
            retailCompanyMapper.updateById(item);
        }
    }
}
