k94314517
2025-04-11 c1b5d8b81d63555f6f0095aa823f07d99f80fe21
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
package com.doumee.service.business.impl;
 
import com.doumee.biz.system.SystemDictDataBiz;
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.Utils;
import com.doumee.dao.business.CompanyMapper;
import com.doumee.dao.business.ContractMapper;
import com.doumee.dao.business.MultifileMapper;
import com.doumee.dao.business.NoticesMapper;
import com.doumee.dao.business.join.ContractJoinMapper;
import com.doumee.dao.business.model.*;
import com.doumee.dao.system.model.SystemUser;
import com.doumee.service.business.ContractService;
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.doumee.service.business.third.SignService;
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.util.CollectionUtils;
 
import java.util.Date;
import java.util.List;
import java.util.Objects;
 
/**
 * 合同信息表Service实现
 * @author 江蹄蹄
 * @date 2024/10/28 19:16
 */
@Service
public class ContractServiceImpl implements ContractService {
 
    @Autowired
    private ContractMapper contractMapper;
 
    @Autowired
    private ContractJoinMapper contractJoinMapper;
 
    @Autowired
    private MultifileMapper multifileMapper;
 
    @Autowired
    private SystemDictDataBiz systemDictDataBiz;
 
    @Autowired
    private CompanyMapper companyMapper; 
    
    @Autowired
    private SignService signService;
 
    @Autowired
    private NoticesMapper noticesMapper;
 
    @Override
    public Integer create(Contract contract) {
        LoginUserInfo loginUserInfo =   (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
        initCreateParam(contract);
        contract.setIsdeleted(Constants.ZERO);
        contract.setCreateDate(new Date());
        contract.setCreator(loginUserInfo.getId());
        contract.setEditor(loginUserInfo.getId());
        contract.setEditDate(new Date());
        contract.setStatus(Constants.ZERO);
        contract.setFileUrl(contract.getFileUrl());
        Company company = companyMapper.selectById(contract.getCompanyId());
        if(Constants.equalsInteger(loginUserInfo.getType(),Constants.ZERO)){
            if(Constants.equalsInteger(company.getType(),Constants.ONE)){
                contract.setSignType(Constants.ZERO);
            }else{
                contract.setSignType(Constants.ONE);
            }
        }else if(Constants.equalsInteger(loginUserInfo.getType(),Constants.TWO)){
            contract.setPartyCompanyId(loginUserInfo.getCompanyId());
            contract.setSignType(Constants.TWO);
        }else{
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"企业用户无法进行该操作!");
        }
        contractMapper.insert(contract);
 
        Multifile file = contract.getMultifile();
        file.setIsdeleted(Constants.ZERO);
        file.setCreateDate(contract.getCreateDate());
        file.setCreator(contract.getCreator());
        file.setObjId(contract.getId());
        file.setObjType(Constants.MultiFile.CONTRACT_PDF.getKey());
        file.setType(Constants.formatIntegerNum(file.getType()));
        multifileMapper.insert(file);
 
 
 
        //存储待办信息
        Constants.NoticeObjectType noticeObjectType = Constants.NoticeObjectType.CONTRACT;
        //平台待办
        Notices notices = new Notices(noticeObjectType,
                Constants.equalsInteger(contract.getSignType(),Constants.TWO)?Constants.TWO:Constants.ZERO
                ,contract.getId(),
                this.getNoticeInfo(contract,company),
                Constants.equalsInteger(contract.getSignType(),Constants.TWO)?
                contract.getPartyCompanyId():null
                ,Constants.NoticeType.ZERO);
        noticesMapper.insert(notices);
 
        return contract.getId();
    }
 
