package com.matrix.system.hive.action; import com.matrix.core.pojo.AjaxResult; import com.matrix.core.tools.DateUtil; import com.matrix.system.common.bean.SysUsers; import com.matrix.system.common.service.SysUsersService; import com.matrix.system.constance.Dictionary; import com.matrix.system.hive.bean.SysBeauticianState; import com.matrix.system.hive.bean.SysWorkBeatuistaff; import com.matrix.system.hive.dao.SysBeauticianStateDao; import com.matrix.system.hive.plugin.util.CollectionUtils; import com.matrix.core.tools.DateUtil; import com.matrix.system.hive.service.SysWorkBeatuistaffService; import com.matrix.system.hive.service.SysWorktimeService; import io.swagger.annotations.ApiModelProperty; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import javax.annotation.Resource; import java.util.ArrayList; import java.util.Date; import java.util.List; /** * @author jiangyouyao * @date 2016-12-24 * @description 占用Controller */ @RequestMapping(value = "admin/occupancy") @Controller public class OccupancyController extends BaseController { @Resource private SysUsersService shopstaffInfoService; @Resource private SysWorkBeatuistaffService sysWorkBeatuistaffService; @Resource private SysWorktimeService worktimeService; @Autowired private SysBeauticianStateDao beauticianStateDao; /** * 获取员工预约情况 */ @RequestMapping(value = "/getKanban") public @ResponseBody AjaxResult showCwzyList(String timeStr) { Long shopId = getMe().getShopId(); return getCwzyList(timeStr,shopId); } public AjaxResult getCwzyList(String timeStr,Long shopId){ if (timeStr == null || timeStr.equals("")) { timeStr = DateUtil.dateToString(new Date(), "yyyy-MM-dd"); } // 查询上班下班的最大时间段 Date currentDate = DateUtil.stringToDate(timeStr, DateUtil.DATE_FORMAT_DD); // 把日期调整为当前查询日期 Date maxTime = worktimeService.findMaxTime(shopId); //如果没有获取到门店的排班时间,无法查询占用情况 if (maxTime == null) { return AjaxResult.buildFailInstance("未查询到门店的排班情况"); } currentDate.setHours(maxTime.getHours()); currentDate.setMinutes(maxTime.getMinutes()); // 上班起始时间 maxTime = new Date(currentDate.getTime()); // 把日期调整为当前查询日期 Date minTime = worktimeService.findMinTime(shopId); currentDate.setHours(minTime.getHours()); currentDate.setMinutes(minTime.getMinutes()); // 上班起始时间 minTime = new Date(currentDate.getTime()); Date startTime = new Date(minTime.getTime()); Long maxSpan = DateUtil.getTimeSpan(maxTime, startTime, DateUtil.MINUTE); List occupancyVoList = new ArrayList<>(); // 获取所有的美疗师 List mlsList = shopstaffInfoService.findByRoleName(true, Dictionary.STAFF_POST_MLS); for (SysUsers user : mlsList) { OccupancyVo occupancyVo = new OccupancyVo(); occupancyVo.setUserName(user.getSuName()); List workStaffs = sysWorkBeatuistaffService.findWorkStaff(timeStr.replaceAll("-", ""), user.getSuId()); if (CollectionUtils.isEmpty(workStaffs)) { //员工休息 occupancyVo.getItems().add(new OccupancySPan(OccupancySPan.WORKTYPE_REST, maxSpan)); } else { //员工不休息1.查询当前以及排版的服务 List beauticianStates = beauticianStateDao.selectByTimeAndUset(startTime, maxTime, user.getSuId()); if (CollectionUtils.isEmpty(beauticianStates)) { //没有排班 occupancyVo.getItems().add(new OccupancySPan(OccupancySPan.WORKTYPE_WORK, maxSpan)); } else { //有排班 for (int i = 0; i < beauticianStates.size(); i++) { SysBeauticianState beauticianState = beauticianStates.get(i); //占用时间段 long occupancySpan = DateUtil.getTimeSpan(beauticianState.getBeginTime(), beauticianState.getEndTime(), DateUtil.MINUTE); StringBuilder msg = new StringBuilder(); OccupancySPan occupancySPan = new OccupancySPan(); occupancySPan.setVipName(beauticianState.getVipName()); if(beauticianState.getProjUse()!=null){ occupancySPan.setProjName(beauticianState.getProjUse().getProjName()); } occupancySPan.setBedName(beauticianState.getBedName()); occupancySPan.setSpanLength(occupancySpan); occupancySPan.setWorkType(OccupancySPan.WORKTYPE_OCCUPANCY); occupancySPan.setServiceId(beauticianState.getServicesId()); occupancySPan.setServiceState(beauticianState.getState()); if (i == 0) { //计算上班时间到第一次盘版直接的空隙 long span = DateUtil.getTimeSpan(minTime, beauticianState.getBeginTime(), DateUtil.MINUTE); occupancyVo.getItems().add(new OccupancySPan(OccupancySPan.WORKTYPE_WORK, span)); } else { if (occupancyVo.isNew(occupancySPan)) { //非第一次则计算上一次的结束时间到本次开始时间之间的间隙 SysBeauticianState beforeBeauticianState = beauticianStates.get(i - 1); long span = DateUtil.getTimeSpan(beforeBeauticianState.getEndTime(), beauticianState.getBeginTime(), DateUtil.MINUTE); occupancyVo.getItems().add(new OccupancySPan(OccupancySPan.WORKTYPE_WORK, span)); } } occupancyVo.addItem(occupancySPan); //最后的占用时间 if (i == beauticianStates.size() - 1) { if (DateUtil.isBeforeDate(maxTime, beauticianState.getEndTime())) { long span = DateUtil.getTimeSpan(beauticianState.getEndTime(), maxTime, DateUtil.MINUTE); occupancyVo.getItems().add(new OccupancySPan(OccupancySPan.WORKTYPE_WORK, span)); } } } } } occupancyVoList.add(occupancyVo); } AjaxResult result = AjaxResult.buildSuccessInstance(occupancyVoList); //计算表头的时间间隔 buildTimeSpan(startTime, maxTime, result); return result; } private void buildTimeSpan(Date startTime, Date maxTime, AjaxResult result) { //每30分钟算一次 List tableHead = new ArrayList<>(); int minutes = startTime.getMinutes(); if (minutes != 0 && minutes != 30) { if (minutes < 30) { minutes = 0; } else { minutes = 30; } } long timeSpan = DateUtil.getTimeSpan(startTime, maxTime, DateUtil.MINUTE); long i = 0; if (timeSpan % 30 == 0) { i = timeSpan / 30; } else { i = timeSpan / 30 + 1; } startTime.setMinutes(minutes); //第一个时间不要 startTime = DateUtil.getDateAfterMinute(startTime, 30); while (i > 0) { tableHead.add(DateUtil.dateToString(startTime, "HH:mm")); startTime = DateUtil.getDateAfterMinute(startTime, 30); i--; } result.putInMap("tableHead", tableHead); result.putInMap("timeSpan", timeSpan); } class OccupancyVo { String userName; List items = new ArrayList<>(); public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public List getItems() { return items; } public void setItems(List items) { this.items = items; } public void addItem(OccupancySPan occupancySPan) { //服务单id相同的合并msg,时间跨度取时间最长的 boolean isNew = true; for (OccupancySPan oldSpan : items) { if (occupancySPan.getServiceId() != null && occupancySPan.getServiceId().equals(oldSpan.getServiceId())) { isNew = false; oldSpan.setSpanLength(oldSpan.spanLength > occupancySPan.spanLength ? oldSpan.spanLength : occupancySPan.spanLength); oldSpan.setProjName(oldSpan.getProjName() + "/" + occupancySPan.getProjName()); break; } } if (isNew) { items.add(occupancySPan); } } public boolean isNew(OccupancySPan occupancySPan) { boolean isNew = true; for (OccupancySPan oldSpan : items) { if (occupancySPan.getServiceId() != null && occupancySPan.getServiceId().equals(oldSpan.getServiceId())) { isNew = false; break; } } return isNew; } } public class OccupancySPan { /** * 休息 */ static final int WORKTYPE_REST = 1; /** * 上班 */ static final int WORKTYPE_WORK = 2; /** * 占用 */ static final int WORKTYPE_OCCUPANCY = 3; @ApiModelProperty(value = "色块长度") Long spanLength; @ApiModelProperty(value = "占用状态,1休息,2 上班,3 占用") int workType; @ApiModelProperty(value = "床位名称") String bedName; @ApiModelProperty(value = "项目名称") String projName; @ApiModelProperty(value = "会员名称") String vipName; @ApiModelProperty(value = "服务单id") Long serviceId; @ApiModelProperty(value = "服务单状态") String serviceState; public OccupancySPan(int workType, Long spanLength) { this.workType = workType; this.spanLength = spanLength; } public OccupancySPan() { } public Long getSpanLength() { return spanLength; } public void setSpanLength(Long spanLength) { this.spanLength = spanLength; } public int getWorkType() { return workType; } public void setWorkType(int workType) { this.workType = workType; } public String getBedName() { return bedName; } public void setBedName(String bedName) { this.bedName = bedName; } public String getProjName() { return projName; } public void setProjName(String projName) { this.projName = projName; } public String getVipName() { return vipName; } public void setVipName(String vipName) { this.vipName = vipName; } public Long getServiceId() { return serviceId; } public void setServiceId(Long serviceId) { this.serviceId = serviceId; } public String getServiceState() { return serviceState; } public void setServiceState(String serviceState) { this.serviceState = serviceState; } } }