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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package com.moral.api.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.moral.api.config.mybatis.wrapper.NullFilterWrapper;
import com.moral.api.entity.Organization;
import com.moral.api.mapper.OrganizationMapper;
import com.moral.api.pojo.dto.organization.OrganizationDTO;
import com.moral.api.pojo.dto.organization.OrganizationQueryDTO;
import com.moral.api.pojo.form.organization.OrganizationDeleteForm;
import com.moral.api.pojo.form.organization.OrganizationInsertForm;
import com.moral.api.pojo.form.organization.OrganizationQueryForm;
import com.moral.api.pojo.form.organization.OrganizationUpdateForm;
import com.moral.api.service.OrganizationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moral.constant.Constants;
import com.moral.constant.ResponseCodeEnum;
import com.moral.util.ConvertUtils;
import com.moral.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
/**
 * <p>
 * 组织表 服务实现类
 * </p>
 *
 * @author moral
 * @since 2021-03-09
 */
@Service
public class OrganizationServiceImpl extends ServiceImpl<OrganizationMapper, Organization> implements OrganizationService {
 
    @Autowired
    OrganizationMapper organizationMapper;
 
    /**
     * @Description: 添加客户组织
     * @Param: [organizationInsertForm]
     * @return: void
     * @Author: 陈凯裕
     * @Date: 2021/3/22
     */
    @Override
    @Transactional
    public OrganizationDTO insertOrganization(OrganizationInsertForm organizationInsertForm) {
        OrganizationDTO organizationDTO = new OrganizationDTO();
        QueryWrapper<Organization> queryWrapper = new QueryWrapper<>();
        //form转换entity
        Organization organization = organizationInsertForm.formConvertEntity();
        //查询组织名称是否已经存在
        Organization existOrganization = new Organization();
        existOrganization.setName(organization.getName());
        existOrganization.setIsDelete(Constants.NOT_DELETE);
        queryWrapper.setEntity(existOrganization);
        Organization existOrganizationResult = organizationMapper.selectOne(queryWrapper);
        if (!ObjectUtils.isEmpty(existOrganizationResult)) {
            organizationDTO.setCode(ResponseCodeEnum.ORGANIZATION_EXIST.getCode());
            organizationDTO.setMsg(ResponseCodeEnum.ORGANIZATION_EXIST.getMsg());
            return organizationDTO;
        }
        //查询父组织是否存在
        Integer parentId = organization.getParentId();
        Organization parentOrganization = new Organization();
        if (!ObjectUtils.isEmpty(parentId)) {
            parentOrganization.setId(parentId);
            parentOrganization.setIsDelete(Constants.NOT_DELETE);
            queryWrapper.setEntity(parentOrganization);
            parentOrganization = organizationMapper.selectOne(queryWrapper);
            if (ObjectUtils.isEmpty(parentOrganization)) {
                organizationDTO.setCode(ResponseCodeEnum.ORGANIZATION_PARENT_NOT_EXIST.getCode());
                organizationDTO.setMsg(ResponseCodeEnum.ORGANIZATION_PARENT_NOT_EXIST.getMsg());
                return organizationDTO;
            }
        }
        //插入组织
        organizationMapper.insert(organization);
        //封装DTO信息
        organizationDTO.setParentOrganization(parentOrganization);
        organizationDTO.setOrganization(organization);
        organizationDTO.setCode(ResponseCodeEnum.SUCCESS.getCode());
        organizationDTO.setMsg(ResponseCodeEnum.SUCCESS.getMsg());
        return organizationDTO;
    }
 
