huangzhen
2023-09-25 1614615fc9319b8626eb028598b894311992c033
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
package com.gkhy.exam.noncoalmine.service.impl;
 
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gkhy.exam.noncoalmine.entity.NcExaminees;
import com.gkhy.exam.noncoalmine.entity.NcStaff;
import com.gkhy.exam.noncoalmine.mapper.NcExamPlanMapper;
import com.gkhy.exam.noncoalmine.entity.NcExamPlan;
import com.gkhy.exam.noncoalmine.model.query.NcExamPlanQuery;
import com.gkhy.exam.noncoalmine.model.vo.ExamPlanExaminee;
import com.gkhy.exam.noncoalmine.model.vo.ExamPlanInfo;
import com.gkhy.exam.noncoalmine.model.vo.ReturnVO;
import com.gkhy.exam.noncoalmine.service.NcExamPlanService;
import com.gkhy.exam.noncoalmine.service.NcExamineesService;
import com.gkhy.exam.noncoalmine.service.NcStaffService;
import com.ruoyi.common.enums.HttpMethod;
import com.ruoyi.common.exception.ServiceException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
 
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
 
import static com.ruoyi.common.signature.SignatureUtils.getObject;
 
/**
 * (NcExamPlan)表服务实现类
 *
 * @author makejava
 * @since 2023-09-18 13:21:55
 */
@Service("ncExamPlanService")
public class NcExamPlanServiceImpl extends ServiceImpl<NcExamPlanMapper, NcExamPlan> implements NcExamPlanService {
    @Autowired
    private NcExamPlanMapper ncExamPlanMapper;
 
    @Autowired
    private NcStaffService ncStaffService;
    @Autowired
    private NcExamineesService ncExamineesService;
    @Override
    public List<NcExamPlan> selectNcExamPlanList(NcExamPlanQuery query) {
        return ncExamPlanMapper.selectNcExamPlanList(query);
    }
    @Transactional
    @Override
    public void syncExamPlan() {
 
        String startTime = "2018-05-25 00:00:00";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String endTime = sdf.format(new Date());
        try {
            startTime = URLEncoder.encode(startTime, "UTF-8");
            endTime = URLEncoder.encode(endTime, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e.getCause());
        }
        String getQueryParam = "startTime=" + startTime + "&endTime=" + endTime;
        String json = getObject(getQueryParam,"/api/v1/exam/plan/enroll/download", HttpMethod.GET);
        ReturnVO<List<ExamPlanInfo<ExamPlanExaminee>>> returnVo = JSONObject.parseObject(json, new TypeReference<ReturnVO<List<ExamPlanInfo<ExamPlanExaminee>>>>() {});
        if(returnVo.getCode() == null || returnVo.getCode() != 200){
            throw new ServiceException("拉取数据异常");
        }
        if(returnVo.getData() == null){
            throw new ServiceException("无数据可更新");
        }
        List<ExamPlanInfo<ExamPlanExaminee>> data = returnVo.getData();
        for (ExamPlanInfo examPlanInfo : data) {
            NcExamPlan ncExamPlan = new NcExamPlan();
            BeanUtils.copyProperties(examPlanInfo,ncExamPlan);
            ncExamPlan.setDelFlag((byte)0);
            ncExamPlan.setId(null);
            ncExamPlan.setExamPlanId(examPlanInfo.getId());
            //判断是否已同步
            if(!this.isExsit(examPlanInfo.getId())){
                //插入
                ncExamPlanMapper.insert(ncExamPlan);
                List<ExamPlanExaminee> examPlanExaminees = examPlanInfo.getExaminees();
                if(!CollectionUtils.isEmpty(examPlanInfo.getExaminees())){
                    for (ExamPlanExaminee examPlanExaminee : examPlanExaminees) {
                        NcExaminees ncExaminees = new NcExaminees();
                        ncExaminees.setExamPlanId(examPlanInfo.getId());
                        ncExaminees.setDelFlag((byte)0);
                        ncExaminees.setCardNum(examPlanExaminee.getCardNum());
                        ncExaminees.setCardType(examPlanExaminee.getCardType());
                        ncExaminees.setName(examPlanExaminee.getName());
                        //判断人员是否存在
                        NcStaff ncStaff = ncStaffService.getByIdCard(examPlanExaminee.getCardNum());
                        if(ncStaff == null){
                            //插入staff表
                            ncStaff = new NcStaff();
                            ncStaff.setName(examPlanExaminee.getName());
                            ncStaff.setSex(examPlanExaminee.getSex());
                            ncStaff.setIdCardNum(examPlanExaminee.getCardNum());
                            ncStaff.setDelFlag((byte)0);
                            ncStaffService.save(ncStaff);
 
                        }
                        ncExaminees.setStaffId(ncStaff.getId());
                        ncExamineesService.save(ncExaminees);
                    }
 
                }
            }
        }
    }
    public boolean isExsit(Long explanId){
        NcExamPlan ncExamPlan = ncExamPlanMapper.selectOne(new LambdaQueryWrapper<NcExamPlan>()
                .eq(NcExamPlan::getDelFlag, (byte) 0)
                .eq(NcExamPlan::getExamPlanId, explanId));
        if(ncExamPlan != null){
            return true;
        }
        return false;
    }
}