package cn.com.lzt.car.driver.controller; import cn.com.lzt.car.cardoc.entity.CarEntity; import cn.com.lzt.car.driver.entity.TBCarDriver; import cn.com.lzt.car.driver.service.impl.TBCarDriverServiceImpl; import cn.hutool.core.date.DateUtil; import com.daju.mix.dao.mapper.TBCarMapper; import org.apache.commons.lang.xwork.StringUtils; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.*; import org.jeecgframework.core.common.controller.BaseController; import org.jeecgframework.core.common.exception.BusinessException; import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery; import org.jeecgframework.core.common.model.json.AjaxJson; import org.jeecgframework.core.common.model.json.DataGrid; import org.jeecgframework.core.util.StringUtil; import org.jeecgframework.tag.core.easyui.TagUtil; import org.jeecgframework.web.system.pojo.base.TSType; import org.jeecgframework.web.system.pojo.base.TSTypegroup; import org.jeecgframework.web.system.pojo.base.TSUser; import org.jeecgframework.web.system.service.SystemService; import org.jeecgframework.web.system.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.servlet.ModelAndView; import javax.annotation.Resource; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.OutputStream; import java.net.URLEncoder; import java.util.*; @Controller @RequestMapping("/tBCarDriverController") public class TBCarDriverController extends BaseController { @Resource private TBCarDriverServiceImpl tbCarDriverService; @Autowired private UserService userService; @Resource TBCarMapper tbCarMapper; @Autowired SystemService systemService; @RequestMapping(params = "driverList") public ModelAndView nlist(HttpServletRequest request) { return new ModelAndView("cn/com/lzt/car/driver/driverList"); } @RequestMapping(params = "datagrid") public void datagrid(TBCarDriver driver, HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) { CriteriaQuery cq = new CriteriaQuery(TBCarDriver.class, dataGrid); //自定义追加查询条件 String name = request.getParameter("userName"); // 查询条件组装器 if (StringUtils.isNotBlank(name)) { // 模糊查询 cq.like("userName", "%" + name + "%"); driver.setUserName(null); } String licenseType = request.getParameter("licenseType"); // 查询条件组装器 if (StringUtils.isNotBlank(licenseType)) { cq.eq("licenseType", licenseType); driver.setLicenseType(null); } String licenseArea = request.getParameter("licenseArea"); // 查询条件组装器 if (StringUtils.isNotBlank(licenseArea)) { // 模糊查询 cq.eq("licenseArea", licenseArea); driver.setLicenseArea(null); } String havingDate = request.getParameter("havingDate"); // 查询条件组装器 if (StringUtils.isNotBlank(havingDate)) { // 模糊查询 cq.eq("havingDate", havingDate ); driver.setHavingDate(null); } String effectiveDate = request.getParameter("effectiveDate"); // 查询条件组装器 if (StringUtils.isNotBlank(effectiveDate)) { // 模糊查询 cq.like("effectiveDate", effectiveDate); driver.setEffectiveDate(null); } //查询条件组装器 org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, driver, request.getParameterMap()); cq.add(); this.tbCarDriverService.getDataGridReturn(cq, true); List resultes = dataGrid.getResults(); for (TBCarDriver tbCarDriver : resultes){ if(StringUtil.isNotEmpty(tbCarDriver.getUserId())){ TSUser tsUser = userService.get(TSUser.class, tbCarDriver.getUserId()); if(tsUser != null){ tbCarDriver.setMobilePhone(tsUser.getMobilePhone()); tbCarDriver.setIdcard(tsUser.getIdcard()); tbCarDriver.setAddress(tsUser.getAddress()); } } } TagUtil.datagrid(response, dataGrid); } @RequestMapping(params = "doAdd") @ResponseBody public AjaxJson doAdd(TBCarDriver driver, HttpServletRequest request) { String message = null; AjaxJson j = new AjaxJson(); message = "驾驶员档案添加成功"; try { //driver.setCreateTime(DateUtil.parse(DateUtil.now(), "YYYY-MM-dd HH:mm:ss")); tbCarDriverService.save(driver); //carService.save(car); } catch (Exception e) { e.printStackTrace(); message = "驾驶员档案添加失败"; throw new BusinessException(e.getMessage()); } j.setMsg(message); return j; } @RequestMapping(params = "goAdd") public ModelAndView goAdd(TBCarDriver driver, HttpServletRequest req) { if (StringUtil.isNotEmpty(driver.getId())) { driver = tbCarDriverService.getEntity(CarEntity.class, driver.getId()); req.setAttribute("driverPage", driver); } return new ModelAndView("cn/com/lzt/car/driver/driver-add"); } @RequestMapping(params = "doUpdate") @ResponseBody public AjaxJson doUpdate(TBCarDriver driver, HttpServletRequest request) { String message = null; AjaxJson j = new AjaxJson(); message = "驾驶员档案更新成功"; TBCarDriver t = tbCarDriverService.get(TBCarDriver.class, driver.getId()); try { t.setUpdateTime(DateUtil.parse(DateUtil.now(), "YYYY-MM-dd HH:mm:ss")); t.setLicenseNum(driver.getLicenseNum()); t.setUserName(driver.getUserName()); t.setUserId(driver.getUserId()); t.setLicenseArea(driver.getLicenseArea()); t.setLicenseType(driver.getLicenseType()); t.setDrivingLicense(driver.getDrivingLicense()); t.setEffectiveDate(driver.getEffectiveDate()); t.setRemarks(driver.getRemarks()); tbCarDriverService.saveOrUpdate(t); } catch (Exception e) { e.printStackTrace(); message = "驾驶员档案更新失败"; throw new BusinessException(e.getMessage()); } j.setMsg(message); return j; } @RequestMapping(params = "goUpdate") public ModelAndView goUpdate(TBCarDriver driver, HttpServletRequest req) { if (StringUtil.isNotEmpty(driver.getId())) { driver = tbCarDriverService.getEntity(TBCarDriver.class, driver.getId()); if(StringUtil.isNotEmpty(driver.getUserId())){ TSUser tsUser = userService.get(TSUser.class, driver.getUserId()); if(tsUser != null){ driver.setMobilePhone(tsUser.getMobilePhone()); driver.setIdcard(tsUser.getIdcard()); driver.setAddress(tsUser.getAddress()); } } req.setAttribute("driverPage", driver); } return new ModelAndView("cn/com/lzt/car/driver/driver-update"); } @RequestMapping(params = "doDel") @ResponseBody public AjaxJson doDel(TBCarDriver driver, HttpServletRequest request) { String message = null; AjaxJson j = new AjaxJson(); driver = tbCarDriverService.getEntity(TBCarDriver.class, driver.getId()); message = "驾驶员档案删除成功"; try { tbCarDriverService.delete(driver); } catch (Exception e) { e.printStackTrace(); message = "驾驶员档案删除失败"; throw new BusinessException(e.getMessage()); } j.setMsg(message); return j; } /** * 批量删除 * * @return */ @RequestMapping(params = "doBatchDel") @ResponseBody public AjaxJson doBatchDel(String ids, HttpServletRequest request) { String message = null; AjaxJson j = new AjaxJson(); message = "驾驶员档案删除成功"; try { for (String id : ids.split(",")) { TBCarDriver driver = tbCarDriverService.getEntity(TBCarDriver.class, Long.parseLong(id) ); tbCarDriverService.delete(driver); } } catch (Exception e) { e.printStackTrace(); message = "驾驶员档案删除失败"; throw new BusinessException(e.getMessage()); } j.setMsg(message); return j; } @RequestMapping(params = "upload") public ModelAndView upload(HttpServletRequest req) { req.setAttribute("controller_name", "tBCarDriverController"); return new ModelAndView("common/upload/common_excel_upload"); } /** * 导出excel * * @param request * @param response */ @RequestMapping(params = "exportXls") public void exportXls(TBCarDriver car, HttpServletRequest request, HttpServletResponse response , DataGrid dataGrid, ModelMap modelMap) throws IOException { CriteriaQuery cq = new CriteriaQuery(TBCarDriver.class, dataGrid); org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, car, request.getParameterMap()); List cars = this.tbCarDriverService.getListByCriteriaQuery(cq, false); HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet("驾驶员档案管理"); HSSFRow title = sheet.createRow(0); title.createCell(0).setCellValue("驾照档案号"); title.createCell(1).setCellValue("员工姓名"); title.createCell(2).setCellValue("员工电话"); title.createCell(3).setCellValue("员工身份证号"); title.createCell(4).setCellValue("员工居住地"); title.createCell(5).setCellValue("准驾车型"); title.createCell(6).setCellValue("驾照号码"); title.createCell(7).setCellValue("驾照颁发地"); title.createCell(8).setCellValue("领证日期"); title.createCell(9).setCellValue("有效期"); title.createCell(10).setCellValue("备注"); // 根据数据字典关键字得到数据字典集合 TSTypegroup tsTypegroup = this.systemService.getTypeGroupByCode("licenseType"); Map licenseTypesMap = new HashMap<>(); if(tsTypegroup != null && tsTypegroup.getTSTypes().size() > 0){ List tsTypeList = tsTypegroup.getTSTypes(); for (TSType tsType:tsTypeList){ licenseTypesMap.put(tsType.getTypecode(),tsType.getTypename()); } } for (int i = 1; i < cars.size()+1; i++) { TBCarDriver tbCarDriver = cars.get(i - 1); TSUser tsUser = userService.get(TSUser.class, tbCarDriver.getUserId()); HSSFRow row = sheet.createRow(i); row.createCell(0).setCellValue(tbCarDriver.getLicenseNum()); row.createCell(1).setCellValue(tbCarDriver.getUserName()); row.createCell(2).setCellValue(tsUser.getMobilePhone()); row.createCell(3).setCellValue(tsUser.getIdcard()); row.createCell(4).setCellValue(tsUser.getAddress()); row.createCell(5).setCellValue(licenseTypesMap.getOrDefault(tbCarDriver.getLicenseType(), "未知车型")); row.createCell(6).setCellValue(tbCarDriver.getDrivingLicense()); row.createCell(7).setCellValue(tbCarDriver.getLicenseArea()); row.createCell(8).setCellValue(tbCarDriver.getHavingDate()); row.createCell(9).setCellValue(tbCarDriver.getEffectiveDate()); row.createCell(10).setCellValue(tbCarDriver.getRemarks()); } ServletOutputStream servletOutputStream = response.getOutputStream(); response.reset(); response.setHeader("Content-disposition", "attachment; filename=" + URLEncoder.encode("驾驶员档案管理") + ".xls"); response.setContentType("application/vnd.ms-excel"); wb.write((OutputStream) servletOutputStream); servletOutputStream.close(); } @RequestMapping(params = "exportXlsByT") public void exportXlsByT(TBCarDriver car, HttpServletRequest request, HttpServletResponse response , DataGrid dataGrid, ModelMap modelMap) throws IOException { HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet("驾驶员档案管理"); HSSFRow title = sheet.createRow(0); title.createCell(0).setCellValue("驾照档案号"); title.createCell(1).setCellValue("员工姓名"); title.createCell(2).setCellValue("准驾车型"); title.createCell(3).setCellValue("驾照号码"); title.createCell(4).setCellValue("驾照颁发地"); title.createCell(5).setCellValue("领证日期"); title.createCell(6).setCellValue("有效期"); title.createCell(7).setCellValue("备注"); ServletOutputStream servletOutputStream = response.getOutputStream(); response.reset(); response.setHeader("Content-disposition", "attachment; filename=" + URLEncoder.encode("驾驶员档案管理模板") + ".xls"); response.setContentType("application/vnd.ms-excel"); wb.write((OutputStream) servletOutputStream); servletOutputStream.close(); } @RequestMapping(params = "importExcel") @ResponseBody public AjaxJson importExcel(MultipartFile files) throws IOException { Workbook wb = WorkbookFactory.create(files.getInputStream()); for (int i = 0; i < wb.getNumberOfSheets(); i++) { Sheet sheet = wb.getSheetAt(i); //第一行标题 Row firstRow = sheet.getRow(sheet.getFirstRowNum()); for (int j = sheet.getFirstRowNum() + 1; j <= sheet.getLastRowNum(); j++) { Row row = sheet.getRow(j); if (row.getCell(0) == null ) { continue; } TBCarDriver tbCarDriver = new TBCarDriver(); tbCarDriver.setLicenseNum(getStringCellValue(row.getCell(0))); tbCarDriver.setUserName(getStringCellValue(row.getCell(1))); tbCarDriver.setLicenseType(getStringCellValue(row.getCell(2))); tbCarDriver.setDrivingLicense(getStringCellValue(row.getCell(3))); tbCarDriver.setLicenseArea(getStringCellValue(row.getCell(4))); tbCarDriver.setHavingDate(getStringCellValue(row.getCell(5))); tbCarDriver.setEffectiveDate(getStringCellValue(row.getCell(6))); tbCarDriver.setRemarks(getStringCellValue(row.getCell(7))); tbCarDriverService.save(tbCarDriver); } } AjaxJson ajaxJson = new AjaxJson(); ajaxJson.setMsg("导入成功"); ajaxJson.setSuccess(true); return ajaxJson; } private String getStringCellValue(Cell cell) { try { return Optional.ofNullable(cell).map(Cell::getStringCellValue).orElse(null); } catch (IllegalStateException ex) { try { return Integer.toString(Double.valueOf(cell.getNumericCellValue()).intValue()); }catch (IllegalStateException ex1){ int cellNum = cell.getColumnIndex(); log.error((cellNum + 1) + "格,错误", ex1); return null; } } } /** * 驾驶员驾照到期提醒 */ @RequestMapping(params = "driverRemind") @ResponseBody public AjaxJson driverRemind(){ List list = tbCarMapper.getDriverRemind(); AjaxJson j = new AjaxJson(); j.setMsg("请求成功"); j.setObj(list); j.setSuccess(true); return j; } }