#
vincentlu
3 天以前 b033217d3193b2f5ebabb43cce4c7cb2776df6ee
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
package com.zy.acs.manager.manager.controller;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zy.acs.framework.common.Cools;
import com.zy.acs.framework.common.R;
import com.zy.acs.framework.exception.CoolException;
import com.zy.acs.manager.common.annotation.OperationLog;
import com.zy.acs.manager.common.domain.BaseParam;
import com.zy.acs.manager.common.domain.KeyValVo;
import com.zy.acs.manager.common.domain.PageParam;
import com.zy.acs.manager.common.utils.ExcelUtil;
import com.zy.acs.manager.manager.entity.Sta;
import com.zy.acs.manager.manager.entity.StaReserve;
import com.zy.acs.manager.manager.service.StaReserveService;
import com.zy.acs.manager.manager.service.StaService;
import com.zy.acs.manager.system.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
 
import javax.servlet.http.HttpServletResponse;
import java.util.*;
 
@RestController
@RequestMapping("/api")
public class StaController extends BaseController {
 
    @Autowired
    private StaService staService;
    @Autowired
    private StaReserveService staReserveService;
 
    @PreAuthorize("hasAuthority('manager:sta:list')")
    @PostMapping("/sta/page")
    public R page(@RequestBody Map<String, Object> map) {
        BaseParam baseParam = buildParam(map, BaseParam.class);
        PageParam<Sta, BaseParam> pageParam = new PageParam<>(baseParam, Sta.class);
        return R.ok().add(staService.page(pageParam, pageParam.buildWrapper(true)));
    }
 
    @PreAuthorize("hasAuthority('manager:sta:list')")
    @PostMapping("/sta/list")
    public R list(@RequestBody Map<String, Object> map) {
        return R.ok().add(staService.list());
    }
 
    @PreAuthorize("hasAuthority('manager:sta:list')")
    @PostMapping({"/sta/many/{ids}", "/stas/many/{ids}"})
    public R many(@PathVariable Long[] ids) {
        return R.ok().add(staService.listByIds(Arrays.asList(ids)));
    }
 
    @PreAuthorize("hasAuthority('manager:sta:list')")
    @GetMapping("/sta/{id}")
    public R get(@PathVariable("id") Long id) {
        return R.ok().add(staService.getById(id));
    }
 
    @PreAuthorize("hasAuthority('manager:sta:save')")
    @OperationLog("Create Sta")
    @PostMapping("/sta/save")
    public R save(@RequestBody Sta sta) {
        if (staService.count(new LambdaQueryWrapper<Sta>()
                .eq(Sta::getStaNo, sta.getStaNo())) > 0) {
            return R.error("sta No. has already exist !");
        }
        Long loginUserId = getLoginUserId();
        Date now = new Date();
        sta.setCreateBy(loginUserId);
        sta.setCreateTime(now);
        sta.setUpdateBy(loginUserId);
        sta.setUpdateTime(now);
        if (!staService.save(sta)) {
            return R.error("Save Fail");
        }
        return R.ok("Save Success").add(sta);
    }
 
    @PreAuthorize("hasAuthority('manager:sta:update')")
    @OperationLog("Update Sta")
    @PostMapping("/sta/update")
    public R update(@RequestBody Sta sta) {
        // 唯一性校验:只有当 sta No. 非空时才校验(避免空值误判)
        if (!Cools.isEmpty(sta.getStaNo())) {
            boolean exists = staService.count(new LambdaQueryWrapper<Sta>()
                    .eq(Sta::getStaNo, sta.getStaNo())
                    .ne(Sta::getId, sta.getId())) > 0;
            if (exists) {
                throw new CoolException("failed to update, because sta No. has already exist !");
            }
        }
        sta.setUpdateBy(getLoginUserId());
        sta.setUpdateTime(new Date());
        if (!staService.updateById(sta)) {
            return R.error("Update Fail");
        }
        return R.ok("Update Success").add(sta);
    }
 
    @PreAuthorize("hasAuthority('manager:sta:remove')")
    @OperationLog("Delete Sta")
    @PostMapping("/sta/remove/{ids}")
    @Transactional
    public R remove(@PathVariable Long[] ids) {
        for (Long id : ids) {
            staReserveService.remove(new LambdaQueryWrapper<StaReserve>().eq(StaReserve::getStaId, id));
            if (!staService.removeById(id)) {
                throw new CoolException("remove Fail");
            }
        }
        return R.ok("Delete Success").add(ids);
    }
 
    @PreAuthorize("hasAuthority('manager:sta:list')")
    @PostMapping("/sta/query")
    public R query(@RequestParam(required = false) String condition) {
        List<KeyValVo> vos = new ArrayList<>();
        LambdaQueryWrapper<Sta> wrapper = new LambdaQueryWrapper<>();
        if (!Cools.isEmpty(condition)) {
            wrapper.like(Sta::getStaNo, condition);
        }
        staService.page(new Page<>(1, 30), wrapper).getRecords().forEach(
                item -> vos.add(new KeyValVo(item.getId(), item.getStaNo()))
        );
        return R.ok().add(vos);
    }
 
    @PreAuthorize("hasAuthority('manager:sta:list')")
    @PostMapping("/sta/export")
    public void export(@RequestBody Map<String, Object> map, HttpServletResponse response) throws Exception {
        ExcelUtil.build(ExcelUtil.create(staService.list(), Sta.class), response);
    }
 
}