package com.jpsoft.smart.modules.base.controller; import com.github.pagehelper.Page; import com.jpsoft.smart.modules.common.utils.PojoUtils; import com.jpsoft.smart.modules.common.dto.Sort; import com.jpsoft.smart.modules.common.dto.MessageResult; import com.jpsoft.smart.modules.base.entity.PersonInfo; import com.jpsoft.smart.modules.base.service.PersonInfoService; import com.jpsoft.smart.modules.sys.entity.User; import com.jpsoft.smart.modules.sys.service.UserService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletRequest; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.text.SimpleDateFormat; import java.util.*; @RestController @RequestMapping("/base/personInfo") @Api(description = "personInfo") public class PersonInfoController { private Logger logger = LoggerFactory.getLogger(getClass()); @Autowired private PersonInfoService personInfoService; @Autowired private UserService userService; @ApiOperation(value="保存信息") @PostMapping("save") public MessageResult save(@RequestBody PersonInfo personInfo,@RequestAttribute String subject){ User user = userService.get(subject); MessageResult msgResult = new MessageResult<>(); try { int affectCount = 0; if(personInfo.getId() != null) { personInfo.setCompanyId(user.getCompanyId()); personInfo.setDelFlag(false); personInfo.setCreateBy(subject); personInfo.setCreateTime(new Date()); affectCount = personInfoService.insert(personInfo); }else{ personInfo.setUpdateBy(subject); personInfo.setUpdateTime(new Date()); affectCount = personInfoService.update(personInfo); } if (affectCount > 0) { msgResult.setResult(true); msgResult.setData(personInfo); }else{ msgResult.setResult(false); msgResult.setMessage("数据库保存失败"); } } catch(Exception ex){ logger.error(ex.getMessage(),ex); msgResult.setResult(false); msgResult.setMessage(ex.getMessage()); } return msgResult; } @ApiOperation(value="获取详情信息") @GetMapping("detail/{id}") public MessageResult detail(@PathVariable("id") String id){ MessageResult msgResult = new MessageResult<>(); try { PersonInfo personInfo = personInfoService.get(id); if (personInfo != null) { msgResult.setResult(true); msgResult.setData(personInfo); } else { msgResult.setResult(false); msgResult.setMessage("数据库不存在该记录!"); } } catch(Exception ex){ logger.error(ex.getMessage(),ex); msgResult.setResult(false); msgResult.setMessage(ex.getMessage()); } return msgResult; } @ApiOperation(value="删除") @PostMapping("delete/{id}") public MessageResult delete(@PathVariable("id") String id,@RequestAttribute String subject){ MessageResult msgResult = new MessageResult<>(); try { PersonInfo personInfo = personInfoService.get(id); personInfo.setDelFlag(true); personInfo.setUpdateBy(subject); personInfo.setUpdateTime(new Date()); int affectCount = personInfoService.update(personInfo); if (affectCount > 0) { msgResult.setResult(true); msgResult.setData(affectCount); } else { msgResult.setResult(false); msgResult.setMessage("删除失败"); } } catch(Exception ex){ logger.error(ex.getMessage(),ex); msgResult.setResult(false); msgResult.setMessage(ex.getMessage()); } return msgResult; } @ApiOperation(value="批量删除") @PostMapping("batchDelete") public MessageResult batchDelete(@RequestBody List idList,@RequestAttribute String subject){ MessageResult msgResult = new MessageResult<>(); try { int affectCount = 0; for (String id : idList) { PersonInfo personInfo = personInfoService.get(id); personInfo.setDelFlag(true); personInfo.setUpdateBy(subject); personInfo.setUpdateTime(new Date()); affectCount += personInfoService.update(personInfo); } if (affectCount > 0) { msgResult.setResult(true); msgResult.setData(affectCount); } else { msgResult.setResult(false); msgResult.setMessage("删除失败"); } } catch(Exception ex){ logger.error(ex.getMessage(),ex); msgResult.setResult(false); msgResult.setMessage(ex.getMessage()); } return msgResult; } @ApiOperation(value="列表") @RequestMapping(value = "pageList",method = RequestMethod.POST) @ApiImplicitParams({ @ApiImplicitParam(name = "name",value = "姓名", required = false, paramType = "form",dataType = "String"), @ApiImplicitParam(name = "position1",value = "一级位置", required = false, paramType = "form",dataType = "String"), @ApiImplicitParam(name = "position2",value = "二级位置", required = false, paramType = "form",dataType = "String"), @ApiImplicitParam(name = "position3",value = "三级位置", required = false, paramType = "form",dataType = "String"), @ApiImplicitParam(name = "position4",value = "四级位置", required = false, paramType = "form",dataType = "String"), @ApiImplicitParam(name = "position5",value = "五级位置", required = false, paramType = "form",dataType = "String") }) public MessageResult pageList( @RequestParam(value="name",defaultValue="") String name, @RequestParam(value="position1",defaultValue="") String position1, @RequestParam(value="position2",defaultValue="") String position2, @RequestParam(value="position3",defaultValue="") String position3, @RequestParam(value="position4",defaultValue="") String position4, @RequestParam(value="position5",defaultValue="") String position5, @RequestParam(value="pageIndex",defaultValue="1") int pageIndex, @RequestParam(value="pageSize",defaultValue="20") int pageSize, @RequestAttribute String subject){ //当前用户ID System.out.println(subject); MessageResult msgResult = new MessageResult<>(); Map searchParams = new HashMap<>(); List sortList = new ArrayList<>(); sortList.add(new Sort("id_","asc")); if (StringUtils.isNotEmpty(name)) { searchParams.put("name","%" + name + "%"); } if (StringUtils.isNotEmpty(position1)) { searchParams.put("position1",position1); } if (StringUtils.isNotEmpty(position2)) { searchParams.put("position2",position2); } if (StringUtils.isNotEmpty(position3)) { searchParams.put("position3",position3); } if (StringUtils.isNotEmpty(position4)) { searchParams.put("position4",position4); } if (StringUtils.isNotEmpty(position5)) { searchParams.put("position5",position5); } Page page = personInfoService.pageSearch(searchParams,pageIndex,pageSize,true,sortList); msgResult.setResult(true); msgResult.setData(PojoUtils.pageWrapper(page)); return msgResult; } @ApiOperation(value="人脸授权") @PostMapping("EnabledFace") @ApiImplicitParams({ @ApiImplicitParam(name = "id",value = "人员编号", required = false, paramType = "form",dataType = "String") }) public MessageResult EnabledFace(@RequestParam(value="id",defaultValue="") String id,@RequestAttribute String subject){ MessageResult msgResult = new MessageResult<>(); try { PersonInfo personInfo = personInfoService.get(id); if(personInfo.getFaceEnabled()){ personInfo.setFaceEnabled(false); }else{ personInfo.setFaceEnabled(true); } personInfo.setUpdateBy(subject); personInfo.setUpdateTime(new Date()); int affectCount = personInfoService.update(personInfo); if (affectCount > 0) { msgResult.setResult(true); msgResult.setData(personInfo); }else{ msgResult.setResult(false); msgResult.setMessage("数据库保存失败"); } } catch(Exception ex){ logger.error(ex.getMessage(),ex); msgResult.setResult(false); msgResult.setMessage(ex.getMessage()); } return msgResult; } @ApiOperation(value="刷卡授权") @PostMapping("EnabledCard") @ApiImplicitParams({ @ApiImplicitParam(name = "id",value = "人员编号", required = false, paramType = "form",dataType = "String") }) public MessageResult EnabledCard(@RequestParam(value="id",defaultValue="") String id,@RequestAttribute String subject){ MessageResult msgResult = new MessageResult<>(); try { PersonInfo personInfo = personInfoService.get(id); if(personInfo.getCardEnabled()){ personInfo.setCardEnabled(false); }else{ personInfo.setCardEnabled(true); } personInfo.setUpdateBy(subject); personInfo.setUpdateTime(new Date()); int affectCount = personInfoService.update(personInfo); if (affectCount > 0) { msgResult.setResult(true); msgResult.setData(personInfo); }else{ msgResult.setResult(false); msgResult.setMessage("数据库保存失败"); } } catch(Exception ex){ logger.error(ex.getMessage(),ex); msgResult.setResult(false); msgResult.setMessage(ex.getMessage()); } return msgResult; } @ApiOperation(value="手机授权") @PostMapping("EnabledApp") @ApiImplicitParams({ @ApiImplicitParam(name = "id",value = "人员编号", required = false, paramType = "form",dataType = "String") }) public MessageResult EnabledApp(@RequestParam(value="id",defaultValue="") String id,@RequestAttribute String subject){ MessageResult msgResult = new MessageResult<>(); try { PersonInfo personInfo = personInfoService.get(id); if(personInfo.getAppEnabled()){ personInfo.setAppEnabled(false); }else{ personInfo.setAppEnabled(true); } personInfo.setUpdateBy(subject); personInfo.setUpdateTime(new Date()); int affectCount = personInfoService.update(personInfo); if (affectCount > 0) { msgResult.setResult(true); msgResult.setData(personInfo); }else{ msgResult.setResult(false); msgResult.setMessage("数据库保存失败"); } } catch(Exception ex){ logger.error(ex.getMessage(),ex); msgResult.setResult(false); msgResult.setMessage(ex.getMessage()); } return msgResult; } @ApiOperation(value="访客授权") @PostMapping("EnabledGuest") @ApiImplicitParams({ @ApiImplicitParam(name = "id",value = "人员编号", required = false, paramType = "form",dataType = "String") }) public MessageResult EnabledGuest(@RequestParam(value="id",defaultValue="") String id,@RequestAttribute String subject){ MessageResult msgResult = new MessageResult<>(); try { PersonInfo personInfo = personInfoService.get(id); if(personInfo.getGuestEnabled()){ personInfo.setGuestEnabled(false); }else{ personInfo.setGuestEnabled(true); } personInfo.setUpdateBy(subject); personInfo.setUpdateTime(new Date()); int affectCount = personInfoService.update(personInfo); if (affectCount > 0) { msgResult.setResult(true); msgResult.setData(personInfo); }else{ msgResult.setResult(false); msgResult.setMessage("数据库保存失败"); } } catch(Exception ex){ logger.error(ex.getMessage(),ex); msgResult.setResult(false); msgResult.setMessage(ex.getMessage()); } return msgResult; } // @ApiOperation(value="导入企业人员") // @PostMapping("importXls") // @ApiImplicitParams({ // @ApiImplicitParam(name = "companyId",value = "企业ID", required = true, paramType = "form",dataType = "String"), // @ApiImplicitParam(name = "uploadFile",value = "上传文件", required = true,paramType="form", dataType = "__file") // }) // public MessageResult importXls(@RequestParam(value="companyId",defaultValue="") String companyId, // MultipartFile uploadFile, // @RequestAttribute String subject){ // User user = userService.get(subject); // // MessageResult msgResult = new MessageResult<>(); // PersonInfo personInfo = new PersonInfo(); // // try { // POIUtils poiUtils = new POIUtils(uploadFile.getInputStream()); // int sheetIndex = 0; // Sheet sheet1 = poiUtils.getSheetAt(sheetIndex); // // int affectCount = 0; // int failCount = 0; // int validateColIndex = 7; // // for(int rowIndex=1 ; rowIndex<=sheet1.getLastRowNum(); rowIndex++){ // try { // String name = (String)poiUtils.getCellValue(sheetIndex,rowIndex,1); // String cardType = (String)poiUtils.getCellValue(sheetIndex,rowIndex,2); // String cardNo = (String)poiUtils.getCellValue(sheetIndex,rowIndex,3); // String sex = (String)poiUtils.getCellValue(sheetIndex,rowIndex,4); //// String age = array[5].toString(); // String jobName = (String)poiUtils.getCellValue(sheetIndex,rowIndex,5); // String healthStatus = (String)poiUtils.getCellValue(sheetIndex,rowIndex,6); // // if(StringUtils.isEmpty(name)){ // break; // } // // Map searchParams = new HashMap<>(); // searchParams.put("companyId", company.getId()); // searchParams.put("cardNo", cardNo); // searchParams.put("delFlag", false); // // List sortList = new ArrayList<>(); // Page page = companyMemberService.pageSearch(searchParams, 1, 100, sortList); // // if (page.size() > 0) { // sheet1.getRow(rowIndex).createCell(validateColIndex).setCellValue("身份证已导入!"); // failCount++; // continue; // } else { // companyMember.setId(UUID.randomUUID().toString()); // if (StringUtils.isNotEmpty(company.getId())) companyMember.setCompanyId(company.getId()); // if (StringUtils.isNotEmpty(name)) companyMember.setName(name); // // CheckIdCard cic = null; // // if(StringUtils.isEmpty(cardNo)){ // sheet1.getRow(rowIndex).createCell(validateColIndex).setCellValue("身份证不能为空!"); // failCount++; // continue; // } // else{ // cardNo = cardNo.trim().toUpperCase(); // } // // for(DataDictionary dd : ddList){ // if(dd.getName().equals(cardType)) { // companyMember.setCardType(dd.getValue()); // break; // } // } // // if (StringUtils.isEmpty(companyMember.getCardType())){ // sheet1.getRow(rowIndex).createCell(validateColIndex).setCellValue("证件类型未选择!"); // failCount++; // continue; // } // // if (cardType.contains("身份证")) { // cic = new CheckIdCard(cardNo); // // if (!cic.validate()){ // sheet1.getRow(rowIndex).createCell(validateColIndex).setCellValue("身份证验证失败!"); // failCount++; // // continue; // } // } // // companyMember.setCardNo(cardNo); // // List jobsList = jobsService.findByName(jobsName); // // if (jobsList.size() > 0) { // companyMember.setJobsId(jobsList.get(0).getId()); // } // // if (StringUtils.isNotEmpty(healthStatus)) companyMember.setHealthStatus(healthStatus); // // companyMember.setStatus("0"); // companyMember.setDelFlag(false); // companyMember.setCreateBy(principal.getName()); // companyMember.setCreateTime(new Date()); // // if (companyMemberService.insert(companyMember) > 0) { // affectCount++; // } // } // } // catch(Exception innerEx){ // logger.error(innerEx.getMessage(),innerEx); // } // } // // if (failCount>0){ // //有导入失败的记录 // msgResult.setResult(false); // msgResult.setMessage("数据成功导入" + affectCount + "条,有" + failCount + "条数据未导入成功,错误原因请查看报表。"); // // //todo 只保留错误数据的sheet // int rowIndex = 1; // // while(rowIndex<= sheet1.getLastRowNum()){ // Cell cell = sheet1.getRow(rowIndex).getCell(validateColIndex); // // if (cell==null || StringUtils.isEmpty(cell.getStringCellValue())){ // sheet1.removeRow(sheet1.getRow(rowIndex)); // // if (rowIndex