package com.jilongda.manage.controller;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.jilongda.common.basic.ApiResult;
import com.jilongda.common.basic.PageInfo;
import com.jilongda.common.constants.WarehousingConstant;
import com.jilongda.common.enums.WarehousingTypeEnum;
import com.jilongda.common.security.JwtTokenUtils;
import com.jilongda.common.utils.CodeGenerateUtils;
import com.jilongda.common.utils.UUIDUtil;
import com.jilongda.manage.dto.GetCurrentByParam;
import com.jilongda.manage.dto.GetCurrentByParamLens;
import com.jilongda.manage.dto.TWarehousingDTO;
import com.jilongda.manage.dto.TWarehousingLensDTO;
import com.jilongda.manage.model.*;
import com.jilongda.manage.query.TFrameGoodsQuery;
import com.jilongda.manage.query.TLensGoodsQuery;
import com.jilongda.manage.query.TWarehousingDetailLensQuery;
import com.jilongda.manage.query.TWarehousingDetailQuery;
import com.jilongda.manage.service.*;
import com.jilongda.manage.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
/**
*
* 镜架-镜片出库入库表 前端控制器
*
*
* @author 无关风月
* @since 2024-12-09
*/
@Api(tags = "镜架-镜片出库入库")
@RestController
@RequestMapping("/t-warehousing")
public class TWarehousingController {
@Autowired
private TWarehousingService warehousingService;
@Autowired
private TFrameWarehousingDetailService frameWarehousingDetailService;
@Autowired
private TLensWarehousingDetailService lensWarehousingDetailService;
@Autowired
private TFrameGoodsService frameGoodsService;
@Autowired
private TLensGoodsService lensGoodsService;
@Autowired
private TModelService modelService;
@Autowired
private TLensSeriesService lensSeriesService;
@Autowired
private TStoreService storeService;
@Autowired
private TBrandService brandService;
/**
* 通过型号查询色号列表
*/
@ApiOperation(value = "镜架-根据品牌id、型号名称、色号名称、材质id查询当前库存")
@PostMapping(value = "/getCurrentByParamFrame")
public ApiResult getCurrentByParamFrame(@RequestBody GetCurrentByParam getCurrentByParam) {
// 根据型号名称 查询型号列表ids
List collect = modelService.lambdaQuery()
.eq(StringUtils.hasLength(getCurrentByParam.getModel()),TModel::getName, getCurrentByParam.getModel())
.eq(Objects.nonNull(getCurrentByParam.getMaterialId()),TModel::getMaterialId,getCurrentByParam.getMaterialId())
.eq(StringUtils.hasLength(getCurrentByParam.getColor()),TModel::getColor,getCurrentByParam.getColor())
.eq(Objects.nonNull(getCurrentByParam.getBrandId()),TModel::getBrandId,getCurrentByParam.getBrandId())
.list().stream().map(TModel::getId).collect(Collectors.toList());
TFrameGoods one = frameGoodsService.lambdaQuery().in(TFrameGoods::getModelId, collect)
.eq(TFrameGoods::getColor, getCurrentByParam.getColor())
.eq(TFrameGoods::getStoreId, getCurrentByParam.getStoreId()).one();
if (one!=null){
return ApiResult.success(one.getTotal());
}
return ApiResult.success(0);
}
@ApiOperation(value = "镜片-根据系列id、球/非球、品牌id、折射率、ballMirror、columnMirror查询当前库存")
@PostMapping(value = "/getCurrentByParamLens")
public ApiResult getCurrentByParamLens(@RequestBody GetCurrentByParamLens dto) {
TLensGoods one = lensGoodsService.lambdaQuery().in(TLensGoods::getSeriesId, dto.getSeriesId())
.eq(dto.getLensType()!=null,TLensGoods::getLensType, dto.getLensType())
.eq(dto.getRefractiveIndex()!=null,TLensGoods::getRefractiveIndex, dto.getRefractiveIndex())
.eq(dto.getBallMirror()!=null,TLensGoods::getBallMirror, dto.getBallMirror())
.eq(dto.getColumnMirror()!=null,TLensGoods::getColumnMirror, dto.getColumnMirror())
.eq(dto.getStoreId()!=null,TLensGoods::getStoreId, dto.getStoreId())
.one();
if (one!=null){
return ApiResult.success(one.getTotal());
}
return ApiResult.success(0);
}
@ApiOperation(value = "镜架库存分页列表")
@PostMapping(value = "/frameReceiptList")
public ApiResult> frameReceiptList(@RequestBody TFrameGoodsQuery query) {
return ApiResult.success(frameGoodsService.lensReceiptList(query));
}
@ApiOperation(value = "镜片库存分页列表")
@PostMapping(value = "/lensReceiptList")
public ApiResult> lensReceiptList(@RequestBody TLensGoodsQuery query) {
return ApiResult.success(lensGoodsService.lensReceiptList(query));
}
@ApiOperation(value = "镜片库存分页列表-启用禁用")
@PostMapping(value = "/updateStateLens")
public ApiResult updateStateLens(Integer id) {
TLensGoods byId = lensGoodsService.getById(id);
if (byId.getStatus()==1){
byId.setStatus(2);
}else {
byId.setStatus(1);
}
lensGoodsService.updateById(byId);
return ApiResult.success();
}
@ApiOperation(value = "镜架库存分页列表-启用禁用")
@PostMapping(value = "/updateStateFrame")
public ApiResult updateStateFrame(Integer id) {
TFrameGoods byId = frameGoodsService.getById(id);
if (byId.getStatus()==1){
byId.setStatus(2);
}else {
byId.setStatus(1);
}
frameGoodsService.updateById(byId);
return ApiResult.success();
}
/**
* 出入库单列表
*/
@ApiOperation(value = "镜架出入库单列表")
@PostMapping(value = "/inventoryReceiptList")
public ApiResult> inventoryReceiptList(@RequestBody TWarehousingDetailQuery query) {
return ApiResult.success(warehousingService.inventoryReceiptList(query));
}
@ApiOperation(value = "镜片出入库单列表")
@PostMapping(value = "/inventoryReceiptLensList")
public ApiResult> inventoryReceiptLensList(@RequestBody TWarehousingDetailLensQuery query) {
return ApiResult.success(warehousingService.inventoryReceiptLensList(query));
}
/**
* 库存明细记录列表
*/
@ApiOperation(value = "镜架库存明细记录列表")
@PostMapping(value = "/detailList")
public ApiResult> detailList(@RequestBody TWarehousingDetailQuery query) {
PageInfo frameWarehousingDetailVOPageInfo = warehousingService.detailList(query);
return ApiResult.success(frameWarehousingDetailVOPageInfo);
}
@ApiOperation(value = "镜片出入库记录列表")
@PostMapping(value = "/detailLensList")
public ApiResult> detailLensList(@RequestBody TWarehousingDetailLensQuery query) {
return ApiResult.success(warehousingService.detailListLens(query));
}
/**
* 库存明细记录列表
*/
@ApiOperation(value = "镜片库存明细记录列表--库存详情")
@PostMapping(value = "/detailLensListDetail")
public ApiResult detailLensListDetail(Integer id) {
TLensGoodsDetailVO tLensGoodsDetailVO = new TLensGoodsDetailVO();
TLensGoods byId = lensGoodsService.getById(id);
TStore byId1 = storeService.getById(byId.getStoreId());
if (byId1!=null){
tLensGoodsDetailVO.setStoreName(byId1.getName());
}
TLensSeries byId2 = lensSeriesService.getById(byId.getSeriesId());
if (byId2!=null){
Integer brandId = byId2.getBrandId();
TBrand byId3 = brandService.getById(brandId);
String t1 = "";
switch (byId.getLensType()){
case 1:
t1="球面";
break;
case 2:
t1="非球面";
break;
case 3:
t1="双非";
break;
}
tLensGoodsDetailVO.setTitle(byId3.getName()+byId2.getName()+t1+" "+byId.getRefractiveIndex());
}
// 查询这个商品的库存明细
List list = lensWarehousingDetailService.lambdaQuery().eq(TLensWarehousingDetail::getSeriesId, byId.getSeriesId())
.eq(TLensWarehousingDetail::getRefractiveIndex, byId.getRefractiveIndex()).list();
tLensGoodsDetailVO.setList(list);
return ApiResult.success(tLensGoodsDetailVO);
}
/**
* 添加镜架出库,入库,作废,退货
*/
@ApiOperation(value = "添加镜架出库,入库,作废,退货")
@PostMapping(value = "/outBound")
public ApiResult outBound(@Validated @RequestBody TWarehousingDTO dto) {
// 获取当前用户
String username = JwtTokenUtils.getUsername();
dto.setCreateBy(username);
dto.setCreateTime(LocalDateTime.now());
dto.setType(WarehousingTypeEnum.FRAME.getCode());
warehousingService.save(dto);
// 添加明细
List frameWarehousingDetails = dto.getFrameWarehousingDetails();
frameWarehousingDetails.forEach(detail -> {
detail.setWarehousingId(dto.getId());
detail.setCode(WarehousingConstant.OUT_BOUND+CodeGenerateUtils.generateVolumeSn());
});
if (dto.getStatus()==1||dto.getStatus()==3){
List list1 = new ArrayList<>();
for (TFrameWarehousingDetail frameWarehousingDetail : frameWarehousingDetails) {
TFrameGoods one = frameGoodsService.lambdaQuery().eq(TFrameGoods::getModelId, frameWarehousingDetail.getModelId())
.eq(TFrameGoods::getColor, frameWarehousingDetail.getColor()).one();
if (one!=null){
if (one.getTotal()-frameWarehousingDetail.getTotal()<0){
return ApiResult.failed("库存不足");
}
// 增加对应库存
one.setTotal(one.getTotal()-frameWarehousingDetail.getTotal());
list1.add(one);
}
}
if (!list1.isEmpty())frameGoodsService.updateBatchById(list1);
}
frameWarehousingDetailService.saveBatch(frameWarehousingDetails);
if (dto.getStatus()==2 || dto.getStatus()==4){
List list1 = new ArrayList<>();
List list2 = new ArrayList<>();
for (TFrameWarehousingDetail frameWarehousingDetail : frameWarehousingDetails) {
TFrameGoods one = frameGoodsService.lambdaQuery().eq(TFrameGoods::getModelId, frameWarehousingDetail.getModelId())
.eq(TFrameGoods::getColor, frameWarehousingDetail.getColor()).one();
if (one!=null){
// 增加对应库存
one.setTotal(one.getTotal()+frameWarehousingDetail.getTotal());
list1.add(one);
}else {
// 新增
TFrameGoods tFrameGoods = new TFrameGoods();
tFrameGoods.setColor(frameWarehousingDetail.getColor());
tFrameGoods.setTotal(frameWarehousingDetail.getTotal());
tFrameGoods.setModelId(frameWarehousingDetail.getModelId());
tFrameGoods.setStoreId(dto.getStoreId());
list2.add(tFrameGoods);
}
}
if (!list1.isEmpty())frameGoodsService.updateBatchById(list1);
if (!list2.isEmpty())frameGoodsService.saveBatch(list2);
}
return ApiResult.success(dto.getId());
}
/**
* 添加镜片出库,入库,作废,退货
*/
@ApiOperation(value = "添加镜片出库,入库,作废,退货")
@PostMapping(value = "/outBoundLens")
public ApiResult outBoundLens(@Validated @RequestBody TWarehousingLensDTO dto) {
// 获取当前用户
String username = JwtTokenUtils.getUsername();
dto.setCreateBy(username);
dto.setCreateTime(LocalDateTime.now());
dto.setType(WarehousingTypeEnum.LENS.getCode());
warehousingService.save(dto);
// 添加明细
List frameWarehousingDetails = dto.getLensWarehousingDetails();
frameWarehousingDetails.forEach(detail -> {
detail.setWarehousingId(dto.getId());
detail.setCode(WarehousingConstant.OUT_BOUND+CodeGenerateUtils.generateVolumeSn());
});
if (dto.getStatus()==1||dto.getStatus()==3){
List list1 = new ArrayList<>();
for (TLensWarehousingDetail frameWarehousingDetail : frameWarehousingDetails) {
TLensGoods one = lensGoodsService.lambdaQuery().eq(TLensGoods::getSeriesId, frameWarehousingDetail.getSeriesId())
.eq(TLensGoods::getRefractiveIndex, frameWarehousingDetail.getRefractiveIndex())
.eq(TLensGoods::getLensType, frameWarehousingDetail.getType())
.eq(TLensGoods::getBallMirror, frameWarehousingDetail.getBallMirror())
.eq(TLensGoods::getColumnMirror, frameWarehousingDetail.getColumnMirror())
.one();
if (one!=null){
if (one.getTotal()-frameWarehousingDetail.getTotal()<0){
return ApiResult.failed("库存不足");
}
// 减少对应库存
one.setTotal(one.getTotal()-frameWarehousingDetail.getTotal());
list1.add(one);
}
}
if (!list1.isEmpty())lensGoodsService.updateBatchById(list1);
}
lensWarehousingDetailService.saveBatch(frameWarehousingDetails);
if (dto.getStatus()==2 || dto.getStatus()==4){
List list1 = new ArrayList<>();
List list2 = new ArrayList<>();
for (TLensWarehousingDetail frameWarehousingDetail : frameWarehousingDetails) {
TLensGoods one = lensGoodsService.lambdaQuery().eq(TLensGoods::getSeriesId, frameWarehousingDetail.getSeriesId())
.eq(TLensGoods::getRefractiveIndex, frameWarehousingDetail.getRefractiveIndex())
.eq(TLensGoods::getBallMirror, frameWarehousingDetail.getBallMirror())
.eq(TLensGoods::getLensType, frameWarehousingDetail.getType())
.eq(TLensGoods::getColumnMirror, frameWarehousingDetail.getColumnMirror())
.one();
if (one!=null){
// 增加对应库存
one.setTotal(one.getTotal()+frameWarehousingDetail.getTotal());
list1.add(one);
}else {
// 新增
TLensGoods tFrameGoods = new TLensGoods();
tFrameGoods.setLensType(frameWarehousingDetail.getType());
tFrameGoods.setSeriesId(frameWarehousingDetail.getSeriesId());
tFrameGoods.setBallMirror(frameWarehousingDetail.getBallMirror());
tFrameGoods.setColumnMirror(frameWarehousingDetail.getColumnMirror());
tFrameGoods.setRefractiveIndex(frameWarehousingDetail.getRefractiveIndex());
tFrameGoods.setTotal(frameWarehousingDetail.getTotal());
tFrameGoods.setStoreId(dto.getStoreId());
list2.add(tFrameGoods);
}
}
if (!list1.isEmpty())lensGoodsService.updateBatchById(list1);
if (!list2.isEmpty())lensGoodsService.saveBatch(list2);
}
return ApiResult.success(dto.getId());
}
/**
* 查询详情
*/
@ApiOperation(value = "镜架查询详情")
@GetMapping(value = "/getDetailById")
public ApiResult getDetailById(@RequestParam Integer id) {
TWarehousing warehousing = warehousingService.getById(id);
TWarehousingVO vo = new TWarehousingVO();
BeanUtils.copyProperties(warehousing, vo);
List list = frameWarehousingDetailService.list(Wrappers.lambdaQuery(TFrameWarehousingDetail.class)
.eq(TFrameWarehousingDetail::getWarehousingId, id));
vo.setFrameWarehousingDetails(list);
// 统计数量
vo.setTotalNum(list.stream().mapToInt(TFrameWarehousingDetail::getTotal).sum());
return ApiResult.success(vo);
}
@ApiOperation(value = "镜片查询详情")
@GetMapping(value = "/getDetailLensById")
public ApiResult getDetailLensById(@RequestParam Integer id) {
TWarehousing warehousing = warehousingService.getById(id);
TWarehousingLensVO vo = new TWarehousingLensVO();
BeanUtils.copyProperties(warehousing, vo);
List list = lensWarehousingDetailService.list(Wrappers.lambdaQuery(TLensWarehousingDetail.class)
.eq(TLensWarehousingDetail::getWarehousingId, id));
vo.setLensWarehousingDetails(list);
// 统计数量
vo.setTotalNum(list.stream().mapToInt(TLensWarehousingDetail::getTotal).sum());
return ApiResult.success(vo);
}
}