wuzeyu
2024-08-28 55798c815327639fdc8f1874acf2ec543c596aa1
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
package com.dy.pipIrrProject.controller;
 
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.dy.common.aop.SsoAop;
import com.dy.common.webUtil.BaseResponse;
import com.dy.common.webUtil.BaseResponseUtils;
import com.dy.common.webUtil.QueryResultVo;
import com.dy.common.webUtil.ResultCodeMsg;
import com.dy.pipIrrGlobal.pojoBa.BaClient;
import com.dy.pipIrrGlobal.pojoPr.PrController;
import com.dy.pipIrrGlobal.voPr.VoController;
import com.dy.pipIrrGlobal.voSe.VoActiveCard;
import com.dy.pipIrrProject.result.ProjectResultCode;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
 
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.*;
 
/**
 * @author ZhuBaoMin
 * @date 2023-12-29 10:06
 * @LastEditTime 2023-12-29 10:06
 * @Description
 */
 
@Slf4j
@Tag(name = "控制器管理", description = "控制器操作")
@RestController
@RequestMapping(path = "controller")
@RequiredArgsConstructor
public class ControllerCtrl {
    private final ControllerSv controllerSv;
 
    /**
     * 根据指定条件获取控制器列表
     *
     * @param vo 查询条件
     * @return 符合条件的控制器列表
     */
    @Operation(summary = "获得一页控制器记录", description = "返回一页控制器数据")
    @ApiResponses(value = {
            @ApiResponse(
                    responseCode = ResultCodeMsg.RsCode.SUCCESS_CODE,
                    description = "返回一页控制器数据(BaseResponse.content:QueryResultVo[{}])",
                    content = {@Content(mediaType = MediaType.APPLICATION_JSON_VALUE,
                            schema = @Schema(implementation = VoActiveCard.class))}
            )
    })
    @GetMapping(path = "/getControllers")
    @SsoAop()
    public BaseResponse<QueryResultVo<List<VoController>>> getControllers(QueryVo vo) {
        try {
            QueryResultVo<List<VoController>> res = controllerSv.getControllers(vo);
            if (res.itemTotal != null && res.itemTotal > 0) {
                return BaseResponseUtils.buildSuccess(res);
            } else {
                return BaseResponseUtils.buildSuccess(ProjectResultCode.NO_RECORDS.getMessage());
            }
            //return BaseResponseUtils.buildSuccess(res);
        } catch (Exception e) {
            log.error("获取控制器记录异常", e);
            return BaseResponseUtils.buildException(e.getMessage());
        }
    }
 
    /**
     * 根据控制器编号获取控制器列表
     *
     * @param rtuAddr 控制器地址
     * @return 符合条件的控制器列表
     */
    @Operation(summary = "获得控制器记录", description = "返回取控制器数据")
    @ApiResponses(value = {
            @ApiResponse(
                    responseCode = ResultCodeMsg.RsCode.SUCCESS_CODE,
                    description = "返回控制器数据(BaseResponse.content:QueryResultVo[{}])",
                    content = {@Content(mediaType = MediaType.APPLICATION_JSON_VALUE,
                            schema = @Schema(implementation = BaClient.class))}
            )
    })
    @GetMapping(path = "controller_list")
    @SsoAop()
    public BaseResponse<List<Map<String, Object>>> getControllersByCode(String rtuAddr) {
        try {
            List<Map<String, Object>> list = Optional.ofNullable(controllerSv.getControllersByAddr(rtuAddr)).orElse(new ArrayList<>());
            if (list.size() <= 0) {
                return BaseResponseUtils.buildErrorMsg(ProjectResultCode.NO_RECORDS.getMessage());
            }
            return BaseResponseUtils.buildSuccess(list);
        } catch (Exception e) {
            log.error("查询控制器异常", e);
            return BaseResponseUtils.buildException(e.getMessage());
        }
    }
 
