111
k94314517
2024-01-20 87cc98263db6d1eeac48d0c06852c559cd0838ea
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
package com.doumee.service.business.impl;
 
import com.doumee.core.constants.ResponseStatus;
import com.doumee.core.exception.BusinessException;
import com.doumee.core.model.LoginUserInfo;
import com.doumee.core.model.PageData;
import com.doumee.core.model.PageWrap;
import com.doumee.core.utils.Constants;
import com.doumee.core.utils.DateUtil;
import com.doumee.core.utils.Utils;
import com.doumee.dao.business.*;
import com.doumee.dao.business.dto.InsuranceApplyOptDTO;
import com.doumee.dao.business.dto.InsuranceApplyQueryDTO;
import com.doumee.dao.business.join.ApplyLogJoinMapper;
import com.doumee.dao.business.join.InsuranceApplyJoinMapper;
import com.doumee.dao.business.model.*;
import com.doumee.dao.system.model.SystemUser;
import com.doumee.service.business.InsuranceApplyService;
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.github.xiaoymin.knife4j.core.util.CollectionUtils;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import io.swagger.models.auth.In;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * 投保申请信息表Service实现
 * @author 江蹄蹄
 * @date 2024/01/16 10:03
 */
@Service
public class InsuranceApplyServiceImpl implements InsuranceApplyService {
 
    @Autowired
    private InsuranceApplyMapper insuranceApplyMapper;
 
    @Autowired
    private InsuranceApplyJoinMapper insuranceApplyJoinMapper;
 
    @Autowired
    private ApplyDetailMapper applyDetailMapper;
 
    @Autowired
    private ApplyLogMapper applyLogMapper;
 
    @Autowired
    private ApplyLogJoinMapper applyLogJoinMapper;
 
    @Autowired
    private SolutionsMapper solutionsMapper;
 
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private DuSolutionMapper duSolutionMapper;
    @Autowired
    private DuWorktypeMapper duWorktypeMapper;
 
