xuhy
2025-02-28 8f9dba748594660d3b4512ebfb07bddf9f61c9f8
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
package com.ruoyi.web.controller.api;
 
 
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
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.enums.BusinessType;
import com.ruoyi.common.utils.*;
import com.ruoyi.system.dto.TTenantDTO;
import com.ruoyi.system.export.ContractExport;
import com.ruoyi.system.model.TContract;
import com.ruoyi.system.model.TTenant;
import com.ruoyi.system.query.TContractQuery;
import com.ruoyi.system.query.TTenantQuery;
import com.ruoyi.system.service.TContractService;
import com.ruoyi.system.service.TTenantService;
import com.ruoyi.system.vo.TenantVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
 
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
 
/**
 * <p>
 * 租户 前端控制器
 * </p>
 *
 * @author xiaochen
 * @since 2025-01-17
 */
@Api(tags = "租户管理")
@RestController
@RequestMapping("/t-tenant")
public class TTenantController {
 
    private final TTenantService tenantService;
    private final TContractService contractService;
    @Autowired
    public TTenantController(TTenantService tenantService, TContractService contractService) {
        this.tenantService = tenantService;
        this.contractService = contractService;
    }
 
    /**
     * 获取租户管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:tenant:list')")
    @ApiOperation(value = "获取租户分页列表")
    @PostMapping(value = "/pageList")
    public R<PageInfo<TenantVO>> pageList(@RequestBody TTenantQuery query) {
        return R.ok(tenantService.pageList(query));
    }
 
    /**
     * 添加租户管理
     */
    @PreAuthorize("@ss.hasPermi('system:tenant:add')")
    @Log(title = "租户信息-新增租户", businessType = BusinessType.INSERT)
    @ApiOperation(value = "添加租户")
    @PostMapping(value = "/add")
    public R<Boolean> add(@Validated @RequestBody TTenantDTO dto) {
        if(tenantService.isExit(dto)){
            return R.fail("该账号已存在");
        }
        // 密码加密
        dto.setPassword(SecurityUtils.encryptPassword(dto.getPassword()));
        return R.ok(tenantService.save(dto));
    }
 
    /**
     * 修改租户
     */
    @PreAuthorize("@ss.hasPermi('system:tenant:update')")
    @Log(title = "租户信息-修改租户", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改租户")
    @PostMapping(value = "/update")
    public R<Boolean> update(@Validated @RequestBody TTenantDTO dto) {
        if(tenantService.isExit(dto)){
            return R.fail("该账号已存在");
        }
        // 密码加密
        if(StringUtils.isNotBlank(dto.getPassword())){
            dto.setPassword(SecurityUtils.encryptPassword(dto.getPassword()));
        }
        return R.ok(tenantService.updateById(dto));
    }
 
    /**
     * 查看租户详情
     */
    @PreAuthorize("@ss.hasPermi('system:tenant:detail')")
    @ApiOperation(value = "查看租户详情")
    @GetMapping(value = "/getDetailById")
    public R<TTenant> getDetailById(@RequestParam String id) {
        TTenant tenant = tenantService.getById(id);
//        tenant.setTenantAttributes(StringUtils.isNotEmpty(tenant.getTenantAttributes())?DictUtils.getDictLabel(DictConstants.DICT_TYPE_TENANT_ATTRIBUTE,tenant.getTenantAttributes()):"");
//        tenant.setTenantType(StringUtils.isNotEmpty(tenant.getTenantType())?DictUtils.getDictLabel(DictConstants.DICT_TYPE_TENANT_TYPE,tenant.getTenantType()):"");
        return R.ok(tenant);
    }
 
    @PreAuthorize("@ss.hasPermi('system:contract:list')")
    @ApiOperation(value = "获取合同分页列表")
    @GetMapping(value = "/contractListByTenantId")
    public R<List<TContract>> contractListByTenantId(@RequestParam String tenantId) {
        return R.ok(contractService.list(Wrappers.lambdaQuery(TContract.class).eq(TContract::getTenantId, tenantId)));
    }
 
    /**
     * 删除租户
     */
    @PreAuthorize("@ss.hasPermi('system:tenant:delete')")
    @Log(title = "租户信息-删除租户", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除租户")
    @DeleteMapping(value = "/deleteById")
    public R<Boolean> deleteById(@RequestParam String id) {
        return R.ok(tenantService.removeById(id));
    }
 
    /**
     * 批量删除租户
     */
    @PreAuthorize("@ss.hasPermi('system:tenant:delete')")
    @Log(title = "租户信息-删除租户", businessType = BusinessType.DELETE)
    @ApiOperation(value = "批量删除租户")
    @DeleteMapping(value = "/deleteByIds")
    public R<Boolean> deleteByIds(@RequestBody List<String> ids) {
        return R.ok(tenantService.removeByIds(ids));
    }
 
    /**
     * 导出
     */
    @ApiOperation(value = "导出")
    @PreAuthorize("@ss.hasPermi('system:contract:export')")
    @Log(title = "导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void exportOpticalInspection(@Validated @RequestBody TContractQuery query)
    {
        List<ContractExport> contractExports = new ArrayList<>();
        List<TContract> exportList = contractService.list(Wrappers.lambdaQuery(TContract.class).eq(TContract::getTenantId, query.getTenantId()));
        for (TContract contract : exportList) {
            ContractExport contractExport = new ContractExport();
            contractExport.setContractNumber(contract.getContractNumber());
            contractExport.setContractName(contract.getContractName());
            contractExport.setPartyOneName(contract.getPartyOneName());
            contractExport.setPartyTwoName(contract.getPartyTwoName());
            contractExport.setCreateTime(DateUtils.localDateTimeToStringYear(contract.getCreateTime()));
            contractExport.setStartTime(DateUtils.localDateTimeToStringYear(contract.getStartTime()));
            contractExport.setEndTime(DateUtils.localDateTimeToStringYear(contract.getEndTime()));
            contractExport.setPayType(DictUtils.getDictLabel(DictConstants.DICT_TYPE_CONTRACT_PAY_TYPE,contract.getPayType()));
            contractExport.setDeposit(contract.getDeposit()+"");
            contractExport.setStatus(DictUtils.getDictLabel(DictConstants.DICT_TYPE_CONTRACT_STATUS,contract.getStatus()));
            contractExports.add(contractExport);
        }
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), ContractExport.class, contractExports);
        HttpServletResponse response = WebUtils.response();
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        ServletOutputStream outputStream = null;
        try {
            String fileName = URLEncoder.encode("合同列表.xls", "utf-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            response.setContentType("application/vnd.ms-excel;charset=UTF-8");
            response.setHeader("Pragma", "no-cache");
            response.setHeader("Cache-Control", "no-cache");
            outputStream = response.getOutputStream();
            workbook.write(outputStream);
        } catch (IOException e) {
            e.printStackTrace();
            System.err.println("合同列表信息导出失败");
        } finally {
            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}