ProductController.java 4.65 KB
package vion.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import vion.Global;
import vion.enums.ResultEnum;
import vion.model.Product;
import vion.model.Store;
import vion.service.IProductSerrvice;
import vion.service.IStoreSerrvice;
import vion.utils.ResultUtil;
import vion.vo.ProductVO;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping(Global.BASE_URL)
@Slf4j
public class ProductController {

    @Autowired
    private IProductSerrvice productSerrvice;
    @Autowired
    private IStoreSerrvice storeSerrvice;

    @GetMapping("/products")
    @ResponseBody
    public Object getProductList(@RequestParam(name = "accountId",required=false)Integer account_id,
                                 @RequestParam(name = "storeId",required=false)Integer store_id,
                                 @RequestParam(name = "name",required=false)String name,
                                 @RequestParam(name = "brand",required=false)Integer brand,
                                 @RequestParam(name = "functionary",required=false)Integer functionary,
                                 @RequestParam(name = "pageNum")Integer pageNum,
                                 @RequestParam(name = "pageSize")Integer pageSize){

        //pageNum当前页,pageSize每页条数
        Page page= PageHelper.startPage(pageNum,pageSize);

        QueryWrapper<Product> wrapper=new QueryWrapper<>();
        if(store_id!=null){
            wrapper.eq("store_id",store_id);
        }
        if(account_id!=null)
            wrapper.eq("account_id",account_id);
        if(name!=null)
            wrapper.eq("name",name);
        if(brand!=null)
            wrapper.eq("brand",brand);
        if(functionary!=null)
            wrapper.eq("functionary",functionary);


        List<Product> productList=productSerrvice.list(wrapper);
        List<Store> storeList=storeSerrvice.list();

        List<ProductVO> productVOS=new ArrayList<>();
        ProductVO productVO;
        for(Product pro:productList){
            productVO=new ProductVO();
            productVO.setStoreName(storeList.stream().filter(store -> store.getId().equals(pro.getStoreId())).collect(Collectors.toList()).get(0).getName());
            productVO.setId(pro.getId());
            productVO.setStoreId(pro.getStoreId());
            productVO.setMaterialNo(pro.getMaterialNo());
            productVO.setName(pro.getName());
            productVO.setFunctionary(pro.getFunctionary());
            productVO.setBrand(pro.getBrand());
            productVO.setProductCount(pro.getProductCount());
            productVO.setProductPrice(pro.getProductPrice());
            productVO.setProductTotalPrice(pro.getProductTotalPrice());
            productVO.setProductSubtotal(pro.getProductSubtotal());
            productVO.setCreateTime(pro.getCreateTime());
            productVO.setModifyTime(pro.getModifyTime());
            productVO.setRemark(pro.getRemark());
            productVO.setAccountId(pro.getAccountId());

            productVOS.add(productVO);
        }

        productVOS.sort(Comparator.comparing(ProductVO::getId));

        PageInfo pageInfo=new PageInfo<>(page);
        pageInfo.setList(productList);
        if (pageInfo != null) {
            return ResultUtil.success(pageInfo);
        } else {
            return ResultUtil.error(ResultEnum.SELECT_ERROR);
        }
    }

    @GetMapping("/product")
    @ResponseBody
    public Object getProductByID(@RequestParam(name = "projectId")Integer project_id){

        Product product=productSerrvice.getById(project_id);

        if (product != null) {
            return ResultUtil.success(product);
        } else {
            return ResultUtil.error(ResultEnum.SELECT_ERROR);
        }
    }

    @PostMapping("/products")
    @ResponseBody
    public Object saveOrUpdate(@RequestBody Product data) {

        try {
            if(data.getId()==null){
                productSerrvice.save(data);
                return ResultUtil.success();
            }else{
                data.setModifyTime(new Date());
                Product product=productSerrvice.saveAndReturn(data);
                return ResultUtil.success(product);
            }

        }catch (Exception e) {
            //return ResultUtil.error(ResultEnum.SELECT_ERROR);
            return e.getMessage().toString();
        }
    }
}