五十七.采购完成功能代码编写

先准备完成采购功能所需的几个参数实体,如下:
PurchaseDoneQo

import lombok.Data;
import javax.validation.constraints.NotNull;
import java.util.List;

@Data
public class PurchaseDoneQo {
    @NotNull
    private Long id;//采购单id
    private List<PurchaseItemDoneQo> items;
}

PurchaseItemDoneQo

import lombok.Data;

@Data
public class PurchaseItemDoneQo {
    private Long itemId;
    private Integer status;
    private String reason;
}

由于采购完成后入库需要sku名称,所以需要远程调用商品服务,新建feign文件夹以及ProductFeignService类,如下:
在这里插入图片描述
ProductFeignService类:

import com.jiejie.common.utils.R;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@FeignClient("webshop-product")
public interface ProductFeignService {
    @RequestMapping("/prodect/skuinfo/info/{skuId}")
    R info(@PathVariable("skuId") Long skuId);
}

WareSkuService类下添加如下方法:

    /**
     * 添加库存
     *
     * @param skuId
     * @param wareId
     * @param skuNum
     */
    void addStock(Long skuId, Long wareId, Integer skuNum);

WareSkuServiceImpl类添加addStock()方法的实现,如下:

@Override
    public void addStock(Long skuId, Long wareId, Integer skuNum) {
        //如果还没有这个库存记录则做新增操作
        List<WareSkuEntity> entities = wareSkuDao.selectList(new QueryWrapper<WareSkuEntity>().eq("sku_id", skuId).eq("ware_id", wareId));
        if (entities == null || entities.size() == 0) {
            WareSkuEntity skuEntity = new WareSkuEntity();
            skuEntity.setSkuId(skuId);
            skuEntity.setStock(skuNum);
            skuEntity.setWareId(wareId);
            skuEntity.setStockLocked(0);
            try {
                R info = productFeignService.info(skuId);
                Map<String, Object> data = (Map<String, Object>) info.get("skuInfo");
                if (info.getCode() == 0) {
                    skuEntity.setSkuName((String) data.get("skuName"));
                }
            } catch (Exception e) {

            }
            wareSkuDao.insert(skuEntity);
        } else {
            wareSkuDao.addStock(skuId, wareId, skuNum);
        }
    }

WareSkuDao添加如下接口方法:

    /**
     * 新增库存
     *
     * @param skuId
     * @param wareId
     * @param skuNum
     */
    void addStock(Long skuId, Long wareId, Integer skuNum);

WareSkuDao.xml添加如下内容:

    <!-- 新增库存 -->
    <update id="addStock">
        UPDATE wms_ware_sku SET stock=stock+#{skuNum} WHERE sku_id=#{skuId} AND ware_id=#{wareId}
    </update>

PurchaseService类下添加如下方法:

    /**
     * 完成采购功能
     *
     * @param doneQo
     */
    void done(PurchaseDoneQo doneQo);

PurchaseServiceImpl类添加方法的实现,如下:

    @Autowired
    private PurchaseDetailService detailService;

    @Autowired
    private WareSkuService wareSkuService;
    
    @Transactional
    @Override
    public void done(PurchaseDoneQo doneQo) {
        Long id = doneQo.getId();
        //2、修改采购项的状态
        Boolean flag = true;
        List<PurchaseItemDoneQo> items = doneQo.getItems();
        List<PurchaseDetailEntity> updates = new ArrayList<>();
        for (PurchaseItemDoneQo item : items) {
            PurchaseDetailEntity detailEntity = new PurchaseDetailEntity();
            if (item.getStatus() == WareConstant.PurchaseDetailStatusEnum.HASERROR.getCode()) {
                flag = false;
                detailEntity.setStatus(item.getStatus());
            } else {
                detailEntity.setStatus(WareConstant.PurchaseDetailStatusEnum.FINISH.getCode());
                ////3、将采购成功的进行入库
                PurchaseDetailEntity entity = detailService.getById(item.getItemId());
                wareSkuService.addStock(entity.getSkuId(), entity.getWareId(), entity.getSkuNum());

            }
            detailEntity.setId(item.getItemId());
            updates.add(detailEntity);
        }
        detailService.updateBatchById(updates);
        //1、改变采购单状态
        PurchaseEntity purchaseEntity = new PurchaseEntity();
        purchaseEntity.setId(id);
        purchaseEntity.setStatus(flag ? WareConstant.PurchaseStatusEnum.FINISH.getCode() : WareConstant.PurchaseStatusEnum.HASERROR.getCode());
        purchaseEntity.setUpdateTime(new Date());
        this.updateById(purchaseEntity);
    } 

PurchaseController类添加如下接口:

    @PostMapping("/done")
    public R finish(@RequestBody PurchaseDoneQo doneQo) {
        purchaseService.done(doneQo);
        return R.ok();
    }

采购完成功能相关代码编写完成。

你可能感兴趣的:(从零开始搭建一个电商系统,采购完成功能代码编写)