package com.gs.xiaomi.example;
|
|
import com.gs.xiaomi.dto.BCS101Response;
|
import com.gs.xiaomi.dto.SnListItemDto;
|
import com.gs.xiaomi.entity.SnListItem;
|
import com.gs.xiaomi.service.SnListItemService;
|
import com.gs.xiaomi.util.SnListItemConverter;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Component;
|
|
import java.util.List;
|
|
/**
|
* SnListItem使用示例
|
* 演示如何使用DTO和Entity进行数据处理和持久化
|
*/
|
@Component
|
public class SnListItemUsageExample {
|
|
@Autowired
|
private SnListItemService snListItemService;
|
|
/**
|
* 示例1: 从BCS101接口响应中保存SN列表数据
|
*
|
* @param response BCS101接口响应
|
* @param deliveryMainId 送货单主表ID
|
* @param zzasn 送货单号
|
*/
|
public void saveSNListFromResponse(BCS101Response response, Long deliveryMainId, String zzasn) {
|
// 1. 从响应中获取DTO列表
|
List<SnListItemDto> dtoList = response.getBody().getSnList();
|
|
// 2. 将DTO列表转换为Entity列表,同时设置关联信息
|
List<SnListItem> entityList = SnListItemConverter.toEntityList(dtoList, deliveryMainId, zzasn);
|
|
// 3. 批量保存到数据库
|
snListItemService.saveBatch(entityList);
|
}
|
|
/**
|
* 示例2: 根据送货单号查询SN列表并转换为DTO
|
*
|
* @param zzasn 送货单号
|
* @return DTO列表
|
*/
|
public List<SnListItemDto> querySnListByZzasn(String zzasn) {
|
// 1. 从数据库查询Entity列表
|
List<SnListItem> entityList = snListItemService.lambdaQuery()
|
.eq(SnListItem::getZzasn, zzasn)
|
.list();
|
|
// 2. 将Entity列表转换为DTO列表
|
return SnListItemConverter.toDtoList(entityList);
|
}
|
|
/**
|
* 示例3: 单个DTO保存
|
*
|
* @param dto SN条码DTO
|
* @param deliveryMainId 送货单主表ID
|
* @param zzasn 送货单号
|
*/
|
public void saveSingleSNItem(SnListItemDto dto, Long deliveryMainId, String zzasn) {
|
// 1. DTO转Entity
|
SnListItem entity = SnListItemConverter.toEntity(dto, deliveryMainId, zzasn);
|
|
// 2. 保存到数据库
|
snListItemService.save(entity);
|
}
|
|
/**
|
* 示例4: 根据SN号查询详细信息
|
*
|
* @param snNo SN号
|
* @return Entity对象
|
*/
|
public SnListItem queryBySnNo(String snNo) {
|
return snListItemService.lambdaQuery()
|
.eq(SnListItem::getSnNo, snNo)
|
.one();
|
}
|
|
/**
|
* 示例5: 根据小米料号和送货单号查询
|
*
|
* @param mpnId 小米料号
|
* @param zzasn 送货单号
|
* @return Entity列表
|
*/
|
public List<SnListItem> queryByMpnIdAndZzasn(String mpnId, String zzasn) {
|
return snListItemService.lambdaQuery()
|
.eq(SnListItem::getMpnId, mpnId)
|
.eq(SnListItem::getZzasn, zzasn)
|
.list();
|
}
|
|
/**
|
* 示例6: 根据装箱号查询所有SN
|
*
|
* @param cartonId 装箱号
|
* @return Entity列表
|
*/
|
public List<SnListItem> queryByCartonId(String cartonId) {
|
return snListItemService.lambdaQuery()
|
.eq(SnListItem::getCartonId, cartonId)
|
.orderByAsc(SnListItem::getCreatedTime)
|
.list();
|
}
|
|
/**
|
* 示例7: 更新SN条码信息
|
*
|
* @param id 记录ID
|
* @param dto 更新的DTO数据
|
* @return 是否更新成功
|
*/
|
public boolean updateSnItem(Long id, SnListItemDto dto) {
|
// 1. DTO转Entity
|
SnListItem entity = SnListItemConverter.toEntity(dto);
|
entity.setId(id);
|
|
// 2. 更新到数据库
|
return snListItemService.updateById(entity);
|
}
|
|
/**
|
* 示例8: 删除指定送货单的所有SN数据
|
*
|
* @param zzasn 送货单号
|
* @return 是否删除成功
|
*/
|
public boolean deleteByZzasn(String zzasn) {
|
return snListItemService.lambdaUpdate()
|
.eq(SnListItem::getZzasn, zzasn)
|
.remove();
|
}
|
|
/**
|
* 示例9: 统计某个送货单的SN数量
|
*
|
* @param zzasn 送货单号
|
* @return SN数量
|
*/
|
public long countByZzasn(String zzasn) {
|
return snListItemService.lambdaQuery()
|
.eq(SnListItem::getZzasn, zzasn)
|
.count();
|
}
|
|
/**
|
* 示例10: 分页查询SN列表
|
*
|
* @param zzasn 送货单号
|
* @param pageNum 页码
|
* @param pageSize 每页大小
|
* @return Entity列表
|
*/
|
public List<SnListItem> queryByPage(String zzasn, int pageNum, int pageSize) {
|
return snListItemService.lambdaQuery()
|
.eq(SnListItem::getZzasn, zzasn)
|
.orderByDesc(SnListItem::getCreatedTime)
|
.last("OFFSET " + (pageNum - 1) * pageSize + " ROWS FETCH NEXT " + pageSize + " ROWS ONLY")
|
.list();
|
}
|
}
|