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);
|
}
|
}
|