rk
2025-09-22 cf2391a86bdea88196d49cd33949570f74c0985d
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
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.Constants.DeclareStatus;
import com.doumee.core.utils.Constants.ProjectRecord;
import com.doumee.core.utils.Constants.UserType;
import com.doumee.core.utils.DateUtil;
import com.doumee.core.utils.Utils;
import com.doumee.dao.business.DeclareExpertMapper;
import com.doumee.dao.business.DeclareHistoryMapper;
import com.doumee.dao.business.DeclaresJoinMapper;
import com.doumee.dao.business.DeclaresMapper;
import com.doumee.dao.business.model.Company;
import com.doumee.dao.business.model.DeclareExpert;
import com.doumee.dao.business.model.DeclareHistory;
import com.doumee.dao.business.model.Declares;
import com.doumee.dao.business.model.Project;
import com.doumee.dao.system.SystemUserMapper;
import com.doumee.dao.system.model.SystemUser;
import com.doumee.service.business.DeclareExpertService;
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.yulichang.wrapper.MPJLambdaWrapper;
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 org.springframework.util.CollectionUtils;
 
import java.util.Date;
import java.util.List;
import java.util.Objects;
 
/**
 * 项目申报分配专家关联表Service实现
 * @author 江蹄蹄
 * @date 2023/02/15 08:55
 */
@Service
public class DeclareExpertServiceImpl implements DeclareExpertService {
 
    @Autowired
    private DeclareExpertMapper declareExpertMapper;
 
    @Autowired
    private DeclaresMapper declaresMapper;
    @Autowired
    private DeclaresJoinMapper declaresJoinMapper;
 
    @Autowired
    private DeclareHistoryMapper declareHistoryMapper;
    @Autowired
    private SystemUserMapper systemUserMapper;
 
    @Override
    public Integer create(DeclareExpert declareExpert) {
        declareExpertMapper.insert(declareExpert);
        return declareExpert.getId();
    }
 
    @Override
    public void deleteById(Integer id) {
        declareExpertMapper.deleteById(id);
    }
 
    @Override
    public void delete(DeclareExpert declareExpert) {
        UpdateWrapper<DeclareExpert> deleteWrapper = new UpdateWrapper<>(declareExpert);
        declareExpertMapper.delete(deleteWrapper);
    }
 
