puzhibing
2026-09-28 0f7f9ebdf8c7eb42c74a994b602f33c0d38aafb7
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
package com.ruoyi.other.controller;
 
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.PageInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.Logical;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.other.api.domain.TGroupCompany;
import com.ruoyi.other.service.TGroupCompanyService;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
 
/**
 * <p>
 * 公司(企业)管理 前端控制器
 * </p>
 *
 * @author mxcd
 * @since 2026-09-22
 */
@RestController
@RequestMapping("/group-company")
public class TGroupCompanyController {
 
    @Resource
    private TGroupCompanyService groupCompanyService;
 
    /**
     * 分页查询公司
     */
    @RequiresPermissions(value = {"/companyManagement/list", "/fleet/list", "/vinManagement/list", "/chargingCard/list"}, logical = Logical.OR)
    @GetMapping("/page")
    @ApiOperation(value = "分页查询公司", tags = {"管理后台-公司管理"})
    public AjaxResult<PageInfo<TGroupCompany>> page(@RequestParam(value = "current", defaultValue = "1") Integer current,
                                                     @RequestParam(value = "size", defaultValue = "10") Integer size,
                                                     @RequestParam(value = "groupId", required = false) Integer groupId,
                                                     @RequestParam(value = "name", required = false) String name,
                                                     @RequestParam(value = "contactName", required = false) String contactName,
                                                     @RequestParam(value = "contactPhone", required = false) String contactPhone,
                                                     @RequestParam(value = "status", required = false) Integer status) {
        PageInfo<TGroupCompany> page = new PageInfo<>(current, size);
        groupCompanyService.lambdaQuery()
                .eq(groupId != null, TGroupCompany::getGroupId, groupId)
                .like(StringUtils.hasText(name), TGroupCompany::getName, name)
                .like(StringUtils.hasText(contactName), TGroupCompany::getContactName, contactName)
                .like(StringUtils.hasText(contactPhone), TGroupCompany::getContactPhone, contactPhone)
                .eq(status != null, TGroupCompany::getStatus, status)
                .orderByDesc(TGroupCompany::getId)
                .page(page);
        return AjaxResult.success(page);
    }
 
    /**
     * 公司详情
     */
    @RequiresPermissions(value = {"/companyManagement/list"}, logical = Logical.OR)
    @GetMapping("/detail")
    @ApiOperation(value = "公司详情", tags = {"管理后台-公司管理"})
    public AjaxResult<TGroupCompany> detail(@RequestParam("id") Integer id) {
        return AjaxResult.success(groupCompanyService.getDetail(id));
    }
 
    /**
     * 添加公司
     */
    @RequiresPermissions(value = {"/companyManagement/list"}, logical = Logical.OR)
    @PostMapping("/add")
    @ApiOperation(value = "添加公司", tags = {"管理后台-公司管理"})
    @Log(title = "【公司管理】添加公司", businessType = BusinessType.INSERT)
    public AjaxResult add(@RequestBody TGroupCompany company) {
        if (company.getGroupId() == null) {
            return AjaxResult.error("所属集团不能为空");
        }
        if (!StringUtils.hasText(company.getName())) {
            return AjaxResult.error("公司名称不能为空");
        }
        company.setId(null);
        company.setDelFlag(false);
        if (company.getStatus() == null) {
            company.setStatus(1);
        }
        groupCompanyService.save(company);
        return AjaxResult.success();
    }
 
    /**
     * 编辑公司
     */
    @RequiresPermissions(value = {"/companyManagement/list"}, logical = Logical.OR)
    @PostMapping("/edit")
    @ApiOperation(value = "编辑公司", tags = {"管理后台-公司管理"})
    @Log(title = "【公司管理】编辑公司", businessType = BusinessType.UPDATE)
    public AjaxResult edit(@RequestBody TGroupCompany company) {
        if (company.getId() == null) {
            return AjaxResult.error("公司ID不能为空");
        }
        groupCompanyService.updateById(company);
        return AjaxResult.success();
    }
 
    /**
     * 删除公司
     */
    @RequiresPermissions(value = {"/companyManagement/list"}, logical = Logical.OR)
    @DeleteMapping("/del")
    @ApiOperation(value = "删除公司", tags = {"管理后台-公司管理"})
    @Log(title = "【公司管理】删除公司", businessType = BusinessType.DELETE)
    public AjaxResult del(@RequestParam("id") Integer id) {
        if (id == null) {
            return AjaxResult.error("公司ID不能为空");
        }
        groupCompanyService.removeById(id);
        return AjaxResult.success();
    }
 
