MrShi
17 小时以前 39fc2d6754953e41a7334a2166347baacfcfb40a
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
package com.doumee.service.business.impl;
 
import com.doumee.core.constants.ResponseStatus;
import com.doumee.core.exception.BusinessException;
import com.doumee.core.utils.Constants;
import com.doumee.dao.business.CarsMapper;
import com.doumee.dao.business.model.Cars;
import com.doumee.dao.business.model.JkCabinet;
import com.doumee.dao.business.model.JkCabinetGrid;
import com.doumee.service.business.third.model.LoginUserInfo;
import com.doumee.service.business.third.model.PageData;
import com.doumee.service.business.third.model.PageWrap;
import com.doumee.core.utils.Utils;
import com.doumee.dao.business.JkKeysMapper;
import com.doumee.dao.business.model.JkKeys;
import com.doumee.service.business.JkKeysService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.interfaces.MPJBaseJoin;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
 
import java.util.Date;
import java.util.List;
import java.util.Objects;
 
/**
 * 钥匙基本信息表Service实现
 * @author 江蹄蹄
 * @date 2025/09/28 09:01
 */
@Service
public class JkKeysServiceImpl implements JkKeysService {
 
    @Autowired
    private JkKeysMapper jkKeysMapper;
 
    @Autowired
    private CarsMapper carsMapper;
 
