zxl
2026-03-25 d1dfb6e35f38e27fd960dc3ad0130c8d0f5c39bb
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
package com.tievd.jyz.controller;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.tievd.cube.commons.annotations.AutoLog;
import com.tievd.cube.commons.annotations.DictApi;
import com.tievd.cube.commons.base.CubeController;
import com.tievd.cube.commons.base.Result;
import com.tievd.jyz.entity.ActOrgRef;
import com.tievd.jyz.entity.Activity;
import com.tievd.jyz.service.IActOrgRefService;
import com.tievd.jyz.service.IActivityService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
 
/**
 * Activity
 *
 * @author      cube
 * @since       2023-08-15
 * @version     V2.0.0
 */
@Slf4j
@DictApi
@RestController
@RequestMapping("/jyz/activity")
@Tag(name = "活动接口")
public class ActivityController extends CubeController<Activity, IActivityService> {
 
  @Autowired
  private IActivityService activityService;
  
  @Autowired
  IActOrgRefService actOrgRefService;
  
  @Autowired
  private com.tievd.jyz.mapper.OilRecordMapper oilRecordMapper;
  
  /**
   * 分页列表查询
   */
  @GetMapping("/list")
  public Result<?> queryPageList(Activity activity,
                    @RequestParam(defaultValue="1") Integer pageNo,
                    @RequestParam(defaultValue="10") Integer pageSize,
                    HttpServletRequest req) {
    Page<Activity> page = new Page<>(pageNo, pageSize);
    MPJLambdaWrapper<Activity> wrapper = new MPJLambdaWrapper<>(activity);
    wrapper.selectAll(Activity.class)
            .leftJoin(ActOrgRef.class, ActOrgRef::getActId, Activity::getId)
            .select("GROUP_CONCAT(org_code) orgCodes")
            .groupBy(Activity::getId);
    IPage<Activity> pageList = activityService.page(page, wrapper);
    return Result.ok(pageList);
  }
  
  
  @GetMapping("/listAll")
  @Operation(description = "活动下拉")
  public Result<?> listAll(@RequestParam String orgCode) {
    MPJLambdaWrapper<Activity> wrapper = new MPJLambdaWrapper<>();
    wrapper.selectAll(Activity.class)
            .innerJoin(ActOrgRef.class, ActOrgRef::getActId, Activity::getId)
            .likeRight(ActOrgRef::getOrgCode, orgCode)
            .groupBy(Activity::getId);
    List<Activity> res = activityService.list(wrapper);
    return Result.ok(res);
  }
  
  /**
   * 添加
   */
  @AutoLog("Activity-添加")
  @PostMapping("/add")
  public Result<?> add(@RequestBody Activity activity) {
    long count = activityService.count(new LambdaQueryWrapper<Activity>().eq(Activity::getName, activity.getName()));
    if (count > 0) {
      return Result.error("活动名称重复,请重新命名");
    }
    
    activityService.save(activity);
    updateRef(activity);
    return Result.ok();
  }
  
  void updateRef(Activity activity) {
    String orgCodes = activity.getOrgCodes();
    if (StringUtils.isNotBlank(orgCodes)) {
      String[] orgCodeArr = orgCodes.split(",");
      List<ActOrgRef> refs = new ArrayList<>();
      for (int i = 0; i < orgCodeArr.length; i++) {
        refs.add(new ActOrgRef().setActId(activity.getId()).setOrgCode(orgCodeArr[i]));
      }
      actOrgRefService.saveBatch(refs);
    }
  }
  
  void removeRef(Integer actId){
    ActOrgRef actOrgRef = new ActOrgRef().setActId(actId);
    actOrgRefService.remove(new QueryWrapper<>(actOrgRef));
  }
  
  /**
   * 编辑
   */
  @AutoLog("Activity-编辑")
  @PutMapping("/edit")
  public Result<?> edit(@RequestBody Activity activity) {
    long count = activityService.count(new LambdaQueryWrapper<Activity>().eq(Activity::getName, activity.getName()));
    if (count > 0) {
      return Result.error("活动名称重复,请重新命名");
    }
    activityService.updateById(activity);
    removeRef(activity.getId());
    updateRef(activity);
    return Result.ok();
  }
  
  /**
   * 通过id删除
   */
  @AutoLog("Activity-通过id删除")
  @DeleteMapping("/delete")
  public Result<?> delete(@RequestParam Integer id) {
    activityService.removeById(id);
    removeRef(id);
    return Result.ok();
  }
  
  /**
   * 批量删除
   */
  @AutoLog("Activity-批量删除")
  @DeleteMapping("/deleteBatch")
  public Result<?> deleteBatch(@RequestParam String ids) {
    this.activityService.removeByIds(Arrays.asList(ids.split(",")));
    return Result.ok();
  }
  
  /**
   * 通过id查询
   */
  @GetMapping("/queryById")
  public Result<?> queryById(@RequestParam String id) {
    Activity activity = activityService.getById(id);
    return Result.ok(activity);
  }
  
  /**
   * 查看车辆加油频次
   */
  @GetMapping("/getVehicleFrequency")
  @Operation(description = "查看车辆加油频次")
  public Result<?> getVehicleFrequency(@RequestParam Integer actId) {
    Activity activity = activityService.getById(actId);
    if (activity == null) {
      return Result.error("活动不存在");
    }
    
    List<ActOrgRef> orgRefs = actOrgRefService.list(new LambdaQueryWrapper<ActOrgRef>().eq(ActOrgRef::getActId, actId));
    if (orgRefs == null || orgRefs.isEmpty()) {
      return Result.error("活动未关联机构");
    }
    
    List<String> orgCodes = new ArrayList<>();
    for (ActOrgRef ref : orgRefs) {
      orgCodes.add(ref.getOrgCode());
    }
    
    java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String startTimeStr = sdf.format(activity.getStartTime());
    String endTimeStr = sdf.format(activity.getEndTime());
    
    java.util.Map<String, Object> result = new java.util.HashMap<>();
    
    result.put("activity", activity);
    result.put("beforeActivity", oilRecordMapper.getVehicleFrequencyBeforeActivity(startTimeStr, orgCodes));
    result.put("duringActivity", oilRecordMapper.getVehicleFrequencyDuringActivity(startTimeStr, endTimeStr, orgCodes));
    result.put("afterActivity", oilRecordMapper.getVehicleFrequencyAfterActivity(endTimeStr, orgCodes));
    
    return Result.ok(result);
  }
 
  /**
   * 导出excel
   */
  @RequestMapping("/exportXls")
  public void exportXls(HttpServletRequest request, HttpServletResponse response, Activity activity)  throws IOException {
      super.exportXls(request, response, activity, "Activity");
  }
 
  /**
   * 通过excel导入数据
   */
  @PostMapping("/importExcel")
  public Result<?> importExcel(HttpServletRequest request) throws Exception {
      return super.importExcel(request, Activity.class);
  }
}