package init; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.*; import java.util.Map.Entry; import com.alibaba.fastjson.JSON; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.kiisoo.ic.app.IcApplication; import com.kiisoo.ic.store.entity.PoiStore; import com.kiisoo.ic.store.entity.PoiStoreStaff; import com.kiisoo.ic.store.mapper.PoiStoreDOMapper; import com.kiisoo.ic.store.mapper.PoiStoreStaffDOMapper; import com.kiisoo.ic.store.service.PoiStoreStaffService; import com.kiisoo.ic.system.entity.PrivilageUserDO; import com.kiisoo.ic.system.mapper.PrivilageUserDOMapper; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.DateUtil; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.stereotype.Service; import org.springframework.test.context.junit4.SpringRunner; /** * 组织数据初始化 * Created by hc on 2020/4/21 */ @RunWith(SpringRunner.class) @SpringBootTest(classes = IcApplication.class) public class OrganizationInit { static String sellerPath = "D:\\Backup\\桌面\\各种客户端\\bsd\\智慧导购\\初始人员数据20200422.xlsx"; static String[] sellerColumns= {"店铺","店铺编码","userid","姓名","账号","别名","职务","性别","手机","座机","个人邮箱","地址"}; static String filePath = null; static String columns[] = null; static Integer col = null; static Integer maxRow = null; //seller static String dataType = null; //初始化行列数据 static { //文件路径 filePath = sellerPath; //对应文件所取列名 columns = sellerColumns; //需要取的行数,若全部则置零 maxRow = 0; dataType = "seller"; } @Autowired private PoiStoreStaffDOMapper poiStoreStaffDOMapper; @Autowired private PoiStoreDOMapper poiStoreDOMapper; @Autowired private PrivilageUserDOMapper privilageUserDOMapper; /** * 入库 */ private void inDB() { if(dataType.equals("seller")){ // System.out.println(sellerMap.size()); inDBSeller(); } } //获取字符串转换实体 public void getEntity(String json){ //字符串转实体 UserInitDO u = JSON.parseObject(json, UserInitDO.class); duplicateRemovalSeller(u); } //导购去重map private Map sellerMap = new HashMap<>(); //去重导购 public void duplicateRemovalSeller(UserInitDO u){ sellerMap.put(u.getShopCode()+u.getAccount(), u); } //导购入库 public void inDBSeller(){ sellerMap.forEach((k,v) -> { String sellerCode = v.getAccount() == null ? v.getPhone() : v.getAccount(); String phone = v.getPhone() == null ? v.getAccount() : v.getPhone(); String shopCode = v.getShopCode(); long shopId = 0; if(sellerCode == null || phone == null){ System.out.println("无法找到唯一标示异常"); } //查看店铺 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(v.getName()); us.setMobil(phone); us.setEmail(v.getEmail()); us.setAddress(v.getAdress()); us.setStatus(1); us.setCreateTime(new Date()); us.setUpdateTime(new Date()); us.setAbbreviation(v.getAlias()); try{ privilageUserDOMapper.insert(us); }catch (Exception e){ us.setAbbreviation(null); privilageUserDOMapper.insert(us); } } //增加导购 PoiStoreStaff p = new PoiStoreStaff(); p.setStoreId(shopId); p.setUserId(us.getId()); p.setStoreCode(v.getShopCode()); p.setStaffCode(sellerCode); p.setType(getType(v.getPosition())); p.setStatus(1l); p.setCreateTime(new Date()); p.setUpdateTime(new Date()); if(v.getCustomerUserId() != null && !v.getCustomerUserId().equals("mal")){ p.setCustomerUserId(Long.parseLong(v.getCustomerUserId())); } poiStoreStaffDOMapper.insert(p); }); } public long getType(String position){ //零售经理, 店长, 信息主管, 零售主管, 店助, 导购, 店员, 已离职, PO //1 店长 2 副店长 3 店长助理 4 导购 5零售经理 6信息主管 7店员 8PO 9零售主管 10已离职 11未定义 long type = 0; if(position == null){ type = 11; }else if(position.equals("零售经理")) { type = 5; }else if(position.equals("店长")){ type = 1; }else if(position.equals("信息主管")){ type = 6; }else if(position.equals("零售主管")){ type = 9; }else if(position.equals("店助")){ type = 3; }else if(position.equals("导购")){ type = 4; }else if(position.equals("店员")){ type = 7; }else if(position.equals("已离职")){ type = 10; }else if(position.contains("PO")){ type = 8; } return type; } static { col = columns.length; } @Test public void initData() { Workbook wb =null; Sheet sheet = null; Row row = null; List> list = null; String cellData = null; wb = readExcel(filePath); if(wb != null){ //用来存放表中数据 list = new ArrayList>(); //获取第一个sheet sheet = wb.getSheetAt(0); //获取最大行数 int rownum = sheet.getPhysicalNumberOfRows(); //获取第一行 row = sheet.getRow(0); //获取最大列数 int colnum = col; for (int i = 1; i map = new LinkedHashMap(); row = sheet.getRow(i); if(row !=null){ for (int j=0;j map : list) { StringBuffer sb = new StringBuffer(); sb.append("{"); for (Entry entry : map.entrySet()) { sb.append("\""); sb.append(entry.getKey()); sb.append("\":\""); sb.append(entry.getValue()+"\","); } sb.deleteCharAt(sb.length() - 1); sb.append("}"); getEntity(sb.toString()); i ++; if(maxRow == i){ break; } } inDB(); System.out.println("OK"); } //读取excel public static Workbook readExcel(String filePath){ Workbook wb = null; if(filePath==null){ return null; } String extString = filePath.substring(filePath.lastIndexOf(".")); InputStream is = null; try { is = new FileInputStream(filePath); if(".xls".equals(extString)){ return wb = new HSSFWorkbook(is); }else if(".xlsx".equals(extString)){ return wb = new XSSFWorkbook(is); }else{ return wb = null; } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return wb; } public static Object getCellFormatValue(Cell cell){ Object cellValue = null; if(cell!=null){ //判断cell类型 switch(cell.getCellType()){ case Cell.CELL_TYPE_NUMERIC:{ cellValue = String.valueOf(cell.getNumericCellValue()); break; } case Cell.CELL_TYPE_FORMULA:{ //判断cell是否为日期格式 if(DateUtil.isCellDateFormatted(cell)){ //转换为日期格式YYYY-mm-dd cellValue = cell.getDateCellValue(); }else{ //数字 cellValue = String.valueOf(cell.getNumericCellValue()); } break; } case Cell.CELL_TYPE_STRING:{ cellValue = cell.getRichStringCellValue().getString(); break; } default: cellValue = ""; } }else{ cellValue = ""; } return cellValue; } }