    /**
     * 切换公司启用状态
     */
    @RequiresPermissions(value = {"/companyManagement/list"}, logical = Logical.OR)
    @PostMapping("/status")
    @ApiOperation(value = "切换公司启用状态", tags = {"管理后台-公司管理"})
    @Log(title = "【公司管理】切换公司状态", businessType = BusinessType.UPDATE)
    public AjaxResult status(@RequestParam("id") Integer id, @RequestParam("status") Integer status) {
        if (id == null) {
            return AjaxResult.error("公司ID不能为空");
        }
        TGroupCompany update = new TGroupCompany();
        update.setId(id);
        update.setStatus(status);
        groupCompanyService.updateById(update);
        return AjaxResult.success();
    }
 
    /**
     * 远程接口:公司详情(VIN码车辆所属公司、可用余额校验、卡片额度分配概览使用,内部调用)
     * 返回含车队数量、月充值金额、累计充值到账金额(统计值,非表字段)
     */
    @PostMapping("/remote/get/{id}")
    @ApiOperation(value = "远程-公司详情", tags = {"内部调用"})
    public R<TGroupCompany> remoteGet(@PathVariable("id") Integer id) {
        if (id == null) {
            return R.fail("公司ID不能为空");
        }
        return R.ok(groupCompanyService.getDetail(id));
    }
 
    /**
     * 远程接口:扣减公司可用余额(条件更新保证余额充足,返回扣减后余额,内部调用)
     */
    @PostMapping("/remote/deduct-balance")
    @ApiOperation(value = "远程-扣减公司可用余额", tags = {"内部调用"})
    public R<BigDecimal> remoteDeductBalance(@RequestParam("companyId") Integer companyId,
                                             @RequestParam("amount") BigDecimal amount) {
        if (companyId == null) {
            return R.fail("公司ID不能为空");
        }
        if (amount == null || amount.compareTo(BigDecimal.ZERO) <= 0) {
            return R.fail("扣减金额必须大于0");
        }
        if (amount.scale() > 2) {
            return R.fail("扣减金额最多保留2位小数");
        }
        TGroupCompany company = groupCompanyService.getById(companyId);
        if (company == null) {
            return R.fail("公司不存在或已删除");
        }
        if (!Integer.valueOf(1).equals(company.getStatus())) {
            return R.fail("公司已停用,无法扣减可用余额");
        }
        boolean updated = groupCompanyService.lambdaUpdate()
                .eq(TGroupCompany::getId, companyId)
                .ge(TGroupCompany::getAvailableBalance, amount)
                .setSql("available_balance = available_balance - " + amount.toPlainString())
                .update();
        if (!updated) {
            return R.fail("公司可用余额不足");
        }
        TGroupCompany latest = groupCompanyService.getById(companyId);
        return R.ok(latest == null ? null : latest.getAvailableBalance());
    }
 
    /**
     * 远程接口:退还公司可用余额(返回退还后余额,内部调用)
     */
    @PostMapping("/remote/refund-balance")
    @ApiOperation(value = "远程-退还公司可用余额", tags = {"内部调用"})
    public R<BigDecimal> remoteRefundBalance(@RequestParam("companyId") Integer companyId,
                                             @RequestParam("amount") BigDecimal amount) {
        if (companyId == null) {
            return R.fail("公司ID不能为空");
        }
        if (amount == null || amount.compareTo(BigDecimal.ZERO) <= 0) {
            return R.fail("退还金额必须大于0");
        }
        if (amount.scale() > 2) {
            return R.fail("退还金额最多保留2位小数");
        }
        boolean updated = groupCompanyService.lambdaUpdate()
                .eq(TGroupCompany::getId, companyId)
                .setSql("available_balance = IFNULL(available_balance, 0) + " + amount.toPlainString())
                .update();
        if (!updated) {
            return R.fail("公司不存在或已删除");
        }
        TGroupCompany latest = groupCompanyService.getById(companyId);
        return R.ok(latest == null ? null : latest.getAvailableBalance());
    }
 
}