    /**
     * 添加控制器
     *
     * @param po            控制器实体对象
     * @param bindingResult
     * @return 添加是否成功
     */
    @Operation(summary = "添加控制器记录", description = "添加控制器记录")
    @ApiResponses(value = {
            @ApiResponse(
                    responseCode = ResultCodeMsg.RsCode.SUCCESS_CODE,
                    description = "操作结果:true:成功,false:失败(BaseResponse.content)",
                    content = {@Content(mediaType = MediaType.APPLICATION_JSON_VALUE,
                            schema = @Schema(implementation = Boolean.class))}
            )
    })
    @PostMapping(path = "add", consumes = MediaType.APPLICATION_JSON_VALUE)
    @Transactional(rollbackFor = Exception.class)
    @SsoAop()
    //public BaseResponse<Boolean> add(@RequestBody @Valid DtoController po, BindingResult bindingResult){
    public BaseResponse<Boolean> add(@RequestBody @Valid PrController po, BindingResult bindingResult) {
        if (bindingResult != null && bindingResult.hasErrors()) {
            return BaseResponseUtils.buildErrorMsg(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }
 
        //PrController prController = DtoToPojo.INSTANCT.po2vo(po);
        //Date operateTime = new Date();
        //prController.setOperatedt(operateTime);
        //prController.setDeleted((byte)0);
        //prController.setReporttime(operateTime);
 
        Date operateTime = new Date();
        po.setOperateDt(operateTime);
        po.setDeleted((byte) 0);
        po.setFindDt(operateTime);
        if (po.getAddWays() == null) {
            po.setAddWays((byte) 1);
        }
        Integer rec = Optional.ofNullable(controllerSv.addController(po)).orElse(0);
        if (rec == 0) {
            return BaseResponseUtils.buildErrorMsg(ProjectResultCode.CONTROLLER_FAIL.getMessage());
        }
        return BaseResponseUtils.buildSuccess(true);
    }
 
    /**
     * 根据控制器编号删除控制器
     *
     * @param map
     * @return
     */
    @Operation(summary = "删除控制器记录", description = "删除控制器记录")
    @ApiResponses(value = {
            @ApiResponse(
                    responseCode = ResultCodeMsg.RsCode.SUCCESS_CODE,
                    description = "操作结果:true:成功,false:失败(BaseResponse.content)",
                    content = {@Content(mediaType = MediaType.APPLICATION_JSON_VALUE,
                            schema = @Schema(implementation = Boolean.class))}
            )
    })
    @PostMapping(path = "delete")
    @SsoAop()
    public BaseResponse<Boolean> delete(@RequestBody Map map) {
        if (map == null || map.size() <= 0) {
            return BaseResponseUtils.buildErrorMsg(ProjectResultCode.PLEASE_INPUT_CONTROLLER_ID.getMessage());
        }
 
        Long controllerId = Long.parseLong(map.get("controllerId").toString());
        Integer recordCount = Optional.ofNullable(controllerSv.deleteControllerById(controllerId)).orElse(0);
        if (recordCount == 0) {
            return BaseResponseUtils.buildErrorMsg(ProjectResultCode.DELETE_CONTROLLER_FAIL.getMessage());
        }
        return BaseResponseUtils.buildSuccess(true);
    }
 
    /**
     * 获取未绑控制器的取水口列表
     *
     * @return
     */
    @Operation(summary = "获得未绑控制器的取水口记录", description = "返回未绑控制器的取水口数据")
    @ApiResponses(value = {
            @ApiResponse(
                    responseCode = ResultCodeMsg.RsCode.SUCCESS_CODE,
                    description = "返回控制器数据(BaseResponse.content:QueryResultVo[{}])",
                    content = {@Content(mediaType = MediaType.APPLICATION_JSON_VALUE,
                            schema = @Schema(implementation = BaClient.class))}
            )
    })
    @GetMapping(path = "nobinding_intakes")
    @SsoAop()
    public BaseResponse<JSONArray> getNoBindingIntakes() {
        try {
            JSONArray array = controllerSv.getNoBindingIntakes();
//            if (array.size() <= 0) {
//                return BaseResponseUtils.buildFail(ProjectResultCode.NO_BINDING_INTAKE.getMessage());
//            }
            return BaseResponseUtils.buildSuccess(array);
        } catch (Exception e) {
            log.error("查询控制器异常", e);
            return BaseResponseUtils.buildException(e.getMessage());
        }
    }
 
    /**
     * 导出控制器列表
     *
     * @param response
     * @param vo
     */
    @SneakyThrows(IOException.class)
    @RequestMapping(value = "/export", method = RequestMethod.GET)
    public void export(HttpServletResponse response, QueryVo vo) {
        setExcelRespProp(response, "控制器列表");
        //List<VoDivide> memberList = LocalJsonUtil.getListFromJson("json/members.json", VoDivide.class);
        List<VoController> memberList = controllerSv.export(vo);
 
        EasyExcel.write(response.getOutputStream())
                .head(VoController.class)
                .excelType(ExcelTypeEnum.XLSX)
                .sheet("控制器列表")
                .doWrite(memberList);
    }
 
    /**
     * 设置excel下载响应头属性
     */
    private void setExcelRespProp(HttpServletResponse response, String rawFileName) throws UnsupportedEncodingException {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode(rawFileName, "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
    }
 
 
}