    @Override
    @Transactional(rollbackFor = {Exception.class,BusinessException.class})
    public Integer create(InsuranceApply insuranceApply) {
        if (Objects.isNull(insuranceApply)
            || Objects.isNull(insuranceApply.getSolutionId())
            || Objects.isNull(insuranceApply.getApplyStartTime())
            || Objects.isNull(insuranceApply.getApplyEndTime())
            || !CollectionUtils.isNotEmpty(insuranceApply.getApplyDetailList())
        ) {
            throw new BusinessException(ResponseStatus.BAD_REQUEST);
        }
        LoginUserInfo loginUserInfo = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
        if(!loginUserInfo.getType().equals(Constants.ONE)){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"用户类型错误:非企业用户无法进行该操作");
        }
        Solutions solutions = solutionsMapper.selectById(insuranceApply.getSolutionId());
        if(Objects.isNull(solutions)){
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"未查询到方案信息");
        }
 
        List<ApplyDetail> applyDetailList = insuranceApply.getApplyDetailList();
        //判断是否存在重复信息
        List<Integer> memberIdList = applyDetailList.stream().map(i->i.getMemberId()).collect(Collectors.toList());
        Set<Integer> set = new HashSet<>(memberIdList);
        if(memberIdList.size() != set.size()){
            throw new BusinessException(ResponseStatus.DATA_ERRO.getCode(),"员工信息存在重复信息");
        }
        Constants.InsuranceApplyStatus insuranceApplyStatus = Constants.InsuranceApplyStatus.UPLOAD;
        insuranceApply.setCreateDate(new Date());
        insuranceApply.setCreator(loginUserInfo.getId());
        insuranceApply.setIsdeleted(Constants.ZERO);
        insuranceApply.setCompanyId(loginUserInfo.getCompanyId());
        insuranceApply.setCheckInfo(insuranceApplyStatus.getName());
        insuranceApply.setCheckDate(new Date());
        insuranceApply.setCheckUserId(loginUserInfo.getId());
        insuranceApply.setStatus(insuranceApplyStatus.getKey());
        insuranceApplyMapper.insert(insuranceApply);
        //查询保险方案下的所有派遣单位
        List<DuSolution> duSolutionList = duSolutionMapper.selectList(new QueryWrapper<DuSolution>().lambda()
                        .eq(DuSolution::getIsdeleted,Constants.ZERO)
                        .eq(DuSolution::getStatus,Constants.ONE)
                .eq(DuSolution::getSolutionId,insuranceApply.getSolutionId()));
        if(!CollectionUtils.isNotEmpty(duSolutionList)){
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"数据异常:保险方案下未查询到派遣单位");
        }
        //查询所有派遣单位下的工种
        List<Integer> duSolutionIdList = duSolutionList.stream().map(i->i.getId()).collect(Collectors.toList());
        List<DuWorktype> duWorktypeList = duWorktypeMapper.selectList(new QueryWrapper<DuWorktype>().lambda()
                .eq(DuWorktype::getIsdeleted,Constants.ZERO)
                .eq(DuWorktype::getStatus,Constants.ONE)
                .in(DuWorktype::getDuSolutionId,duSolutionIdList));
        if(!CollectionUtils.isNotEmpty(duWorktypeList)){
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"数据异常:保险方案下未查询到工种信息");
        }
        for (int i = 0; i < applyDetailList.size(); i++) {
            ApplyDetail applyDetail = applyDetailList.get(i);
            if(Objects.isNull(applyDetail)
                ||Objects.isNull(applyDetail.getMemberId())
                ||Objects.isNull(applyDetail.getDuId())
                ||Objects.isNull(applyDetail.getWorktypeId())
            ){
                throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"投保员工信息数据缺失");
            }
            applyDetail.setCreateDate(new Date());
            applyDetail.setCreator(loginUserInfo.getId());
            applyDetail.setIsdeleted(Constants.ZERO);
            applyDetail.setSortnum(i);
            applyDetail.setApplyId(insuranceApply.getId());
            if(Objects.isNull(applyDetail.getMemberId())){
                throw new BusinessException(ResponseStatus.DATA_ERRO.getCode(),"【"+applyDetail.getMemberName()+"】员工信息存在异常数据!");
            }
            Member member = memberMapper.selectById(applyDetail.getMemberId());
            if(Objects.isNull(member)){
                throw new BusinessException(ResponseStatus.DATA_ERRO.getCode(),"【"+applyDetail.getMemberName()+"】员工信息存在异常数据!");
            }
            applyDetail.setIdcardNo(member.getIdcardNo());
            applyDetail.setSex(member.getSex());
            applyDetail.setFee(this.countDetailFee(solutions,insuranceApply.getApplyEndTime(),insuranceApply.getApplyStartTime()));
            //验证派遣单位信息是否存在
            if(duSolutionList.stream().filter(d->d.getDispatchUnitId().equals(applyDetail.getDuId())).collect(Collectors.toList()).size()<=Constants.ZERO){
                throw new BusinessException(ResponseStatus.DATA_ERRO.getCode(),"【"+applyDetail.getMemberName()+"】员工派遣单位未查询到!");
            }
            if(duWorktypeList.stream().filter(d->d.getDuSolutionId().equals(applyDetail.getDuId())&&d.getWorkTypeId().equals(applyDetail.getWorktypeId()))
                    .collect(Collectors.toList()).size()<=Constants.ZERO){
                throw new BusinessException(ResponseStatus.DATA_ERRO.getCode(),"【"+applyDetail.getMemberName()+"】员工工种信息未查询到!");
            }
            applyDetailMapper.insert(applyDetail);
        }
        //存储日志数据
        this.saveApplyLog(insuranceApply,insuranceApplyStatus,null);
        return insuranceApply.getId();
    }
 
    public void saveApplyLog(InsuranceApply insuranceApply,Constants.InsuranceApplyStatus insuranceApplyStatus,String content){
        LoginUserInfo loginUserInfo = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
        ApplyLog applyLog = new ApplyLog();
        applyLog.setCreateDate(new Date());
        applyLog.setCreator(loginUserInfo.getId());
        applyLog.setIsdeleted(Constants.ZERO);
        applyLog.setApplyId(insuranceApply.getId());
        applyLog.setTitle(insuranceApplyStatus.getName());
        applyLog.setContent(content);
        applyLog.setObjType(insuranceApplyStatus.getKey());
        applyLog.setObjId(Integer.toString(insuranceApply.getId()));
        applyLog.setStatus(insuranceApply.getStatus());
        applyLogMapper.insert(applyLog);
    }
 
 
 
    public BigDecimal countDetailFee(Solutions solutions ,Date startDate,Date endDate){
        //查询保险实际周期
        Integer cycle = DateUtil.calculateBetween(endDate,startDate,solutions.getDataType());
        if(cycle==-1){
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"日期信息错误!");
        }
        return solutions.getPrice().multiply(new BigDecimal(cycle));
    }
 
