xuhy
2025-04-11 40f3409658593f38759f826c467c7e83200efe34
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
package com.ruoyi.web.controller.api;
 
 
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.basic.PageInfo;
import com.ruoyi.common.constant.DictConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.model.LoginUserApplet;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.enums.ProcessCategoryEnum;
import com.ruoyi.common.utils.CodeGenerateUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.WebUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.bo.ProcessStartBO;
import com.ruoyi.system.dto.RevokeDTO;
import com.ruoyi.system.dto.SignContractDTO;
import com.ruoyi.system.model.TContract;
import com.ruoyi.system.model.TContractRentType;
import com.ruoyi.system.model.THouse;
import com.ruoyi.system.model.TTenant;
import com.ruoyi.system.query.TContractAppletQuery;
import com.ruoyi.system.query.TContractQuery;
import com.ruoyi.system.service.*;
import com.ruoyi.system.vo.TContractAppletVO;
import com.ruoyi.system.vo.TContractVO;
import com.ruoyi.web.controller.tool.ImportExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
 
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 合同管理 前端控制器
 * </p>
 *
 * @author xiaochen
 * @since 2025-01-17
 */
@RestController
@Api(tags = "我的合同")
 
@RequestMapping("/t-contract")
public class TContractController {
    @Autowired
    private TContractService contractService;
    @Autowired
    private TContractRentTypeService contractRentTypeService;
    @Autowired
    private THouseService houseService;
    @Autowired
    private TBillService billService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private TTenantService tTenantService;
    @Autowired
    private StateProcessTemplateService stateProcessTemplateService;
    @ApiOperation(value = " 签订合同")
    @PostMapping(value = "/signContract")
    public R signContract(@RequestBody SignContractDTO dto) {
        return contractService.signContract(dto);
    }
    @ApiOperation(value = "我的合同分页列表")
    @PostMapping(value = "/contractList")
    public R<PageInfo<TContract>> contractList(@RequestBody TContractAppletQuery query) {
        LoginUserApplet loginUserApplet = tokenService.getLoginUserApplet();
        if (loginUserApplet==null){
            return R.fail(401,"登录失效");
        }
        query.setTenantId(loginUserApplet.getUserId());
        return R.ok(contractService.contractAppletList(query));
    }
 
    @Log(title = "合同管理-撤销审批", businessType =  BusinessType.UPDATE)
    @ApiOperation(value = "撤销审批")
    @PostMapping(value = "/updateContractStatus")
    public R<Boolean> updateContractStatus(@RequestBody RevokeDTO dto) {
        TContract contract = contractService.getById(dto.getContractId());
        contract.setStatus("1");
        contractService.updateById(contract);
        // 撤销审批实例
        stateProcessTemplateService.revoke(dto.getInstanceId());
        return R.ok();
    }
    @ApiOperation(value = "查询合同信息信息")
    @GetMapping(value = "/getContractById")
    public R<TContractAppletVO> getContractById(@RequestParam String id) {
        LoginUserApplet loginUserApplet = tokenService.getLoginUserApplet();
        if (loginUserApplet==null){
            return R.fail(401,"登录失效");
        }
        TContractAppletVO res = new TContractAppletVO();
        TContract contract = contractService.getById(id);
        BeanUtils.copyProperties(contract,res);
        res.setStartTimeString(DateUtils.localDateTimeToStringYear(contract.getStartTime()));
        res.setEndTimeString(DateUtils.localDateTimeToStringYear(contract.getEndTime()));
        res.setPayType(DictUtils.getDictLabel(DictConstants.DICT_TYPE_CONTRACT_PAY_TYPE,res.getPayType()));
        res.setStatus(DictUtils.getDictLabel(DictConstants.DICT_TYPE_CONTRACT_STATUS,res.getStatus()));
        TContractRentType contractRentType = contractRentTypeService.lambdaQuery().eq(TContractRentType::getContractId, id).one();
        if (contractRentType!=null){
            BeanUtils.copyProperties(contractRentType,res);
        }
        TTenant tTenant = tTenantService.getById(contract.getTenantId());
        res.setTenant(tTenant);
        TContract oldContract = contractService.getOne(new LambdaQueryWrapper<TContract>()
                .eq(TContract::getHouseId,contract.getHouseId())
                .eq(TContract::getTenantId,loginUserApplet.getUserId())
                .eq(TContract::getStatus, 4)
                .le(TContract::getStartTime, LocalDateTime.now())
                .ge(TContract::getEndTime, LocalDateTime.now()));
        THouse house = houseService.getById(contract.getHouseId());
        if (oldContract!=null){
            house.setTenantType(oldContract.getPayType());
        }
        res.setHouse(house);
        return R.ok(res);
    }
 
