dbd-meeting/ics-admin/src/main/java/com/ics/admin/controller/CustomerStaffController.java

447 lines
17 KiB
Java

package com.ics.admin.controller;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.EasyExcel;
import com.ics.admin.domain.*;
import com.ics.admin.domain.meeting.*;
import com.ics.admin.listener.ImportPowerWaterFeeListener;
import com.ics.admin.service.*;
import com.ics.admin.service.meeting.*;
import com.ics.admin.utils.ExcelView;
import com.ics.admin.vo.ImportPowerWaterFeeVO;
import com.ics.common.constant.Constants;
import com.ics.common.core.controller.BaseController;
import com.ics.common.core.domain.BaseEntity;
import com.ics.common.core.domain.IcsCustomerStaff;
import com.ics.common.core.domain.R;
import com.ics.common.utils.DateUtils;
import com.ics.common.utils.GuavaCacheUtil;
import com.ics.common.utils.StringUtils;
import com.ics.common.utils.poi.ExcelUtil;
import com.ics.system.domain.User;
import com.ics.system.service.ICurrentUserService;
import com.ics.system.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.jxls.common.Context;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.wf.jwtp.annotation.Ignore;
import org.wf.jwtp.annotation.RequiresPermissions;
import org.wf.jwtp.util.SubjectUtil;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
/**
* 企业员工 提供者
*
* @author ics
* @date 2024-02-19
*/
@Slf4j
@RestController
@RequestMapping("/admin/staff")
public class CustomerStaffController extends BaseController {
private final static String ACCESS_USERID = Constants.ACCESS_USERID;
@Autowired
private IIcsCustomerStaffService icsCustomerStaffService;
@Autowired
private IUserService userService;
@Autowired
private ICustomerService customerService;
@Autowired
private IRoomContentService roomContentService;
@Autowired
private IRoomService roomService;
@Autowired
private ICurrentUserService currentUserService;
@Autowired
private IRoomEquipmentService roomEquipmentService;
@Autowired
private IDetailEquipmentService detailEquipmentService;
@Autowired
private IUserEquipmentService userEquipmentService;
@Autowired
private IEquipmentService equipmentService;
@Autowired
private IBuildingDetailService buildingDetailService;
/**
* app的密钥值
*/
@Value("${dfs.path}")
private String path;
/**
* 查询企业员工
*/
@GetMapping("get/{id}")
public IcsCustomerStaff get(@PathVariable("id") Long id) {
return icsCustomerStaffService.selectIcsCustomerStaffById(id);
}
/**
* 查询企业员工列表
*/
@RequiresPermissions("admin:staff:list")
@GetMapping("list")
public R list(IcsCustomerStaff icsCustomerStaff) {
startPage();
String customerId = icsCustomerStaff.getCustomerId();
if (customerId != null && !"".equals(customerId)) {
icsCustomerStaff.setIcsCustomerId(Long.valueOf(customerId));
}
// Integer customerId = getLoginCustomerId();
// icsCustomerStaff.setIcsCustomerId(customerId.longValue());
icsCustomerStaff.setDataType(Constants.CUSTOMER_VISIT);
return result(icsCustomerStaffService.selectIcsCustomerStaffList(icsCustomerStaff));
}
@Ignore
@GetMapping("getStaffListNotId")
public R getStaffListNotId(IcsCustomerStaff icsCustomerStaff) {
startPage();
String customerId = icsCustomerStaff.getCustomerId();
if (customerId != null && !"".equals(customerId)) {
icsCustomerStaff.setIcsCustomerId(Long.valueOf(customerId));
}
// Integer customerId = getLoginCustomerId();
// icsCustomerStaff.setIcsCustomerId(customerId.longValue());
// icsCustomerStaff.setDataType(Constants.CUSTOMER_STAFF);
return result(icsCustomerStaffService.getStaffListNotId(icsCustomerStaff));
}
@Ignore
@GetMapping("getStaffListByUser")
public R getStaffListByUser(IcsCustomerStaff icsCustomerStaff) {
startPage();
String customerId = icsCustomerStaff.getCustomerId();
if (customerId != null && !"".equals(customerId)) {
icsCustomerStaff.setIcsCustomerId(Long.valueOf(customerId));
}
List<IcsCustomerStaff> staffListByUser = icsCustomerStaffService.getStaffListByUser(icsCustomerStaff);
if (icsCustomerStaff.getStaffId() != null) {
IcsCustomerStaff customerStaff = icsCustomerStaffService.selectIcsCustomerStaffById(icsCustomerStaff.getStaffId());
if (null != customerStaff) {
staffListByUser.add(customerStaff);
}
}
return result(staffListByUser);
}
/**
* 新增保存企业员工
*/
@RequiresPermissions("admin:staff:add")
@PostMapping("save")
public R addSave(@RequestBody IcsCustomerStaff icsCustomerStaff) {
icsCustomerStaff.setCreateTime(new Date());
icsCustomerStaff.setCreateBy(getLoginName());
icsCustomerStaff.setDataType(Constants.CUSTOMER_STAFF);
int i = icsCustomerStaffService.insertIcsCustomerStaff(icsCustomerStaff);
return toAjax(i);
}
/**
* 修改保存企业员工
*/
@Ignore
@PostMapping("update")
public R editSave(@RequestBody IcsCustomerStaff icsCustomerStaff) {
icsCustomerStaff.setDataType(Constants.CUSTOMER_STAFF);
icsCustomerStaff.setUpdateTime(new Date());
return toAjax(icsCustomerStaffService.updateIcsCustomerStaff(icsCustomerStaff));
}
/**
* 新增企业员工
*
* @param icsCustomerStaff
* @return
*/
@Ignore
@PostMapping("updateStaff")
public R updateStaff(@RequestBody IcsCustomerStaff icsCustomerStaff) {
IcsCustomerStaff customerStaff = icsCustomerStaffService.selectIcsCustomerStaffById(Long.valueOf(icsCustomerStaff.getMobile()));
if (customerStaff != null) {
customerStaff.setIcsCustomerId(Long.valueOf(icsCustomerStaff.getCustomerId()));
ArrayList<Long> ids = new ArrayList<>();
//根据企业id 查询对应的房间
Customer customer = customerService.selectCustomerById(Long.valueOf(icsCustomerStaff.getCustomerId()));
if (customer != null) {
String roomId = customer.getRoomId();
List<String> roomIds = StrUtil.split(roomId, ',');
List<Long> collect = roomIds.stream().map(Long::valueOf).collect(Collectors.toList());
for (Long roomid : collect) {
Room room = roomService.selectRoomById(roomid);
if (null != room) {
Long id = room.getId();
RoomEquipment roomEquipment = roomEquipmentService.selectByRoomId(id);
if (null != roomEquipment) {
ids.add(roomEquipment.getEquipmentId());
}
List<DetailEquipment> detailEquipments = detailEquipmentService.selectByRoomId(id);
if (CollUtil.isNotEmpty(detailEquipments)) {
for (DetailEquipment detailEquipment : detailEquipments) {
ids.add(detailEquipment.getEquipmentId());
}
}
}
}
}
if (CollUtil.isNotEmpty(ids)) {
for (Long id : ids) {
UserEquipment userEquipment = new UserEquipment();
userEquipment.setEquipmentId(id);
userEquipment.setUserId(Long.valueOf(icsCustomerStaff.getMobile()));
userEquipment.setStartTime(customer.getStartDate());
userEquipment.setEndDate(customer.getEndDate());
userEquipmentService.insertUserEquipment(userEquipment);
}
}
}
customerStaff.setUpdateTime(new Date());
return toAjax(icsCustomerStaffService.updateIcsCustomerStaff(customerStaff));
}
/**
* 删除企业员工
*
* @param icsCustomerStaff
* @return
*/
@Ignore
@PostMapping("updateStaffByCustomer")
public R updateStaffByCustomer(@RequestBody IcsCustomerStaff icsCustomerStaff) {
IcsCustomerStaff customerStaff = icsCustomerStaffService.selectIcsCustomerStaffById(Long.valueOf(icsCustomerStaff.getId()));
customerStaff.setUpdateTime(new Date());
return toAjax(icsCustomerStaffService.updateByCustomer(customerStaff));
}
/**
* 删除企业员工
*/
@RequiresPermissions("admin:staff:remove")
@PostMapping("remove")
public R remove(String ids) {
return toAjax(icsCustomerStaffService.deleteIcsCustomerStaffByIds(ids));
}
@Ignore
@GetMapping("selectCustomerStaffList")
public R selectCustomerStaffList(IcsCustomerStaff icsCustomerStaff) {
startPage();
String customerId = icsCustomerStaff.getCustomerId();
if (customerId != null && !"".equals(customerId)) {
icsCustomerStaff.setIcsCustomerId(Long.valueOf(customerId));
}
List<IcsCustomerStaff> icsCustomerStaffs = icsCustomerStaffService.selectCustomerStaffList(icsCustomerStaff);
for (IcsCustomerStaff customerStaff : icsCustomerStaffs) {
//获取设备数量
UserEquipment userEquipment = new UserEquipment();
userEquipment.setUserId(customerStaff.getId());
List<UserEquipment> equipments = userEquipmentService.selectUserEquipmentList(userEquipment);
customerStaff.setNum(equipments.size());
}
return result(icsCustomerStaffs);
}
// @RequiresPermissions("admin:staff:import")
@Ignore
@PostMapping("/importData")
public R importData(MultipartFile file) throws Exception {
// }
ExcelUtil<IcsCustomerStaff> util = new ExcelUtil<IcsCustomerStaff>(IcsCustomerStaff.class);
List<IcsCustomerStaff> userList = util.importExcel(file.getInputStream());
for (IcsCustomerStaff customerStaff : userList) {
long currentUserId = getCurrentUserId();
User user = userService.selectUserById(currentUserId);
if (user != null) {
customerStaff.setIcsCustomerId(user.getCustomerId());
}
}
String message = icsCustomerStaffService.importCustomerStaff(userList);
return R.data(message);
}
@Ignore
@PostMapping("/exportTemplate")
public void exportTemplate(HttpServletResponse response) throws IOException {
List<IcsCustomerStaff> icsCustomerStaffs = icsCustomerStaffService.selectIcsCustomerStaffList(new IcsCustomerStaff());
ExcelUtil<IcsCustomerStaff> util = new ExcelUtil<IcsCustomerStaff>(IcsCustomerStaff.class);
util.exportExcel(icsCustomerStaffs, "客户员工表");
}
//授权用户设备权限,查询出所有的设备
@RequiresPermissions("meeting:roomContent:list")
@PostMapping("/selectUserDeviceList")
public R selectUserDeviceList() {
//根据园区 角色 查询所有的设备信息
boolean isAdmin = SubjectUtil.hasRole(getRequest(), "manager");
if (isAdmin) {
Long parkId = currentUserService.getParkId();
Long tenantId = currentUserService.getTenantId();
Equipment equipment1 = new Equipment();
equipment1.setParkId(parkId);
equipment1.setTenantId(tenantId);
//根据园区id 查询所有的设备信息
List<Equipment> equipment = equipmentService.selectEquipmentList(equipment1);
List<Equipment> equipment2 = selectEquipmentListByIds(equipment);
return R.data(equipment2);
}
boolean b = SubjectUtil.hasRole(getRequest(), "admin");
if (b) {
User user = userService.selectUserById(getCurrentUserId());
if (null != user.getCustomerId()) {
ArrayList<Long> ids = new ArrayList<>();
Customer customer = customerService.selectCustomerById(user.getCustomerId());
//根据企业 查询对应的 房间,和对应的楼层
String roomId = customer.getRoomId();
List<String> roomIds = StrUtil.split(roomId, ',');
List<Long> collect = roomIds.stream().map(Long::valueOf).collect(Collectors.toList());
for (Long roomid : collect) {
Room room = roomService.selectRoomById(roomid);
if (null != room) {
Long id = room.getId();
RoomEquipment roomEquipment = roomEquipmentService.selectByRoomId(id);
if (null != roomEquipment) {
ids.add(roomEquipment.getEquipmentId());
}
List<DetailEquipment> detailEquipments = detailEquipmentService.selectByRoomId(id);
if (CollUtil.isNotEmpty(detailEquipments)) {
for (DetailEquipment detailEquipment : detailEquipments) {
ids.add(detailEquipment.getEquipmentId());
}
}
}
}
if (CollUtil.isNotEmpty(ids)) {
List<Equipment> equipment = equipmentService.selectListByIds(ids);
List<Equipment> equipment1 = selectEquipmentListByIds(equipment);
return R.data(equipment1);
} else {
return R.data(new ArrayList<Equipment>());
}
}
}
List<Equipment> equipment = equipmentService.selectEquipmentList(new Equipment());
List<Equipment> equipment1 = selectEquipmentListByIds(equipment);
return R.data(equipment1);
}
/**
* 查询当前用户的所有设备
*
* @return
*/
@RequiresPermissions("meeting:roomContent:list")
@PostMapping("/selectEquipmentListById")
public R selectEquipmentListById(@RequestBody UserEquipment equipment) {
List<UserEquipment> equipments = userEquipmentService.selectUserEquipmentList(equipment);
List<Long> collect = equipments.stream().map(UserEquipment::getEquipmentId).collect(Collectors.toList());
if (CollUtil.isNotEmpty(collect)) {
List<Equipment> equipment1 = equipmentService.selectListByIds(collect);
List<Equipment> equipment2 = selectEquipmentListByIds(equipment1);
return R.data(equipment2);
}
return R.data(equipments);
}
public List<Equipment> selectEquipmentListByIds(List<Equipment> equipment) {
for (Equipment equipment2 : equipment) {
RoomEquipment roomEquipment = roomEquipmentService.selectByEquipmentId(equipment2.getId());
if (roomEquipment != null) {
Room room = roomService.selectRoomById(roomEquipment.getRoomId());
equipment2.setRoomId(room.getId());
equipment2.setBuildId(room.getBuildingDetailId());
equipment2.setRoomName(room.getName());
BuildingDetail buildingDetail = buildingDetailService.selectBuildingDetailById(room.getBuildingDetailId());
if (buildingDetail != null) {
equipment2.setBuildName(buildingDetail.getFloorName());
}
} else {
DetailEquipment detailEquipment = detailEquipmentService.selectByEquipmentId(equipment2.getId());
if (null != detailEquipment) {
BuildingDetail buildingDetail = buildingDetailService.selectBuildingDetailById(detailEquipment.getBuildingDetailId());
if (buildingDetail != null) {
equipment2.setBuildName(buildingDetail.getFloorName());
}
}
}
}
return equipment;
}
@RequiresPermissions("meeting:roomContent:list")
@PostMapping("/saveUserEquipment")
public R saveUserEquipment(@RequestBody UserEquipment equipment) {
for (Long id : equipment.getUserIds()) {
UserEquipment userEquipment = new UserEquipment();
userEquipment.setEquipmentId(id);
userEquipment.setUserId(equipment.getUserId());
Customer customer = customerService.selectCustomerById(equipment.getUserId());
if (customer != null) {
userEquipment.setStartTime(customer.getStartDate());
userEquipment.setEndDate(customer.getEndDate());
}
userEquipmentService.insertUserEquipment(userEquipment);
}
return R.ok();
}
}