xuhy
2025-10-13 4dcc6bd1aef24a781afe467aaca59546f2f46444
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
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.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.WebUtils;
import com.ruoyi.system.dto.TCrmSupplierDTO;
import com.ruoyi.system.export.TCrmBranchExport;
import com.ruoyi.system.export.TCrmSupplierExport;
import com.ruoyi.system.model.*;
import com.ruoyi.system.query.TCrmBranchQuery;
import com.ruoyi.system.query.TCrmSupplierQuery;
import com.ruoyi.system.service.*;
import com.ruoyi.system.vo.TCrmSupplierVO;
import io.swagger.annotations.Api;
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.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.List;
import java.util.Objects;
import java.util.stream.Collectors;
 
/**
 * <p>
 * crm供应商 前端控制器
 * </p>
 *
 * @author xiaochen
 * @since 2025-08-20
 */
@Api(tags = "crm供应商管理")
@RestController
@RequestMapping("/t-crm-supplier")
public class TCrmSupplierController {
 
    private final TCrmSupplierService crmSupplierService;
    private final ISysUserService sysUserService;
    private final TCrmWarehouseService crmWarehouseService;
    @Autowired
    public TCrmSupplierController(TCrmSupplierService crmSupplierService, ISysUserService sysUserService, TCrmWarehouseService crmWarehouseService) {
        this.crmSupplierService = crmSupplierService;
        this.sysUserService = sysUserService;
        this.crmWarehouseService = crmWarehouseService;
    }
 
    /**
     * 获取crm供应商管理管理列表
     */
    @ApiOperation(value = "获取crm供应商管理分页列表")
    @PostMapping(value = "/pageList")
    public R<PageInfo<TCrmSupplierVO>> pageList(@RequestBody TCrmSupplierQuery query) {
        return R.ok(crmSupplierService.pageList(query));
    }
 
    /**
     * 获取crm供应商管理管理列表
     */
    @ApiOperation(value = "获取crm供应商管理列表")
    @PostMapping(value = "/list")
    public R<List<TCrmSupplier>> list() {
        return R.ok(crmSupplierService.list(Wrappers.lambdaQuery(TCrmSupplier.class)
                .eq(TCrmSupplier::getStatus,1)
                .orderByDesc(TCrmSupplier::getCreateTime)));
    }
 
    /**
     * 添加crm供应商管理管理
     */
    @Log(title = "crm供应商管理信息-新增crm供应商管理", businessType = BusinessType.INSERT)
    @ApiOperation(value = "添加crm供应商管理")
    @PostMapping(value = "/add")
    public R<Boolean> add(@Validated @RequestBody TCrmSupplierDTO dto) {
        return crmSupplierService.addSupplier(dto);
    }
 
    /**
     * 修改crm供应商管理
     */
    @Log(title = "crm供应商管理信息-修改crm供应商管理", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改crm供应商管理")
    @PostMapping(value = "/update")
    public R<Boolean> update(@Validated @RequestBody TCrmSupplierDTO dto) {
        return crmSupplierService.updateSupplier(dto);
    }
 
    /**
     * 查看crm供应商管理详情
     */
    @ApiOperation(value = "查看crm供应商管理详情")
    @GetMapping(value = "/getDetailById")
    public R<TCrmSupplierVO> getDetailById(@RequestParam String id) {
        TCrmSupplier crmSupplier = crmSupplierService.getById(id);
        TCrmSupplierVO crmSupplierVO = new TCrmSupplierVO();
        BeanUtils.copyProperties(crmSupplier, crmSupplierVO);
        // 查询仓库
        TCrmWarehouse crmWarehouse = crmWarehouseService.getById(crmSupplier.getWarehouseId());
        if(Objects.nonNull(crmWarehouse)){
            crmSupplierVO.setWarehouseName(crmWarehouse.getWarehouseName());
        }
        return R.ok(crmSupplierVO);
    }
 
    /**
     * 删除crm供应商管理
     */
    @Log(title = "crm供应商管理信息-删除crm供应商管理", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除crm供应商管理")
    @DeleteMapping(value = "/deleteById")
    public R<Boolean> deleteById(@RequestParam String id) {
        // 删除账号
        TCrmSupplier crmBranch = crmSupplierService.getById(id);
        sysUserService.deleteUserById(crmBranch.getUserId());
        return R.ok(crmSupplierService.removeById(id));
    }
 
    /**
     * 批量删除crm供应商管理
     */
    @Log(title = "crm供应商管理信息-删除crm供应商管理", businessType = BusinessType.DELETE)
    @ApiOperation(value = "批量删除crm供应商管理")
    @DeleteMapping(value = "/deleteByIds")
    public R<Boolean> deleteByIds(@RequestBody List<String> ids) {
        // 删除账号
        List<TCrmSupplier> crmSupplierList = crmSupplierService.list(Wrappers.lambdaQuery(TCrmSupplier.class).in(TCrmSupplier::getId, ids));
        List<Long> userIds = crmSupplierList.stream().map(TCrmSupplier::getUserId).collect(Collectors.toList());
        sysUserService.deleteUserByIds(userIds);
        return R.ok(crmSupplierService.removeByIds(ids));
    }
 
    /**
     * 批量删除crm供应商管理
     */
    @Log(title = "crm供应商管理信息-供应商管理解冻冻结", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "供应商管理解冻冻结",notes = "状态 1=使用中 2=冻结")
    @PutMapping(value = "/thawOrFreeze")
    public R<Boolean> thawOrFreeze(@RequestParam(value = "id")String id,
                                   @RequestParam(value = "status")Integer status) {
        TCrmSupplier crmSupplier = crmSupplierService.getById(id);
        crmSupplier.setStatus(status);
        crmSupplierService.updateById(crmSupplier);
        // 查询用户
        SysUser sysUser = sysUserService.selectUserById(crmSupplier.getUserId());
        switch (status){
            case 1:
                // 解冻
                sysUser.setStatus("0");
                sysUserService.updateUser(sysUser);
                break;
            case 2:
                // 冻结
                sysUser.setStatus("1");
                sysUserService.updateUser(sysUser);
                break;
        }
        return R.ok();
    }
 
    @Log(title = "crm供应商管理信息-供应商管理导出", businessType = BusinessType.EXPORT)
    @ApiOperation(value = "供应商管理导出")
    @PostMapping("/exportList")
    public void exportList(@RequestBody TCrmSupplierQuery query){
        List<TCrmSupplierExport> crmSupplierExports = crmSupplierService.exportList(query);
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), TCrmSupplierExport.class, crmSupplierExports);
        HttpServletResponse response = WebUtils.response();
        assert response != null;
        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();
        } finally {
            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
 
}