唐耀东
2022-05-13 85e6498e0125ea32c9c743faad0ee0863c4b278a
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
package com.ruoyi.oa.service.impl;
 
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.http.HttpStatus;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.oa.domain.SgNvr;
import com.ruoyi.oa.mapper.SgNvrMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.oa.domain.bo.SgIpcBo;
import com.ruoyi.oa.domain.vo.SgIpcVo;
import com.ruoyi.oa.domain.SgIpc;
import com.ruoyi.oa.mapper.SgIpcMapper;
import com.ruoyi.oa.service.ISgIpcService;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.List;
import java.util.Map;
import java.util.Collection;
 
/**
 * ipc设备Service业务层处理
 *
 * @author ruoyi
 * @date 2022-05-11
 */
@Service
public class SgIpcServiceImpl extends ServicePlusImpl<SgIpcMapper, SgIpc, SgIpcVo> implements ISgIpcService {
 
    @Autowired
    private SgNvrMapper nvrMapper;
 
    @Override
    public SgIpcVo queryById(Long id) {
        return getVoById(id);
    }
 
    @Override
    public TableDataInfo<SgIpcVo> queryPageList(SgIpcBo bo) {
        PagePlus<SgIpc, SgIpcVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo));
        return PageUtils.buildDataInfo(result);
    }
 
    @Override
    public List<SgIpcVo> queryList(SgIpcBo bo) {
        return listVo(buildQueryWrapper(bo));
    }
 
    private LambdaQueryWrapper<SgIpc> buildQueryWrapper(SgIpcBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SgIpc> lqw = Wrappers.lambdaQuery();
        lqw.eq(SgIpc::getSchoolId, bo.getSchoolId());
        lqw.eq(bo.getNvrId() != null, SgIpc::getNvrId, bo.getNvrId());
        lqw.eq(bo.getBuildingId() != null, SgIpc::getBuildingId, bo.getBuildingId());
        lqw.eq(bo.getOrganizationId() != null, SgIpc::getOrganizationId, bo.getOrganizationId());
        lqw.eq(bo.getConstructionBatchId() != null, SgIpc::getConstructionBatchId, bo.getConstructionBatchId());
        lqw.like(StringUtils.isNotBlank(bo.getModel()), SgIpc::getModel, bo.getModel());
        lqw.orderByDesc(SgIpc::getUpdateTime);
        return lqw;
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertByBo(SgIpcBo bo) {
        List<SgIpc> list = baseMapper.selectList(new LambdaQueryWrapper<SgIpc>()
            .eq(SgIpc::getSchoolId, bo.getSchoolId()).eq(SgIpc::getMac, bo.getMac()));
        if (list.size() > 0) {
            throw new ServiceException("MAC重复", HttpStatus.HTTP_PARTIAL);
        }
        SgNvr nvr = nvrMapper.selectById(bo.getNvrId());
        if (!bo.getOrganizationId().equals(nvr.getOrganizationId())) {
            throw new ServiceException("IPC所属单位与所选NVR所属单位不符", HttpStatus.HTTP_PARTIAL);
        }
        SgIpc add = BeanUtil.toBean(bo, SgIpc.class);
        validEntityBeforeSave(add);
        boolean flag = save(add);
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByBo(SgIpcBo bo) {
        List<SgIpc> list = baseMapper.selectList(new LambdaQueryWrapper<SgIpc>()
            .eq(SgIpc::getSchoolId, bo.getSchoolId())
            .eq(SgIpc::getMac, bo.getMac())
            .ne(SgIpc::getId, bo.getId()));
        if (list.size() > 0) {
            throw new ServiceException("MAC重复", HttpStatus.HTTP_PARTIAL);
        }
        SgNvr nvr = nvrMapper.selectById(bo.getNvrId());
        if (!bo.getOrganizationId().equals(nvr.getOrganizationId())) {
            throw new ServiceException("IPC所属单位与所选NVR所属单位不符", HttpStatus.HTTP_PARTIAL);
        }
        SgIpc update = BeanUtil.toBean(bo, SgIpc.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }
 
    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(SgIpc entity) {
        //TODO 做一些数据校验,如唯一约束
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}