package com.matrix.system.dataMove; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.util.StrUtil; import com.alibaba.fastjson.JSONObject; import com.baomidou.dynamic.datasource.annotation.DS; import com.matrix.component.redis.RedisClient; import com.matrix.core.tools.StringUtils; import com.matrix.system.common.bean.SysUsers; import com.matrix.system.common.constance.AppConstance; import com.matrix.system.common.dao.SysUsersDao; import com.matrix.system.common.tools.PasswordUtil; import com.matrix.system.hive.bean.*; import com.matrix.system.hive.dao.*; import com.matrix.system.hive.service.SysVipInfoService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.io.UnsupportedEncodingException; import java.security.NoSuchAlgorithmException; import java.util.*; /** * @author wzy * @date 2021-03-18 **/ @Service public class DateMoveServiceImpl { @Autowired private JdbcTemplate jdbcTemplate; @Autowired private SysShopInfoDao sysShopInfoDao; @Autowired private SysUsersDao sysUsersDao; @Autowired private SysVipLevelDao sysVipLevelDao; @Autowired private SysVipInfoDao sysVipInfoDao; @Autowired private SysVipInfoService sysVipInfoService; @Autowired private SysSupplierTypeDao sysSupplierTypeDao; @Autowired private SysSupplierInfoDao sysSupplierInfoDao; @Autowired private ShoppingGoodsCategoryDao shoppingGoodsCategoryDao; @Autowired private RedisClient redisClient; private Long companyId = 35L; private Long shopId = 38L; public List> dataMove() { return jdbcTemplate.queryForList("select * from sys_vip_info limit 10"); } public List mapsToListBean(List> maps, Class t) { List list = new ArrayList<>(); for (Map map : maps) { String s = JSONObject.toJSONString(map); T object = JSONObject.parseObject(s, t); list.add(object); } return list; } /** * 旧系统商店数据 * * @return */ @DS("slave") public List oldShopInfo() { List> maps = jdbcTemplate.queryForList("select * from sys_shop_info"); List shopInfos = new ArrayList<>(); for (Map map : maps) { String s = JSONObject.toJSONString(map); SysShopInfo sysShopInfo = JSONObject.parseObject(s, SysShopInfo.class); shopInfos.add(sysShopInfo); } return shopInfos; } public void toNewShopInfo(List shopInfos) { if (CollUtil.isNotEmpty(shopInfos)) { Map oldAndNewId = new HashMap<>(); for (SysShopInfo shopInfo : shopInfos) { if (shopInfo.getShopName().equals("总部")) { shopInfo.setShopType(1); shopInfo.setIsOpenNet(2); } else { shopInfo.setShopType(2); shopInfo.setIsOpenNet(1); } shopInfo.setShopShortName(shopInfo.getShopName()); shopInfo.setCompanyId(companyId); SysShopInfo query = new SysShopInfo(); query.setShopNo(shopInfo.getShopNo()); Long oldId = shopInfo.getId(); shopInfo.setId(null); // TODO 若迁往正式环境,这里需要变动 if (!shopInfo.getShopName().contains("美度") && !shopInfo.getShopName().contains("总店")) { sysShopInfoDao.insert(shopInfo); oldAndNewId.put(oldId, shopInfo.getId()); } else { SysShopInfo sysShopInfo = sysShopInfoDao.selectByShopName(shopInfo.getShopName()); oldAndNewId.put(oldId, sysShopInfo.getId()); } } redisClient.saveMapValue("shopId", oldAndNewId); } } @DS("slave") public List oldSysUsers() throws UnsupportedEncodingException, NoSuchAlgorithmException { List> maps = jdbcTemplate.queryForList("select * from users"); List list = new ArrayList<>(); String shopIds = redisClient.getCachedValue("shopId"); Map shopMap = JSONObject.parseObject(shopIds, Map.class); for (Map map : maps) { SysUsers sysUsers = new SysUsers(); sysUsers.setSuAccount((String) map.get("account")); sysUsers.setSuId((Long) map.get("id")); sysUsers.setSuName((String) map.get("name")); sysUsers.setSuTel((String) map.get("tel")); sysUsers.setSuPhoto((String) map.get("photo")); sysUsers.setSuEmail((String) map.get("email")); sysUsers.setSuUserType((String) map.get("user_type")); sysUsers.setSuAccountStatus((String) map.get("account_status")); sysUsers.setSuValid("Y"); sysUsers.setCompanyId(companyId); Long aaa = (Long) map.get("shop_id"); System.out.println(aaa); if (aaa != null) { Integer shopId = (Integer) shopMap.get(aaa.toString()); if (shopId != null) { sysUsers.setShopId(Long.parseLong(shopId.toString())); } } sysUsers.setSuRegisterTime((Date) map.get("register_time")); sysUsers.setCreateBy(AppConstance.SYSTEM_USER); sysUsers.setUpdateBy(AppConstance.SYSTEM_USER); sysUsers.setSuPassword(PasswordUtil.getEncrypUserPwd(sysUsers)); list.add(sysUsers); } return list; } @Transactional(rollbackFor = Exception.class) public void newSysUsers(List list) { if (CollUtil.isNotEmpty(list)) { Map sysUsersOldAndNew = new HashMap<>(); for (SysUsers sysUsers : list) { Long oldId = sysUsers.getSuId(); sysUsers.setSuId(null); sysUsersDao.insert(sysUsers); sysUsersOldAndNew.put(oldId, sysUsers.getSuId()); } redisClient.saveMapValue("sys_users", sysUsersOldAndNew); } } @DS("slave") public List vipLevelOld() { List> maps = jdbcTemplate.queryForList("select * from sys_vip_level"); return mapsToListBean(maps, SysVipLevel.class); } @Transactional(rollbackFor = Exception.class) public void vipLevelNew(List list) { if (CollUtil.isNotEmpty(list)) { Map ids = new HashMap<>(); for (SysVipLevel sysVipLevel : list) { Long oldId = sysVipLevel.getId(); sysVipLevel.setId(null); sysVipLevel.setCompanyId(companyId); sysVipLevel.setShopId(38L); sysVipLevelDao.insert(sysVipLevel); ids.put(oldId, sysVipLevel.getId()); } redisClient.saveMapValue("vip_level", ids); } } @DS("slave") public List vipInfoOld() { List> maps = jdbcTemplate.queryForList("select * from sys_vip_info"); return mapsToListBean(maps, SysVipInfo.class); } @Transactional(rollbackFor = Exception.class) public void vipInfoNew(List list) { if (CollUtil.isNotEmpty(list)) { Map vipInfoIds = new HashMap<>(); String shopIds = redisClient.getCachedValue("shopId"); Map shopMap = JSONObject.parseObject(shopIds, Map.class); String sysUsersIds = redisClient.getCachedValue("sys_users"); Map sysUsersMap = JSONObject.parseObject(sysUsersIds, Map.class); String vipLevelIds = redisClient.getCachedValue("vip_level"); Map vipLevelIdsMap = JSONObject.parseObject(vipLevelIds, Map.class); for (SysVipInfo sysVipInfo : list) { Long shopId = sysVipInfo.getShopId(); if (shopId != null) { Object newId = shopMap.get(shopId.toString()); if (newId != null) { sysVipInfo.setShopId(Long.parseLong(newId.toString())); } } Long staffId = sysVipInfo.getStaffId(); if (staffId != null) { Object o = sysUsersMap.get(staffId.toString()); if (o != null) { sysVipInfo.setStaffId(Long.parseLong(o.toString())); } } Long levelId = sysVipInfo.getLevelId(); if (levelId != null) { Object o = vipLevelIdsMap.get(levelId.toString()); if (o != null) { sysVipInfo.setLevelId(Long.parseLong(o.toString())); } } sysVipInfo.setCompanyId(companyId); Long oldId = sysVipInfo.getId(); sysVipInfo.setId(null); if (StrUtil.isNotBlank(sysVipInfo.getVipName())) { sysVipInfo.setZjm(StringUtils.toHanyuPinyin(sysVipInfo.getVipName()) + "," + StringUtils.toHeadWordHanyuPinyin(sysVipInfo.getVipName())); } sysVipInfoDao.insert(sysVipInfo); vipInfoIds.put(oldId, sysVipInfo.getId()); sysVipInfoService.addVipDefaultCard(sysVipInfo.getId()); } } } @DS("slave") public List supplierTypeOld() { List> maps = jdbcTemplate.queryForList("select * from sys_supplier_type"); return mapsToListBean(maps, SysSupplierType.class); } @Transactional(rollbackFor = Exception.class) public void supplierNew(List list) { if (CollUtil.isNotEmpty(list)) { Map maps = new HashMap<>(); for (SysSupplierType sysSupplierType : list) { sysSupplierType.setShopId(shopId); sysSupplierType.setCompanyId(companyId); Long oldId = sysSupplierType.getId(); sysSupplierType.setId(null); sysSupplierTypeDao.insert(sysSupplierType); maps.put(oldId, sysSupplierType.getId()); } redisClient.saveMapValue("supplier_type", maps); } } @DS("slave") public List supplierInfoOld() { List> maps = jdbcTemplate.queryForList("select * from sys_supplier_info"); return mapsToListBean(maps, SysSupplierInfo.class); } @Transactional(rollbackFor = Exception.class) public void supplierInfoNew(List list) { if (CollUtil.isNotEmpty(list)) { Map maps = new HashMap<>(); String supplierTypeIds = redisClient.getCachedValue("supplier_type"); Map supplierTypeMap = JSONObject.parseObject(supplierTypeIds, Map.class); for (SysSupplierInfo sysSupplierInfo : list) { sysSupplierInfo.setShopId(shopId); sysSupplierInfo.setCompanyId(companyId); Object o = supplierTypeMap.get(sysSupplierInfo.getSupplType().toString()); if (o != null) { sysSupplierInfo.setSupplType(Long.parseLong(o.toString())); } Long oldId = sysSupplierInfo.getId(); sysSupplierInfo.setId(null); sysSupplierInfo.setSupplNo("MD" + sysSupplierInfo.getSupplNo()); sysSupplierInfoDao.insert(sysSupplierInfo); maps.put(oldId, sysSupplierInfo.getId()); } redisClient.saveMapValue("supplier_info", maps); } } @DS("slave") public List goodsCategoryOld() { String sql = "select * from shopping_goods_category"; List> maps = jdbcTemplate.queryForList(sql); return mapsToListBean(maps, ShoppingGoodsCategory.class); } @Transactional(rollbackFor = Exception.class) public void goodsCategoryNew(List list) { if (CollUtil.isNotEmpty(list)) { Map map = new HashMap<>(); for (ShoppingGoodsCategory shoppingGoodsCategory : list) { shoppingGoodsCategory.setShopId(shopId); shoppingGoodsCategory.setCompanyId(companyId); Long oldId = shoppingGoodsCategory.getId(); shoppingGoodsCategory.setId(null); shoppingGoodsCategoryDao.insert(shoppingGoodsCategory); map.put(oldId, shoppingGoodsCategory.getId()); } redisClient.saveMapValue("goods_category", map); } } @Transactional(rollbackFor = Exception.class) public void goodsCategoryParentIdUpdate() { String goodsCategoryIds = redisClient.getCachedValue("goods_category"); Map maps = JSONObject.parseObject(goodsCategoryIds, HashMap.class); for (Map.Entry entry : maps.entrySet()) { String sql = "update shopping_goods_category set parent_id=" + entry.getValue().toString() + " where parent_id="+ entry.getKey() +" and company_id=35"; jdbcTemplate.update(sql); } } @DS("slave") public List shoppingGoodsOld() { List> maps = jdbcTemplate.queryForList("select * from shopping_goods"); List shoppingGoods = mapsToListBean(maps, ShoppingGoods.class); return shoppingGoods; } public void shoppingGoodsNew() {} }