You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

757 lines
42 KiB

package jnpf.service.impl;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.StrUtil;
import cn.xuyanwu.spring.file.storage.FileInfo;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jnpf.base.UserInfo;
import jnpf.base.vo.DownloadVO;
import jnpf.config.ConfigValueUtil;
import jnpf.constant.FileTypeConstant;
import jnpf.constant.PermissionConst;
import jnpf.entity.*;
import jnpf.enump.RemovalManagementEnum;
import jnpf.enump.SelectionOrderingMainEnum;
import jnpf.exception.DataException;
import jnpf.file.FileApi;
import jnpf.file.FileUploadApi;
import jnpf.mapper.TSelectionOrderingMainMapper;
import jnpf.model.dto.SelectionOrderingMainDto;
import jnpf.permission.entity.OrganizeEntity;
import jnpf.permission.service.OrganizeService;
import jnpf.service.*;
import jnpf.util.*;
import jnpf.utils.EasyPoiUtils;
import jnpf.utils.SnowFlake;
import lombok.AllArgsConstructor;
import org.apache.catalina.User;
import org.apache.poi.ss.formula.functions.T;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
/**
* 选品下单主单信息表(TSelectionOrderingMain)表服务实现类
*
* @author common
* @since 2024-10-21 17:29:22
*/
@Service("tSelectionOrderingMainService")
@AllArgsConstructor
public class TSelectionOrderingMainServiceImpl extends ServiceImpl<TSelectionOrderingMainMapper, TSelectionOrderingMain> implements TSelectionOrderingMainService {
private final RedisSerialNumberUtils redisSerialNumberUtils;
private final OrganizeService organizeService;
private final TSelectionOrderingBooksService selectionOrderingBooksService;
private final TCommonRelationService tCommonRelationService;
private final TBookGoWhereService tBookGoWhereService;
private final FileUploadApi fileUploadApi;
private final FileApi fileApi;
private final TRemovalManagementService removalManagementService;
@Override
public IPage<TSelectionOrderingMain> selectPage(IPage<TSelectionOrderingMain> iPage, Map<String, Object> params) {
return this.baseMapper.selectPage(iPage, params);
}
@Override
@Transactional
public Long selectionOrdering(SelectionOrderingMainDto.SelectionOrderingParam param) {
//校验下单类型
if (StrUtil.isBlank(param.getOrderType())) {
throw new DataException("请选择下单类型");
}
//校验下单类型是否正确
SelectionOrderingMainEnum.OrderTypeEnum orderTypeEnum = SelectionOrderingMainEnum.OrderTypeEnum.getEnumByCode(param.getOrderType());
if (null == orderTypeEnum) {
throw new DataException("请选择正确的下单类型");
}
//校验保存类型
if (StrUtil.isBlank(param.getSaveType())) {
throw new DataException("请选择保存类型");
}
SelectionOrderingMainEnum.MainStatusEnum mainStatusEnum;
//校验保存类型 配置对应状态
if (StrUtil.equals(param.getSaveType(), "1")) {
mainStatusEnum = SelectionOrderingMainEnum.MainStatusEnum.TO_BE_SUBMITTED;
} else {
//提交时校验是否为重点书类型
if (SelectionOrderingMainEnum.OrderTypeEnum.KEY_BOOKS == orderTypeEnum) {
mainStatusEnum = SelectionOrderingMainEnum.MainStatusEnum.PENDING_VERIFICATION;
} else {
mainStatusEnum = SelectionOrderingMainEnum.MainStatusEnum.PENDING_ORDER;
}
}
TSelectionOrderingMain tSelectionOrderingMain = new TSelectionOrderingMain();
String idPrefix = LocalDateTimeUtil.format(LocalDateTime.now(), "yyyyMMdd");
UserInfo user;
//1-业务部 2-版本馆 3-样书室 4-重点书
switch (orderTypeEnum) {
case BUSINESS_DEPARTMENT:
user = UserProvider.getUser();
tSelectionOrderingMain.setRemark(param.getRemark());
break;
case VERSION_LIBRARY:
user = new UserInfo();
user.setId("");
user.setUserName("系统");
user.setOrganizeId("");
//跟出版社名称获取单位名称-传入的必须为统一出版社的数据
String press = param.getSelectionOrderingBooksList().stream().map(SelectionOrderingMainDto.TLibraryParam::getPress).filter(StrUtil::isNotBlank).findFirst().orElse("");
if (StrUtil.isBlank(press)) return 0L;
Optional<OrganizeEntity> optionalOrganizeEntity = organizeService.lambdaQuery()
.eq(OrganizeEntity::getFullName, press)
.eq(OrganizeEntity::getEnabledMark, "1").list().stream().findFirst();
if (!optionalOrganizeEntity.isPresent()) return 0L;
OrganizeEntity organizeEntity = optionalOrganizeEntity.get();
tSelectionOrderingMain.setCreateCorpId(organizeEntity.getId());
tSelectionOrderingMain.setCreateCorpName(organizeEntity.getFullName());
break;
case SAMPLE_BOOKSTORE:
user = UserProvider.getUser();
//样品库下单
if (StrUtil.equals(param.getSaveType(), "2")) {
mainStatusEnum = SelectionOrderingMainEnum.MainStatusEnum.ORDERED;
tSelectionOrderingMain.setOrderPersonId(UserProvider.getUser().getUserId());
tSelectionOrderingMain.setOrderPersonName(UserProvider.getUser().getUserName());
tSelectionOrderingMain.setOrderPersonTime(LocalDateTime.now());
if (!param.getSubmitOther()) break;
//修改业务部/版本馆的状态为已下单
Optional<OrganizeEntity> deptOptional = organizeService.lambdaQuery()
.eq(OrganizeEntity::getId, UserProvider.getUser().getOrganizeId())
.eq(OrganizeEntity::getEnabledMark, "1").list().stream().findFirst();
if (deptOptional.isPresent()) {
OrganizeEntity dept = deptOptional.get();
OrganizeEntity corp = getCorp(dept);
List<TSelectionOrderingMain> tSelectionOrderingMainList = this.lambdaQuery()
.eq(TSelectionOrderingMain::getDelFlag, "0")
.eq(TSelectionOrderingMain::getMainStatus, SelectionOrderingMainEnum.MainStatusEnum.PENDING_ORDER.getCode())
.in(TSelectionOrderingMain::getType, Arrays.asList(SelectionOrderingMainEnum.OrderTypeEnum.BUSINESS_DEPARTMENT.getCode(), SelectionOrderingMainEnum.OrderTypeEnum.VERSION_LIBRARY.getCode()))
.eq(TSelectionOrderingMain::getCreateCorpId, corp.getId())
.list();
if (CollUtil.isEmpty(tSelectionOrderingMainList)) break;
for (TSelectionOrderingMain selectionOrderingMain : tSelectionOrderingMainList) {
selectionOrderingMain.setMainStatus(SelectionOrderingMainEnum.MainStatusEnum.ORDERED.getCode());
selectionOrderingMain.setMainStatusName(SelectionOrderingMainEnum.MainStatusEnum.ORDERED.getDescribe());
selectionOrderingMain.setOrderPersonId(UserProvider.getUser().getUserId());
selectionOrderingMain.setOrderPersonName(UserProvider.getUser().getUserName());
selectionOrderingMain.setOrderPersonTime(LocalDateTime.now());
this.updateById(selectionOrderingMain);
}
final UserInfo userInfo = UserProvider.getUser();
CompletableFuture.runAsync(() -> {
//样书室提交时查询业务部/版本馆提交的选书
List<TSelectionOrderingBooks> tSelectionOrderingBooksList = selectionOrderingBooksService.lambdaQuery()
.in(TSelectionOrderingBooks::getRelationId, tSelectionOrderingMainList.stream().map(TSelectionOrderingMain::getId).collect(Collectors.toList()))
.list();
for (TSelectionOrderingBooks tSelectionOrderingBooks : tSelectionOrderingBooksList) {
//配置所属类型 用于收货时使用,默认给样书室
TSelectionOrderingMain selectionOrderingMain = this.lambdaQuery()
.eq(TSelectionOrderingMain::getId, tSelectionOrderingBooks.getRelationId())
.eq(TSelectionOrderingMain::getDelFlag,"0")
.one();
if (null==selectionOrderingMain)continue;
//查询去向
for (TCommonRelation tCommonRelation : tCommonRelationService.lambdaQuery()
.eq(TCommonRelation::getSourceId, tSelectionOrderingBooks.getId())
.eq(TCommonRelation::getType, TCommonRelationService.Type.SAMPLE_BOOK_DESTINATION.type)
.list()) {
//去向表生成数据
TBookGoWhere bookGoWhere = new TBookGoWhere();
;
bookGoWhere.setId(SnowFlake.nextId());
bookGoWhere.setBookNumber(tSelectionOrderingBooks.getBookNumber());
bookGoWhere.setTargetName(tCommonRelation.getTargetId());
bookGoWhere.setRelationId(tSelectionOrderingBooks.getId().toString());
bookGoWhere.setShouldSend(Integer.valueOf(tCommonRelation.getExt()));
bookGoWhere.setActualSend(Integer.valueOf(tCommonRelation.getExt()));
bookGoWhere.setType("0");
bookGoWhere.setOrderType(selectionOrderingMain.getType());
bookGoWhere.setUpdateBy(userInfo.getUserId());
bookGoWhere.setUpdateTime(LocalDateTime.now());
bookGoWhere.setUpdateName(userInfo.getUserName());
bookGoWhere.setCreateBy(userInfo.getUserId());
bookGoWhere.setCreateTime(LocalDateTime.now());
bookGoWhere.setCreateName(userInfo.getUserName());
bookGoWhere.setCreateDeptName(selectionOrderingMain.getCreateDeptName());
bookGoWhere.setCreateCorpName(selectionOrderingMain.getCreateCorpName());
bookGoWhere.setCreateDeptId(selectionOrderingMain.getCreateDeptId());
bookGoWhere.setCreateCorpId(selectionOrderingMain.getCreateCorpId());
tBookGoWhereService.save(bookGoWhere);
}
}
});
}
}
break;
case KEY_BOOKS:
user = UserProvider.getUser();
//配置默认数量
for (SelectionOrderingMainDto.TLibraryParam tLibraryParam : param.getSelectionOrderingBooksList()) {
if (null==tLibraryParam.getSelfReserved()||tLibraryParam.getSelfReserved()==0){
//重点书默认一本
tLibraryParam.setSelfReserved(1);
}
}
break;
default:
throw new DataException("请选择正确的下单类型");
}
//查询创建人单位及部门
if (StrUtil.isNotBlank(user.getOrganizeId())) {
Optional<OrganizeEntity> deptOptional = organizeService.lambdaQuery()
.eq(OrganizeEntity::getId, user.getOrganizeId())
.eq(OrganizeEntity::getEnabledMark, "1").list().stream().findFirst();
if (deptOptional.isPresent()) {
OrganizeEntity dept = deptOptional.get();
OrganizeEntity corp = getCorp(dept);
tSelectionOrderingMain.setCreateDeptName(dept.getFullName());
tSelectionOrderingMain.setCreateCorpName(corp.getFullName());
tSelectionOrderingMain.setCreateDeptId(dept.getId());
tSelectionOrderingMain.setCreateCorpId(corp.getId());
}
}
//校验修改还是新增
if (null != param.getId()) {
//校验书单是否已提交
if (this.lambdaQuery()
.eq(TSelectionOrderingMain::getId, param.getId())
.ne(TSelectionOrderingMain::getMainStatus, SelectionOrderingMainEnum.MainStatusEnum.TO_BE_SUBMITTED.getCode())
.eq(TSelectionOrderingMain::getDelFlag, "0")
.exists()) {
throw new DataException("该选品不为待提交状态,不可修改");
}
tSelectionOrderingMain.setId(param.getId());
} else {
//新增
tSelectionOrderingMain.setId(Long.valueOf(String.format("%s%05d", idPrefix, redisSerialNumberUtils.generateSerialNumber(idPrefix))));
tSelectionOrderingMain.setCreateId(user.getUserId());
tSelectionOrderingMain.setCreateTime(LocalDateTime.now());
tSelectionOrderingMain.setCreateName(user.getUserName());
}
//配置主状态
tSelectionOrderingMain.setMainStatus(mainStatusEnum.getCode());
tSelectionOrderingMain.setType(orderTypeEnum.getCode());
tSelectionOrderingMain.setMainStatusName(mainStatusEnum.getDescribe());
//构建实体类
tSelectionOrderingMain.setUpdateId(user.getUserId());
tSelectionOrderingMain.setUpdateTime(LocalDateTime.now());
tSelectionOrderingMain.setUpdateName(user.getUserName());
this.saveOrUpdate(tSelectionOrderingMain);
//todo 保存选书
//保存选书
checkBooks(param, tSelectionOrderingMain);
return tSelectionOrderingMain.getId();
}
//保存选书
private void checkBooks(SelectionOrderingMainDto.SelectionOrderingParam param, TSelectionOrderingMain tSelectionOrderingMain) {
List<SelectionOrderingMainDto.TLibraryParam> librarylist = param.getSelectionOrderingBooksList();
if (CollUtil.isEmpty(librarylist)) return;
for (SelectionOrderingMainDto.TLibraryParam library : librarylist) {
TSelectionOrderingBooks tSelectionOrderingBooks;
List<SelectionOrderingMainDto.BookTurnoverInfo> bookTurnoverInfoList = library.getBookTurnoverInfoList();
//先校验是否为更改还是新增
if (null == param.getId()) {
//先进行保存选书
tSelectionOrderingBooks = saveSelectionOrderingBooks(library, tSelectionOrderingMain);
//todo
//保存对应关系
saveRelation(bookTurnoverInfoList, tSelectionOrderingBooks, param, tSelectionOrderingMain);
continue;
}
//到这里就是为(wei二声)更改了
//校验是否为新增
tSelectionOrderingBooks = selectionOrderingBooksService.lambdaQuery().eq(TSelectionOrderingBooks::getId, library.getId())
.eq(TSelectionOrderingBooks::getDelFlag, "0").one();
if (null == tSelectionOrderingBooks) {
tSelectionOrderingBooks = saveSelectionOrderingBooks(library, tSelectionOrderingMain);
}
//删除所有关系直接新增,防止有问题
tCommonRelationService.lambdaUpdate()
.eq(TCommonRelation::getSourceId, tSelectionOrderingBooks.getId())
.eq(TCommonRelation::getType, TCommonRelationService.Type.SAMPLE_BOOK_DESTINATION.type)
.remove();
//新增关系
saveRelation(bookTurnoverInfoList, tSelectionOrderingBooks, param, tSelectionOrderingMain);
}
}
@NotNull
private TSelectionOrderingBooks saveSelectionOrderingBooks(SelectionOrderingMainDto.TLibraryParam library, TSelectionOrderingMain tSelectionOrderingMain) {
TSelectionOrderingBooks tSelectionOrderingBooks;
tSelectionOrderingBooks = BeanUtil.copyProperties(library, TSelectionOrderingBooks.class);
tSelectionOrderingBooks.setId(SnowFlake.nextId());
tSelectionOrderingBooks.setCreateId(UserProvider.getLoginUserId());
tSelectionOrderingBooks.setRelationId(tSelectionOrderingMain.getId().toString());
tSelectionOrderingBooks.setCreateName(UserProvider.getUser().getUserName());
tSelectionOrderingBooks.setCreateTime(LocalDateTime.now());
if (StrUtil.hasBlank(library.getBookName(), library.getAuthor(), library.getPress(), library.getCoverImage(), library.getPrice())) {
tSelectionOrderingBooks.setProblemFlag("1");
}
tSelectionOrderingBooks.setCreateTime(LocalDateTime.now());
selectionOrderingBooksService.saveOrUpdate(tSelectionOrderingBooks);
return tSelectionOrderingBooks;
}
//新增去向关系
private void saveRelation(List<SelectionOrderingMainDto.BookTurnoverInfo> bookTurnoverInfoList, TSelectionOrderingBooks tSelectionOrderingBooks, SelectionOrderingMainDto.SelectionOrderingParam param, TSelectionOrderingMain tSelectionOrderingMain) {
//总数量
//自预留数量
Integer totalQuantity = tSelectionOrderingBooks.getSelfReserved();
if (CollUtil.isEmpty(bookTurnoverInfoList)) {
//校验批量配置
if (CollUtil.isNotEmpty(param.getSelfReservedInfoList())) {
Optional<SelectionOrderingMainDto.SelfReservedInfo> selfReservedInfo = param.getSelfReservedInfoList().stream().filter(bookNumber -> StrUtil.equals(bookNumber.getBookNumber(), tSelectionOrderingBooks.getBookNumber().toString())).findFirst();
if (selfReservedInfo.isPresent()) {
//走正常流程
tSelectionOrderingBooks.setSelfReserved(selfReservedInfo.get().getSelfReservedNumber());
saveRelation(param.getBookTurnoverInfoList(), tSelectionOrderingBooks, param, tSelectionOrderingMain);
return;
}
}
//更改总量
selectionOrderingBooksService.lambdaUpdate()
.set(TSelectionOrderingBooks::getQuantity, totalQuantity)
.eq(TSelectionOrderingBooks::getId, tSelectionOrderingBooks.getId())
.update();
return;
}
for (SelectionOrderingMainDto.BookTurnoverInfo bookTurnoverInfo : bookTurnoverInfoList) {
TCommonRelation tCommonRelation = new TCommonRelation();
tCommonRelation.setSourceId(tSelectionOrderingBooks.getId().toString());
tCommonRelation.setType(TCommonRelationService.Type.SAMPLE_BOOK_DESTINATION.type);
tCommonRelation.setTargetId(bookTurnoverInfo.getTarget());
tCommonRelation.setExt(bookTurnoverInfo.getNumber().toString());
tCommonRelationService.save(tCommonRelation);
//样书室提交时
if (StrUtil.equals(param.getOrderType(), "3") && StrUtil.equals(param.getSaveType(), "2")) {
//去向表生成数据
TBookGoWhere bookGoWhere = new TBookGoWhere();
UserInfo user = UserProvider.getUser();
bookGoWhere.setId(SnowFlake.nextId());
bookGoWhere.setBookNumber(tSelectionOrderingBooks.getBookNumber());
bookGoWhere.setTargetName(bookTurnoverInfo.getTarget());
bookGoWhere.setRelationId(tSelectionOrderingBooks.getId().toString());
bookGoWhere.setShouldSend(bookTurnoverInfo.getNumber());
bookGoWhere.setActualSend(bookTurnoverInfo.getNumber());
bookGoWhere.setUpdateBy(user.getUserId());
bookGoWhere.setOrderType(param.getOrderType());
bookGoWhere.setType("0");
bookGoWhere.setUpdateTime(LocalDateTime.now());
bookGoWhere.setUpdateName(user.getUserName());
bookGoWhere.setCreateBy(user.getUserId());
bookGoWhere.setCreateTime(LocalDateTime.now());
bookGoWhere.setCreateName(user.getUserName());
bookGoWhere.setCreateDeptName(tSelectionOrderingMain.getCreateDeptName());
bookGoWhere.setCreateCorpName(tSelectionOrderingMain.getCreateCorpName());
bookGoWhere.setCreateDeptId(tSelectionOrderingMain.getCreateDeptId());
bookGoWhere.setCreateCorpId(tSelectionOrderingMain.getCreateCorpId());
tBookGoWhereService.save(bookGoWhere);
}
////计算总量
totalQuantity += bookTurnoverInfo.getNumber();
}
selectionOrderingBooksService.lambdaUpdate()
.set(TSelectionOrderingBooks::getQuantity, totalQuantity)
.eq(TSelectionOrderingBooks::getId, tSelectionOrderingBooks.getId())
.update();
}
//根据部门递归查询公司
private OrganizeEntity getCorp(OrganizeEntity dept) {
if (StrUtil.equals(dept.getCategory(), PermissionConst.COMPANY)) {
return dept;
}
OrganizeEntity info = organizeService.getInfo(dept.getParentId());
if (null == info) {
return dept;
}
return getCorp(info);
}
@Override
public IPage<SelectionOrderingMainDto.CommonTSelectionOrderingMain> queryOrderingListByOrderType(SelectionOrderingMainDto.QueryOrderingListByOrderTypeParam param) {
SelectionOrderingMainEnum.OrderTypeEnum orderTypeEnum = SelectionOrderingMainEnum.OrderTypeEnum.getEnumByCode(param.getOrderType());
if (null == orderTypeEnum) {
throw new DataException("请选择正确的类型");
}
if (orderTypeEnum != SelectionOrderingMainEnum.OrderTypeEnum.VERSION_LIBRARY) {
param.setUserId(UserProvider.getUser().getUserId());
}
if (orderTypeEnum != SelectionOrderingMainEnum.OrderTypeEnum.KEY_BOOKS) {
param.setCorpId(getCorp(organizeService.getInfo(UserProvider.getUser().getOrganizeId())).getId());
}
try {
LocalDateTimeUtil.parse(param.getStartTime(), "yyyy-MM-dd HH:mm:ss");
LocalDateTimeUtil.parse(param.getEndTime(), "yyyy-MM-dd HH:mm:ss");
} catch (Exception e) {
throw new DataException("时间格式错误,应为yyyy-MM-dd HH:mm:ss");
}
return this.baseMapper.queryOrderingListByOrderType(new Page<>(param.getCurrent(), param.getSize()), param);
}
@Override
public DownloadVO exportOrderingListByOrderType(SelectionOrderingMainDto.QueryOrderingListByOrderTypeParam param) {
List<SelectionOrderingMainDto.CommonTSelectionOrderingMain> records = this.queryOrderingListByOrderType(param).getRecords();
String fileName = "导出信息";
DownloadVO vo = DownloadVO.builder().build();
try {
String name = fileName + DateUtil.dateFormatByPattern(new Date(), "yyyyMMddHHmmss") + ".xlsx";
//检验是否导出重点书类型
SelectionOrderingMainEnum.OrderTypeEnum orderTypeEnum = SelectionOrderingMainEnum.OrderTypeEnum.getEnumByCode(param.getOrderType());
MultipartFile multipartFile;
if (orderTypeEnum == SelectionOrderingMainEnum.OrderTypeEnum.KEY_BOOKS) {
multipartFile = EasyPoiUtils.export(BeanUtil.copyToList(records, SelectionOrderingMainDto.KeyBooksExportTSelectionOrderingMain.class), null, fileName, SelectionOrderingMainDto.KeyBooksExportTSelectionOrderingMain.class, name);
} else {
//普通导出
multipartFile = EasyPoiUtils.export(records, null, fileName, SelectionOrderingMainDto.CommonTSelectionOrderingMain.class, name);
}
// String temporaryFilePath = configValueUtil.getTemporaryFilePath();
String temporaryFilePath = fileApi.getPath(FileTypeConstant.TEMPORARY);
FileInfo fileInfo = fileUploadApi.uploadFile(multipartFile, temporaryFilePath, fileName);
vo.setName(fileInfo.getFilename());
vo.setUrl(UploaderUtil.uploaderFile(fileInfo.getFilename() + "#" + "Temporary") + "&name=" + name);
} catch (IOException e) {
throw new RuntimeException(e);
}
return vo;
}
@Override
public SelectionOrderingMainDto.CommonTSelectionOrderingMain getInfoById(Long id, Boolean queryOther) {
SelectionOrderingMainDto.CommonTSelectionOrderingMain tSelectionOrderingMain = this.baseMapper.getInfoById(id);
if (!queryOther) return tSelectionOrderingMain;
// OrganizeEntity corp = getCorp(organizeService.getInfo(UserProvider.getUser().getOrganizeId()));
// //查询本单位所有已提交未下单的选品
// List<TSelectionOrderingMain> tSelectionOrderingMainList = this.lambdaQuery()
// .eq(TSelectionOrderingMain::getDelFlag, "0")
// .eq(TSelectionOrderingMain::getMainStatus, SelectionOrderingMainEnum.MainStatusEnum.PENDING_ORDER.getCode())
// .ne(TSelectionOrderingMain::getType, SelectionOrderingMainEnum.OrderTypeEnum.KEY_BOOKS.getCode())
// .eq(TSelectionOrderingMain::getCreateCorpId, corp.getId())
// .list();
// if (CollUtil.isEmpty(tSelectionOrderingMainList)) return tSelectionOrderingMain;
// //根据下单类型进行分类
// Map<String, List<TSelectionOrderingMain>> orderTypeResult = tSelectionOrderingMainList.stream().collect(Collectors.groupingBy(TSelectionOrderingMain::getType));
// for (String orderTypeCode : orderTypeResult.keySet()) {
// //业务部汇总
// if (StrUtil.equals(orderTypeCode, SelectionOrderingMainEnum.OrderTypeEnum.BUSINESS_DEPARTMENT.getCode())) {
// List<Long> bdIdList = orderTypeResult.get(SelectionOrderingMainEnum.OrderTypeEnum.BUSINESS_DEPARTMENT.getCode()).stream().map(TSelectionOrderingMain::getId).collect(Collectors.toList());
// if (CollUtil.isNotEmpty(bdIdList)) {
// tSelectionOrderingMain.setBusinessDepartmentBooksList(selectionOrderingBooksService.getListByIds(bdIdList));
// }
// }
// //版本馆汇总
// if (StrUtil.equals(orderTypeCode, SelectionOrderingMainEnum.OrderTypeEnum.VERSION_LIBRARY.getCode())) {
// List<Long> bbIdList = orderTypeResult.get(SelectionOrderingMainEnum.OrderTypeEnum.VERSION_LIBRARY.getCode()).stream().map(TSelectionOrderingMain::getId).collect(Collectors.toList());
// if (CollUtil.isNotEmpty(bbIdList)) {
// tSelectionOrderingMain.setVersionLibraryBooksList(selectionOrderingBooksService.getListByIds(bbIdList));
// }
// }
// }
return tSelectionOrderingMain;
}
@Override
@Transactional
public void delete(String id, Boolean checkUser) {
//校验是否存在
TSelectionOrderingMain tSelectionOrderingMain = this.lambdaQuery()
.eq(TSelectionOrderingMain::getId, id)
.eq(TSelectionOrderingMain::getDelFlag, "0")
.one();
if (null == tSelectionOrderingMain) {
throw new DataException("该信息不存在");
}
//校验是否为自己创建
if (checkUser&&!StrUtil.equals(UserProvider.getUser().getUserId(), tSelectionOrderingMain.getCreateId()) ) {
throw new DataException("不可删除[1]");
}
//校验是否为版本馆类型
if (StrUtil.equals(SelectionOrderingMainEnum.OrderTypeEnum.VERSION_LIBRARY.getCode(), tSelectionOrderingMain.getType())) {
throw new DataException("不可删除[2]");
}
//校验是否为待提交类型
if (!StrUtil.equals(SelectionOrderingMainEnum.MainStatusEnum.TO_BE_SUBMITTED.getCode(), tSelectionOrderingMain.getMainStatus())) {
throw new DataException("不可删除[3]");
}
//逻辑删除主表
this.lambdaUpdate()
.set(TSelectionOrderingMain::getDelFlag, "1")
.eq(TSelectionOrderingMain::getId, id)
.update();
//逻辑删除选书表
List<Long> selectionOrderingBooksIdList = selectionOrderingBooksService.lambdaQuery()
.eq(TSelectionOrderingBooks::getRelationId, id)
.eq(TSelectionOrderingBooks::getDelFlag, "0")
.list().stream().map(TSelectionOrderingBooks::getId).collect(Collectors.toList());
if (CollUtil.isEmpty(selectionOrderingBooksIdList)) return;
selectionOrderingBooksService.lambdaUpdate()
.set(TSelectionOrderingBooks::getDelFlag, "1")
.in(TSelectionOrderingBooks::getId, selectionOrderingBooksIdList)
.update();
tBookGoWhereService.lambdaUpdate()
.set(TBookGoWhere::getDelFlag, "1")
.in(TBookGoWhere::getRelationId, selectionOrderingBooksIdList)
.update();
//删除关系表
tCommonRelationService.lambdaUpdate()
.in(TCommonRelation::getSourceId, selectionOrderingBooksIdList)
.eq(TCommonRelation::getType, TCommonRelationService.Type.SAMPLE_BOOK_DESTINATION.type)
.remove();
}
@Override
public void presidentSelection(SelectionOrderingMainDto.PresidentSelectionParam param) {
if (null==param.getSaveType()){
throw new DataException("请选择保存类型");
}
//校验该重点选品单是否存在
if (null==param.getId()){
throw new DataException("请选择选品单");
}
TSelectionOrderingMain selectionOrderingMain = this.getById(param.getId());
if (null==selectionOrderingMain){
throw new DataException("该选品单已删除");
}
//校验是否已选过
if (!StrUtil.equals(SelectionOrderingMainEnum.MainStatusEnum.PENDING_VERIFICATION.getCode(),selectionOrderingMain.getMainStatus())){
throw new DataException("该选品重点书籍已进行过筛选");
}
//删除所有社长所选书籍
tCommonRelationService.lambdaUpdate()
.eq(TCommonRelation::getSourceId,param.getId())
.eq(TCommonRelation::getType,TCommonRelationService.Type.BOOKS_CHOSEN_BY_THE_PRESIDENT.type)
.remove();
if (CollUtil.isEmpty(param.getBookId())){
return;
}
for (Long bookId : param.getBookId()) {
TCommonRelation commonRelation=new TCommonRelation();
commonRelation.setId(SnowFlake.nextId());
commonRelation.setSourceId(param.getId().toString());
commonRelation.setType(TCommonRelationService.Type.BOOKS_CHOSEN_BY_THE_PRESIDENT.type);
commonRelation.setTargetId(bookId.toString());
commonRelation.setCreateTime(LocalDateTime.now());
commonRelation.setCreateBy(UserProvider.getUser().getUserId());
commonRelation.setCreateName(UserProvider.getUser().getUserName());
tCommonRelationService.save(commonRelation);
}
if (StrUtil.equals(param.getSaveType(),"1")){
return;
}
this.lambdaUpdate()
.set(TSelectionOrderingMain::getMainStatus,SelectionOrderingMainEnum.MainStatusEnum.RECOMMENDED.getCode())
.set(TSelectionOrderingMain::getMainStatusName,SelectionOrderingMainEnum.MainStatusEnum.RECOMMENDED.getDescribe())
.eq(TSelectionOrderingMain::getId, param.getId())
.update();
//添加该单号对应的领导是否选书标记
TCommonRelation commonRelation=new TCommonRelation();
commonRelation.setId(SnowFlake.nextId());
commonRelation.setSourceId(param.getId().toString());
commonRelation.setType(TCommonRelationService.Type.LEADER_SELECTION_BOOK_MARK.type);
commonRelation.setExt("false");
commonRelation.setCreateTime(LocalDateTime.now());
commonRelation.setCreateBy(UserProvider.getUser().getUserId());
commonRelation.setCreateName(UserProvider.getUser().getUserName());
tCommonRelationService.save(commonRelation);
}
@Override
public IPage<TSelectionOrderingBooks> aggregateQuery(SelectionOrderingMainDto.AggregateQuery param) {
if (StrUtil.isBlank(param.getOrderType())){
throw new DataException("请选择查询类型");
}
OrganizeEntity corp = getCorp(organizeService.getInfo(UserProvider.getUser().getOrganizeId()));
//查询本单位所有已提交未下单的选品
List<TSelectionOrderingMain> tSelectionOrderingMainList = this.lambdaQuery()
.eq(TSelectionOrderingMain::getDelFlag, "0")
.eq(TSelectionOrderingMain::getMainStatus, SelectionOrderingMainEnum.MainStatusEnum.PENDING_ORDER.getCode())
.eq(TSelectionOrderingMain::getType, param.getOrderType())
.ne(TSelectionOrderingMain::getType, SelectionOrderingMainEnum.OrderTypeEnum.KEY_BOOKS.getCode())
.eq(TSelectionOrderingMain::getCreateCorpId, corp.getId())
.list();
if (CollUtil.isEmpty(tSelectionOrderingMainList)){
return new Page<>();
}
return selectionOrderingBooksService.pageListByIds(new Page<>(param.getCurrent(),param.getSize()),tSelectionOrderingMainList.stream().map(TSelectionOrderingMain::getId).collect(Collectors.toList()));
}
@Override
public IPage<TSelectionOrderingBooks> pageSelectedBooks(SelectionOrderingMainDto.PageSelectedBooks param) {
if (null==param.getId()){
throw new DataException("请选择选品单");
}
TSelectionOrderingMain selectionOrderingMain = this.getById(param.getId());
if (null==selectionOrderingMain){
throw new DataException("该选品单已删除");
}
return selectionOrderingBooksService.pageListByIds(new Page<>(param.getCurrent(),param.getSize()), Collections.singletonList(param.getId()));
}
@Override
public IPage<TSelectionOrderingBooks> queryBooksFromPresident(SelectionOrderingMainDto.QueryBooksFromPresident param) {
if (null==param.getId()){
throw new DataException("请选择选品单");
}
TSelectionOrderingMain selectionOrderingMain = this.getById(param.getId());
if (null==selectionOrderingMain){
throw new DataException("该选品单已删除");
}
List<Long> bookIdList = tCommonRelationService.lambdaQuery()
.eq(TCommonRelation::getSourceId, param.getId())
.eq(TCommonRelation::getType, TCommonRelationService.Type.BOOKS_CHOSEN_BY_THE_PRESIDENT.type)
.list().stream().map(TCommonRelation::getTargetId).map(Long::parseLong).collect(Collectors.toList());
if (CollUtil.isEmpty(bookIdList)){
return new Page<>();
}
return selectionOrderingBooksService.lambdaQuery()
.in(TSelectionOrderingBooks::getId,bookIdList)
.eq(TSelectionOrderingBooks::getDelFlag,"0")
.page(new Page<>(param.getCurrent(),param.getSize()));
}
@Override
public List<SelectionOrderingMainDto.QueryOrderingListForLeaderResponse> queryOrderingListForLeader() {
//查询所有
// List<Long> orderingList = tCommonRelationService.lambdaQuery()
// .eq(TCommonRelation::getType, TCommonRelationService.Type.BOOKS_CHOSEN_BY_THE_PRESIDENT.type)
// .eq(TCommonRelation::getExt,"false")
// .list().stream().map(TCommonRelation::getSourceId).map(Long::parseLong).collect(Collectors.toList());
//查询所有重点数选品列表
return this.baseMapper.queryOrderingListForLeader();
// return Collections.emptyList();
}
@Override
public void leaderSelection(SelectionOrderingMainDto.PresidentSelectionParam param) {
if (null==param.getSaveType()){
throw new DataException("请选择保存类型");
}
//校验该重点选品单是否存在
if (null==param.getId()){
throw new DataException("请选择选品单");
}
TSelectionOrderingMain selectionOrderingMain = this.getById(param.getId());
if (null==selectionOrderingMain){
throw new DataException("该选品单已删除");
}
//删除所有领导所选书籍
tCommonRelationService.lambdaUpdate()
.eq(TCommonRelation::getSourceId,param.getId())
.eq(TCommonRelation::getType,TCommonRelationService.Type.BOOKS_CHOSEN_BY_THE_LEADER.type)
.remove();
if (CollUtil.isEmpty(param.getBookId())){
return;
}
for (Long bookId : param.getBookId()) {
TCommonRelation commonRelation=new TCommonRelation();
commonRelation.setId(SnowFlake.nextId());
commonRelation.setSourceId(param.getId().toString());
commonRelation.setType(TCommonRelationService.Type.BOOKS_CHOSEN_BY_THE_LEADER.type);
commonRelation.setTargetId(bookId.toString());
commonRelation.setCreateTime(LocalDateTime.now());
commonRelation.setCreateBy(UserProvider.getUser().getUserId());
commonRelation.setCreateName(UserProvider.getUser().getUserName());
tCommonRelationService.save(commonRelation);
}
if (StrUtil.equals(param.getSaveType(),"1")){
return;
}
//添加该单号对应的领导是否选书标记
tCommonRelationService.lambdaUpdate()
.set(TCommonRelation::getExt,"true")
.eq(TCommonRelation::getSourceId,param.getId().toString())
.eq(TCommonRelation::getType,TCommonRelationService.Type.LEADER_SELECTION_BOOK_MARK.type)
.update();
//生成重点书下架单
generateOrder(param);
}
private void generateOrder(SelectionOrderingMainDto.PresidentSelectionParam param) {
TRemovalManagement removalManagement=new TRemovalManagement();
//新增单号
String idPrefix = "ZD" + LocalDateTimeUtil.format(LocalDateTime.now(), "yyyyMMdd");
removalManagement.setId(String.format("%s%03d", idPrefix, redisSerialNumberUtils.generateSerialNumber(idPrefix)));
removalManagement.setCreateTime(LocalDateTime.now());
removalManagement.setCreateName("领导");
removalManagement.setUpdateTime(LocalDateTime.now());
removalManagement.setUpdateName("领导");
removalManagement.setSaveType(SelectionOrderingMainEnum.OrderTypeEnum.SAMPLE_BOOKSTORE.getCode());
removalManagement.setType(RemovalManagementEnum.TypeEnum.ZD.getCode());
removalManagement.setMainStatus(RemovalManagementEnum.MainStatusEnum.TO_BE_TAKEN_DOWN.getCode());
removalManagement.setMainStatusName(RemovalManagementEnum.MainStatusEnum.TO_BE_TAKEN_DOWN.getDescribe());
//查询册数
int quantity = selectionOrderingBooksService.lambdaQuery()
.in(TSelectionOrderingBooks::getId, param.getBookId())
.eq(TSelectionOrderingBooks::getDelFlag, "0")
.list()
.stream().mapToInt(TSelectionOrderingBooks::getQuantity).sum();
removalManagement.setRemoveNumber(quantity);
//查询部门 因为是领导免登陆选书 所以只能根据选品单获取单位信息
TSelectionOrderingMain selectionOrderingMain = this.getById(param.getId());
if (null!=selectionOrderingMain){
removalManagement.setCreateCorpId(selectionOrderingMain.getCreateCorpId());
removalManagement.setCreateCorpName(selectionOrderingMain.getCreateCorpName());
}
removalManagementService.save(removalManagement);
}
@Override
public List<TSelectionOrderingBooks> queryBooksFromLeader(SelectionOrderingMainDto.QueryBooksFromPresident param) {
if (null==param.getId()){
throw new DataException("请选择选品单");
}
TSelectionOrderingMain selectionOrderingMain = this.getById(param.getId());
if (null==selectionOrderingMain){
throw new DataException("该选品单已删除");
}
List<Long> bookIdList = tCommonRelationService.lambdaQuery()
.eq(TCommonRelation::getSourceId, param.getId())
.eq(TCommonRelation::getType, TCommonRelationService.Type.BOOKS_CHOSEN_BY_THE_LEADER.type)
.list().stream().map(TCommonRelation::getTargetId).map(Long::parseLong).collect(Collectors.toList());
if (CollUtil.isEmpty(bookIdList)){
return new ArrayList<>();
}
return selectionOrderingBooksService.lambdaQuery()
.in(TSelectionOrderingBooks::getId,bookIdList)
.eq(TSelectionOrderingBooks::getDelFlag,"0")
.list();
}
}