package com.stylefeng.guns.modular.system.controller.specialTrain; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.mapper.EntityWrapper; import com.baomidou.mybatisplus.plugins.Page; import com.stylefeng.guns.core.base.controller.BaseController; import com.stylefeng.guns.core.common.constant.factory.PageFactory; import com.stylefeng.guns.core.log.LogObjectHolder; import com.stylefeng.guns.core.shiro.ShiroKit; import com.stylefeng.guns.core.util.SinataUtil; import com.stylefeng.guns.core.util.ToolUtil; import com.stylefeng.guns.modular.system.model.*; import com.stylefeng.guns.modular.system.dao.LineShiftDriverMapper; import com.stylefeng.guns.modular.system.service.*; import com.stylefeng.guns.modular.system.util.ResultUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import java.math.BigDecimal; /** * 跨城线路管理控制器 * * @author fengshuonan * @Date 2020-09-15 16:17:29 */ @Controller @RequestMapping("/tLine") public class TLineController extends BaseController { private String PREFIX = "/system/tLine/"; @Autowired private ITLineService tLineService; @Autowired private ITSiteService itSiteService; @Autowired private ITLineSiteService itLineSiteService; @Autowired private ITLineShiftService itLineShiftService; @Autowired private ITLineDestinationService itLineDestinationService; @Autowired private ITCompanyService itCompanyService; @Autowired private ITLineCompanyService itLineCompanyService; @Autowired private LineShiftDriverMapper lineShiftDriverMapper; @Autowired private ITDriverService tDriverService; /** * 跳转到跨城站点管理首页 */ @RequestMapping("") public String index() { return PREFIX + "tLine.html"; } /** * 跳转到行程列表页 */ @RequestMapping("/travelListPage") public String travelListPage() { return PREFIX + "travelList.html"; } /** * 跳转到行程订单列表页 */ @RequestMapping("/travelOrdersPage") public String travelOrdersPage(Integer lineShiftDriverId, Integer lineId, Integer driverId, Model model) { model.addAttribute("lineShiftDriverId", lineShiftDriverId); model.addAttribute("lineId", lineId); model.addAttribute("driverId", driverId); return PREFIX + "travelOrders.html"; } /** * 跳转到添加跨城站点管理 */ @RequestMapping("/tLine_add") public String tLineAdd(Model model) { //站点 Integer companyId = null; if(ShiroKit.getUser().getRoleType() != 1){ companyId = ShiroKit.getUser().getObjectId(); } List siteList = itSiteService.selectList(new EntityWrapper().eq("state", 1) .eq(null != companyId, "companyId", companyId)); model.addAttribute("siteList",siteList); return PREFIX + "tLine_add.html"; } /** * 分配企业页面 */ @RequestMapping("/tLine_addCompany/{tLineId}") public String tLine_addCompany(@PathVariable Integer tLineId,Model model) { TLine tLine = tLineService.selectById(tLineId); model.addAttribute("tLine",tLine); //查询所有企业 List companyList = itCompanyService.selectList(new EntityWrapper().last(" where not FIND_IN_SET(state,'1') and not FIND_IN_SET(flag,'3')")); model.addAttribute("companyList",companyList); //查询已分配企业 List> lineCompanyList = tLineService.getLineCompanyList(tLineId); model.addAttribute("lineCompanyList",lineCompanyList); return PREFIX + "tLine_addCompany.html"; } @RequestMapping("/set/{redict}") public String set(Model model,@PathVariable String redict) { model.addAttribute("redict",redict); return PREFIX + "set.html"; } @RequestMapping("/set1/{redict}") public String set1(Model model,@PathVariable String redict) { model.addAttribute("redict",redict); return PREFIX + "set1.html"; } /** * 跳转到修改跨城站点管理 */ @RequestMapping("/tLine_update/{tLineId}") public String tLineUpdate(@PathVariable Integer tLineId, Model model) { TLine tLine = tLineService.selectById(tLineId); model.addAttribute("item",tLine); LogObjectHolder.me().set(tLine); //查找站点起点+终点 TLineSite start = itLineSiteService.selectOne(new EntityWrapper().eq("lineId", tLineId).eq("type", 1)); model.addAttribute("start",start); TLineSite end = itLineSiteService.selectOne(new EntityWrapper().eq("lineId", tLineId).eq("type", 2)); model.addAttribute("end",end); //站点 Integer companyId = null; if(ShiroKit.getUser().getRoleType() != 1){ companyId = ShiroKit.getUser().getObjectId(); } List siteList = itSiteService.selectList(new EntityWrapper().eq("state", 1) .eq(null != companyId, "companyId", companyId)); model.addAttribute("siteList",siteList); //查询所有班次 List lineShiftList = itLineShiftService.selectList(new EntityWrapper().eq("lineId", tLineId).last(" and not FIND_IN_SET(state,'3')")); model.addAttribute("lineShiftList",lineShiftList); //查询线路目的地 List lineDestinationList = itLineDestinationService.selectList( new EntityWrapper().eq("lineId", tLineId).ne("state", 3).orderBy("isMain", false).orderBy("id", true)); model.addAttribute("lineDestinationList", lineDestinationList); return PREFIX + "tLine_edit.html"; } /** * 获取跨城站点管理列表 */ @RequestMapping(value = "/list") @ResponseBody public Object list(String insertTime,String name,String insertUser,Integer state) { String beginTime = null; String endTime = null; if (SinataUtil.isNotEmpty(insertTime)) { String[] timeArray = insertTime.split(" - "); beginTime = timeArray[0]; endTime = timeArray[1]; } Page> page = new PageFactory>().defaultPage(); Integer companyId = null; if(ShiroKit.getUser().getRoleType() != 1){ companyId = ShiroKit.getUser().getObjectId(); } page.setRecords(tLineService.getLineList(page, beginTime, endTime, name, insertUser, state, companyId)); return super.packForBT(page); } /** * 获取行程列表 */ @RequestMapping(value = "/travelList") @ResponseBody public Object travelList(String city, String lineName, String driverKeyword, Integer status) { Page> page = new PageFactory>().defaultPage(); Integer roleType = ShiroKit.getUser().getRoleType(); Integer companyId = ShiroKit.getUser().getObjectId(); page.setRecords(tLineService.getTravelList(page, city, lineName, driverKeyword, status, roleType, companyId)); return super.packForBT(page); } /** * 获取行程对应订单列表 */ @RequestMapping(value = "/travelOrders") @ResponseBody public Object travelOrders(Integer lineShiftDriverId, Integer lineId, Integer driverId) { Page> page = new PageFactory>().defaultPage(); Integer roleType = ShiroKit.getUser().getRoleType(); Integer companyId = ShiroKit.getUser().getObjectId(); page.setRecords(tLineService.getTravelOrders(page, lineShiftDriverId, lineId, driverId, roleType, companyId)); return super.packForBT(page); } /** * 新增跨城站点管理 */ @RequestMapping(value = "/add") @ResponseBody public ResultUtil add(TLine tLine,Integer startSiteId,Integer endSiteId,@RequestParam(required = false) String subArr, @RequestParam String shuArr, @RequestParam(required = false) String destArr) { if (!hasCommissionSetting(tLine.getOutSetting())) { return ResultUtil.error("请设置司机抽成"); } ResultUtil charterPriceResult = validateCharterPrice(tLine); if (200 != charterPriceResult.getStatus()) { return charterPriceResult; } JSONArray jsonArray1 = JSON.parseArray(shuArr); int size1 = jsonArray1.size(); for (int i = 0; i < size1; i++) { JSONObject jsonObject = jsonArray1.getJSONObject(i); String shiftTime = jsonObject.getString("shiftTime"); if (SinataUtil.isNotEmpty(shiftTime)){ String[] timeArray = shiftTime.split(" - "); String startTime = timeArray[0]; String endTime = timeArray[1]; if(startTime.compareTo(endTime) >= 0){ return ResultUtil.error("班次结束时间不能小于开始时间"); } } } String name = tLine.getName(); name = name.replaceAll("& lt;", "<"); name = name.replaceAll("& gt;", ">"); name = name.replaceAll("& #40;", "("); name = name.replaceAll("& #41;", ")"); tLine.setName(name); tLine.setState(1); tLine.setInsertTime(new Date()); tLine.setInsertUserId(ShiroKit.getUser().getId()); tLine.setCompanyId(ShiroKit.getUser().getObjectId()); tLineService.insert(tLine); //添加线路站点起点+终点 TLineSite start = new TLineSite(); start.setLineId(tLine.getId()); start.setSiteId(startSiteId); start.setType(1); itLineSiteService.insert(start); TLineSite end = new TLineSite(); end.setLineId(tLine.getId()); end.setSiteId(endSiteId); end.setType(2); itLineSiteService.insert(end); //添加班次设置 ResultUtil shiftResult = addShift(tLine.getId(), shuArr); if(200 != shiftResult.getStatus()){ return shiftResult; } //添加目的地设置 ResultUtil destinationResult = addDestination(tLine.getId(), destArr); if(200 != destinationResult.getStatus()){ return destinationResult; } return ResultUtil.success(); } /** * 添加线路班次 * @param lineId * @param shuArr */ private ResultUtil addShift(Integer lineId, @RequestParam String shuArr) { try { JSONArray jsonArray1 = JSON.parseArray(shuArr); int size1 = jsonArray1.size(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); List list = new ArrayList<>(); for (int i = 0; i < size1; i++) { List tLineShifts = itLineShiftService.selectList(new EntityWrapper().eq("lineId", lineId).ne("state", 3).orderBy("startTime")); JSONObject jsonObject = jsonArray1.getJSONObject(i); TLineShift shift = new TLineShift(); shift.setLineId(lineId); String shiftTime = jsonObject.getString("shiftTime"); String startTime = null; String endTime = null; if (SinataUtil.isNotEmpty(shiftTime)){ String[] timeArray = shiftTime.split(" - "); startTime = timeArray[0]; endTime = timeArray[1]; } Date s1 = sdf.parse("2020-11-11 " + startTime + ":00"); Date e1 = sdf.parse("2020-11-11 " + endTime + ":00"); if(s1.getTime() >= e1.getTime()){ return ResultUtil.error("【" + startTime + " - " + endTime + "】时间段无效,添加失败!"); } boolean b = false; for(TLineShift lineShift : tLineShifts){ Date s2 = sdf.parse("2020-11-11 " + lineShift.getStartTime() + ":00"); Date e2 = sdf.parse("2020-11-11 " + lineShift.getEndTime() + ":00"); if(s2.getTime() < s1.getTime() && e1.getTime() < e2.getTime()){ list.add(startTime + " - " + endTime); b = true; break; } if(s1.getTime() < s2.getTime() && s1.getTime() < e2.getTime() && e1.getTime() > s2.getTime()){ list.add(startTime + " - " + endTime); b = true; break; } if(s1.getTime() > s2.getTime() && s1.getTime() < e2.getTime() && e1.getTime() > e2.getTime()){ list.add(startTime + " - " + endTime); b = true; break; } } if(b){ continue; } shift.setStartTime(startTime); shift.setEndTime(endTime); shift.setCarNum(jsonObject.getInteger("carNum")); shift.setInsertTime(new Date()); shift.setAddTime(jsonObject.getString("time2")); shift.setState(1); itLineShiftService.insert(shift); } if(list.size() > 0){ return ResultUtil.error("【" + JSON.toJSONString(list) + "】时间段与现存时间段重合,添加失败!"); } return ResultUtil.success(); }catch (Exception e){ e.printStackTrace(); return ResultUtil.runErr(); } } /** * 操作跨城站点管理 * optType 1=删除 2=冻结 3=解冻 */ @RequestMapping(value = "/opt") @ResponseBody public Object opt(@RequestParam Integer tLineId,@RequestParam Integer optType) { TLine tLine = tLineService.selectById(tLineId); if (1 == optType){ tLine.setState(3); }else if (2 == optType){ tLine.setState(2); }else if (3 == optType){ tLine.setState(1); } tLineService.updateById(tLine); return SUCCESS_TIP; } /** * 分配企业操作 */ @RequestMapping(value = "/addCompany") @ResponseBody public Object addCompany(Integer lineId,@RequestParam String comArr) { //删除分配企业 itLineCompanyService.delete(new EntityWrapper().eq("lineId",lineId)); //添加企业 if (SinataUtil.isNotEmpty(comArr)){ JSONArray jsonArray = JSON.parseArray(comArr); int size = jsonArray.size(); for (int i = 0; i < size; i++) { JSONObject jsonObject = jsonArray.getJSONObject(i); TLineCompany lineCompany = new TLineCompany(); lineCompany.setLineId(lineId); lineCompany.setCompanyId(jsonObject.getInteger("companyId")); lineCompany.setInsertTime(new Date()); lineCompany.setInsertUserId(ShiroKit.getUser().getId()); lineCompany.setAutoPlan(2); itLineCompanyService.insert(lineCompany); } } return SUCCESS_TIP; } /** * 查询线路已分配企业 */ @RequestMapping(value = "/lineCompanyList") @ResponseBody public Object lineCompanyList(Integer lineId) { if (lineId == null) { return ResultUtil.error("线路ID不能为空"); } return ResultUtil.success(tLineService.getLineCompanyList(lineId)); } /** * 排班管理页面 */ @RequestMapping(value = "/schedulePage") public String schedulePage(Integer lineId, Integer companyId, Model model) { TLine line = tLineService.selectById(lineId); List lineCompanies = getLineCompanies(lineId); TLineCompany currentLineCompany = null; if (companyId != null) { currentLineCompany = checkLineCompany(lineId, companyId); } if (currentLineCompany == null && lineCompanies.size() > 0) { currentLineCompany = lineCompanies.get(0); companyId = currentLineCompany.getCompanyId(); } model.addAttribute("lineId", lineId); model.addAttribute("companyId", companyId); model.addAttribute("lineName", line == null ? "" : line.getName()); model.addAttribute("companyName", getLineCompanyNames(lineCompanies)); model.addAttribute("autoPlan", currentLineCompany == null || currentLineCompany.getAutoPlan() == null ? 2 : currentLineCompany.getAutoPlan()); return PREFIX + "tLine_schedule.html"; } /** * 排班管理-月份数据 */ @RequestMapping(value = "/schedule/month") @ResponseBody public Object scheduleMonth(Integer lineId, Integer companyId, String month) { try { if (!hasLineCompanies(lineId)) { return ResultUtil.error("线路未关联企业"); } if (SinataUtil.isEmpty(month)) { month = new SimpleDateFormat("yyyy-MM").format(new Date()); } SimpleDateFormat monthSdf = new SimpleDateFormat("yyyy-MM"); Date monthDate = monthSdf.parse(month); Calendar cal = Calendar.getInstance(); cal.setTime(monthDate); cal.set(Calendar.DAY_OF_MONTH, 1); String monthStart = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime()); int maxDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH); cal.set(Calendar.DAY_OF_MONTH, maxDay); String monthEnd = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime()); List> stats = lineShiftDriverMapper.getMonthScheduleStats(lineId, monthStart, monthEnd, companyId, null); Map dayAssignMap = new HashMap<>(); for (Map stat : stats) { dayAssignMap.put(String.valueOf(stat.get("dayStr")), Integer.parseInt(String.valueOf(stat.get("assignNum")))); } List shifts = itLineShiftService.selectList(new EntityWrapper().eq("lineId", lineId).ne("state", 3)); int totalNeed = 0; for (TLineShift shift : shifts) { totalNeed += (shift.getCarNum() == null ? 0 : shift.getCarNum()); } List> dayList = new ArrayList<>(); cal.setTime(monthDate); cal.set(Calendar.DAY_OF_MONTH, 1); for (int i = 1; i <= maxDay; i++) { cal.set(Calendar.DAY_OF_MONTH, i); String dayStr = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime()); int assignNum = dayAssignMap.get(dayStr) == null ? 0 : dayAssignMap.get(dayStr); Map obj = new HashMap<>(); obj.put("day", dayStr); obj.put("assignNum", assignNum); obj.put("totalNeed", totalNeed); if (totalNeed <= 0) { obj.put("status", 0); obj.put("statusText", "无班次"); } else if (assignNum >= totalNeed) { obj.put("status", 1); obj.put("statusText", "已排班"); } else { obj.put("status", 2); obj.put("statusText", "未排满"); } dayList.add(obj); } Map result = new HashMap<>(); result.put("month", month); result.put("totalNeed", totalNeed); result.put("days", dayList); return ResultUtil.success(result); } catch (Exception ex) { ex.printStackTrace(); return ResultUtil.runErr(); } } /** * 排班管理-某天班次详情 */ @RequestMapping(value = "/schedule/dayDetail") @ResponseBody public Object scheduleDayDetail(Integer lineId, Integer companyId, String day) { try { if (!hasLineCompanies(lineId)) { return ResultUtil.error("线路未关联企业"); } List> shiftList = lineShiftDriverMapper.getDayShiftSchedule(lineId, day, companyId, null); for (Map shift : shiftList) { Integer lineShiftId = Integer.valueOf(String.valueOf(shift.get("lineShiftId"))); List> drivers = lineShiftDriverMapper.getDayShiftDrivers(lineShiftId, day, companyId, null); int carNum = shift.get("carNum") == null ? 0 : Integer.parseInt(String.valueOf(shift.get("carNum"))); int assignNum = shift.get("assignNum") == null ? 0 : Integer.parseInt(String.valueOf(shift.get("assignNum"))); shift.put("drivers", drivers); shift.put("remainNum", Math.max(carNum - assignNum, 0)); shift.put("canAdd", assignNum < carNum ? 1 : 0); } return ResultUtil.success(shiftList); } catch (Exception ex) { ex.printStackTrace(); return ResultUtil.runErr(); } } /** * 排班管理-可添加司机 */ @RequestMapping(value = "/schedule/drivers") @ResponseBody public Object scheduleDrivers(Integer lineId, Integer companyId, Integer lineShiftId, String day) { try { if (!hasLineCompanies(lineId)) { return ResultUtil.error("线路未关联企业"); } return ResultUtil.success(lineShiftDriverMapper.getAvailableDrivers(lineId, lineShiftId, day, companyId, null)); } catch (Exception ex) { ex.printStackTrace(); return ResultUtil.runErr(); } } /** * 排班管理-新增排班 */ @RequestMapping(value = "/schedule/add") @ResponseBody public Object scheduleAdd(Integer lineId, Integer companyId, Integer lineShiftId, String day, @RequestParam String driverIds) { try { if (!hasLineCompanies(lineId)) { return ResultUtil.error("线路未关联企业"); } TLineShift shift = itLineShiftService.selectById(lineShiftId); if (shift == null || shift.getState() == 3 || !shift.getLineId().equals(lineId)) { return ResultUtil.error("班次不存在"); } Date dayDate = parseDay(day); List> currentDrivers = lineShiftDriverMapper.getDayShiftDrivers(lineShiftId, day, companyId, null); int currentAssignNum = currentDrivers == null ? 0 : currentDrivers.size(); int carNum = shift.getCarNum() == null ? 0 : shift.getCarNum(); if (currentAssignNum >= carNum) { return ResultUtil.error("当前班次已排满"); } List driverIdList = parseIdList(driverIds); if (driverIdList.size() <= 0) { return ResultUtil.error("请选择司机"); } List> availableDrivers = lineShiftDriverMapper.getAvailableDrivers(lineId, lineShiftId, day, companyId, null); Map seatMap = new HashMap<>(); for (Map driver : availableDrivers) { Integer driverId = Integer.valueOf(String.valueOf(driver.get("driverId"))); Integer seatNum = driver.get("seatNum") == null ? 5 : Integer.valueOf(String.valueOf(driver.get("seatNum"))); seatMap.put(driverId, seatNum); } List validDriverIds = new ArrayList<>(); for (Integer driverId : driverIdList) { if (seatMap.containsKey(driverId)) { validDriverIds.add(driverId); } } if (validDriverIds.size() <= 0) { return ResultUtil.error("没有可添加的司机"); } if (currentAssignNum + validDriverIds.size() > carNum) { return ResultUtil.error("可排班人数不足,当前班次最多还能添加" + (carNum - currentAssignNum) + "名司机"); } for (Integer driverId : validDriverIds) { Integer count = lineShiftDriverMapper.countExistsSchedule(lineShiftId, driverId, day); if (count != null && count > 0) { continue; } int seatNum = seatMap.get(driverId) == null ? 5 : seatMap.get(driverId); if (seatNum <= 0) { seatNum = 5; } LineShiftDriver record = new LineShiftDriver(); record.setLineShiftId(lineShiftId); record.setDriverId(driverId); record.setDay(dayDate); record.setTotalSeat(seatNum); record.setLaveSeat(seatNum); record.setLaveSeatNumber(buildSeatNoStr(seatNum)); record.setInserTime(new Date()); record.setAssignType(2); lineShiftDriverMapper.insert(record); } return ResultUtil.success(); } catch (Exception ex) { ex.printStackTrace(); return ResultUtil.runErr(); } } /** * 排班管理-删除手动排班 */ @RequestMapping(value = "/schedule/delete") @ResponseBody public Object scheduleDelete(Integer scheduleId, Integer lineId, Integer companyId) { try { if (!hasLineCompanies(lineId)) { return ResultUtil.error("线路未关联企业"); } LineShiftDriver record = lineShiftDriverMapper.selectById(scheduleId); if (record == null) { return ResultUtil.error("排班记录不存在"); } if (record.getAssignType() == null || record.getAssignType() != 2) { return ResultUtil.error("仅支持删除手动排班记录"); } TLineShift shift = itLineShiftService.selectById(record.getLineShiftId()); if (shift == null || !shift.getLineId().equals(lineId)) { return ResultUtil.error("排班记录线路不匹配"); } TDriver driver = tDriverService.selectById(record.getDriverId()); if (driver == null || !checkDriverBelongLineCompanies(driver, lineId)) { return ResultUtil.error("司机企业信息不匹配"); } lineShiftDriverMapper.deleteById(scheduleId); return ResultUtil.success(); } catch (Exception ex) { ex.printStackTrace(); return ResultUtil.runErr(); } } /** * 排班管理-复制排班 */ @RequestMapping(value = "/schedule/copy") @ResponseBody public Object scheduleCopy(Integer lineId, Integer companyId, String sourceStart, String sourceEnd, String targetStart) { try { if (!hasLineCompanies(lineId)) { return ResultUtil.error("线路未关联企业"); } Date sourceStartDate = parseDay(sourceStart); Date sourceEndDate = parseDay(sourceEnd); Date targetStartDate = parseDay(targetStart); if (sourceEndDate.before(sourceStartDate)) { return ResultUtil.error("复制结束日期不能早于开始日期"); } List sourceList = lineShiftDriverMapper.getManualSchedulesForCopy(lineId, sourceStart, sourceEnd, companyId, null); int addNum = 0; int skipNum = 0; for (LineShiftDriver source : sourceList) { int diff = getDayDiff(sourceStartDate, source.getDay()); Date targetDay = addDays(targetStartDate, diff); String targetDayStr = formatDay(targetDay); Integer count = lineShiftDriverMapper.countExistsSchedule(source.getLineShiftId(), source.getDriverId(), targetDayStr); if (count != null && count > 0) { skipNum++; continue; } LineShiftDriver target = new LineShiftDriver(); target.setLineShiftId(source.getLineShiftId()); target.setDriverId(source.getDriverId()); target.setDay(targetDay); target.setLaveSeat(source.getLaveSeat()); target.setLaveSeatNumber(source.getLaveSeatNumber()); target.setTotalSeat(source.getTotalSeat()); target.setInserTime(new Date()); target.setAssignType(2); lineShiftDriverMapper.insert(target); addNum++; } Map result = new HashMap<>(); result.put("addNum", addNum); result.put("skipNum", skipNum); return ResultUtil.success(result); } catch (Exception ex) { ex.printStackTrace(); return ResultUtil.runErr(); } } /** * 排班管理-保存智能排班开关 */ @RequestMapping(value = "/schedule/autoPlan") @ResponseBody public Object scheduleAutoPlan(Integer lineId, Integer companyId, Integer autoPlan) { if (autoPlan == null || (autoPlan != 1 && autoPlan != 2)) { return ResultUtil.error("智能排班参数错误"); } if (companyId == null) { companyId = getDefaultLineCompanyId(lineId); } TLineCompany lineCompany = checkLineCompany(lineId, companyId); if (lineCompany == null) { return ResultUtil.error("线路未关联该企业"); } lineCompany.setAutoPlan(autoPlan); itLineCompanyService.updateById(lineCompany); return ResultUtil.success(); } /** * 修改跨城站点管理 */ @RequestMapping(value = "/update") @ResponseBody public ResultUtil update(TLine tLine,Integer startSiteId,Integer endSiteId,@RequestParam(required = false) String subArr, @RequestParam String shuArr, @RequestParam(required = false) String destArr) { if (!hasCommissionSetting(tLine.getOutSetting())) { return ResultUtil.error("请设置司机抽成"); } ResultUtil charterPriceResult = validateCharterPrice(tLine); if (200 != charterPriceResult.getStatus()) { return charterPriceResult; } JSONArray jsonArray1 = JSON.parseArray(shuArr); int size1 = jsonArray1.size(); for (int i = 0; i < size1; i++) { JSONObject jsonObject = jsonArray1.getJSONObject(i); String shiftTime = jsonObject.getString("shiftTime"); if (SinataUtil.isNotEmpty(shiftTime)){ String[] timeArray = shiftTime.split(" - "); String startTime = timeArray[0]; String endTime = timeArray[1]; if(startTime.compareTo(endTime) >= 0){ return ResultUtil.error("班次结束时间不能小于开始时间"); } } } String name = tLine.getName(); name = name.replaceAll("& lt;", "<"); name = name.replaceAll("& gt;", ">"); name = name.replaceAll("& #40;", "("); name = name.replaceAll("& #41;", ")"); tLine.setName(name); tLineService.updateById(tLine); //修改站点起点 TLineSite start = itLineSiteService.selectOne(new EntityWrapper().eq("lineId", tLine.getId()).eq("type", 1)); if (SinataUtil.isNotEmpty(start)){ if (start.getSiteId().intValue() != startSiteId.intValue()){ start.setSiteId(startSiteId); itLineSiteService.updateById(start); } }else{ start = new TLineSite(); start.setLineId(tLine.getId()); start.setSiteId(startSiteId); start.setType(1); itLineSiteService.insert(start); } //修改站点终点 TLineSite end = itLineSiteService.selectOne(new EntityWrapper().eq("lineId", tLine.getId()).eq("type", 2)); if (SinataUtil.isNotEmpty(end)){ if (end.getSiteId().intValue() != endSiteId.intValue()){ end.setSiteId(endSiteId); itLineSiteService.updateById(end); } }else{ end = new TLineSite(); end.setLineId(tLine.getId()); end.setSiteId(endSiteId); end.setType(2); itLineSiteService.insert(end); } //删除线路班次 itLineShiftService.delete(new EntityWrapper().eq("lineId",tLine.getId())); //添加班次设置 ResultUtil shiftResult = addShift(tLine.getId(), shuArr); if(200 != shiftResult.getStatus()){ return shiftResult; } //删除线路目的地并重建 itLineDestinationService.delete(new EntityWrapper().eq("lineId", tLine.getId())); ResultUtil destinationResult = addDestination(tLine.getId(), destArr); if(200 != destinationResult.getStatus()){ return destinationResult; } return ResultUtil.success(); } /** * 添加线路目的地配置 */ private boolean hasCommissionSetting(String outSetting) { if (SinataUtil.isEmpty(outSetting)) { return false; } try { JSONObject config = JSON.parseObject(outSetting); JSONArray rules = config.getJSONArray("rules"); if (hasCommissionRule(rules)) { return true; } JSONArray timeRules = config.getJSONArray("time_rules"); if (timeRules != null) { for (int i = 0; i < timeRules.size(); i++) { JSONArray oneTimeRules = timeRules.getJSONArray(i); if (hasCommissionRule(oneTimeRules)) { return true; } } } } catch (Exception e) { return false; } return false; } private ResultUtil validateCharterPrice(TLine tLine) { if (tLine.getCharterPrice5() == null) { return ResultUtil.error("请填写5座包车价格"); } if (tLine.getCharterPrice6() == null) { return ResultUtil.error("请填写6座包车价格"); } if (tLine.getCharterPrice7() == null) { return ResultUtil.error("请填写7座包车价格"); } if (tLine.getCharterPrice5().compareTo(BigDecimal.ZERO) < 0 || tLine.getCharterPrice6().compareTo(BigDecimal.ZERO) < 0 || tLine.getCharterPrice7().compareTo(BigDecimal.ZERO) < 0) { return ResultUtil.error("包车价格不能小于0"); } return ResultUtil.success(); } private boolean hasCommissionRule(JSONArray rules) { if (rules == null || rules.size() <= 0) { return false; } for (int i = 0; i < rules.size(); i++) { JSONObject rule = rules.getJSONObject(i); if (rule != null && rule.containsKey("percent") && SinataUtil.isNotEmpty(rule.getString("percent"))) { return true; } } return false; } private ResultUtil addDestination(Integer lineId, String destArr) { if (SinataUtil.isEmpty(destArr)) { return ResultUtil.error("请至少设置一个目的地"); } JSONArray jsonArray = JSON.parseArray(destArr); int size = jsonArray.size(); if(size <= 0){ return ResultUtil.error("请至少设置一个目的地"); } if(size > 3){ return ResultUtil.error("最多只能添加3个目的地"); } int mainCount = 0; for (int i = 0; i < size; i++) { JSONObject jsonObject = jsonArray.getJSONObject(i); String name = ToolUtil.formatting(jsonObject.getString("name")); if (SinataUtil.isEmpty(name)) { return ResultUtil.error("目的地名称不能为空"); } Integer fenceType = jsonObject.getInteger("fenceType"); if (fenceType == null || (fenceType != 1 && fenceType != 2)) { return ResultUtil.error("围栏类型错误"); } String coordinate = jsonObject.getString("coordinate"); if (SinataUtil.isEmpty(coordinate)) { return ResultUtil.error("目的地围栏不能为空"); } BigDecimal price5; BigDecimal price6; BigDecimal price7; try { price5 = new BigDecimal(jsonObject.getString("price5")); price6 = jsonObject.getString("price6") != null ? new BigDecimal(jsonObject.getString("price6")) : BigDecimal.ZERO; price7 = new BigDecimal(jsonObject.getString("price7")); } catch (Exception ex) { return ResultUtil.error("目的地价格格式不正确"); } if (price5.compareTo(BigDecimal.ZERO) < 0 || price6.compareTo(BigDecimal.ZERO) < 0 || price7.compareTo(BigDecimal.ZERO) < 0) { return ResultUtil.error("目的地价格不能小于0"); } Integer isMain = jsonObject.getInteger("isMain"); if (isMain != null && isMain == 1) { mainCount++; } if(mainCount > 1){ return ResultUtil.error("最多只能设置一个主要目的地"); } Double defaultPointLon = jsonObject.getDouble("defaultPointLon"); Double defaultPointLat = jsonObject.getDouble("defaultPointLat"); if (fenceType == 1) { String[] arr = coordinate.split(";"); if (arr.length != 2) { return ResultUtil.error("圆形围栏坐标格式错误"); } String[] center = arr[0].split(","); if (center.length != 2) { return ResultUtil.error("圆形围栏中心点格式错误"); } try { defaultPointLon = Double.valueOf(center[0]); defaultPointLat = Double.valueOf(center[1]); double radius = Double.valueOf(arr[1]); if(radius <= 0 || radius > 5000){ return ResultUtil.error("圆形围栏半径必须在(0,5000]米"); } } catch (Exception ex) { return ResultUtil.error("圆形围栏坐标格式错误"); } } else { if(defaultPointLon == null || defaultPointLat == null){ return ResultUtil.error("多边形围栏需要设置默认点"); } } TLineDestination destination = new TLineDestination(); destination.setLineId(lineId); destination.setName(name); destination.setFenceType(fenceType); destination.setCoordinate(coordinate); destination.setDefaultPointLon(defaultPointLon); destination.setDefaultPointLat(defaultPointLat); destination.setPrice5(price5); destination.setPrice6(price6); destination.setPrice7(price7); Integer state = jsonObject.getInteger("state"); destination.setState(state == null ? 1 : state); destination.setIsMain(isMain == null ? 0 : isMain); destination.setAddTime(jsonObject.getString("time")); destination.setInsertTime(new Date()); itLineDestinationService.insert(destination); } return ResultUtil.success(); } private TLineCompany checkLineCompany(Integer lineId, Integer companyId) { if (lineId == null || companyId == null) { return null; } return itLineCompanyService.selectOne(new EntityWrapper().eq("lineId", lineId).eq("companyId", companyId)); } private List getLineCompanies(Integer lineId) { if (lineId == null) { return new ArrayList<>(); } return itLineCompanyService.selectList(new EntityWrapper().eq("lineId", lineId).orderBy("id", true)); } private boolean hasLineCompanies(Integer lineId) { return getLineCompanies(lineId).size() > 0; } private Integer getDefaultLineCompanyId(Integer lineId) { List lineCompanies = getLineCompanies(lineId); if (lineCompanies.size() <= 0) { return null; } return lineCompanies.get(0).getCompanyId(); } private String getLineCompanyNames(List lineCompanies) { if (lineCompanies == null || lineCompanies.size() <= 0) { return ""; } List names = new ArrayList<>(); for (TLineCompany lineCompany : lineCompanies) { if (lineCompany == null || lineCompany.getCompanyId() == null) { continue; } TCompany company = itCompanyService.selectById(lineCompany.getCompanyId()); if (company != null && SinataUtil.isNotEmpty(company.getName())) { names.add(company.getName()); } } return String.join("、", names); } private Integer getCompanyType(Integer companyId) { TCompany company = itCompanyService.selectById(companyId); return company == null ? null : company.getType(); } private boolean checkDriverBelongCompany(TDriver driver, Integer companyId) { if (driver == null || companyId == null) { return false; } Integer companyType = getCompanyType(companyId); if (companyType == null) { return false; } if (companyType == 2) { return driver.getCompanyId() != null && driver.getCompanyId().intValue() == companyId.intValue(); } if (companyType == 3) { return driver.getFranchiseeId() != null && driver.getFranchiseeId().intValue() == companyId.intValue(); } return false; } private boolean checkDriverBelongLineCompanies(TDriver driver, Integer lineId) { if (driver == null || lineId == null) { return false; } List lineCompanies = getLineCompanies(lineId); for (TLineCompany lineCompany : lineCompanies) { if (checkDriverBelongCompany(driver, lineCompany.getCompanyId())) { return true; } } return false; } private Date parseDay(String day) throws Exception { return new SimpleDateFormat("yyyy-MM-dd").parse(day); } private String formatDay(Date day) { return new SimpleDateFormat("yyyy-MM-dd").format(day); } private int getDayDiff(Date start, Date end) { long oneDay = 24L * 60L * 60L * 1000L; long startTime = start.getTime() / oneDay; long endTime = end.getTime() / oneDay; return (int) (endTime - startTime); } private Date addDays(Date date, int days) { Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.add(Calendar.DAY_OF_MONTH, days); return cal.getTime(); } private String buildSeatNoStr(int seatNum) { StringBuilder builder = new StringBuilder(); for (int i = 1; i <= seatNum; i++) { if (i > 1) { builder.append(","); } builder.append(i); } return builder.toString(); } private List parseIdList(String ids) { List list = new ArrayList<>(); if (SinataUtil.isEmpty(ids)) { return list; } try { if (ids.startsWith("[")) { JSONArray array = JSON.parseArray(ids); for (int i = 0; i < array.size(); i++) { Integer id = array.getInteger(i); if (id != null && !list.contains(id)) { list.add(id); } } } else { String[] arr = ids.split(","); for (String s : arr) { if (SinataUtil.isNotEmpty(s)) { Integer id = Integer.valueOf(s.trim()); if (!list.contains(id)) { list.add(id); } } } } } catch (Exception ex) { ex.printStackTrace(); } return list; } }