package cn.com.goldenwater.dcproj.controller.ducha; import cn.com.goldenwater.core.web.BaseController; import cn.com.goldenwater.core.web.BaseResponse; import cn.com.goldenwater.dcproj.model.BisInspAllRlationPers; import cn.com.goldenwater.dcproj.service.BisInspAllRlationPersService; import cn.com.goldenwater.dcproj.target.Authority; import cn.com.goldenwater.dcproj.util.ReadExcelUtil; import cn.com.goldenwater.dcproj.utils.impexcel.ExpAndImpUtil; import cn.com.goldenwater.id.util.UuidUtil; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.apache.commons.lang3.StringUtils; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletResponse; import java.io.File; import java.io.IOException; import java.nio.file.Paths; import java.util.*; /** * @author zhaohg * @date 2019-2-23 */ @Api(value = "新用户管理", tags = "006新用户管理") @RestController @RequestMapping("/bis/insp") public class BisInspAllRlationPersBatchController extends BaseController { private Logger logger = LoggerFactory.getLogger(getClass()); @Value("${export.templatePath}") private String templatePath; @Autowired private BisInspAllRlationPersService bisInspAllRlationPersService; @Authority(roles = "1") @ApiOperation("获取导入模板") @GetMapping(value = "/getExl") public BaseResponse getExl(HttpServletResponse response) { String path = templatePath + File.separator + "person_list.xls"; try { ExpAndImpUtil.downloadFile(response, path, "用户导入表"); } catch (Exception e) { logger.error(e.getMessage(), e); } return buildSuccessResponse(); } @ApiOperation("上传 xls 导入数据") @RequestMapping(value = "/impExl", method = RequestMethod.POST) public BaseResponse> impExl(@RequestParam("file") MultipartFile multfile, @RequestParam(value = "orgId", required = false) String orgId) throws IOException, InvalidFormatException { // 获取文件名 String fileName = multfile.getOriginalFilename(); // 获取文件后缀 String prefix = fileName.substring(fileName.lastIndexOf(".")); // 用uuid作为文件名,防止生成的临时文件重复 String uuid = UuidUtil.uuid(); File file = File.createTempFile(uuid, prefix); // MultipartFile to File multfile.transferTo(Paths.get(file.getPath())); Map map = new HashMap<>(2); int shu = 0; int success = 0; Map failReason = new HashMap<>(); if (StringUtils.isBlank(orgId)) { orgId = getCurrentOrgId(); } String[][] data = ReadExcelUtil.getData(file, 1); if (data.length > 0) { for (String[] datum : data) { if (StringUtils.isBlank(datum[3])) { continue; } BisInspAllRlationPers pers = new BisInspAllRlationPers(); try { pers.setOrgId(orgId); pers.setPersName(datum[1]); pers.setSex(getSex(datum[2])); pers.setMobilenumb(datum[3]); pers.setPersType(getPersType(datum[4])); pers.setDpnm(datum[5]); pers.setAdmDuty(datum[6]); pers.setDppost(datum[7]); pers.setPwd(getPwd(datum[8], datum[3])); bisInspAllRlationPersService.createUser(pers); success++; } catch (Exception e) { logger.error(e.getMessage(), e); shu++; failReason.put(pers.getPersName() + "[" + pers.getMobilenumb() + "]", e.getMessage()); } } } map.put("false", shu); map.put("success", success); map.put("failReason", failReason); return buildSuccessResponse(map); } /** * 获取性别 * * @param value 数据 * @return 1:男 2:女 */ private String getSex(String value) { Set set = new HashSet<>(Arrays.asList("1", "2")); if (set.contains(value) || StringUtils.isBlank(value)) { return value; } if (value.contains("男")) { return "1"; } else if (value.contains("女")) { return "2"; } else { return null; } } /** * 获取人员类型 * * @param value 数据 * @return 1.管理员 2.机构领导 3.督察人员 4.稽查人员 */ private String getPersType(String value) { Set set = new HashSet<>(Arrays.asList("1", "2", "3", "4")); if (set.contains(value)) { return value; } if (StringUtils.isBlank(value)) { return "3"; } if (value.contains("管理")) { return "1"; } else if (value.contains("机构")) { return "2"; } else if (value.contains("督查")) { return "3"; } else if (value.contains("稽察")) { return "4"; } else { return "3"; } } /** * 获取密码: 默认密码是手机号码 * * @param value 数据 * @return 1.管理员 2.机构领导 3.督察人员 4.稽查人员 */ private String getPwd(String value, String phone) { if (StringUtils.isBlank(value)) { return phone; } return value.trim(); } }