Junjie
2024-03-26 26956bc393bb08eb868aeafde10c187336ad5888
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
package com.zy.asrs.wcs.core.map.service;
 
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zy.asrs.framework.common.Cools;
import com.zy.asrs.framework.exception.CoolException;
import com.zy.asrs.wcs.common.domain.enums.DictType;
import com.zy.asrs.wcs.core.map.controller.param.MapDataParam;
import com.zy.asrs.wcs.core.map.controller.param.MapQueryParam;
import com.zy.asrs.wcs.system.entity.Dict;
import com.zy.asrs.wcs.system.service.DictService;
import com.zy.asrs.wcs.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.Date;
import java.util.Optional;
 
/**
 * Created by vincent on 3/15/2024
 */
@Service
public class MapService {
 
    @Autowired
    private UserService userService;
    @Autowired
    private DictService dictService;
 
    public String getMapFloorList(Long userId) {
        String floorKey = "map-floor-list";
        Dict dict = dictService.getOne(new LambdaQueryWrapper<Dict>().eq(Dict::getFlag, floorKey));
        if (null == dict) {
            dict = new Dict();
            dict.setName(floorKey);
            dict.setFlag(floorKey);
            dict.setType(DictType.JSON.flag);
            dict.setValue("[\n" +
                    "    {\n" +
                    "        label: '1F',\n" +
                    "        value: 1\n" +
                    "    },\n" +
                    "    {\n" +
                    "        label: '2F',\n" +
                    "        value: 2\n" +
                    "    },\n" +
                    "    {\n" +
                    "        label: '3F',\n" +
                    "        value: 3\n" +
                    "    },\n" +
                    "    {\n" +
                    "        label: '4F',\n" +
                    "        value: 4\n" +
                    "    },\n" +
                    "    {\n" +
                    "        label: '5F',\n" +
                    "        value: 5\n" +
                    "    },\n" +
                    "]");
            dict.setCreateBy(userId);
            dict.setUpdateBy(userId);
            if (!dictService.save(dict)) {
                throw new CoolException("服务器内部错误");
            }
        }
        return Optional.ofNullable(dict).map(Dict::getValue).orElse(null);
    }
 
    public String getMapData(MapQueryParam param, Long userId) {
        String mapKey = getMapKey(param.getFloor());
        Dict dict = dictService.getOne(new LambdaQueryWrapper<Dict>().eq(Dict::getFlag, mapKey));
        return Optional.ofNullable(dict).map(Dict::getValue).orElse(null);
    }
 
    public void saveMapData(MapDataParam param, Long userId) {
        String mapKey = getMapKey(param.getFloor());
        Dict dict = dictService.getOne(new LambdaQueryWrapper<Dict>().eq(Dict::getFlag, mapKey));
        if (Cools.isEmpty(dict)) {
           dict = new Dict();
           dict.setName(mapKey);
           dict.setFlag(mapKey);
           dict.setType(DictType.JSON.flag);
           dict.setValue(JSON.toJSONString(param.getItemList()));
           dict.setCreateBy(userId);
           dict.setUpdateBy(userId);
           if (!dictService.save(dict)) {
               throw new CoolException("服务器内部错误");
           }
        } else {
            dict.setValue(JSON.toJSONString(param.getItemList()));
            dict.setUpdateTime(new Date());
            dict.setUpdateBy(userId);
            if (!dictService.updateById(dict)) {
                throw new CoolException("服务器内部错误");
            }
        }
    }
 
    private String getMapKey(Integer floor) {
        if (null != floor) {
            return "map-" + floor;
        }
        return "map";
    }
 
}