    @Override
    public Integer create(JkKeys jkKeys) {
        if(Objects.isNull(jkKeys)
        || Objects.isNull(jkKeys.getCarId())
        || Objects.isNull(jkKeys.getRoleType())
        || StringUtils.isBlank(jkKeys.getCode())
        || StringUtils.isBlank(jkKeys.getRfidLable())
        ){
            throw new BusinessException(ResponseStatus.BAD_REQUEST);
        }
        //查询编号是否重复
        if(jkKeysMapper.selectCount(new QueryWrapper<JkKeys>().lambda().eq(JkKeys::getCode,jkKeys.getCode()).eq(JkKeys::getIsdeleted,Constants.ZERO))>Constants.ZERO){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"钥匙编号已存在!");
        }
        Cars cars = carsMapper.selectById(jkKeys.getCarId());
        if(Objects.isNull(cars)||Constants.equalsInteger(cars.getIsdeleted(),Constants.ONE)){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"车辆信息未查询到!");
        }
        JkKeys carsKeys = jkKeysMapper.selectOne(new QueryWrapper<JkKeys>().lambda().eq(JkKeys::getCarId,jkKeys.getCarId()).eq(JkKeys::getIsdeleted,Constants.ZERO).last("limit 1"));
        if(Objects.nonNull(carsKeys)){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"当前车辆已被钥匙["+carsKeys.getCode()+"]绑定使用!");
        }
        JkKeys rfidKeys = jkKeysMapper.selectOne(new QueryWrapper<JkKeys>().lambda().eq(JkKeys::getRfidLable,jkKeys.getRfidLable()).eq(JkKeys::getIsdeleted,Constants.ZERO).last("limit 1"));
        if(Objects.nonNull(rfidKeys)){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"当前RFID标签已被钥匙["+rfidKeys.getCode()+"]使用!");
        }
        LoginUserInfo loginUserInfo = jkKeys.getLoginUserInfo();
        jkKeys.setCreateDate(new Date());
        jkKeys.setEditDate(jkKeys.getCreateDate());
        jkKeys.setCreator(loginUserInfo.getId());
        jkKeys.setEditor(loginUserInfo.getId());
        jkKeys.setIsdeleted(Constants.ZERO);
        jkKeys.setStatus(Constants.ZERO);
        jkKeys.setCarCode(cars.getCode());
        jkKeysMapper.insert(jkKeys);
        return jkKeys.getId();
    }
 
    @Override
    public void deleteById(Integer id) {
        JkKeys jkKeys = jkKeysMapper.selectById(id);
        if(Objects.isNull(jkKeys)||Constants.equalsInteger(jkKeys.getIsdeleted(),Constants.ONE)){
            throw new BusinessException(ResponseStatus.DATA_EMPTY);
        }
        if(!Constants.equalsInteger(jkKeys.getStatus(),Constants.ZERO)){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"钥匙已绑定,无法进行删除");
        }
        jkKeysMapper.deleteById(id);
    }
 
    @Override
    public void delete(JkKeys jkKeys) {
        UpdateWrapper<JkKeys> deleteWrapper = new UpdateWrapper<>(jkKeys);
        jkKeysMapper.delete(deleteWrapper);
    }
 
    @Override
    public void deleteByIdInBatch(List<Integer> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return;
        }
        jkKeysMapper.deleteBatchIds(ids);
    }
 
    @Override
    public void updateById(JkKeys jkKeys) {
        if(Objects.isNull(jkKeys)
                || Objects.isNull(jkKeys.getId())
                || Objects.isNull(jkKeys.getCarId())
                || Objects.isNull(jkKeys.getRoleType())
                || StringUtils.isBlank(jkKeys.getCode())
                || StringUtils.isBlank(jkKeys.getRfidLable())
        ){
            throw new BusinessException(ResponseStatus.BAD_REQUEST);
        }
        //查询编号是否重复
        if(jkKeysMapper.selectCount(new QueryWrapper<JkKeys>().lambda().ne(JkKeys::getId,jkKeys.getId()).eq(JkKeys::getCode,jkKeys.getCode()).eq(JkKeys::getIsdeleted,Constants.ZERO))>Constants.ZERO){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"钥匙编号已存在!");
        }
        Cars cars = carsMapper.selectById(jkKeys.getCarId());
        if(Objects.isNull(cars)||Constants.equalsInteger(cars.getIsdeleted(),Constants.ONE)){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"车辆信息未查询到!");
        }
        JkKeys carsKeys = jkKeysMapper.selectOne(new QueryWrapper<JkKeys>().lambda().ne(JkKeys::getId,jkKeys.getId()).eq(JkKeys::getCarId,jkKeys.getCarId()).eq(JkKeys::getIsdeleted,Constants.ZERO).last("limit 1"));
        if(Objects.nonNull(carsKeys)){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"当前车辆已被钥匙["+carsKeys.getCode()+"]绑定使用!");
        }
        JkKeys rfidKeys = jkKeysMapper.selectOne(new QueryWrapper<JkKeys>().lambda().ne(JkKeys::getId,jkKeys.getId()).eq(JkKeys::getRfidLable,jkKeys.getRfidLable()).eq(JkKeys::getIsdeleted,Constants.ZERO).last("limit 1"));
        if(Objects.nonNull(rfidKeys)){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"当前RFID标签已被钥匙["+rfidKeys.getCode()+"]使用!");
        }
        LoginUserInfo loginUserInfo = jkKeys.getLoginUserInfo();
        jkKeys.setEditDate(new Date());
        jkKeys.setEditor(loginUserInfo.getId());
        jkKeys.setCarCode(cars.getCode());
        jkKeysMapper.updateById(jkKeys);
    }
 
    @Override
    public void updateByIdInBatch(List<JkKeys> jkKeyss) {
        if (CollectionUtils.isEmpty(jkKeyss)) {
            return;
        }
        for (JkKeys jkKeys: jkKeyss) {
            this.updateById(jkKeys);
        }
    }
 
    @Override
    public JkKeys findById(Integer id) {
        return jkKeysMapper.selectById(id);
    }
 
    @Override
    public JkKeys findOne(JkKeys jkKeys) {
        QueryWrapper<JkKeys> wrapper = new QueryWrapper<>(jkKeys);
        return jkKeysMapper.selectOne(wrapper);
    }
 
    @Override
    public List<JkKeys> findList(JkKeys jkKeys) {
        jkKeys.setIsdeleted(Constants.ZERO);
        jkKeys.setStatus(Constants.ZERO);
        QueryWrapper<JkKeys> wrapper = new QueryWrapper<>(jkKeys);
        return jkKeysMapper.selectList(wrapper);
    }
  
    @Override
    public PageData<JkKeys> findPage(PageWrap<JkKeys> pageWrap) {
        IPage<JkKeys> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity());
        QueryWrapper<JkKeys> queryWrapper = new QueryWrapper<>();
        Utils.MP.blankToNull(pageWrap.getModel());
        JkKeys model = pageWrap.getModel();
        MPJLambdaWrapper<JkKeys> wrapper = new MPJLambdaWrapper<JkKeys>()
                .selectAll(JkKeys.class)
                .selectAs(JkCabinetGrid::getCode,JkKeys::getGridCode)
                .selectAs(JkCabinet::getName,JkKeys::getCabinetName)
                .leftJoin(Cars.class,Cars::getId,JkKeys::getCarId)
                .leftJoin(JkCabinetGrid.class,JkCabinetGrid::getKeyId,JkKeys::getId)
                .leftJoin(JkCabinet.class,JkCabinet::getId,JkCabinetGrid::getCabinetId)
                .eq(JkKeys::getIsdeleted,Constants.ZERO)
                .like(StringUtils.isNotBlank(model.getCode()),JkKeys::getCode,model.getCode())
                .like(StringUtils.isNotBlank(model.getCarCode()),JkKeys::getCarCode,model.getCode());
        IPage<JkKeys> iPage = jkKeysMapper.selectJoinPage(page,JkKeys.class,wrapper);
        for (JkKeys jkKeys:iPage.getRecords()) {
            jkKeys.setIsBinding(StringUtils.isBlank(jkKeys.getGridCode())?Constants.ZERO:Constants.ONE);
        }
        return PageData.from(iPage);
    }
 
 
 
    @Override
    public long count(JkKeys jkKeys) {
        QueryWrapper<JkKeys> wrapper = new QueryWrapper<>(jkKeys);
        return jkKeysMapper.selectCount(wrapper);
    }
}