forked from gzzfw/backEnd/gz-dyh

wangwh
2024-08-28 b47d4a7accabce974e19d2a1ffb3c5c67507d74b
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
package cn.huge.module.cases.service;
 
import cn.huge.base.common.exception.ServiceException;
import cn.huge.base.common.utils.DateUtils;
import cn.huge.base.common.utils.IdUtils;
import cn.huge.base.common.utils.ObjectUtils;
import cn.huge.module.cases.domain.dto.RegisterSaveDTO;
import cn.huge.module.cases.domain.po.CaseAgent;
import cn.huge.module.cases.domain.po.CaseInfoUnfold;
import cn.huge.module.cases.domain.po.CasePerson;
import cn.huge.module.client.api.impl.CustClientImpl;
import cn.huge.module.client.api.impl.UtilsClientImpl;
import cn.huge.module.cases.dao.mapper.CaseInfoMapper;
import cn.huge.module.cases.domain.po.CaseInfo;
import cn.huge.module.constant.BaseConsts;
import cn.huge.module.constant.CaseBaseConsts;
import cn.huge.module.constant.CaseBaseConstsEnum;
import cn.huge.module.cust.dto.CtUserDTO;
import cn.huge.module.draft.domain.po.CasedraftAgent;
import cn.huge.module.draft.domain.po.CasedraftInfo;
import cn.huge.module.draft.domain.po.CasedraftPerson;
import cn.huge.module.draft.service.CasedraftInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
 