    @Override
    public void deleteByIdInBatch(List<Integer> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return;
        }
        declareExpertMapper.deleteBatchIds(ids);
    }
 
    @Override
    public void updateById(DeclareExpert declareExpert) {
        declareExpertMapper.updateById(declareExpert);
    }
 
    @Override
    public void updateByIdInBatch(List<DeclareExpert> declareExperts) {
        if (CollectionUtils.isEmpty(declareExperts)) {
            return;
        }
        for (DeclareExpert declareExpert: declareExperts) {
            this.updateById(declareExpert);
        }
    }
 
    @Override
    public DeclareExpert findById(Integer id) {
        return declareExpertMapper.selectById(id);
    }
 
    @Override
    public DeclareExpert findOne(DeclareExpert declareExpert) {
        QueryWrapper<DeclareExpert> wrapper = new QueryWrapper<>(declareExpert);
        return declareExpertMapper.selectOne(wrapper);
    }
 
    @Override
    public List<DeclareExpert> findList(DeclareExpert declareExpert) {
        QueryWrapper<DeclareExpert> wrapper = new QueryWrapper<>(declareExpert);
        return declareExpertMapper.selectList(wrapper);
    }
  
    @Override
    public PageData<DeclareExpert> findPage(PageWrap<DeclareExpert> pageWrap) {
        IPage<DeclareExpert> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity());
        QueryWrapper<DeclareExpert> queryWrapper = new QueryWrapper<>();
        Utils.MP.blankToNull(pageWrap.getModel());
        if (pageWrap.getModel().getId() != null) {
            queryWrapper.lambda().eq(DeclareExpert::getId, pageWrap.getModel().getId());
        }
 
        if (pageWrap.getModel().getCreator() != null) {
            queryWrapper.lambda().eq(DeclareExpert::getCreator, pageWrap.getModel().getCreator());
        }
        if (pageWrap.getModel().getCreateDate() != null) {
            queryWrapper.lambda().ge(DeclareExpert::getCreateDate, Utils.Date.getStart(pageWrap.getModel().getCreateDate()));
            queryWrapper.lambda().le(DeclareExpert::getCreateDate, Utils.Date.getEnd(pageWrap.getModel().getCreateDate()));
        }
        if (pageWrap.getModel().getEditor() != null) {
            queryWrapper.lambda().eq(DeclareExpert::getEditor, pageWrap.getModel().getEditor());
        }
        if (pageWrap.getModel().getEditDate() != null) {
            queryWrapper.lambda().ge(DeclareExpert::getEditDate, Utils.Date.getStart(pageWrap.getModel().getEditDate()));
            queryWrapper.lambda().le(DeclareExpert::getEditDate, Utils.Date.getEnd(pageWrap.getModel().getEditDate()));
        }
        if (pageWrap.getModel().getIsdeleted() != null) {
            queryWrapper.lambda().eq(DeclareExpert::getIsdeleted, pageWrap.getModel().getIsdeleted());
        }
        if (pageWrap.getModel().getRemark() != null) {
            queryWrapper.lambda().eq(DeclareExpert::getRemark, pageWrap.getModel().getRemark());
        }
        if (pageWrap.getModel().getExpertId() != null) {
            queryWrapper.lambda().eq(DeclareExpert::getExpertId, pageWrap.getModel().getExpertId());
        }
        if (pageWrap.getModel().getDeclareId() != null) {
            queryWrapper.lambda().eq(DeclareExpert::getDeclareId, pageWrap.getModel().getDeclareId());
        }
        if (pageWrap.getModel().getScore() != null) {
            queryWrapper.lambda().eq(DeclareExpert::getScore, pageWrap.getModel().getScore());
        }
        if (pageWrap.getModel().getStatus() != null) {
            queryWrapper.lambda().eq(DeclareExpert::getStatus, pageWrap.getModel().getStatus());
        }
        if (pageWrap.getModel().getScoreDate() != null) {
            queryWrapper.lambda().ge(DeclareExpert::getScoreDate, Utils.Date.getStart(pageWrap.getModel().getScoreDate()));
            queryWrapper.lambda().le(DeclareExpert::getScoreDate, Utils.Date.getEnd(pageWrap.getModel().getScoreDate()));
        }
        for(PageWrap.SortData sortData: pageWrap.getSorts()) {
            if (sortData.getDirection().equalsIgnoreCase(PageWrap.DESC)) {
                queryWrapper.orderByDesc(sortData.getProperty());
            } else {
                queryWrapper.orderByAsc(sortData.getProperty());
            }
        }
        return PageData.from(declareExpertMapper.selectPage(page, queryWrapper));
    }
 
    @Override
    public long count(DeclareExpert declareExpert) {
        QueryWrapper<DeclareExpert> wrapper = new QueryWrapper<>(declareExpert);
        return declareExpertMapper.selectCount(wrapper);
    }
 
 
    /**
     * 分配专家
     * @param declareExpert
     * @throws BusinessException
     */
    @Override
    @Transactional(rollbackFor = {BusinessException.class, Exception.class})
    public void assignExperts(DeclareExpert declareExpert){
        if(declareExpert.getDeclareId()==null||declareExpert.getExpertId()==null){
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),ResponseStatus.BAD_REQUEST.getMessage());
        }
        LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
        //查询申报记录并验证权限和状态合法
        Declares model = getDeclaresAndValid(user,declareExpert);
        //查询已分配专家记录,只有一条专家分配记录
        DeclareExpert result = getDeclareExpertRecord(declareExpert.getDeclareId());
        //处理申报记录状态和录入历史数据
        DeclareHistory history = initDeclareAndHistory( model,result,user);
        //插入数据
        declareExpert.setCreator(user.getId());
        declareExpert.setIsdeleted(Constants.ZERO);
        declareExpert.setCreateDate(history.getCreateDate());
        declareExpert.setStatus(Constants.ZERO);
        /*if(Objects.nonNull(result)){
            result.setExpertId(declareExpert.getExpertId());
            declareExpertMapper.updateById(result);
        }else{*/
            declareExpertMapper.insert(declareExpert);
       /* }*/
    }
 
    private DeclareHistory initDeclareAndHistory( Declares model,DeclareExpert result,
        LoginUserInfo user) {
        Date date = new Date();
        Declares declares=new Declares();
        declares.setId(model.getId());
        declares.setEditDate(date);
        declares.setEditor(user.getId());
        declares.setStatus(Constants.DeclareStatus.SELECT_EXPERT.getKey());
 
        DeclareHistory history = new DeclareHistory();
        history.setCreateDate(date);
        history.setIsdeleted(Constants.ZERO);
        history.setCheckor(user.getId());
        history.setStatus(declares.getStatus());
        history.setDeclareId(model.getId());
        history.setCheckor(user.getId());
        history.setCheckDate(date);
 
        String checkInfo ;
        if(result!=null){
            //重新分配专家 删除原有的分配记录
            deleteOldRecord(result,user.getId());
            if(Constants.equalsInteger(model.getEdType(),Constants.ONE)){
                //如果是综合服务单位分配
                history.setType(ProjectRecord.SD_RESELECT_EXPERT.getKey());
                checkInfo = ProjectRecord.SD_RESELECT_EXPERT.getNoteinfo();
            }else{
                history.setType(ProjectRecord.SJ_RESELECT_EXPERT.getKey());
                checkInfo = ProjectRecord.SJ_RESELECT_EXPERT.getNoteinfo();
            }
        }else{
            //首次分配
            if(Constants.equalsInteger(model.getEdType(),Constants.ONE)){
                //如果是综合服务单位分配
                history.setType(ProjectRecord.SD_SELECT_EXPERT.getKey());
                checkInfo = ProjectRecord.SD_SELECT_EXPERT.getNoteinfo();
            }else{
                checkInfo = ProjectRecord.SJ_SELECT_EXPERT.getNoteinfo();
                history.setType(ProjectRecord.SJ_SELECT_EXPERT.getKey());
            }
        }
        history.setCheckInfo(checkInfo.replace("${param2}", Constants.UserType.getName(user.getType()))
            .replace("${param3}", StringUtils.defaultString(user.getUsername(),""))
            .replace("${param4}", DeclareStatus.SELECT_EXPERT.getNoteinfo()));
        declares.setCheckType(history.getType());
        declares.setCheckor(history.getCheckor());
        declares.setCheckInfo(history.getCheckInfo());
        declaresMapper.updateById(declares);
        //审核记录表
        declareHistoryMapper.insert(history);
        return  history;
    }
 
    private void deleteOldRecord(DeclareExpert result,Integer userId) {
        result.setEditDate(new Date());
        result.setEditor(userId);
        result.setIsdeleted(Constants.ONE);
        //逻辑删除原有的分配记录
        declareExpertMapper.updateById(result);
    }
 
    private DeclareExpert getDeclareExpertRecord(Integer declareId) {
        DeclareExpert query=new DeclareExpert();
        query.setIsdeleted(Constants.ZERO);
        query.setDeclareId(declareId);
        DeclareExpert result=  declareExpertMapper.selectOne(new QueryWrapper<>(query).last("limit 1") );
        return result;
    }
 
    /**
     *  查询申报记录并验证权限和状态合法
     * @param user
     * @param declareExpert
     * @return
     */
    private Declares getDeclaresAndValid(LoginUserInfo user, DeclareExpert declareExpert) {
 
        MPJLambdaWrapper<Declares> queryWrapper =new MPJLambdaWrapper<>();
        queryWrapper.selectAll(Declares.class);
        queryWrapper.leftJoin(Project.class,Project::getId,Declares::getProjectId);
        queryWrapper.leftJoin(Company.class,Company::getId,Declares::getCompanyId);
        queryWrapper.selectAs(Project::getEdType,Declares::getEdType);
        queryWrapper.selectAs(Company::getCityId,Declares::getCityId);
        queryWrapper.eq(Declares::getId,declareExpert.getDeclareId());
        queryWrapper.last("limit 1");
        Declares declares = declaresJoinMapper.selectJoinOne(Declares.class,queryWrapper);
 
        if(declares==null ||Constants.equalsInteger(declares.getIsdeleted(),Constants.ONE)){
            throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"对不起,企业报名信息不存在!");
        }
        if(!(Constants.equalsInteger(declares.getStatus(), DeclareStatus.SERVICE_DONE.getKey())
                ||Constants.equalsInteger(declares.getStatus(), DeclareStatus.BACK_SERVER_DONE.getKey())
                ||Constants.equalsInteger(declares.getStatus(), DeclareStatus.SELECT_EXPERT.getKey()))){
            //只有服务完成或者已完成退回修改(待分配专家)和已分配专家两个状态可以操作
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"对不起,企业报名状态已流转,不能进行当前操作!");
        }
      /*  if(Constants.equalsInteger(declares.getCityId(),user.getCityId())){
            //如果项目不是本城市的,不能操作非本市的企业报名数据
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"对不起,该企业非管辖范围内数据,不能进行当前操作!");
        }*/
        if(Constants.equalsInteger(declares.getEdType(),Constants.ONE)){
            //如果是综合服务单位分配
            if(!(Constants.equalsInteger(user.getType(), UserType.SD_ADMIN.getKey())||Constants.equalsInteger(user.getType(), UserType.SD_CHILD.getKey()))){
                throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"对不起,非权限内数据,不能进行当前操作!");
            }
            if(Constants.equalsInteger(user.getType(), UserType.SD_CHILD.getKey())&&  !Constants.equalsInteger(declares.getSdUserId(),user.getId())){
                //如果是子账号,只能管理佩服到自己的数据
                throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"对不起,非权限内数据,不能进行当前操作!");
            }
        }else{
            //如果是市局分配
            if(!Constants.equalsInteger(user.getType(), UserType.SJ.getKey())  ){
                throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"对不起,非权限内数据,不能进行当前操作!");
            }
        }
        SystemUser expert = systemUserMapper.selectById(declareExpert.getExpertId());
        if(expert == null || !Constants.equalsInteger(expert.getType(), UserType.EXPERT.getKey())){
            //专家信息不存在
            throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"对不起,您选择的专家信息不存在,请返回刷新页面重试!");
        }
        return declares;
    }
}