package com.fdkankan.sale.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fdkankan.sale.common.PageInfo;
import com.fdkankan.sale.common.ResultCode;
import com.fdkankan.sale.entity.Part;
import com.fdkankan.sale.entity.PartLog;
import com.fdkankan.sale.entity.Repair;
import com.fdkankan.sale.exception.BusinessException;
import com.fdkankan.sale.mapper.IPartMapper;
import com.fdkankan.sale.service.IPartLogService;
import com.fdkankan.sale.service.IPartService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fdkankan.sale.vo.request.PartParam;
import com.fdkankan.sale.vo.response.RepairRegisterPartVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
/**
*
* 服务实现类
*
*
* @author
* @since 2022-12-07
*/
@Service
public class PartServiceImpl extends ServiceImpl implements IPartService {
@Autowired
IPartLogService partLogService;
@Override
public Object pageList(PartParam param) {
LambdaQueryWrapper wrapper = getWrapper(param);
Page page = this.page(new Page<>(param.getPageNum(), param.getPageSize()), wrapper);
return PageInfo.PageInfo(page);
}
@Override
public Object allList(PartParam param) {
if(param.getCameraType() == null){
throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
}
LambdaQueryWrapper wrapper = getWrapper(param);
wrapper.eq(Part::getCameraType,0);
wrapper.eq(Part::getStatus,0);
return this.list(wrapper);
}
private LambdaQueryWrapper getWrapper(PartParam param) {
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
if(StringUtils.isNotBlank(param.getPartName())){
wrapper.like(Part::getPartName,param.getPartName());
}
if(param.getCameraType() != null){
wrapper.eq(Part::getCameraType,param.getCameraType());
}
wrapper.orderByDesc(Part::getCreateTime);
return wrapper;
}
@Override
public void inStock(Part part,Long userId) {
if(part.getPartId() == null || part.getPartStock() == null){
throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
}
LambdaUpdateWrapper wrapper = new LambdaUpdateWrapper<>();
wrapper.eq(Part::getPartId,part.getPartId());
wrapper.setSql("part_stock = part_stock +" + part.getPartStock());
this.update(wrapper);
partLogService.saveLog(part.getPartId(),part.getPartStock(),userId,0,null);
}
@Override
public void outStock(Integer partId, Integer count,Long userId,String repairId) {
LambdaUpdateWrapper wrapper = new LambdaUpdateWrapper<>();
wrapper.eq(Part::getPartId,partId);
wrapper.setSql("part_stock = part_stock -" + count);
this.update(wrapper);
partLogService.saveLog(partId,count,userId,1,repairId);
}
@Override
public void recovery(Integer partId, Integer partCount, Long userId, String repairId) {
partLogService.saveLog(partId,partCount,userId,2,repairId);
}
}