/**
 * @title: 纠纷信息主表业务逻辑处理
 * @Description 纠纷信息主表业务逻辑处理
 * @company hugeinfo
 * @author wangwh
 * @Time 2024-08-27 10:00:57
 * @version 1.0.0
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class CaseInfoService extends ServiceImpl<CaseInfoMapper, CaseInfo>{
 
    @Autowired
    private CaseInfoMapper mapper;
 
    @Autowired
    private UtilsClientImpl utilsClient;
 
    @Autowired
    private CasedraftInfoService draftInfoService;
 
    @Autowired
    private CasePersonService personService;
 
    @Autowired
    private CaseAgentService agentService;
 
    @Autowired
    private CustClientImpl custClient;
 
    /**
    * 更新对象
    * @param entity 对象
    */
    public void updateCaseInfo(CaseInfo entity){
        try{
            mapper.updateCaseInfo(entity);
        }catch (Exception e){
            log.error("[CaseInfoService.updateCaseInfo]调用失败,异常信息:"+e, e);
            throw new ServiceException("CaseInfoService.updateCaseInfo", e);
        }
    }
 
    /**
    * 条件更新对象
    * @param entity 对象
    * @param terms 条件
    */
    public void updateCaseInfoTerms(CaseInfo entity, Map<String, Object> terms){
        try{
            mapper.updateCaseInfoTerms(entity, terms);
        }catch (Exception e){
            log.error("[CaseInfoService.updateCaseInfoTerms]调用失败,异常信息:"+e, e);
            throw new ServiceException("CaseInfoService.updateCaseInfoTerms", e);
        }
    }
 
    /**
    * 根据编号物理删除
    * @param id 查询条件集合
    */
    public void deleteCaseInfo(String id){
        try{
            mapper.deleteCaseInfo(id);
        }catch (Exception e){
            log.error("[CaseInfoService.deleteCaseInfo]调用失败,异常信息:"+e, e);
            throw new ServiceException("CaseInfoService.deleteCaseInfo", e);
        }
    }
 
    /**
    * 按条件查询
    * @param terms 条件
    * @return List
    */
    public List<CaseInfo> listTerms(Map<String, Object> terms){
        return mapper.listTerms(terms);
    }
 
    /**
    * 按条件统计
    * @param terms 条件
    * @return long
    */
    public long countTerms(Map<String, Object> terms){
        return mapper.countTerms(terms);
    }
 
    /**
    * 按条件分页查询
    * @param page 分页对象
    * @param terms 条件
    * @return Page
    */
    public Page<CaseInfo> pageQuery(PageRequest page, Map<String, Object> terms){
        long total = mapper.countTerms(terms);
        List<CaseInfo> content = mapper.pageTerms(page, terms);
        return new PageImpl<CaseInfo>(content, page, total);
    }
 
    /**
    * 新增或更新对象
    * @param caseInfo 实体对象
    */
    public void saveCaseInfo(CaseInfo caseInfo){
        try{
            Date nowDate = DateUtils.getNowDate();
            // 判断是否新增
            if (IdUtils.checkNewId(caseInfo.getId())){
                caseInfo.setId(utilsClient.getNewTimeId());
                caseInfo.setCreateTime(nowDate);
            }
            caseInfo.setUpdateTime(nowDate);
            this.saveOrUpdate(caseInfo);
        }catch (Exception e){
            log.error("[CaseInfoService.saveCaseInfo]调用失败,异常信息:"+e, e);
            throw new ServiceException("CaseInfoService.saveCaseInfo", e);
        }
    }
 
    /**
     * PC端-纠纷登记-保存纠纷信息-正常案件
     * @param registerSaveDTO 纠纷信息
     * @param userId 用户编号
     * @return String 纠纷编号
     */
    public String caseRegister(RegisterSaveDTO registerSaveDTO, String userId){
        try {
            // 获取当前登录用户
            CtUserDTO loginUser = custClient.clientGetUserAll(userId);
 
            Date nowDate = DateUtils.getNowDate();
            StringBuffer plaintiffs = new StringBuffer();
            StringBuffer defendants = new StringBuffer();
            StringBuffer pagents = new StringBuffer();
            StringBuffer dagents = new StringBuffer();
            int peopleNum = 0;
 
            CaseInfo caseInfo = new CaseInfo();
            CaseInfoUnfold caseInfoUnfold = new CaseInfoUnfold();
            BeanUtils.copyProperties(registerSaveDTO, caseInfo);
            caseInfo.setId(utilsClient.getNewTimeId());
            caseInfo.setInputUnitId(loginUser.getUnitId());
            caseInfo.setInputUnitName(loginUser.getUnitName());
            caseInfo.setInputUserId(loginUser.getId());
            caseInfo.setInputUserName(loginUser.getTrueName());
            caseInfo.setInputWay(CaseBaseConsts.INPUT_WAY_1);
            caseInfo.setCreateTime(nowDate);
            caseInfo.setUpdateTime(nowDate);
            caseInfo.setDeleteStatus(BaseConsts.DELETE_STATUS_0);
            //todo case_ref生成、case_title生成
 
            caseInfoUnfold.setId(utilsClient.getNewTimeId());
            caseInfoUnfold.setCaseId(caseInfo.getId());
            caseInfoUnfold.setCreateTime(nowDate);
            caseInfoUnfold.setUpdateTime(nowDate);
 
 
            // 常规登记-保存当事人
            peopleNum += this.saveCasePerson(registerSaveDTO, plaintiffs, pagents, defendants, dagents);
            caseInfo.setPeopleNum(peopleNum);
            caseInfo.setPlaintiffs(plaintiffs.toString());
            caseInfo.setPagents(pagents.toString());
            caseInfo.setDefendants(defendants.toString());
            caseInfo.setDagents(dagents.toString());
            //todo 流程接口修改status、status_name、process、process_name
            //todo 自行受理
            if(1 == registerSaveDTO.getIsSelfAccept()){
                caseInfoUnfold.setMediateUnitId(loginUser.getUnitId());
                caseInfoUnfold.setMediateUnitName(loginUser.getUnitName());
            }
 
            this.saveOrUpdate(caseInfo);
            // 删除草稿案件
            if(1 == registerSaveDTO.getIsDraft() ){
                draftInfoService.removeDraftInfo(registerSaveDTO.getId());
            }
 
            return registerSaveDTO.getId();
        }catch (Exception e){
            log.error("service方法[CaseInfoService.caseRegister]调用异常:"+e, e);
            throw new ServiceException("CaseInfoService.caseRegister", e);
        }
    }
 
    /**
     * pc端-常规登记-保存正式案件-保存人员信息
     * @param registerSaveDTO 纠纷信息
     * @param plaintiffs 申请人
     * @param pagents 申请人代理人
     * @param defendants 被申请人
     * @param dagents 被申请人代理人
     * @return int
     */
    private int saveCasePerson(RegisterSaveDTO registerSaveDTO, StringBuffer plaintiffs, StringBuffer pagents,
                               StringBuffer defendants, StringBuffer dagents){
        int peopleNum = 0;
        List<String> newPersonIdList = new ArrayList<>();
        List<String> newAgentIdList = new ArrayList<>();
        // 保存申请人
        List<CasePerson> plaintPersonList = registerSaveDTO.getPlaintiffList();
        if (CollectionUtils.isNotEmpty(plaintPersonList)) {
            for (CasePerson casePerson : plaintPersonList) {
                newPersonIdList.add(casePerson.getId());
                casePerson.setCaseId(registerSaveDTO.getId());
                casePerson.setPerType(CaseBaseConstsEnum.PERSON_TYPE_1.getIndex());
                casePerson.setPerTypeName(CaseBaseConstsEnum.PERSON_TYPE_1.getDes());
                casePerson.setCustId(registerSaveDTO.getCustId());
                casePerson.setCreateTime(registerSaveDTO.getUpdateTime());
                casePerson.setUpdateTime(registerSaveDTO.getUpdateTime());
                //todo 绑定当事人小程序
 
                personService.saveOrUpdate(casePerson);
 
                if (StringUtils.isNotEmpty(plaintiffs.toString())) {
                    plaintiffs.append(BaseConsts.COMMA);
                }
                plaintiffs.append(casePerson.getTrueName());
                peopleNum++;
 
            }
            //保存代理人
            List<CaseAgent> pagentList = registerSaveDTO.getPagentList();
            if (CollectionUtils.isNotEmpty(pagentList)) {
                for(CaseAgent caseAgent: pagentList){
                    newAgentIdList.add(caseAgent.getId());
                    caseAgent.setId(utilsClient.getNewTimeId());
                    caseAgent.setCaseId(registerSaveDTO.getId());
                    caseAgent.setAgentTypeName(CaseBaseConstsEnum.getDes(caseAgent.getAgentType()));
 
                    caseAgent.setCustId(registerSaveDTO.getCustId());
                    caseAgent.setCreateTime(registerSaveDTO.getUpdateTime());
                    caseAgent.setUpdateTime(registerSaveDTO.getUpdateTime());
 
                    agentService.saveOrUpdate(caseAgent);
 
                    if (StringUtils.isNotEmpty(pagents.toString())) {
                        pagents.append(BaseConsts.COMMA);
                    }
                    pagents.append(caseAgent.getTrueName());
                }
 
                //todo 绑定当事人小程序
            }
        }
        // 保存被申请人
        List<CasePerson> defendPersonList = registerSaveDTO.getDefendantList();
        if(CollectionUtils.isNotEmpty(defendPersonList)){
            for (CasePerson casePerson : defendPersonList) {
                newPersonIdList.add(casePerson.getId());
                casePerson.setCaseId(registerSaveDTO.getId());
                casePerson.setPerType(CaseBaseConstsEnum.PERSON_TYPE_2.getIndex());
                casePerson.setPerTypeName(CaseBaseConstsEnum.PERSON_TYPE_2.getDes());
                casePerson.setCustId(registerSaveDTO.getCustId());
                casePerson.setCreateTime(registerSaveDTO.getUpdateTime());
                casePerson.setUpdateTime(registerSaveDTO.getUpdateTime());
                //todo 绑定当事人小程序
                personService.saveOrUpdate(casePerson);
                if (StringUtils.isNotEmpty(defendants.toString())) {
                    defendants.append(BaseConsts.COMMA);
                }
                defendants.append(casePerson.getTrueName());
                peopleNum++;
            }
            //保存代理人
            List<CaseAgent> pagentList = registerSaveDTO.getPagentList();
            if (CollectionUtils.isNotEmpty(pagentList)) {
                for(CaseAgent caseAgent: pagentList){
                    newAgentIdList.add(caseAgent.getId());
                    caseAgent.setId(utilsClient.getNewTimeId());
                    caseAgent.setCaseId(registerSaveDTO.getId());
                    caseAgent.setAgentTypeName(CaseBaseConstsEnum.getDes(caseAgent.getAgentType()));
                    caseAgent.setCustId(registerSaveDTO.getCustId());
                    caseAgent.setCreateTime(registerSaveDTO.getUpdateTime());
                    caseAgent.setUpdateTime(registerSaveDTO.getUpdateTime());
                    agentService.saveOrUpdate(caseAgent);
                    if (StringUtils.isNotEmpty(dagents.toString())) {
                        dagents.append(BaseConsts.COMMA);
                    }
                    dagents.append(caseAgent.getTrueName());
                    //todo 绑定当事人小程序
                }
            }
 
        }
 
        // 删除冗余的申请人
        if (CollectionUtils.isNotEmpty(newPersonIdList)) {
            List<String> oldPersonIdList = personService.listIdByCaseId(registerSaveDTO.getId());
            for (String oldPersonId : oldPersonIdList) {
                if (!newPersonIdList.contains(oldPersonId)) {
                    personService.removeById(oldPersonId);
                }
            }
        }else{
            QueryWrapper<CasePerson> casePersonQueryWrapper = new QueryWrapper<>();
            casePersonQueryWrapper.eq("case_id", registerSaveDTO.getId());
            personService.remove(casePersonQueryWrapper);
        }
        //删除冗余的代理人
        if(CollectionUtils.isNotEmpty(newAgentIdList)){
            List<String> oldAgentIdList = agentService.listIdByCaseId(registerSaveDTO.getId());
            for(String oldAgentId: oldAgentIdList){
                if(!newAgentIdList.contains(oldAgentId)){
                    agentService.removeById(oldAgentId);
                }
            }
        }else{
            QueryWrapper<CaseAgent> caseAgentQueryWrapper = new QueryWrapper<>();
            caseAgentQueryWrapper.eq("case_id", registerSaveDTO.getId());
            agentService.remove(caseAgentQueryWrapper);
        }
 
        return peopleNum;
    }
}