//    public static void main(String[] args) {
//        Date date1 = DateUtil.StringToDate("2023-03-01 00:00:00");
//        Date date2 = DateUtil.StringToDate("2023-04-01 00:00:00");
//        System.out.println(DateUtil.calculateBetween(date1,date2,0));
//        System.out.println(DateUtil.calculateBetween(date1,date2,1));
//        System.out.println(DateUtil.calculateBetween(date1,date2,2));
//        System.out.println(DateUtil.calculateBetween(date1,date2,3));
//
//    }
 
 
    @Override
    public void deleteById(Integer id) {
        insuranceApplyMapper.deleteById(id);
    }
 
    @Override
    public void delete(InsuranceApply insuranceApply) {
        UpdateWrapper<InsuranceApply> deleteWrapper = new UpdateWrapper<>(insuranceApply);
        insuranceApplyMapper.delete(deleteWrapper);
    }
 
    @Override
    public void deleteByIdInBatch(List<Integer> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return;
        }
        insuranceApplyMapper.deleteBatchIds(ids);
    }
 
    @Override
    public void updateById(InsuranceApply insuranceApply) {
        insuranceApplyMapper.updateById(insuranceApply);
    }
 
    @Override
    public void updateByIdInBatch(List<InsuranceApply> insuranceApplys) {
        if (CollectionUtils.isEmpty(insuranceApplys)) {
            return;
        }
        for (InsuranceApply insuranceApply: insuranceApplys) {
            this.updateById(insuranceApply);
        }
    }
 
    @Override
    public InsuranceApply findById(Integer id) {
        return insuranceApplyMapper.selectById(id);
    }
 
    @Override
    public InsuranceApply findOne(InsuranceApply insuranceApply) {
        QueryWrapper<InsuranceApply> wrapper = new QueryWrapper<>(insuranceApply);
        return insuranceApplyMapper.selectOne(wrapper);
    }
 
    @Override
    public List<InsuranceApply> findList(InsuranceApply insuranceApply) {
        QueryWrapper<InsuranceApply> wrapper = new QueryWrapper<>(insuranceApply);
        return insuranceApplyMapper.selectList(wrapper);
    }
  
    @Override
    public PageData<InsuranceApply> findPage(PageWrap<InsuranceApplyQueryDTO> pageWrap) {
        LoginUserInfo loginUserInfo = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
        IPage<InsuranceApply> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity());
        MPJLambdaWrapper<InsuranceApply> queryWrapper = new MPJLambdaWrapper<>();
        Utils.MP.blankToNull(pageWrap.getModel());
        queryWrapper.selectAll(InsuranceApply.class);
        queryWrapper.selectAs(Company::getName,InsuranceApply::getCompanyName);
        queryWrapper.selectAs(Solutions::getName,InsuranceApply::getSolutionsName);
        queryWrapper.eq(InsuranceApply::getIsdeleted,Constants.ZERO);
        //企业人员查看本企业数据
        if(loginUserInfo.getType().equals(Constants.ONE)){
            queryWrapper.eq(InsuranceApply::getCompanyId, loginUserInfo.getCompanyId());
        }
        if (pageWrap.getModel().getSolutionId() != null) {
            queryWrapper.eq(InsuranceApply::getSolutionId, pageWrap.getModel().getSolutionId());
        }
        if (pageWrap.getModel().getEndTimeS() != null) {
            queryWrapper.ge(InsuranceApply::getEndTime, Utils.Date.getStart(pageWrap.getModel().getEndTimeS()));
        }
        if (pageWrap.getModel().getEndTimeE() != null) {
            queryWrapper.le(InsuranceApply::getEndTime, Utils.Date.getEnd(pageWrap.getModel().getEndTimeE()));
        }
        if (pageWrap.getModel().getStartTimeS() != null) {
            queryWrapper.ge(InsuranceApply::getStartTime, Utils.Date.getStart(pageWrap.getModel().getStartTimeS()));
        }
        if (pageWrap.getModel().getStartTimeE() != null) {
            queryWrapper.le(InsuranceApply::getStartTime, Utils.Date.getEnd(pageWrap.getModel().getStartTimeE()));
        }
        if (pageWrap.getModel().getCreateTimeS() != null) {
            queryWrapper.ge(InsuranceApply::getCreateDate, Utils.Date.getStart(pageWrap.getModel().getCreateTimeS()));
        }
        if (pageWrap.getModel().getCreateTimeE() != null) {
            queryWrapper.le(InsuranceApply::getCreateDate, Utils.Date.getEnd(pageWrap.getModel().getCreateTimeE()));
        }
 
        if (pageWrap.getModel().getCode() != null) {
            queryWrapper.eq(InsuranceApply::getCode, pageWrap.getModel().getCode());
        }
        if (pageWrap.getModel().getStatus() != null) {
            queryWrapper.eq(InsuranceApply::getStatus, pageWrap.getModel().getStatus());
        }
        for(PageWrap.SortData sortData: pageWrap.getSorts()) {
            if (sortData.getDirection().equalsIgnoreCase(PageWrap.DESC)) {
                queryWrapper.orderByDesc(sortData.getProperty());
            } else {
                queryWrapper.orderByAsc(sortData.getProperty());
            }
        }
        PageData<InsuranceApply> pageData = PageData.from(insuranceApplyJoinMapper.selectJoinPage(page,InsuranceApply.class, queryWrapper));
        return pageData;
    }
 
 
 
    @Override
    public long count(InsuranceApply insuranceApply) {
        QueryWrapper<InsuranceApply> wrapper = new QueryWrapper<>(insuranceApply);
        return insuranceApplyMapper.selectCount(wrapper);
    }
 
 
 
    @Override
    public InsuranceApply findDetail(Integer id) {
        InsuranceApply insuranceApply = insuranceApplyMapper.selectById(id);
        if(Objects.isNull(insuranceApply)){
            throw new BusinessException(ResponseStatus.DATA_EMPTY);
        }
        insuranceApply.setInsureNum(applyDetailMapper.selectCount(new QueryWrapper<ApplyDetail>().lambda().eq(ApplyDetail::getApplyId,insuranceApply.getId())
                .eq(ApplyDetail::getIsdeleted,Constants.ZERO)));
 
        //查询操作记录
        List<ApplyLog> applyLogList = applyLogJoinMapper.selectJoinList(ApplyLog.class,
                new MPJLambdaWrapper<ApplyLog>()
                        .selectAll(ApplyLog.class)
                        .selectAs(Member::getName,ApplyLog::getCreatorName)
                        .selectAs(Company::getName,ApplyLog::getCompanyName)
                        .leftJoin(Member.class,Member::getId,ApplyLog::getCreator)
                        .leftJoin(Company.class,Company::getId,Member::getCompanyId)
                        .eq(ApplyLog::getApplyId,insuranceApply.getId())
                        .orderByAsc(ApplyLog::getCreateDate)
        );
        insuranceApply.setApplyLogList(applyLogList);
        return insuranceApply;
    }
 
 
    @Override
    @Transactional(rollbackFor = {Exception.class,BusinessException.class})
    public void applyOpt(InsuranceApplyOptDTO insuranceApplyOptDTO){
        if(Objects.isNull(insuranceApplyOptDTO)
            ||Objects.isNull(insuranceApplyOptDTO.getApplyId())
            ||StringUtils.isBlank(insuranceApplyOptDTO.getOptIllustration())){
            throw new BusinessException(ResponseStatus.BAD_REQUEST);
        }
        LoginUserInfo loginUserInfo = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
        if(loginUserInfo.getType().equals(Constants.ZERO)){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"非企业端用户无法进行该操作");
        }
        InsuranceApply insuranceApply = insuranceApplyMapper.selectById(insuranceApplyOptDTO.getApplyId());
        if(Objects.isNull(insuranceApply)){
            throw new BusinessException(ResponseStatus.DATA_EMPTY);
        }
        if(insuranceApply.getIsdeleted().equals(Constants.ONE)){
            throw new BusinessException(ResponseStatus.DATA_ERRO.getCode(),"数据已删除,无法进行该操作");
        }
        Constants.InsuranceApplyStatus insuranceApplyStatus =  Constants.InsuranceApplyStatus.COMPANY_APPLY_RETURN;
        if(insuranceApplyOptDTO.getOptType().equals(Constants.ONE)){
            if(insuranceApply.getStatus().equals(Constants.InsuranceApplyStatus.COMPANY_APPLY_RETURN.getKey())){
                throw new BusinessException(ResponseStatus.DATA_ERRO.getCode(),"退回申请中,请勿重复操作");
            }
            if(!(insuranceApply.getStatus().equals(Constants.InsuranceApplyStatus.UPLOAD.getKey())
                    ||insuranceApply.getStatus().equals(Constants.InsuranceApplyStatus.WAIT_SIGNATURE.getKey())
                    ||insuranceApply.getStatus().equals(Constants.InsuranceApplyStatus.SIGNATURE.getKey()))){
                throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"数据状态非可申请退回!");
            }
        }else if(insuranceApplyOptDTO.getOptType().equals(Constants.TWO)){
            insuranceApplyStatus =  Constants.InsuranceApplyStatus.CLOSE;
            if(insuranceApply.getStatus().equals(Constants.InsuranceApplyStatus.CLOSE.getKey())){
                throw new BusinessException(ResponseStatus.DATA_ERRO.getCode(),"已关闭,请勿重复操作");
            }
            if(!(insuranceApply.getStatus().equals(Constants.InsuranceApplyStatus.PLATFORM_AGREE.getKey())
                    ||insuranceApply.getStatus().equals(Constants.InsuranceApplyStatus.PLATFORM_RETURN.getKey()))){
                throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"数据状态非可申请退回!");
            }
        }
        insuranceApply.setStatus(insuranceApplyStatus.getKey());
        insuranceApply.setCheckDate(new Date());
        insuranceApply.setCheckInfo(insuranceApplyOptDTO.getOptIllustration());
        insuranceApply.setCheckUserId(loginUserInfo.getId());
        insuranceApplyMapper.updateById(insuranceApply);
        //存储日志数据
        this.saveApplyLog(insuranceApply,insuranceApplyStatus,null);
    }
 
 
 
}