huanghongfa
2021-09-02 177249c76aeea0b4bf8d8816d4994e3b445b45ce
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
package com.panzhihua.service_community.service.impl;
 
import java.util.List;
 
import javax.annotation.Resource;
 
import org.springframework.beans.BeanUtils;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.panzhihua.common.model.dtos.community.PageComStreetDTO;
import com.panzhihua.common.model.vos.R;
import com.panzhihua.common.model.vos.community.ComActVO;
import com.panzhihua.common.model.vos.community.ComStreetVO;
import com.panzhihua.service_community.dao.ComActDAO;
import com.panzhihua.service_community.dao.ComStreetDAO;
import com.panzhihua.service_community.model.dos.ComActDO;
import com.panzhihua.service_community.model.dos.ComStreetDO;
import com.panzhihua.service_community.service.ComStreetService;
 
/**
 * @author: llming
 * @description: 街道
 **/
@Service
public class ComStreetServiceImpl implements ComStreetService {
    @Resource
    private ComActDAO comActDAO;
 
    @Resource
    private ComStreetDAO comStreetDAO;
 
    /**
     * 新增社区
     *
     * @param comStreetVO
     *            街道信息
     * @return 新增结果
     */
    @Override
    public R addStreet(ComStreetVO comStreetVO) {
        String password = comStreetVO.getPassword();
        String encode = new BCryptPasswordEncoder().encode(password);
        comStreetVO.setPassword(encode);
        ComStreetDO comStreetDO = new ComStreetDO();
        LambdaQueryWrapper<ComStreetDO> param = new QueryWrapper<ComStreetDO>().lambda();
        param.eq(ComStreetDO::getName, comStreetVO.getName());
        param.eq(ComStreetDO::getProvinceCode, comStreetVO.getProvinceCode());
        param.eq(ComStreetDO::getCityCode, comStreetVO.getCityCode());
        param.eq(ComStreetDO::getAreaCode, comStreetVO.getAreaCode());
        Integer integer = comStreetDAO.selectCount(param);
        if (integer > 0) {
            return R.fail(500, "街道已经存在");
        }
        BeanUtils.copyProperties(comStreetVO, comStreetDO);
        int insert = comStreetDAO.insert(comStreetDO);
        if (insert > 0) {
            ComStreetDO comStreetDO1 = comStreetDAO.selectOne(param);
            BeanUtils.copyProperties(comStreetDO1, comStreetVO);
            return R.ok(comStreetVO);
        }
        return R.fail(500, "");
    }
 
    /**
     * 查询街道
     *
     * @param comStreetVO
     *            街道名
     * @return 社区集合
     */
    @Override
    public R listStreet(ComStreetVO comStreetVO) {
        LambdaQueryWrapper<ComStreetDO> param = new QueryWrapper<ComStreetDO>().lambda();
        if (comStreetVO.getProvinceCode() != null) {
            param.eq(ComStreetDO::getProvinceCode, comStreetVO.getProvinceCode());
        }
        if (comStreetVO.getCityCode() != null) {
            param.eq(ComStreetDO::getCityCode, comStreetVO.getCityCode());
        }
        if (comStreetVO.getAreaCode() != null) {
            param.eq(ComStreetDO::getAreaCode, comStreetVO.getAreaCode());
        }
        List<ComStreetVO> voList = Lists.newArrayList();
        List<ComStreetDO> comStreetDOS = comStreetDAO.selectList(param);
        comStreetDOS.forEach(dos -> {
            ComStreetVO vo = new ComStreetVO();
            BeanUtils.copyProperties(dos, vo);
            voList.add(vo);
        });
        return R.ok(voList);
    }
 
    /**
     * 社区详情
     *
     * @param streetId
     *            街道id
     * @return 社区详情
     */
    @Override
    public R detailStreet(Long streetId) {
        ComStreetDO comStreetDO = comStreetDAO.selectById(streetId);
        if (ObjectUtils.isEmpty(comStreetDO)) {
            return R.fail();
        }
        ComActVO comActVO = new ComActVO();
        BeanUtils.copyProperties(comStreetDO, comActVO);
        comActVO.setAreaName(comActDAO.selectAreaName(comActVO.getAreaCode()));
        return R.ok(comActVO);
    }
 
    /**
     * 分页查询社区
     *
     * @param pageComStreetDTO
     *            查询参数
     * @return 分页集合
     */
    @Override
    public R pageStreet(PageComStreetDTO pageComStreetDTO) {
        Page page = new Page<>();
        Long pageNum = pageComStreetDTO.getPageNum();
        Long pageSize = pageComStreetDTO.getPageSize();
        if (null == pageNum || 0 == pageNum) {
            pageNum = 1L;
        }
        if (null == pageSize || 0 == pageSize) {
            pageSize = 10L;
        }
        page.setSize(pageSize);
        page.setCurrent(pageNum);
        LambdaQueryWrapper<ComStreetDO> userLambdaQueryWrapper = Wrappers.lambdaQuery();
        if (pageComStreetDTO.getName() != null) {
            userLambdaQueryWrapper.like(ComStreetDO::getName, pageComStreetDTO.getName());
        }
        Page userPage = new Page(pageNum, pageSize);
        IPage<ComStreetDO> doPager =
            comStreetDAO.selectPage(userPage, userLambdaQueryWrapper.orderByDesc(ComStreetDO::getCreateAt));
        return R.ok(doPager);
    }
 
    /**
     * 批量删除街道
     *
     * @param streetIds
     *            社区id
     * @return 删除结果
     */
    @Override
    public R delectStreat(List<Long> streetIds) {
        // 不能删除,已经绑定了社区的街道
        LambdaQueryWrapper<ComActDO> comActDOParam =
            new QueryWrapper<ComActDO>().lambda().in(ComActDO::getStreetId, streetIds);
        List<ComActDO> comActDOS = comActDAO.selectList(comActDOParam);
        if (comActDOS != null && comActDOS.size() > 0) {
            return R.fail("删除失败,批量删除街道中,街道下有社区无法删除!");
        }
        int delete = comStreetDAO.deleteBatchIds(streetIds);
        if (delete > 0) {
            return R.ok();
        }
        return R.fail();
    }
 
}