    public String getNoticeInfo(Contract contract,Company company){
        String noticeInfo = contract.getName();
        //商户信息
        if(Constants.equalsInteger(contract.getSignType(),Constants.TWO)){
            Company shop = companyMapper.selectById(contract.getPartyCompanyId());
            noticeInfo  = noticeInfo + "("+shop.getName()+"/"+company.getName()+")";
        }else{
            noticeInfo  = noticeInfo + "(平台/"+company.getName()+")";
        }
        return noticeInfo;
    }
 
 
    private void initCreateParam(Contract contract) {
        if(StringUtils.isBlank(contract.getName())
                || Objects.isNull(contract.getType())
                || Objects.isNull(contract.getCompanyId())
                || Objects.isNull(contract.getStartTime())
                || Objects.isNull(contract.getEndTime())
                || Objects.isNull(contract.getMultifile())
                 || Objects.isNull(contract.getMultifile().getFileurl())
        ){
            throw new BusinessException(ResponseStatus.BAD_REQUEST);
        }
    }
 
 
    @Override
    public void deleteById(Integer id) {
        LoginUserInfo loginUserInfo =   (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
        Contract contract = contractJoinMapper.selectById(id);
        if(Objects.isNull(contract)){
            throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"未查询到合同信息");
        }
        if(!Constants.equalsInteger(contract.getStatus(),Constants.CONTRACT_STATUS.CANCEL.getKey())){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"合同信息信息已流转");
        }
        contract.setStatus(Constants.CONTRACT_STATUS.CLOSE.getKey());
        contract.setIsdeleted(Constants.ONE);
        contract.setEditor(loginUserInfo.getId());
        contract.setEditDate(new Date());
        contractMapper.updateById(contract);
    }
 
    @Override
    public void delete(Contract contract) {
        UpdateWrapper<Contract> deleteWrapper = new UpdateWrapper<>(contract);
        contractMapper.delete(deleteWrapper);
    }
 
    @Override
    public void deleteByIdInBatch(List<Integer> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return;
        }
        contractMapper.deleteBatchIds(ids);
    }
 
    @Override
    public void updateById(Contract contract) {
        contractMapper.updateById(contract);
    }
 
    @Override
    public void updateByIdInBatch(List<Contract> contracts) {
        if (CollectionUtils.isEmpty(contracts)) {
            return;
        }
        for (Contract contract: contracts) {
            this.updateById(contract);
        }
    }
 
    @Override
    public Contract findById(Integer id) {
        MPJLambdaWrapper<Contract> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(Contract.class);
        queryWrapper.select("  ifnull(t2.name,'安徽云易保科技有限公司') as partyCompanyName , t3.name as companyName ");
        queryWrapper.select(" t4.REALNAME as firstSignUserName  , t5.REALNAME as doneSignUserName ");
        queryWrapper.leftJoin(SystemUser.class,SystemUser::getId,Contract::getCreator);
        queryWrapper.leftJoin(" company t2 on t2.id = t.PARTY_COMPANY_ID ");
        queryWrapper.leftJoin(" company t3 on t3.id = t.COMPANY_ID ");
        queryWrapper.leftJoin(" system_user t4 on t4.id = t.SIGN_USER_ID  ");
        queryWrapper.leftJoin(" system_user t5 on t5.id = t.COM_SIGN_USER_ID  ");
        queryWrapper.eq(Contract::getId,id);
        queryWrapper.last(" limit 1");
        Contract contract = contractJoinMapper.selectJoinOne(Contract.class,queryWrapper);
 
 
        Multifile multifile = multifileMapper.selectOne(new QueryWrapper<Multifile>().lambda()
                .eq(Multifile::getObjType,Constants.MultiFile.CONTRACT_PDF.getKey())
                .eq(Multifile::getObjId,contract.getId())
                .last(" limit 1 ")
        );
 
        if(Objects.nonNull(multifile)&&StringUtils.isNotBlank(multifile.getFileurl())){
            String url = systemDictDataBiz.queryByCode(Constants.OSS,Constants.RESOURCE_PATH).getCode()+
                    systemDictDataBiz.queryByCode(Constants.OSS,Constants.CONTRACT).getCode();
            multifile.setFileurlFull(url + multifile.getFileurl());
            contract.setMultifile(multifile);
        }
 
        if(Constants.equalsInteger(contract.getStatus(),Constants.ONE) || Constants.equalsInteger(contract.getStatus(),Constants.THREE) ){
            Multifile multiFirstFile = multifileMapper.selectOne(new QueryWrapper<Multifile>().lambda()
                    .eq(Multifile::getObjType,Constants.MultiFile.CONTRACT_FIRST_PDF.getKey())
                    .eq(Multifile::getObjId,contract.getId())
                    .last(" limit 1 ")
            );
            String path = systemDictDataBiz.queryByCode(Constants.OSS,Constants.RESOURCE_PATH).getCode()+
                    systemDictDataBiz.queryByCode(Constants.OSS,Constants.APPLY_FILE).getCode();
 
            if(Objects.nonNull(multiFirstFile)&&StringUtils.isNotBlank(multiFirstFile.getFileurl())){
                multiFirstFile.setFileurlFull(path + multiFirstFile.getFileurl());
                contract.setMultiFirstFile(multiFirstFile);
            }
 
            if(StringUtils.isNotBlank(contract.getDoneFileUrl())){
                Multifile multiDoneFile = multifileMapper.selectOne(new QueryWrapper<Multifile>().lambda()
                        .eq(Multifile::getObjType,Constants.MultiFile.CONTRACT_DONE_PDF.getKey())
                        .eq(Multifile::getObjId,contract.getId())
                        .last(" limit 1 ")
                );
 
                if(Objects.nonNull(multiDoneFile)&&StringUtils.isNotBlank(multiDoneFile.getFileurl())){
                    multiDoneFile.setFileurlFull(path + multiDoneFile.getFileurl());
                    contract.setMultiDoneFile(multiDoneFile);
                }
            }
 
        }
 
        return contract;
    }
 
    @Override
    public Contract findOne(Contract contract) {
        QueryWrapper<Contract> wrapper = new QueryWrapper<>(contract);
        return contractMapper.selectOne(wrapper);
    }
 
    @Override
    public List<Contract> findList(Contract contract) {
        QueryWrapper<Contract> wrapper = new QueryWrapper<>(contract);
        return contractMapper.selectList(wrapper);
    }
  
    @Override
    public PageData<Contract> findPage(PageWrap<Contract> pageWrap) {
        IPage<Contract> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity());
        MPJLambdaWrapper<Contract> queryWrapper = new MPJLambdaWrapper<>();
        Utils.MP.blankToNull(pageWrap.getModel());
        queryWrapper.selectAll(Contract.class);
        queryWrapper.select("  ifnull(t2.name,'安徽云易保科技有限公司') as partyCompanyName , t3.name as companyName ");
        queryWrapper.leftJoin(SystemUser.class,SystemUser::getId,Contract::getCreator);
        queryWrapper.leftJoin(" company t2 on t2.id = t.PARTY_COMPANY_ID ");
        queryWrapper.leftJoin(" company t3 on t3.id = t.COMPANY_ID ");
        queryWrapper.eq(Contract::getIsdeleted,Constants.ZERO);
        queryWrapper.eq(Objects.nonNull(pageWrap.getModel().getCompanyId()),
                Contract::getCompanyId,pageWrap.getModel().getCompanyId());
 
        queryWrapper.like(StringUtils.isNotBlank(pageWrap.getModel().getName()),
                Contract::getName,pageWrap.getModel().getName());
 
        queryWrapper.eq(Objects.nonNull(pageWrap.getModel().getStatus()),
                Contract::getStatus,pageWrap.getModel().getStatus());
 
        queryWrapper.eq(Objects.nonNull(pageWrap.getModel().getSignType()),
                Contract::getSignType,pageWrap.getModel().getSignType());
 
        LoginUserInfo loginUserInfo =   (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
        if(Constants.equalsInteger(loginUserInfo.getType(),Constants.ONE)){
            //企业可看数据
            queryWrapper.eq(Contract::getCompanyId,loginUserInfo.getCompanyId());
        }else if(Constants.equalsInteger(loginUserInfo.getType(),Constants.TWO)){
            //商户可看到的数据
            queryWrapper.and(i->i.eq(Contract::getPartyCompanyId,loginUserInfo.getCompanyId()).or()
                    .eq(Contract::getCompanyId,loginUserInfo.getCompanyId()));
        }
        if(Objects.nonNull(pageWrap.getModel().getQueryStatus())){
            if(Constants.equalsInteger(pageWrap.getModel().getQueryStatus(),Constants.ZERO)){
                queryWrapper.apply(" t.status = 0 and t.sign_type in (0,1) ");
            }else if(Constants.equalsInteger(pageWrap.getModel().getQueryStatus(),Constants.ONE)){
                queryWrapper.apply(" ( (t.status = 0 and t.sign_type = 2 ) or ( t.status = 1 and t.sign_type = 0 )  )");
            }else if(Constants.equalsInteger(pageWrap.getModel().getQueryStatus(),Constants.TWO)){
                queryWrapper.apply(" ( (t.status = 1 and t.sign_type = 1 ) or ( t.status = 1 and t.sign_type = 2 )  )");
            }else{
                queryWrapper.eq(Contract::getStatus,pageWrap.getModel().getQueryStatus());
            }
        }
        queryWrapper.orderByDesc(Contract::getCreateDate);
        PageData<Contract> pageData = PageData.from(contractJoinMapper.selectJoinPage(page,Contract.class, queryWrapper));
        for (Contract contract:pageData.getRecords()) {
            this.dealQueryStauts(contract);
        }
        return pageData;
    }
 
    public void dealQueryStauts(Contract contract){
        if(Constants.equalsInteger(contract.getSignType(),Constants.ZERO)){
            if(Constants.equalsInteger(contract.getStatus(),Constants.ZERO)){
                contract.setQueryStatus(Constants.ZERO);
            }else if(Constants.equalsInteger(contract.getStatus(),Constants.ONE)){
                contract.setQueryStatus(Constants.ONE);
            }else{
                contract.setQueryStatus(contract.getStatus());
            }
        }else if(Constants.equalsInteger(contract.getSignType(),Constants.ONE)){
            if(Constants.equalsInteger(contract.getStatus(),Constants.ZERO)){
                contract.setQueryStatus(Constants.ZERO);
            }else if(Constants.equalsInteger(contract.getStatus(),Constants.ONE)){
                contract.setQueryStatus(Constants.TWO);
            }else{
                contract.setQueryStatus(contract.getStatus());
            }
        }else{
            if(Constants.equalsInteger(contract.getStatus(),Constants.ZERO)){
                contract.setQueryStatus(Constants.ONE);
            }else if(Constants.equalsInteger(contract.getStatus(),Constants.ONE)){
                contract.setQueryStatus(Constants.TWO);
            }else{
                contract.setQueryStatus(contract.getStatus());
            }
        }
    }
 
    @Override
    public void cancel(Integer id) {
        LoginUserInfo loginUserInfo =   (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
        Contract contract = contractJoinMapper.selectById(id);
        if(Objects.isNull(contract)){
            throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"未查询到合同信息");
        }
        if(!Constants.equalsInteger(contract.getStatus(),Constants.CONTRACT_STATUS.WAIT.getKey())){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"合同信息信息已流转");
        }
        if(StringUtils.isNotBlank(contract.getApplyNo())){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"合同签署中,无法进行取消");
        }
 
        contract.setStatus(Constants.CONTRACT_STATUS.CANCEL.getKey());
        contract.setCancelDate(new Date());
        contract.setCancelUserId(loginUserInfo.getId());
        contract.setEditor(loginUserInfo.getId());
        contract.setEditDate(new Date());
        contractMapper.updateById(contract);
 
        //删除待办
        Constants.NoticeObjectType noticeObjectType = Constants.NoticeObjectType.CONTRACT;
        noticesMapper.delete(new QueryWrapper<Notices>().lambda().eq(Notices::getObjType,noticeObjectType.getKey()).eq(Notices::getObjId,contract.getId()));
 
    }
 
 
    @Override
    public long count(Contract contract) {
        QueryWrapper<Contract> wrapper = new QueryWrapper<>(contract);
        return contractMapper.selectCount(wrapper);
    }
 
 
    @Override
    public  String getContractSignLink(Integer id) {
        if(id == null ){
            throw  new BusinessException(ResponseStatus.BAD_REQUEST);
        }
        LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
        MPJLambdaWrapper<Contract> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(Contract.class);
        queryWrapper.select("  ifnull(t2.name,'安徽云易保科技有限公司') as partyCompanyName , t3.name as companyName ");
        queryWrapper.leftJoin(SystemUser.class,SystemUser::getId,Contract::getCreator);
        queryWrapper.leftJoin(" company t2 on t2.id = t.PARTY_COMPANY_ID ");
        queryWrapper.leftJoin(" company t3 on t3.id = t.COMPANY_ID ");
        queryWrapper.eq(Contract::getIsdeleted,Constants.ZERO);
        queryWrapper.eq(Contract::getId,id);
        queryWrapper.last(" limit 1");
        Contract contract = contractJoinMapper.selectJoinOne(Contract.class,queryWrapper);
        if(contract == null ||!Constants.equalsInteger(contract.getIsdeleted(),Constants.ZERO) ){
            throw  new BusinessException(ResponseStatus.DATA_EMPTY);
        }
        Company company = companyMapper.selectById(contract.getCompanyId());
        if(Objects.isNull(company)){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"未查询到企业信息!");
        }
        //当前签署人 0=我方;2=企业/商户
        Integer signatory =  Constants.ZERO;
        //判断当前是否可以签署
        if(Constants.equalsInteger(user.getType(),Constants.ZERO)){
            //判断平台是否可以签署
            if(!( !Constants.equalsInteger(contract.getSignType(),Constants.TWO) && Constants.equalsInteger(contract.getStatus(),Constants.ZERO))){
                throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"当前非平台用户可签署流程");
            }
        }else if(Constants.equalsInteger(user.getType(),Constants.TWO)){
            //平台创建的
            if(!Constants.equalsInteger(contract.getSignType(),Constants.TWO) ){
                signatory = Constants.ONE;
                //判断状态是否等于待商户签署 签署企业是否等于当前登录人企业
                if(!(Constants.equalsInteger(contract.getStatus(),Constants.ONE) && Constants.equalsInteger(contract.getCompanyId(),user.getCompanyId()))){
                    throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"当前非商户用户可签署流程");
                }
            }else{
                //商户创建
                //判断状态是否等于待商户签署
                if(!Constants.equalsInteger(contract.getStatus(),Constants.ZERO)){
                    throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"当前非商户用户可签署流程");
                }
            }
        }else{
            signatory = Constants.ONE;
            //企业用户判断
            if(!(Constants.equalsInteger(contract.getCompanyId(),user.getCompanyId()) && Constants.equalsInteger(contract.getStatus(),Constants.ONE) )){
                throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"当前非企业用户可签署流程");
            }
        }
 
        Constants.ApplyLogType applyLogType = null;
        String info = "";
 
        Multifile f = multifileMapper.selectOne(new QueryWrapper<Multifile>().lambda()
                .eq(Multifile::getObjId,contract.getId())
                .eq(Multifile::getObjType,Constants.MultiFile.CONTRACT_PDF.getKey())
                .eq(Multifile::getIsdeleted,Constants.ZERO).last("limit 1"));
        if(f == null || StringUtils.isBlank(f.getFileurl())){
            throw  new BusinessException(ResponseStatus.SERVER_ERROR.getCode(),"对不起,获取待签章文件失败,请联系确认签署文件是否正确!");
        }
        String url = systemDictDataBiz.queryByCode(Constants.OSS,Constants.RESOURCE_PATH).getCode();
        if(Constants.equalsInteger(contract.getStatus(),Constants.ZERO)){
            url = url + systemDictDataBiz.queryByCode(Constants.OSS,Constants.CONTRACT).getCode() + f.getFileurl();
        }else{
            url = url + systemDictDataBiz.queryByCode(Constants.OSS,Constants.APPLY_FILE).getCode() + contract.getFileSignUrl();
        }
 
        String notifyUrl = systemDictDataBiz.queryByCode(Constants.SYSTEM,Constants.SIGN_DONE_NOTIFY_URL).getCode();
 
        String companyName = "";
        String creditCode = "";
        String email = "";
        //平台签署
        if(!Constants.equalsInteger(contract.getSignType(),Constants.TWO) && Constants.equalsInteger(contract.getStatus(),Constants.ZERO)){
            companyName = systemDictDataBiz.queryByCode(Constants.SYSTEM,Constants.PLAT_COMPANY_NAME).getCode();
            creditCode = systemDictDataBiz.queryByCode(Constants.SYSTEM,Constants.PLAT_CREDIT_CODE).getCode();
            email = systemDictDataBiz.queryByCode(Constants.SYSTEM,Constants.PLAT_EMAIL).getCode();
        }else if((Constants.equalsInteger(contract.getSignType(),Constants.ZERO)  && Constants.equalsInteger(contract.getStatus(),Constants.ONE))||
                Constants.equalsInteger(contract.getSignType(),Constants.TWO)  && Constants.equalsInteger(contract.getStatus(),Constants.ZERO)
        )  {
            //商户签署
            Company shop = null;
            if(Constants.equalsInteger(contract.getStatus(),Constants.ONE)){
                shop = companyMapper.selectById(contract.getCompanyId());
            }else{
                shop = companyMapper.selectById(contract.getPartyCompanyId());
            }
            if(Objects.isNull(shop)){
                throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"未查询到商户信息");
            }
            companyName = shop.getName();
            creditCode = shop.getCode();
            email = shop.getEmail();
        } else {
            companyName = Constants.equalsInteger(contract.getStatus(),Constants.ZERO)?contract.getPartyCompanyName():contract.getCompanyName();
            creditCode = company.getCode();
            email = company.getEmail();
        }
        String applyNo = signService.applySign(contract.getName(),url,
                companyName
                ,creditCode,email,null,company.getSignId(),notifyUrl);
        if(StringUtils.isBlank(applyNo) ){
            throw  new BusinessException(ResponseStatus.SERVER_ERROR.getCode(),"对不起,获取在线签章地址失败,请稍后重试!");
        }
        String link = signService.signLink(applyNo,companyName,creditCode);
        if(StringUtils.isBlank(link) ){
            throw  new BusinessException(ResponseStatus.SERVER_ERROR.getCode(),"对不起,获取在线签章地址失败,请稍后重试!");
        }
        Contract update= new Contract();
        update.setId(contract.getId());
        update.setEditor(user.getId());
        update.setEditDate(new Date());
        if(Constants.equalsInteger(signatory,Constants.ZERO)){
            update.setApplyNo(applyNo);
            update.setSignUserId(user.getId());
            update.setSignDate(new Date());
 
            //存储待办信息
            Constants.NoticeObjectType noticeObjectType = Constants.NoticeObjectType.CONTRACT;
            noticesMapper.delete(new QueryWrapper<Notices>().lambda().eq(Notices::getObjType,noticeObjectType.getKey()).eq(Notices::getObjId,update.getId()));
            Notices notices = new Notices(noticeObjectType,
                    Constants.equalsInteger(contract.getSignType(),Constants.TWO)?Constants.ONE:Constants.TWO
                    ,contract.getId(),
                    this.getNoticeInfo(contract,company),
                    contract.getCompanyId()
                    ,Constants.NoticeType.ZERO);
            noticesMapper.insert(notices);
 
        }else{
            update.setDoneApplyNo(applyNo);
            update.setComSignUserId(user.getId());
            update.setComSignDate(new Date());
        }
        contractMapper.updateById(update);
        return  link;
    }
 
 
}