package com.gkhy.hazmat.system.service.impl;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gkhy.hazmat.common.api.CommonPage;
import com.gkhy.hazmat.common.config.IdTableNameHandler;
import com.gkhy.hazmat.common.domain.entity.SysUser;
import com.gkhy.hazmat.common.enums.CodePrexEnum;
import com.gkhy.hazmat.common.enums.EntryStateEnum;
import com.gkhy.hazmat.common.enums.HazmatStatusEnum;
import com.gkhy.hazmat.common.enums.UserTypeEnum;
import com.gkhy.hazmat.common.exception.ApiException;
import com.gkhy.hazmat.common.listener.CustomEventPublisher;
import com.gkhy.hazmat.common.utils.PageUtils;
import com.gkhy.hazmat.common.utils.SecurityUtils;
import com.gkhy.hazmat.common.utils.StringUtils;
import com.gkhy.hazmat.system.domain.*;
import com.gkhy.hazmat.system.mapper.*;
import com.gkhy.hazmat.system.service.HzProductEntryRecordService;
import com.gkhy.hazmat.system.service.HzProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionSynchronization;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
/**
*
* 成品入库记录表 服务实现类
*
*
* @author kzy
* @since 2024-08-06 16:06:49
*/
@Service
public class HzProductEntryRecordServiceImpl extends ServiceImpl implements HzProductEntryRecordService {
@Autowired
private HzProductBasicMapper productBasicMapper;
@Autowired
private HzProductMapper productMapper;
@Autowired
private HzProductService productService;
@Autowired
private HzProductWarehouseRecordMapper productWarehouseRecordMapper;
@Autowired
private CustomEventPublisher customEventPublisher;
@Autowired
private SysCompanyMapper companyMapper;
@Override
public CommonPage selectEntryRecordList(HzProductEntryRecord entryRecord) {
SysUser currentUser = SecurityUtils.getLoginUser().getUser();
if (!currentUser.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())) {
entryRecord.setCompanyId(currentUser.getCompanyId());
}
PageUtils.startPage();
List entryRecordList = baseMapper.selectEntryRecordList(entryRecord);
return CommonPage.restPage(entryRecordList);
}
@Override
public HzProductEntryRecord selectEntryRecordById(Long entryRecordId) {
HzProductEntryRecord entryRecord = baseMapper.selectById(entryRecordId);
SysUser currentUser = SecurityUtils.getLoginUser().getUser();
if (currentUser.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())) {
return entryRecord;
} else if (!entryRecord.getCompanyId().equals(currentUser.getCompanyId())) {
throw new ApiException("无权限查看其它企业数据");
}
return entryRecord;
}
@Override
public int insertEntryRecord(HzProductEntryRecord entryRecord) {
SysUser currentUser=SecurityUtils.getLoginUser().getUser();
HzProductBasic productBasic=productBasicMapper.selectById(entryRecord.getBasicId());
if(productBasic==null){
throw new ApiException("成品基础数据不存在");
}
if(entryRecord.getNum()>productBasic.getMaxEntry()){
throw new ApiException("数量超过单次入库最大数量<"+productBasic.getMaxEntry()+">");
}
entryRecord.setCompanyId(currentUser.getCompanyId());
entryRecord.setCreateBy(currentUser.getUsername());
checkUserAllowed(null,currentUser);
int row=0;
synchronized (this) {
//生成条码
generateCode(entryRecord);
row = baseMapper.insert(entryRecord);
if (row < 1) {
throw new ApiException("新增入库记录失败");
}
}
return row;
}
public void generateCode(HzProductEntryRecord entryRecord){
SysCompany company=companyMapper.selectById(entryRecord.getCompanyId());
String code=company.getCode();
if(StringUtils.isBlank(code)){
throw new ApiException("公司两位编码为空");
}
String currentDate= DateUtil.format(new Date(), DatePattern.PURE_DATE_FORMAT);
StringBuilder prefixBuilder=new StringBuilder().append(CodePrexEnum.GOOD.getCode())
.append(code)
.append(currentDate);
HzProductEntryRecord per=baseMapper.selectLastEntryRecord(prefixBuilder.toString(),entryRecord.getCompanyId());
int startCode=1;
int endCode=startCode+entryRecord.getNum()-1;
if(per!=null){
startCode=per.getEndCode()+1;
endCode=startCode+entryRecord.getNum()-1;
}
entryRecord.setCodePrex(prefixBuilder.toString());
entryRecord.setStartCode(startCode);
entryRecord.setEndCode(endCode);
entryRecord.setState(EntryStateEnum.UNENTER.getCode());
}
@Override
public int deleteEntryRecordById(Long entryRecordId) {
HzProductEntryRecord entryRecord=baseMapper.selectById(entryRecordId);
if(entryRecord==null){
throw new ApiException("入库记录不存在");
}
if(entryRecord.getState().equals(EntryStateEnum.ENTER.getCode())){
throw new ApiException("已入库的记录不能删除");
}
SysUser currentUser = SecurityUtils.getLoginUser().getUser();
checkUserAllowed(entryRecord,currentUser);
baseMapper.deleteById(entryRecordId);
return 0;
}
@Override
@Transactional(rollbackFor = RuntimeException.class)
public void doEntry(Long entryRecordId) {
HzProductEntryRecord entryRecord=getById(entryRecordId);
if(entryRecord.getState().equals(EntryStateEnum.ENTER.getCode())){
throw new ApiException("已完成入库,不能再操作");
}
SysUser currentUser=SecurityUtils.getLoginUser().getUser();
checkUserAllowed(entryRecord,currentUser);
HzProductBasic productBasic=productBasicMapper.selectById(entryRecord.getBasicId());
if(productBasic==null){
throw new ApiException("成品基础数据不存在");
}
//设置分表id
IdTableNameHandler.setCurrentId(currentUser.getCompanyId());
// synchronized (this) {
//获取当前仓库库存
int count = productMapper.selectProductCountOfWarehouse(entryRecord.getWarehouseId(), productBasic.getId(), currentUser.getCompanyId());
//新增危化品变动记录
HzProductWarehouseRecord warehouseRecord = new HzProductWarehouseRecord()
.setWarehouseId(entryRecord.getWarehouseId())
.setCupboardId(entryRecord.getCupboardId())
.setBasicId(productBasic.getId())
.setNum(entryRecord.getNum())
.setCompanyId(currentUser.getCompanyId())
.setCreateId(currentUser.getId())
.setRemaining(entryRecord.getNum() + count);
productWarehouseRecordMapper.insert(warehouseRecord);
int startCode=entryRecord.getStartCode();
int endCode=entryRecord.getEndCode();
List productList = new ArrayList<>();
for (int i = startCode; i <=endCode; i++) {
String lastCode= StringUtils.addZeroForNum(String.valueOf(i),4);
String code=String.format("%s%s",entryRecord.getCodePrex(),lastCode);
productList.add(new HzProduct().setWarehouseId(entryRecord.getWarehouseId())
.setCupboardId(entryRecord.getCupboardId())
.setBasicId(entryRecord.getBasicId())
.setEntryId(entryRecord.getId())
.setRemaining(productBasic.getMetering())
.setCompanyId(currentUser.getCompanyId())
.setState(HazmatStatusEnum.WAREHOUSEIN.getCode())
.setCode(code));
}
//批量创建危化品
if (productList.size() > 100) {
while (!productList.isEmpty()) {
int endIndex = Math.min(productList.size(), 100);
List subList = productList.subList(0, endIndex);
productService.saveBatch(subList);
productList = productList.subList(endIndex, productList.size());
}
} else {
productService.saveBatch(productList);
}
// }
//更新入库记录状态
entryRecord.setState(EntryStateEnum.ENTER.getCode());
updateById(new HzProductEntryRecord().setId(entryRecord.getId()).setState(EntryStateEnum.ENTER.getCode()));
IdTableNameHandler.removeCurrentId();
//异步执行
// 注册一个事务完成后执行的回调
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
@Override
public void afterCommit() {
customEventPublisher.publishEntry(entryRecord.getId(), CodePrexEnum.GOOD.getCode(), currentUser.getCompanyId(), currentUser.getId());
}
});
}
@Override
public int updateEntryRecord(HzProductEntryRecord entryRecord) {
SysUser currentUser = SecurityUtils.getLoginUser().getUser();
checkUserAllowed(entryRecord,currentUser);
HzProductBasic productBasic=productBasicMapper.selectById(entryRecord.getBasicId());
if(productBasic==null){
throw new ApiException("成品基础数据不存在");
}
if(entryRecord.getNum()>productBasic.getMaxEntry()){
throw new ApiException("数量超过单次入库最大数量<"+productBasic.getMaxEntry()+">");
}
HzProductEntryRecord existEr=baseMapper.selectById(entryRecord.getId());
if(existEr.getState().equals(EntryStateEnum.ENTER.getCode())){
throw new ApiException("已经入库,不能再修改");
}
entryRecord.setUpdateBy(currentUser.getUsername());
int row=baseMapper.updateById(entryRecord);
if(row<1){
throw new ApiException("更新入库信息失败");
}
return row;
}
@Override
public CommonPage selectProductListByEntryId(Long entryId) {
SysUser currentUser = SecurityUtils.getLoginUser().getUser();
HzProductEntryRecord entryRecord=getById(entryId);
if(entryRecord==null){
throw new ApiException("入库信息不存在");
}
checkUserAllowed(entryRecord,currentUser);
//设置分表id
IdTableNameHandler.setCurrentId(currentUser.getCompanyId());
PageUtils.startPage();
List productList = productMapper.selectProductList(new HzProduct().setEntryId(entryId));
IdTableNameHandler.removeCurrentId();
return CommonPage.restPage(productList);
}
public void checkUserAllowed(HzProductEntryRecord entryRecord,SysUser user) {
if (user.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())) {
throw new ApiException("管理员不能操作");
}
if(entryRecord!=null){
if(!Objects.equals(user.getCompanyId(), entryRecord.getCompanyId())){
throw new ApiException("无权限操作其他企业数据");
}
}
}
}