    /**
     * @Description: 更新客户组织
     * @Param: [organizationUpdateForm]
     * @return: com.moral.api.pojo.dto.organization.OrganizationDTO
     * @Author: 陈凯裕
     * @Date: 2021/3/24
     */
    @Override
    @Transactional
    public OrganizationDTO updateOrganization(OrganizationUpdateForm organizationUpdateForm) {
        OrganizationDTO organizationDTO = new OrganizationDTO();
        //form转entity
        Organization organization = organizationUpdateForm.formConvertEntity();
 
        //查询组织是否存在
        QueryWrapper<Organization> existWrapper = new QueryWrapper<>();
        Organization existOrganization = new Organization();
        existOrganization.setId(organization.getId());
        existOrganization.setIsDelete(Constants.NOT_DELETE);
        existWrapper.setEntity(existOrganization);
        existOrganization = organizationMapper.selectOne(existWrapper);
        if (ObjectUtils.isEmpty(existOrganization)) {
            organizationDTO.setCode(ResponseCodeEnum.ORGANIZATION_NOT_EXIST.getCode());
            organizationDTO.setMsg(ResponseCodeEnum.ORGANIZATION_NOT_EXIST.getMsg());
            return organizationDTO;
        }
 
        //如果更改了父组织,查询父组织是否存在
        Integer parentId = organization.getParentId();
        Organization parentOrganization = new Organization();
        if (!ObjectUtils.isEmpty(parentId) && parentId != 0) {
            QueryWrapper<Organization> existParentWrapper = new QueryWrapper<>();
            parentOrganization.setId(parentId);
            parentOrganization.setIsDelete(Constants.NOT_DELETE);
            existParentWrapper.setEntity(parentOrganization);
            parentOrganization = organizationMapper.selectOne(existParentWrapper);
            if (ObjectUtils.isEmpty(parentOrganization)) {
                organizationDTO.setCode(ResponseCodeEnum.ORGANIZATION_PARENT_NOT_EXIST.getCode());
                organizationDTO.setMsg(ResponseCodeEnum.ORGANIZATION_PARENT_NOT_EXIST.getMsg());
                return organizationDTO;
            }
        }
 
        //更新组织
        organizationMapper.updateById(organization);
 
        //获取更新后的组合
        organization = organizationMapper.selectById(organization.getId());
 
        //封装DTO信息
        organizationDTO.setParentOrganization(parentOrganization);
        organizationDTO.setOrganization(organization);
        organizationDTO.setCode(ResponseCodeEnum.SUCCESS.getCode());
        organizationDTO.setMsg(ResponseCodeEnum.SUCCESS.getMsg());
        return organizationDTO;
    }
 
    /**
     * @Description: 删除客户组织
     * @Param: [organizationDeleteForm]
     * @return: com.moral.api.pojo.dto.organization.OrganizationDTO
     * @Author: 陈凯裕
     * @Date: 2021/3/25
     */
    @Override
    @Transactional
    public OrganizationDTO deleteOrganization(OrganizationDeleteForm form) {
        OrganizationDTO dto = new OrganizationDTO();
        //取参
        Integer id = form.getOrganizationId();
        //查询组织是否存在
        Organization existOrganization = new Organization();
        existOrganization.setIsDelete(Constants.NOT_DELETE);
        existOrganization.setId(id);
        QueryWrapper queryExistWrapper = new QueryWrapper();
        queryExistWrapper.setEntity(existOrganization);
        existOrganization = organizationMapper.selectOne(queryExistWrapper);
        if (ObjectUtils.isEmpty(existOrganization)) {
            dto.setCode(ResponseCodeEnum.ORGANIZATION_NOT_EXIST.getCode());
            dto.setMsg(ResponseCodeEnum.ORGANIZATION_NOT_EXIST.getMsg());
            return dto;
        }
        //逻辑删除组织
        UpdateWrapper deleteWrapper = new UpdateWrapper();
        deleteWrapper.eq("id", id);
        deleteWrapper.set("is_delete", Constants.DELETE);
        organizationMapper.update(null, deleteWrapper);
        //判断是否删除所有子组织,如果不删除则将子组织parentId赋0
        if (form.getDeleteChildren().equals(Constants.DELETE_CHILDREN_ORG)) {
            UpdateWrapper deleteChildrenWrapper = new UpdateWrapper();
            deleteChildrenWrapper.eq("parent_id", id);
            deleteChildrenWrapper.set("is_delete", Constants.DELETE);
            organizationMapper.update(null, deleteChildrenWrapper);
        } else {
            UpdateWrapper updateChildrenWrapper = new UpdateWrapper();
            updateChildrenWrapper.eq("parent_id", id);
            updateChildrenWrapper.set("parent_id", 0);
            organizationMapper.update(null, updateChildrenWrapper);
        }
 
        dto.setOrganization(existOrganization);
        dto.setCode(ResponseCodeEnum.SUCCESS.getCode());
        dto.setMsg(ResponseCodeEnum.SUCCESS.getMsg());
        return dto;
    }
 
