rk
2 天以前 8d2f5011aee911f31cba0e65a37dd700d6d39e0d
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
package com.doumee.service.business.impl;
 
import com.alibaba.druid.sql.visitor.functions.Concat;
import com.doumee.core.constants.ResponseStatus;
import com.doumee.core.exception.BusinessException;
import com.doumee.core.utils.Constants;
import com.doumee.core.utils.DateUtil;
import com.doumee.dao.business.JkKeysMapper;
import com.doumee.dao.business.model.Cars;
import com.doumee.dao.business.model.JkCabinet;
import com.doumee.dao.business.model.JkKeys;
import com.doumee.service.business.third.model.LoginUserInfo;
import com.doumee.service.business.third.model.PageData;
import com.doumee.core.utils.Utils;
import com.doumee.dao.business.JkCabinetGridMapper;
import com.doumee.dao.business.model.JkCabinetGrid;
import com.doumee.service.business.JkCabinetGridService;
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.doumee.service.business.third.model.PageWrap;
import com.github.yulichang.toolkit.MPJWrappers;
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 com.github.xiaoymin.knife4j.core.util.CollectionUtils;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
 
/**
 * 钥匙柜柜格基本信息表Service实现
 * @author 江蹄蹄
 * @date 2025/09/28 09:01
 */
@Service
public class JkCabinetGridServiceImpl implements JkCabinetGridService {
 
    @Autowired
    private JkCabinetGridMapper jkCabinetGridMapper;
 
    @Autowired
    private JkKeysMapper jkKeysMapper;
 
    @Override
    public Integer create(JkCabinetGrid jkCabinetGrid) {
        jkCabinetGridMapper.insert(jkCabinetGrid);
        return jkCabinetGrid.getId();
    }
 
    @Override
    public void deleteById(Integer id) {
        jkCabinetGridMapper.deleteById(id);
    }
 
    @Override
    public void delete(JkCabinetGrid jkCabinetGrid) {
        UpdateWrapper<JkCabinetGrid> deleteWrapper = new UpdateWrapper<>(jkCabinetGrid);
        jkCabinetGridMapper.delete(deleteWrapper);
    }
 
