package com.ruoyi.chargingPile.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.chargingPile.api.dto.GetSiteListDTO;
import com.ruoyi.chargingPile.api.dto.TChargingGunDTO;
import com.ruoyi.chargingPile.api.model.Site;
import com.ruoyi.chargingPile.api.model.TChargingGun;
import com.ruoyi.chargingPile.api.model.TChargingPile;
import com.ruoyi.chargingPile.api.query.TChargingGunQuery;
import com.ruoyi.chargingPile.api.vo.*;
import com.ruoyi.chargingPile.config.FileUploadConfig;
import com.ruoyi.chargingPile.service.ISiteService;
import com.ruoyi.chargingPile.service.TChargingGunService;
import com.ruoyi.chargingPile.service.TChargingPileService;
import com.ruoyi.chargingPile.util.QRCodeUtils;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.StringUtils;
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.log.enums.OperatorType;
import com.ruoyi.common.security.annotation.Logical;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.order.api.feignClient.ChargingOrderClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
*
* 充电枪 前端控制器
*
*
* @author xiaochen
* @since 2024-08-06
*/
@Api(tags = "充电枪")
@RestController
@RequestMapping("/t-charging-gun")
public class TChargingGunController {
@Autowired
private TChargingGunService chargingGunService;
@Resource
private ChargingOrderClient chargingOrderClient;
@Resource
private TChargingPileService chargingPileService;
@Resource
private ISiteService siteService;
@Autowired
private FileUploadConfig fileUploadConfig;
/**
* 新增临时枪
* @return
*/
@PostMapping("/addGunTemp")
@ResponseBody
R addGunTemp(@RequestBody TChargingGun gun){
chargingGunService.save(gun);
return R.ok(gun.getId());
}
/**
* 查询所有枪
*/
@PostMapping(value = "/getAllGun")
@ResponseBody
public R> getAllGun() {
return R.ok(chargingGunService.list());
}
/**
* 查询所有桩
*/
@PostMapping(value = "/getAllPile")
public R> getAllPile() {
return R.ok(chargingPileService.list());
}
/**
* 查询充电枪列表
*/
@RequiresPermissions(value = {"/chargingPile", "/chargingGun"}, logical = Logical.OR)
@ApiOperation(tags = {"后台-充电枪", "管理后台-充电桩信息"},value = "查询充电枪分页列表")
@PostMapping(value = "/pageList")
public AjaxResult> pageList(@RequestBody TChargingGunQuery query) {
return AjaxResult.ok(chargingGunService.pageList(query));
}
/**
* 添加充电枪管理
*/
@RequiresPermissions(value = {"/chargingPile/add_charging_gun"}, logical = Logical.OR)
@Log(title = "【充电桩信息】添加充电枪", businessType = BusinessType.INSERT,operatorType = OperatorType.MANAGE)
@ApiOperation(tags = {"管理后台-充电桩信息"},value = "添加充电枪")
@PostMapping(value = "/add")
public AjaxResult add(@RequestBody TChargingGunDTO dto) {
return chargingGunService.add(dto);
}
/**
* 修改充电枪
*/
@RequiresPermissions(value = {"/chargingGun/update"}, logical = Logical.OR)
@Log(title = "【充电桩信息】修改充电枪", businessType = BusinessType.UPDATE,operatorType = OperatorType.MANAGE)
@ApiOperation(tags = {"管理后台-充电桩信息"},value = "修改充电枪")
@PostMapping(value = "/update")
public AjaxResult update(@RequestBody TChargingGunDTO dto) {
return chargingGunService.update(dto);
}
/**
* 查看充电枪详情
*/
@RequiresPermissions(value = {"/chargingGun/update", "/chargingGun/select"}, logical = Logical.OR)
@ApiOperation(tags = {"后台-充电枪", "管理后台-充电桩信息"},value = "查看充电枪详情")
@GetMapping(value = "/getDetailById")
public AjaxResult getDetailById(@RequestParam("id") Integer id) {
return AjaxResult.ok(chargingGunService.getById(id));
}
/**
* 删除充电枪
*/
@RequiresPermissions(value = {"/chargingGun/del"}, logical = Logical.OR)
@Log(title = "【充电桩信息】删除充电枪", businessType = BusinessType.DELETE,operatorType = OperatorType.MANAGE)
@ApiOperation(tags = {"管理后台-充电桩信息"},value = "删除充电枪")
@DeleteMapping(value = "/deleteById")
public AjaxResult deleteById(@RequestParam("id") Integer id) {
TChargingGun chargingGun = chargingGunService.getById(id);
chargingGunService.removeById(chargingGun);
return AjaxResult.success();
}
/**
* 批量删除充电枪
*/
@RequiresPermissions(value = {"/chargingGun/del"}, logical = Logical.OR)
@Log(title = "【充电桩信息】批量删除充电枪", businessType = BusinessType.DELETE,operatorType = OperatorType.MANAGE)
@ApiOperation(tags = {"后台-充电枪"},value = "批量删除充电枪")
@DeleteMapping(value = "/deleteByIds")
public AjaxResult deleteByIds(@RequestParam String ids) {
String[] split = ids.split(",");
return AjaxResult.ok(chargingGunService.removeByIds(Arrays.asList(split)));
}
/**
* 结束充电
*/
@RequiresPermissions(value = {"/chargingGun/end_charging"}, logical = Logical.OR)
@Log(title = "【充电桩信息】结束充电", businessType = BusinessType.OTHER,operatorType = OperatorType.MANAGE)
@ApiOperation(tags = {"后台-充电枪"},value = "结束充电")
@PutMapping(value = "/stopCharging")
public AjaxResult stopCharging(@RequestParam("id") String id) {
// 根据充电枪id 查询最新的一笔订单
String data = chargingOrderClient.queryOrderByGunId(id).getData();
// 硬件 结束充电
chargingOrderClient.stopCharging(data);
return AjaxResult.success();
}
@RequiresPermissions(value = {"/chargingGun/download_qr_code"}, logical = Logical.OR)
@ApiOperation(tags = {"管理后台-充电桩信息"},value = "下载二维码")
@GetMapping(value = "/downloadQRCode/{id}")
@Log(title = "【充电桩信息】下载二维码", businessType = BusinessType.OTHER,operatorType = OperatorType.MANAGE)
public void downloadQRCode(@PathVariable Integer id, HttpServletResponse response){
try {
TChargingGun chargingGun = chargingGunService.getById(id);
TChargingPile chargingPile = chargingPileService.getById(chargingGun.getChargingPileId());
String code = StringUtils.isNotEmpty(chargingGun.getFullNumber()) ? chargingGun.getFullNumber() : chargingPile.getCode() + chargingGun.getCode();
String fileName = URLEncoder.encode(code, "UTF-8") + ".jpg";
response.setContentType("application/force-download");
response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);
String url = "https://mxcd.zhinenganguan.com?No=" + code;
String filePath = fileUploadConfig.getLocation() + fileName;
QRCodeUtils.encode(url, filePath);
FileInputStream inputStream = new FileInputStream(filePath);
ServletOutputStream out = response.getOutputStream();
int len = 0;
byte[] buf = new byte[1024];
while ((len = inputStream.read(buf, 0, 1024)) != -1) {
out.write(buf);
}
out.flush();
out.close();
inputStream.close();
//清除服务器上的文件
try {
// 使用Runtime执行命令
Process process = Runtime.getRuntime().exec("sudo rm -rf " + filePath);
// 读取命令的输出
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
// 等待命令执行完成
process.waitFor();
// 关闭流
reader.close();
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}catch (Exception e){
e.printStackTrace();
}
}
@RequiresPermissions(value = {"/chargingGun/download_qr_code"}, logical = Logical.OR)
@ApiOperation(tags = {"管理后台-接口信息"}, value = "批量导出接口二维码")
@PutMapping(value = "/exportQRCodeZip")
@Log(title = "【接口信息】批量导出接口二维码", businessType = BusinessType.EXPORT, operatorType = OperatorType.MANAGE)
public void exportQRCodeZip(@RequestBody TChargingGunQuery query, HttpServletResponse response) {
try {
List list = chargingGunService.exportQRCodeList(query == null ? new TChargingGunQuery() : query);
String zipName = URLEncoder.encode("接口二维码.zip", "UTF-8");
response.setContentType("application/zip");
response.setHeader("Content-Disposition", "attachment;filename=" + zipName);
List names = new ArrayList<>();
try (ZipOutputStream zipOut = new ZipOutputStream(response.getOutputStream())) {
for (TChargingGunVO gun : list) {
String code = StringUtils.isNotEmpty(gun.getFullNumber()) ? gun.getFullNumber() : gun.getChargingPileCode() + gun.getCode();
if (StringUtils.isEmpty(code)) {
continue;
}
String baseName = sanitizeFileName((StringUtils.isNotEmpty(gun.getSiteName()) ? gun.getSiteName() : "电站")
+ (StringUtils.isNotEmpty(gun.getName()) ? gun.getName() : code));
String entryName = baseName + ".jpg";
int index = 1;
while (names.contains(entryName)) {
entryName = baseName + "(" + index++ + ").jpg";
}
names.add(entryName);
zipOut.putNextEntry(new ZipEntry(entryName));
QRCodeUtils.encode("https://mxcd.zhinenganguan.com?No=" + code, zipOut);
zipOut.closeEntry();
}
zipOut.finish();
}
} catch (Exception e) {
e.printStackTrace();
}
}
private String sanitizeFileName(String name) {
if (StringUtils.isEmpty(name)) {
return "qrcode";
}
return name.replaceAll("[\\\\/:*?\\\"<>|]", "_");
}
@Resource
private TokenService tokenService;
/**
* 设备状态统计
*/
@RequiresPermissions(value = {"/workbench"}, logical = Logical.OR)
@ApiOperation(tags = {"后台-工作台"},value = "设备状态统计")
@PutMapping(value = "/gunStatusStatistics")
public R gunStatusStatistics() {
List siteIds = new ArrayList<>();
Long userId = tokenService.getLoginUser().getUserid();
//获取当前登录的siteIds
PageInfo siteList1 = siteService.getSiteList1(userId);
for (GetSiteListDTO datum : siteList1.getRecords()) {
siteIds.add(datum.getId());
}
return R.ok(chargingGunService.gunStatusStatistics(siteIds));
}
/**
* 根据id获取充电枪完整名称
* @param id
* @return
*/
@ResponseBody
@PostMapping(value = "/getAllName/{id}")
public R getAllName(@PathVariable Integer id){
TChargingGun chargingGun = chargingGunService.getById(id);
TChargingPile chargingPile = chargingPileService.getById(chargingGun.getChargingPileId());
return R.ok(chargingPile.getName() + chargingGun.getName());
}
/**
* 根据id获取充电枪详情
* @param id
* @return
*/
@ResponseBody
@PostMapping(value = "/getChargingGunById/{id}")
public R getChargingGunById(@PathVariable Integer id){
TChargingGun chargingGun = chargingGunService.getById(id);
return R.ok(chargingGun);
}
@PostMapping(value = "/getChargingGunByIds")
public R> getChargingGunByIds(@RequestBody List ids){
if(ids == null || ids.isEmpty()){
return R.ok(new ArrayList<>());
}
return R.ok(chargingGunService.listByIds(ids));
}
/**
* 根据id获取充电枪详情
* @param id
* @return
*/
@ResponseBody
@PostMapping(value = "/getAllInfoById/{id}")
public R getAllInfoById(@PathVariable Integer id){
SiteNameVO siteNameVO = new SiteNameVO();
TChargingGun chargingGun = chargingGunService.getById(id);
siteNameVO.setGunName(chargingGun.getName());
siteNameVO.setGunNumber(chargingGun.getCode());
TChargingPile chargingPile = chargingPileService.getById(chargingGun.getChargingPileId());
siteNameVO.setPileName(chargingPile.getName());
siteNameVO.setPileNumber(chargingPile.getNumber());
Site site = siteService.getById(chargingGun.getSiteId());
siteNameVO.setSiteName(site.getName());
return R.ok(siteNameVO);
}
/**
* 根据枪编号获取充电枪
* @param code
* @return
*/
@PostMapping("/getChargingGunByCode")
public R getChargingGunByCode(@RequestBody GetChargingGunByCode code){
TChargingPile chargingPile = chargingPileService.getOne(new LambdaQueryWrapper()
.eq(TChargingPile::getCode, code.getCharging_pile_code()).eq(TChargingPile::getDelFlag, 0));
return R.ok(chargingGunService.getOne(Wrappers.lambdaQuery(TChargingGun.class)
.eq(TChargingGun::getChargingPileId, chargingPile.getId())
.eq(TChargingGun::getCode, code.getCharging_gun_code())
.eq(TChargingGun::getDelFlag, 0)
.last("LIMIT 1")));
}
/**
* 根据枪编号获取充电枪
* @param code
* @return
*/
/**
* 编辑充电枪
* @param chargingGun
* @return
*/
@PostMapping("/updateChargingGunById")
public R updateChargingGunById(@RequestBody TChargingGun chargingGun){
chargingGunService.updateById(chargingGun);
return R.ok();
}
/**
* 根据充电桩id获取枪
* @param chargingPileId
* @return
*/
@PostMapping("/getChargingGunByChargingPileId")
public R> getChargingGunByChargingPileId(@RequestParam("chargingPileId") Integer chargingPileId){
List list = chargingGunService.list(new LambdaQueryWrapper().eq(TChargingGun::getChargingPileId, chargingPileId)
.eq(TChargingGun::getDelFlag, 0));
return R.ok(list);
}
/**
* 根据充电桩ids获取枪
* @return
*/
@PostMapping("/getChargingGunByChargingPileIds")
public R> getChargingGunByChargingPileIds(@RequestBody GetChargingGunByChargingPileIdsVo vo){
if(vo.getChargingPileIds().isEmpty()){
return R.ok(new ArrayList<>());
}
List list = chargingGunService.list(new LambdaQueryWrapper().in(TChargingGun::getChargingPileId, vo.getChargingPileIds())
.eq(TChargingGun::getDelFlag, 0));
return R.ok(list);
}
/**
* 根据枪唯一码查询信息
* @return
*/
@PostMapping("/getChargingGunByFullNumber")
public R getChargingGunByFullNumber(@RequestParam("fullNumber") String fullNumber){
TChargingGun chargingGun = chargingGunService.getOne(new LambdaQueryWrapper().eq(TChargingGun::getDelFlag, 0).eq(TChargingGun::getFullNumber, fullNumber));
return R.ok(chargingGun);
}
/**
* 根据枪唯一码查询信息
* @return
*/
@ApiOperation(tags = {"大屏-对外开放区车位使用情况"},value = "对外开放区车位使用情况")
@GetMapping("/getLargeScreenGun")
public R getLargeScreenGun(){
LargeScreenGunVo largeScreenGunVo = new LargeScreenGunVo();
List tChargingGuns = chargingGunService.list(new LambdaQueryWrapper().eq(TChargingGun::getDelFlag, 0).in(TChargingGun::getSiteId,26).in(TChargingGun::getChargeMode, 1,2,4));
if(tChargingGuns.isEmpty()){
return R.ok(largeScreenGunVo);
}
List supperFast = tChargingGuns.stream().filter(e -> e.getChargeMode() == 1).collect(Collectors.toList());
largeScreenGunVo.setSuperFast(supperFast.size());
int supperFastSize = supperFast.stream().filter(e -> e.getStatus() != 2).collect(Collectors.toList()).size();
largeScreenGunVo.setUseSuperFast(supperFastSize);
List fast = tChargingGuns.stream().filter(e -> e.getChargeMode() == 2).collect(Collectors.toList());
largeScreenGunVo.setFast(fast.size());
int fastSize = fast.stream().filter(e -> e.getStatus() != 2).collect(Collectors.toList()).size();
largeScreenGunVo.setUseFast(fastSize);
List v2g = tChargingGuns.stream().filter(e -> e.getChargeMode() == 4).collect(Collectors.toList());
largeScreenGunVo.setV2g(v2g.size());
int v2gSize = v2g.stream().filter(e -> e.getStatus() != 2).collect(Collectors.toList()).size();
largeScreenGunVo.setUseV2g(v2gSize);
return R.ok(largeScreenGunVo);
}
/**
* 大屏 根据站点ids和充电模式集合查询充电枪集合 没有过滤del_flag
*/
@GetMapping("/getGunIdsBySiteIdsAndChargeModels")
public R> getGunIdsBySiteIdsAndChargeModels(@RequestParam("siteIds") List siteIds ,@RequestParam("chargeModels") List chargeModels){
List list = chargingGunService.list(new LambdaQueryWrapper()
.select(TChargingGun::getId).in(TChargingGun::getSiteId, siteIds)
.in(TChargingGun::getChargeMode, chargeModels));
return R.ok(list.stream().map(TChargingGun::getId).collect(Collectors.toList()));
}
}