package com.remy.goods.controller;

import com.remy.goods.entity.Goods;
import com.remy.goods.service.GoodsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("goods")
public class GoodsServiceController {

    @Autowired
    private GoodsService goodsService;

    @RequestMapping()
    public List<Goods> findAll() {
        return goodsService.findAll();
    }

    @RequestMapping("{serialNo}")
    public Goods findBySerialNo(@PathVariable("serialNo") String serialNo) {
        log.info("query goods by serialNo: {}", serialNo);
        return goodsService.findBySerialNo(serialNo);
    }

    @RequestMapping("query")
    public List<Goods> findBySerialNo(@RequestParam("serialNos") List<String> serialNos) {
        List<Goods> goods = goodsService.findAllByGoodsSerialNos(serialNos);
        log.info("goods size: {}", goods.size());
        return goods;
    }

    @PostMapping()
    public Goods save(@RequestBody Goods goods) {
        goods.setSerialNo(RandomStringUtils.randomAlphanumeric(8));
        return goodsService.save(goods);
    }
}