    @Override
    public void deleteByIdInBatch(List<Integer> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return;
        }
        jkCabinetGridMapper.deleteBatchIds(ids);
    }
 
    @Override
    public void updateById(JkCabinetGrid jkCabinetGrid) {
        if(Objects.isNull(jkCabinetGrid)
                || Objects.isNull(jkCabinetGrid.getId())
        ){
            throw new BusinessException(ResponseStatus.BAD_REQUEST);
        }
        LoginUserInfo loginUserInfo = jkCabinetGrid.getLoginUserInfo();
        JkCabinetGrid model = jkCabinetGridMapper.selectById(jkCabinetGrid.getId());
        if(Objects.isNull(model)|| Constants.equalsInteger(model.getIsdeleted(),Constants.ONE)){
            throw new BusinessException(ResponseStatus.DATA_EMPTY);
        }
        if(Objects.nonNull(jkCabinetGrid.getKeyId())){
            if(Objects.nonNull(model.getKeyId())){
                throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"已绑定,请勿重复绑定");
            }
            JkKeys keysModel = jkKeysMapper.selectById(jkCabinetGrid.getKeyId());
            if(Objects.isNull(keysModel)|| Constants.equalsInteger(keysModel.getIsdeleted(),Constants.ONE)){
                throw new BusinessException(ResponseStatus.DATA_EMPTY);
            }
            if(!Constants.equalsInteger(keysModel.getStatus(), (Constants.ZERO))){
                throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"钥匙已被绑定,请选择其他钥匙绑定");
            }
            //更新钥匙柜信息
            jkCabinetGridMapper.update(new UpdateWrapper<JkCabinetGrid>().lambda()
                    .set(JkCabinetGrid::getKeyId,jkCabinetGrid.getKeyId())
                    .set(JkCabinetGrid::getEditor,loginUserInfo.getId())
                    .set(JkCabinetGrid::getEditDate,DateUtil.getCurrDateTime())
                    .set(JkCabinetGrid::getEditDate, DateUtil.getCurrDateTime())
                    .eq(JkCabinetGrid::getId,jkCabinetGrid.getId()));
            //标记钥匙信息
            jkKeysMapper.update(new UpdateWrapper<JkKeys>().lambda()
                    .set(JkKeys::getStatus,Constants.ONE)
                    .eq(JkKeys::getId,jkCabinetGrid.getKeyId())
            );
            return;
        }else if(Objects.nonNull(jkCabinetGrid.getChannelCode())||Objects.nonNull(jkCabinetGrid.getBoardCode())){
            jkCabinetGridMapper.update(new UpdateWrapper<JkCabinetGrid>().lambda()
                    .set(Objects.nonNull(jkCabinetGrid.getChannelCode()),JkCabinetGrid::getChannelCode,jkCabinetGrid.getChannelCode())
                    .set(Objects.nonNull(jkCabinetGrid.getBoardCode()),JkCabinetGrid::getBoardCode,jkCabinetGrid.getBoardCode())
                    .set(JkCabinetGrid::getEditDate, DateUtil.getCurrDateTime())
                    .eq(JkCabinetGrid::getId,jkCabinetGrid.getId()));
        }
    }
 
    @Override
    public void updateByIdInBatch(List<JkCabinetGrid> jkCabinetGrids) {
        if (CollectionUtils.isEmpty(jkCabinetGrids)) {
            return;
        }
        for (JkCabinetGrid jkCabinetGrid: jkCabinetGrids) {
            this.updateById(jkCabinetGrid);
        }
    }
 
    @Override
    public JkCabinetGrid findById(Integer id) {
        return jkCabinetGridMapper.selectById(id);
    }
 
    @Override
    public JkCabinetGrid findOne(JkCabinetGrid jkCabinetGrid) {
        QueryWrapper<JkCabinetGrid> wrapper = new QueryWrapper<>(jkCabinetGrid);
        return jkCabinetGridMapper.selectOne(wrapper);
    }
 
    @Override
    public List<JkCabinetGrid> findList(JkCabinetGrid jkCabinetGrid) {
        QueryWrapper<JkCabinetGrid> wrapper = new QueryWrapper<>(jkCabinetGrid);
        return jkCabinetGridMapper.selectList(wrapper);
    }
  
    @Override
    public PageData<JkCabinetGrid> findPage(PageWrap<JkCabinetGrid> pageWrap) {
        IPage<JkCabinetGrid> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity());
        Utils.MP.blankToNull(pageWrap.getModel());
        JkCabinetGrid model = pageWrap.getModel();
        MPJLambdaWrapper<JkCabinetGrid> wrapper = new MPJLambdaWrapper<JkCabinetGrid>()
                .selectAll(JkCabinetGrid.class)
                .selectAs(JkCabinet::getName,JkCabinetGrid::getCabinetName)
                .selectAs(JkKeys::getCode,JkCabinetGrid::getKeyCode)
                .leftJoin(JkCabinet.class,JkCabinet::getId,JkCabinetGrid::getCabinetId)
                .leftJoin(JkKeys.class,JkKeys::getId,JkCabinetGrid::getKeyId)
                .eq(JkCabinetGrid::getIsdeleted,Constants.ZERO)
                .eq(Objects.nonNull(model.getCabinetId()),JkCabinetGrid::getCabinetId,model.getCabinetId())
                .eq(Objects.nonNull(model.getStatus()),JkCabinetGrid::getStatus,model.getStatus())
                .isNull(Objects.nonNull(model.getBindStatus())&&Constants.equalsInteger(model.getBindStatus(),Constants.ZERO),JkCabinetGrid::getKeyId)
                .isNotNull(Objects.nonNull(model.getBindStatus())&&Constants.equalsInteger(model.getBindStatus(),Constants.ONE),JkCabinetGrid::getKeyId)
                .like(StringUtils.isNotBlank(model.getKeyCode()),JkKeys::getCode,model.getKeyCode());
        IPage<JkCabinetGrid> iPage = jkCabinetGridMapper.selectJoinPage(page,JkCabinetGrid.class,wrapper);
        for (JkCabinetGrid jkCabinetGrid:iPage.getRecords()) {
            jkCabinetGrid.setBindStatus(Objects.isNull(jkCabinetGrid.getKeyId())?Constants.ZERO:Constants.ONE);
        }
        return PageData.from(iPage);
    }
 
    @Override
    public long count(JkCabinetGrid jkCabinetGrid) {
        QueryWrapper<JkCabinetGrid> wrapper = new QueryWrapper<>(jkCabinetGrid);
        return jkCabinetGridMapper.selectCount(wrapper);
    }
 
    @Override
    public void updateStatusById(JkCabinetGrid jkCabinetGrid) {
        if(Objects.isNull(jkCabinetGrid)
                || Objects.isNull(jkCabinetGrid.getId())
                || Objects.isNull(jkCabinetGrid.getStatus())){
            throw new BusinessException(ResponseStatus.BAD_REQUEST);
        }
        jkCabinetGridMapper.update(null,new UpdateWrapper<JkCabinetGrid>()
                .lambda().set(JkCabinetGrid::getStatus,jkCabinetGrid.getStatus())
                .set(JkCabinetGrid::getEditDate, DateUtil.getCurrDateTime())
                .eq(JkCabinetGrid::getId,jkCabinetGrid.getId()));
    }
 
 
    @Override
    @Transactional(rollbackFor = {Exception.class,BusinessException.class})
    public void unBindKeys(List<Integer> idList){
        if(CollectionUtils.isEmpty(idList)){
            throw new BusinessException(ResponseStatus.BAD_REQUEST);
        }
        if(idList.size()==Constants.ONE){
            JkCabinetGrid model = jkCabinetGridMapper.selectById(idList.get(Constants.ZERO));
            if(Objects.isNull(model)|| Constants.equalsInteger(model.getIsdeleted(),Constants.ONE)){
                throw new BusinessException(ResponseStatus.DATA_EMPTY);
            }
            if(Objects.isNull(model.getKeyId())){
                throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"当前规格未绑定钥匙,请刷新查看");
            }else{
                //更新钥匙柜信息
                jkCabinetGridMapper.update(new UpdateWrapper<JkCabinetGrid>().lambda()
                        .set(JkCabinetGrid::getKeyId,null)
                        .set(JkCabinetGrid::getEditDate, DateUtil.getCurrDateTime())
                        .eq(JkCabinetGrid::getId,model.getId()));
                JkKeys keysModel = jkKeysMapper.selectById(model.getKeyId());
                if(Objects.nonNull(keysModel) && !Constants.equalsInteger(keysModel.getStatus(),Constants.ZERO)){
                    jkKeysMapper.update(new UpdateWrapper<JkKeys>().lambda()
                            .set(JkKeys::getStatus,Constants.ZERO)
                            .eq(JkKeys::getId,keysModel.getId())
                    );
                }
            }
        }else{
            List<JkCabinetGrid> jkCabinetGridList = jkCabinetGridMapper.selectList(new QueryWrapper<JkCabinetGrid>().lambda()
                            .eq(JkCabinetGrid::getIsdeleted,Constants.ZERO)
                            .isNotNull(JkCabinetGrid::getKeyId)
                    .in(JkCabinetGrid::getId,idList));
            if(CollectionUtils.isNotEmpty(jkCabinetGridList)){
                jkCabinetGridMapper.update(new UpdateWrapper<JkCabinetGrid>().lambda()
                        .set(JkCabinetGrid::getKeyId,null)
                        .set(JkCabinetGrid::getEditDate, DateUtil.getCurrDateTime())
                        .eq(JkCabinetGrid::getId,jkCabinetGridList.stream().map(i->i.getId()).collect(Collectors.toList())));
                List<Integer> keyIdList = jkCabinetGridList.stream().filter(i->Objects.nonNull(i.getKeyId())).map(i->i.getKeyId()).collect(Collectors.toList());
                if(CollectionUtils.isNotEmpty(keyIdList)){
                    jkKeysMapper.update(new UpdateWrapper<JkKeys>().lambda()
                            .set(JkKeys::getStatus,Constants.ZERO)
                            .in(JkKeys::getId,keyIdList)
                    );
                }
 
            }
        }
    }
 
 
 
 
 
}