    @ApiOperation(value = "合同导入模板下载")
    @GetMapping("/import-template")
    public void importTemplate() {
        List<TContract> contractList = new ArrayList<>();
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), TContract.class, contractList);
        HttpServletResponse response = WebUtils.response();
        ServletOutputStream outputStream = null;
        try {
            String fileName = URLEncoder.encode("合同导入模板.xls", "utf-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            response.setHeader("content-Type", "application/vnd.ms-excel");
            response.setHeader("Pragma", "no-cache");
            response.setHeader("Cache-Control", "no-cache");
            outputStream = response.getOutputStream();
            workbook.write(outputStream);
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("合同导入模板下载失败!");
        } finally {
            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
 
    @ApiOperation(value = "合同导入")
    @PostMapping("/importContracts")
    @ApiImplicitParam(paramType = "form", name = "file", value = "文件对象", required = true, dataType = "__file")
    public R<String> importContracts(@RequestPart("file") MultipartFile file) {
        ImportParams params = new ImportParams();
//        params.setTitleRows(1);//标题行数
        params.setHeadRows(1); //表头行数
        try {
            InputStream inputStream = file.getInputStream();
            List<TContract> contractList = ExcelImportUtil.importExcel(inputStream, TContract.class, params);
            List<String> errors = new ArrayList<>();
            for (TContract tContract : contractList) {
                try{
                    tContract.setStartTime(DateUtils.stringToLocalDateTime(tContract.getStartTimeStr() + " 00:00:00"));
                    tContract.setEndTime(DateUtils.stringToLocalDateTime(tContract.getEndTimeStr() + " 23:59:59"));
                    tContract.setStartPayTime(DateUtils.stringToLocalDateTime(tContract.getStartPayTimeStr() + " 00:00:00"));
                    tContract.setSignTime(DateUtils.stringToLocalDateTime(tContract.getSignTimeStr() + " 00:00:00"));
                    tContract.setChangeRent(tContract.getMonthRent());
                    tContract.setFirstPayTime(tContract.getStartTime().plusDays(10));
                    contractService.save(tContract);
                    if(tContract.getStatus().equals("2")){
                        //发起合同新增审批
                        ProcessStartBO processStartBO = new ProcessStartBO();
                        processStartBO.setCategory(ProcessCategoryEnum.CATEGORY1.getValue().toString());
                        processStartBO.setModuleName("合同新增审批");
                        processStartBO.setName(tContract.getContractName());
                        //需要显示发起申请人所在单位
                        processStartBO.setRemark("");
                        Map<String, Object> variable = new HashMap<>();
                        variable.put("projectId", tContract.getId());
                        processStartBO.setVariable(variable);
                        //开启工作流程
                        Boolean start = stateProcessTemplateService.start(processStartBO);
                    }
                    if(tContract.getStatus().equals("9")){
                        // 进入签订审批流程
                        ProcessStartBO processStartBO = new ProcessStartBO();
                        processStartBO.setCategory(ProcessCategoryEnum.CATEGORY2.getValue().toString());
                        processStartBO.setModuleName("合同签订审批");
                        processStartBO.setName(tContract.getContractName());
                        processStartBO.setRemark("");
                        Map<String, Object> variable = new HashMap<>();
                        variable.put("projectId", tContract.getId());
                        processStartBO.setVariable(variable);
                        //开启工作流程
                        stateProcessTemplateService.startApplet(processStartBO);
                    }
                }catch (Exception e){
                    errors.add("合同名称:[" + tContract.getContractName() + "],导入错误,忽略导入。");
                }
            }
            Integer errorLines = 0;
            Integer successLines = 0;
            errorLines += errors.size();
            successLines += (contractList.size() - errorLines);
            R<String> stringApiResult = ImportExcelUtil.importReturnMsg(errorLines, successLines, errors);
            return stringApiResult;
        } catch (Exception e) {
            System.out.println("合同导入失败!" + e.getMessage());
            e.printStackTrace();
        }
        return R.ok();
    }
 
}