wuzeyu
2024-05-15 fbd4cf5aeb4274938c12030e5d235c65c20c19c3
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
package com.dy.pipIrrIrrigate.project;
 
import com.dy.common.aop.SsoAop;
import com.dy.common.webUtil.BaseResponse;
import com.dy.common.webUtil.BaseResponseUtils;
import com.dy.common.webUtil.ResultCodeMsg;
import com.dy.pipIrrGlobal.pojoIr.IrProject;
import com.dy.pipIrrIrrigate.result.IrrigateResultCode;
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.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.http.MediaType;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
 
/**
 * @author :WuZeYu
 * @Date :2024/5/14  13:53
 * @LastEditTime :2024/5/14  13:53
 * @Description
 */
@Slf4j
@Tag(name = "项目管理", description = "项目操作")
@RestController
@RequestMapping(path = "project")
@RequiredArgsConstructor
public class ProjectCtrl {
    private final ProjectSv projectSv;
 
    /**
     * 添加项目
     *
     * @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 IrProject po, BindingResult bindingResult) {
        if (bindingResult != null && bindingResult.hasErrors()) {
            return BaseResponseUtils.buildFail(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }
        try {
            Integer rec = Optional.ofNullable(projectSv.addProject(po)).orElse(0);
            if (rec == 0) {
                return BaseResponseUtils.buildFail(IrrigateResultCode.ADD_PROJECT_FAIL.getMessage());
            }
        } catch (Exception e) {
            log.error("添加项目异常", e);
            return BaseResponseUtils.buildException(e.getMessage());
        }
        return BaseResponseUtils.buildSuccess(true);
    }
 
    @PostMapping(path = "delete", consumes = MediaType.APPLICATION_JSON_VALUE)
    @Transactional(rollbackFor = Exception.class)
    @SsoAop()
    public BaseResponse<Boolean> delete(@RequestBody Map map) {
        if (map == null || map.size() <= 0) {
            BaseResponseUtils.buildFail(IrrigateResultCode.PLEASE_INPUT_PROJECT_ID.getMessage());
        }
        Long projectId = Long.parseLong(map.get("projectId").toString());
        try {
            Integer rows = projectSv.deleteProject(projectId);
            if (rows == 0) {
                return BaseResponseUtils.buildFail(IrrigateResultCode.DELETE_PROJECT_FAIL.getMessage());
            }
        } catch (Exception e) {
            log.error("删除项目异常", e);
            return BaseResponseUtils.buildException(e.getMessage());
        }
        return BaseResponseUtils.buildSuccess(true);
    }
}