    /**
     * @Description: 查询客户组织
     * @Param: [organizationQueryForm]
     * @return: com.moral.api.pojo.dto.organization.OrganizationQueryDTO
     * @Author: 陈凯裕
     * @Date: 2021/3/25
     */
    @Override
    public OrganizationQueryDTO queryOrganization(OrganizationQueryForm organizationQueryForm) {
        OrganizationQueryDTO dto = new OrganizationQueryDTO();
        //取参
        Integer pageCount = organizationQueryForm.getPage();
        Integer size = organizationQueryForm.getSize();
        Integer parentId = organizationQueryForm.getParentId();
        String name = organizationQueryForm.getName();
        Integer provinceCode = organizationQueryForm.getProvinceCode();
        Integer cityCode = organizationQueryForm.getCityCode();
        Integer areaCode = organizationQueryForm.getAreaCode();
        Long townCode = organizationQueryForm.getTownCode();
        Long villageCode = organizationQueryForm.getVillageCode();
        String phone = organizationQueryForm.getPhone();
        String email = organizationQueryForm.getEmail();
        String wechat = organizationQueryForm.getWechat();
        String isDelete = organizationQueryForm.getIsDelete();
        String order = organizationQueryForm.getOrder();
        String orderType = organizationQueryForm.getOrderType();
        Date createStartTime = organizationQueryForm.getCreateStartTime();
        Date createEndTime = DateUtils.getDateOfDay(organizationQueryForm.getCreateEndTime(), 1);
        Date expireStartTime = organizationQueryForm.getExpireStartTime();
        Date expireEndTime = DateUtils.getDateOfDay(organizationQueryForm.getExpireEndTime(), 1);
 
        //查询条件
        Page<Organization> page = new Page<>(pageCount, size);
        NullFilterWrapper<Organization> queryWrapper = new NullFilterWrapper<>();
 
        queryWrapper.eq("parent_id", parentId);
        queryWrapper.like("name", name);
        queryWrapper.eq("province_code", provinceCode);
        queryWrapper.eq("city_code", cityCode);
        queryWrapper.eq("area_code", areaCode);
        queryWrapper.eq("town_code", townCode);
        queryWrapper.eq("village_code", villageCode);
        queryWrapper.like("phone", phone);
        queryWrapper.like("email", email);
        queryWrapper.like("wechat", wechat);
        queryWrapper.between("create_time", createStartTime, createEndTime);
        queryWrapper.between("expire_time", expireStartTime, expireEndTime);
 
        if (!ObjectUtils.isEmpty(isDelete)) {
            queryWrapper.eq("is_delete", isDelete);
        } else {
            queryWrapper.eq("is_delete", Constants.NOT_DELETE);
        }
 
        //排序顺序
        if (!ObjectUtils.isEmpty(order)) {
            if (!ObjectUtils.isEmpty(orderType)) {
                if (orderType.equals(Constants.ORDER_ASC))
                    queryWrapper.orderByAsc(ConvertUtils.toLine(order));
                else
                    queryWrapper.orderByDesc(ConvertUtils.toLine(order));
            }
        }
 
        //查询结果
        Page<Organization> resultPage = organizationMapper.selectPage(page, queryWrapper);
        List<Organization> organizations = resultPage.getRecords();
        List<OrganizationDTO> organizationDTOS = new ArrayList<>();
        //查找所有组织的父组织
        for (Organization child : organizations) {
            OrganizationDTO resultDto = new OrganizationDTO();
            Organization parent = organizationMapper.selectById(child.getParentId());
            resultDto.setOrganization(child);
            resultDto.setParentOrganization(parent);
            organizationDTOS.add(resultDto);
        }
 
        dto.setCode(ResponseCodeEnum.SUCCESS.getCode());
        dto.setMsg(ResponseCodeEnum.SUCCESS.getMsg());
        dto.setOrganizationDTOS(organizationDTOS);
        dto.setCurrent(page.getCurrent());
        dto.setPage(page.getPages());
        dto.setSize(page.getSize());
        dto.setTotal(page.getTotal());
        return dto;
    }
 
 
}