package doumeemes.service.business.impl;
|
|
import com.baomidou.mybatisplus.extension.api.R;
|
import doumeemes.core.constants.ResponseStatus;
|
import doumeemes.core.exception.BusinessException;
|
import doumeemes.core.model.ApiResponse;
|
import doumeemes.core.model.LoginUserInfo;
|
import doumeemes.core.model.PageData;
|
import doumeemes.core.model.PageWrap;
|
import doumeemes.core.utils.Constants;
|
import doumeemes.core.utils.DateUtil;
|
import doumeemes.core.utils.Utils;
|
import doumeemes.core.utils.excel.EasyExcelUtil;
|
import doumeemes.core.utils.redis.RedisUtil;
|
import doumeemes.dao.business.*;
|
import doumeemes.dao.business.model.*;
|
import doumeemes.dao.ext.dto.DeviceImportDTO;
|
import doumeemes.dao.ext.dto.QueryDeviceExtDTO;
|
import doumeemes.dao.ext.vo.DeviceExtListVO;
|
import doumeemes.service.business.DeviceService;
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import doumeemes.service.ext.DeviceExtService;
|
import org.apache.commons.lang3.StringUtils;
|
import org.apache.shiro.SecurityUtils;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.data.redis.core.RedisTemplate;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.util.CollectionUtils;
|
import org.springframework.web.multipart.MultipartFile;
|
|
import java.util.*;
|
import java.util.stream.Collectors;
|
|
/**
|
* 生产设备信息表Service实现
|
* @author 江蹄蹄
|
* @date 2022/04/20 09:34
|
*/
|
@Service
|
public class DeviceServiceImpl implements DeviceService {
|
|
@Autowired
|
private DeviceMapper deviceMapper;
|
@Autowired
|
private CompanyUserServiceImpl companyUserService;
|
@Autowired
|
private RedisTemplate<String, Object> redisTemplate;
|
@Autowired
|
private DeviceExtService deviceExtService;
|
@Autowired
|
private UserDeviceMapper userDeviceMapper;
|
|
@Autowired
|
private DepartmentMapper departmentMapper;
|
|
@Autowired
|
private ProceduresMapper proceduresMapper;
|
|
@Autowired
|
private WarehouseLocationMapper warehouseLocationMapper;
|
|
@Override
|
public Integer create(Device device) {
|
deviceMapper.insert(device);
|
return device.getId();
|
}
|
|
@Override
|
public void deleteById(Integer id) {
|
deviceMapper.deleteById(id);
|
}
|
|
@Override
|
public void delete(Device device) {
|
UpdateWrapper<Device> deleteWrapper = new UpdateWrapper<>(device);
|
deviceMapper.delete(deleteWrapper);
|
}
|
|
@Override
|
public void deleteByIdInBatch(List<Integer> ids) {
|
if (CollectionUtils.isEmpty(ids)) {
|
return;
|
}
|
deviceMapper.deleteBatchIds(ids);
|
}
|
|
@Override
|
public void updateById(Device device) {
|
deviceMapper.updateById(device);
|
}
|
|
@Override
|
public void updateByIdInBatch(List<Device> devices) {
|
if (CollectionUtils.isEmpty(devices)) {
|
return;
|
}
|
for (Device device: devices) {
|
this.updateById(device);
|
}
|
}
|
|
@Override
|
public Device findById(Integer id) {
|
return deviceMapper.selectById(id);
|
}
|
|
@Override
|
public Device findOne(Device device) {
|
QueryWrapper<Device> wrapper = new QueryWrapper<>(device);
|
return deviceMapper.selectOne(wrapper.last("limit 1"));
|
}
|
|
@Override
|
public List<Device> findList(Device device) {
|
QueryWrapper<Device> wrapper = new QueryWrapper<>(device);
|
return deviceMapper.selectList(wrapper);
|
}
|
|
@Override
|
public PageData<Device> findPage(PageWrap<Device> pageWrap) {
|
IPage<Device> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity());
|
QueryWrapper<Device> queryWrapper = new QueryWrapper<>();
|
Utils.MP.blankToNull(pageWrap.getModel());
|
if (pageWrap.getModel().getId() != null) {
|
queryWrapper.lambda().eq(Device::getId, pageWrap.getModel().getId());
|
}
|
if (pageWrap.getModel().getDeleted() != null) {
|
queryWrapper.lambda().eq(Device::getDeleted, pageWrap.getModel().getDeleted());
|
}
|
if (pageWrap.getModel().getCreateUser() != null) {
|
queryWrapper.lambda().eq(Device::getCreateUser, pageWrap.getModel().getCreateUser());
|
}
|
if (pageWrap.getModel().getCreateTime() != null) {
|
queryWrapper.lambda().ge(Device::getCreateTime, Utils.Date.getStart(pageWrap.getModel().getCreateTime()));
|
queryWrapper.lambda().le(Device::getCreateTime, Utils.Date.getEnd(pageWrap.getModel().getCreateTime()));
|
}
|
if (pageWrap.getModel().getUpdateUser() != null) {
|
queryWrapper.lambda().eq(Device::getUpdateUser, pageWrap.getModel().getUpdateUser());
|
}
|
if (pageWrap.getModel().getUpdateTime() != null) {
|
queryWrapper.lambda().ge(Device::getUpdateTime, Utils.Date.getStart(pageWrap.getModel().getUpdateTime()));
|
queryWrapper.lambda().le(Device::getUpdateTime, Utils.Date.getEnd(pageWrap.getModel().getUpdateTime()));
|
}
|
if (pageWrap.getModel().getRemark() != null) {
|
queryWrapper.lambda().eq(Device::getRemark, pageWrap.getModel().getRemark());
|
}
|
if (pageWrap.getModel().getRootDepartId() != null) {
|
queryWrapper.lambda().eq(Device::getRootDepartId, pageWrap.getModel().getRootDepartId());
|
}
|
if (pageWrap.getModel().getDepartId() != null) {
|
queryWrapper.lambda().eq(Device::getDepartId, pageWrap.getModel().getDepartId());
|
}
|
if (pageWrap.getModel().getProcedureId() != null) {
|
queryWrapper.lambda().eq(Device::getProcedureId, pageWrap.getModel().getProcedureId());
|
}
|
if (pageWrap.getModel().getCode() != null) {
|
queryWrapper.lambda().eq(Device::getCode, pageWrap.getModel().getCode());
|
}
|
if (pageWrap.getModel().getType() != null) {
|
queryWrapper.lambda().eq(Device::getType, pageWrap.getModel().getType());
|
}
|
if (pageWrap.getModel().getName() != null) {
|
queryWrapper.lambda().eq(Device::getName, pageWrap.getModel().getName());
|
}
|
if (pageWrap.getModel().getModel() != null) {
|
queryWrapper.lambda().eq(Device::getModel, pageWrap.getModel().getModel());
|
}
|
if (pageWrap.getModel().getStatus() != null) {
|
queryWrapper.lambda().eq(Device::getStatus, pageWrap.getModel().getStatus());
|
}
|
if (pageWrap.getModel().getSupplier() != null) {
|
queryWrapper.lambda().eq(Device::getSupplier, pageWrap.getModel().getSupplier());
|
}
|
if (pageWrap.getModel().getStationCode() != null) {
|
queryWrapper.lambda().eq(Device::getStationCode, pageWrap.getModel().getStationCode());
|
}
|
if (pageWrap.getModel().getGroupId() != null) {
|
queryWrapper.lambda().eq(Device::getGroupId, pageWrap.getModel().getGroupId());
|
}
|
if (pageWrap.getModel().getSerialNum() != null) {
|
queryWrapper.lambda().eq(Device::getSerialNum, pageWrap.getModel().getSerialNum());
|
}
|
for(PageWrap.SortData sortData: pageWrap.getSorts()) {
|
if (sortData.getDirection().equalsIgnoreCase(PageWrap.DESC)) {
|
queryWrapper.orderByDesc(sortData.getProperty());
|
} else {
|
queryWrapper.orderByAsc(sortData.getProperty());
|
}
|
}
|
return PageData.from(deviceMapper.selectPage(page, queryWrapper));
|
}
|
|
@Override
|
public long count(Device device) {
|
QueryWrapper<Device> wrapper = new QueryWrapper<>(device);
|
return deviceMapper.selectCount(wrapper);
|
}
|
|
|
@Override
|
public ApiResponse insert(Device device) {
|
LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
|
if(StringUtils.isBlank(device.getCode())){
|
device.setCode(this.getNextCode(user.getCompany().getId()));
|
}
|
if(device.getType()==Constants.ONE){
|
//设备组
|
Device groupdata= this.findById(device.getGroupId());
|
if(groupdata!=null&&groupdata.getFinishWarehouseLocationId()!=null&&!Constants.equalsInteger(groupdata.getFinishWarehouseLocationId(),device.getFinishWarehouseLocationId())){
|
return ApiResponse.failed("所属设备组的生产货位关联错误!");
|
}
|
|
if(groupdata!=null&&groupdata.getProduceWarehouseLocationId()!=null&&!Constants.equalsInteger(groupdata.getProduceWarehouseLocationId(),device.getProduceWarehouseLocationId())){
|
return ApiResponse.failed("所属设备组的已完工货位关联错误!");
|
}
|
|
}
|
QueryDeviceExtDTO queryDeviceExtDTO=new QueryDeviceExtDTO();
|
queryDeviceExtDTO.setDeleted(Constants.ZERO);
|
queryDeviceExtDTO.setCode(device.getCode());
|
queryDeviceExtDTO.setRootDepartId(user.getRootDepartment().getId());
|
List<DeviceExtListVO> list= deviceExtService.getListByCondition(queryDeviceExtDTO);
|
|
/* QueryDeviceExtDTO queryDeviceExtDTO1=new QueryDeviceExtDTO();
|
queryDeviceExtDTO1.setDeleted(Constants.ZERO);
|
queryDeviceExtDTO1.setName(device.getName());
|
queryDeviceExtDTO1.setRootDepartId(user.getRootDepartment().getId());
|
List<DeviceExtListVO> list1= deviceExtService.getListByCondition(queryDeviceExtDTO1);*/
|
if(list.size()>0){
|
return ApiResponse.failed("设备编码,不允许添加");
|
}
|
device.setDeleted(Constants.ZERO);
|
device.setCreateTime(new Date());
|
device.setCreateUser(user.getId());
|
device.setRootDepartId(user.getRootDepartment().getId());
|
device.setDepartId(user.getCurComDepartment().getId());
|
device.setStatus(Constants.ZERO);
|
deviceMapper.insert(device);
|
if(StringUtils.isNotBlank(device.getUserIds())){
|
this.dealDeviceUserId(Constants.ZERO,device.getUserIds(),device.getId(),user);
|
}
|
return ApiResponse.success(null);
|
}
|
|
|
@Override
|
public void dealDeviceUserId(Integer type,String userIds,Integer deviceId,LoginUserInfo userInfo){
|
if(type.equals(Constants.ONE)){
|
userDeviceMapper.delete(new QueryWrapper<UserDevice>().eq("DEVICE_ID",deviceId));
|
}
|
if(StringUtils.isNotEmpty(userIds)){
|
String [] ids=userIds.replace(" ","").split("[.,;,]");
|
for (String id : ids) {
|
CompanyUser companyUser = companyUserService.findById(Integer.valueOf(id));
|
UserDevice userDevice = new UserDevice();
|
userDevice.setCompanyUserId(companyUser.getId());
|
userDevice.setUserId(companyUser.getUserId());
|
userDevice.setCreateTime(new Date());
|
userDevice.setCreateUser(userInfo.getId());
|
userDevice.setDeleted(Constants.ZERO);
|
userDevice.setDeviceId(deviceId);
|
userDevice.setDeviceDate(new Date());
|
userDeviceMapper.insert(userDevice);
|
}
|
}
|
}
|
|
@Transactional(rollbackFor = {Exception.class, BusinessException.class})
|
@Override
|
public void importPlans(MultipartFile file) {
|
LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
|
if(!Constants.equalsInteger(user.getType(),Constants.USERTYPE.COM)){
|
throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "对不起,您无权限进行该操作!");
|
}
|
//解析excel
|
List<DeviceImportDTO> plansList = EasyExcelUtil.importExcel(file, 1, 1, DeviceImportDTO.class);
|
if(plansList == null || plansList.size()==0){
|
throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(), "导入数据内容有误!");
|
}
|
List<String> departName = new ArrayList<>();
|
List<String> procedureName = new ArrayList<>();
|
plansList.forEach(s->{
|
|
if (StringUtils.isBlank(s.getDeviceName())
|
|| s.getDeviceName().length() > 50
|
|| StringUtils.isBlank(s.getDepartName())
|
|| StringUtils.isBlank(s.getProcedureName())
|
|| StringUtils.isBlank(s.getProduceWarehouseLocationName())
|
|| StringUtils.isBlank(s.getFinishWarehouseLocationName())){
|
throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"导入数据内容有误");
|
}
|
departName.add(s.getDepartName());
|
procedureName.add(s.getProcedureName());
|
});
|
|
|
|
plansList.forEach(s->{
|
|
QueryWrapper<Department> wrapper = new QueryWrapper<>();
|
wrapper.lambda()
|
.eq(Department::getRootId,user.getRootDepartment().getId())
|
.eq(Department::getName,departName)
|
.last("limit 1");
|
Department department = departmentMapper.selectOne(wrapper);
|
if(Objects.isNull(department)){
|
throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"工厂名称不存在");
|
}
|
|
QueryWrapper<Procedures> proceduresQuery = new QueryWrapper<>();
|
proceduresQuery.lambda()
|
.eq(Procedures::getRootDepartId,user.getRootDepartment().getId())
|
.eq(Procedures::getDepartId,department.getId())
|
.eq(Procedures::getName,s.getProcedureName());
|
Procedures procedures = proceduresMapper.selectOne(proceduresQuery);
|
if(Objects.isNull(procedures)){
|
throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"工序名称已存在");
|
}
|
QueryDeviceExtDTO queryDeviceExtDTO=new QueryDeviceExtDTO();
|
queryDeviceExtDTO.setDeleted(Constants.ZERO);
|
queryDeviceExtDTO.setCode(s.getDeviceCode());
|
queryDeviceExtDTO.setRootDepartId(user.getRootDepartment().getId());
|
List<DeviceExtListVO> list= deviceExtService.getListByCondition(queryDeviceExtDTO);
|
if(list.size() > 0){
|
throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"设备编码不存在");
|
}
|
|
QueryWrapper<WarehouseLocation> warehouseLocationQuery = new QueryWrapper<>();
|
warehouseLocationQuery.lambda()
|
.eq(WarehouseLocation::getRootDepartId,department.getRootId())
|
.eq(WarehouseLocation::getUnionName
|
,Arrays.asList(s.getFinishWarehouseLocationName(),s.getProduceWarehouseLocationName()));
|
if(Objects.equals(list.size(),2)){
|
throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"库位信息已存在");
|
}
|
List<WarehouseLocation> warehouseLocations = warehouseLocationMapper.selectList(warehouseLocationQuery);
|
|
Map<String, WarehouseLocation> collect = warehouseLocations.stream().collect(Collectors.toMap(w -> w.getUnionName(), w -> w));
|
|
Device device = new Device();
|
device.setDeleted(Constants.ZERO);
|
device.setCreateUser(user.getId());
|
device.setCreateTime(new Date());
|
device.setUpdateUser(user.getId());
|
device.setUpdateTime(new Date());
|
// device.setRemark();
|
device.setRootDepartId(department.getRootId());
|
device.setDepartId(procedures.getId());
|
device.setProcedureId(procedures.getId());
|
|
device.setCode(StringUtils.isNotBlank(device.getCode()) ? device.getCode() : this.getNextCode(user.getCompany().getId()));
|
device.setType(StringUtils.isNotBlank(device.getCode()) ? device.getCode().trim().equals("设备") ? 1 : 0 : 1);
|
device.setName(s.getDeviceName());
|
device.setModel(s.getModel());
|
device.setStatus(Constants.ZERO);
|
// device.setSupplier();
|
device.setStationCode(s.getStationCode());
|
// device.setGroupId();
|
// device.setSerialNum();
|
device.setProduceWarehouseLocationId(collect.get(s.getProduceWarehouseLocationName()).getId());
|
device.setFinishWarehouseLocationId(collect.get(s.getFinishWarehouseLocationName()).getId());
|
deviceMapper.insert(device);
|
if(StringUtils.isNotBlank(s.getUserIds())){
|
this.dealDeviceUserId(Constants.ZERO,device.getUserIds().trim(),device.getId(),user);
|
}
|
|
});
|
}
|
|
|
public synchronized String getNextCode(Integer comId ){
|
String prefix = "S-" + DateUtil.getDate(new Date(),"yyyyMMdd") +"-";
|
Integer countNum = RedisUtil.getObject(redisTemplate,Constants.RedisKeys.COM_DEVIECE_CHECK_KEY+comId,Integer.class);
|
countNum = Constants.formatIntegerNum(countNum)+1;
|
//更新缓存
|
RedisUtil.addObject(redisTemplate,Constants.RedisKeys.COM_DEVIECE_CHECK_KEY+comId,countNum);
|
String nextIndex =Integer.toString( countNum );
|
return prefix + StringUtils.leftPad(nextIndex,4,"0");
|
}
|
|
}
|