wzy
2021-01-19 cc74efb36fd1f1de56b3acb2ba59d4b416eaed4f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
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<OccupancyVo> occupancyVoList = new ArrayList<>();
        // 获取所有的美疗师
        List<SysUsers> mlsList = shopstaffInfoService.findByRoleName(true, Dictionary.STAFF_POST_MLS);
        for (SysUsers user : mlsList) {
            OccupancyVo occupancyVo = new OccupancyVo();
            occupancyVo.setUserName(user.getSuName());
            List<SysWorkBeatuistaff> workStaffs = sysWorkBeatuistaffService.findWorkStaff(timeStr.replaceAll("-", ""), user.getSuId());
 
            if (CollectionUtils.isEmpty(workStaffs)) {
                //员工休息
                occupancyVo.getItems().add(new OccupancySPan(OccupancySPan.WORKTYPE_REST, maxSpan));
            } else {
                //员工不休息1.查询当前以及排版的服务
                List<SysBeauticianState> 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<String> 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<OccupancySPan> items = new ArrayList<>();
 
        public String getUserName() {
            return userName;
        }
 
        public void setUserName(String userName) {
            this.userName = userName;
        }
 
        public List<OccupancySPan> getItems() {
            return items;
        }
 
        public void setItems(List<OccupancySPan> 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;
        }
    }
 
 
}