package com.bonus.canteen.core.supermarket.controller;

import com.bonus.canteen.core.common.export.api.ExportApi;
import com.bonus.canteen.core.common.page.PageVO;
import com.bonus.canteen.core.supermarket.api.SupermarketApi;
import com.bonus.canteen.core.supermarket.dto.*;
import com.bonus.canteen.core.supermarket.service.SupermarketProductService;
import com.bonus.canteen.core.supermarket.vo.AmountBaseVO;
import com.bonus.canteen.core.supermarket.vo.SupermarketProductPageVO;
import com.bonus.canteen.core.supermarket.vo.SupermarketUnAddProductListVO;
import com.bonus.common.core.web.domain.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping({"/api/v1/supermarket-product"})
@Api(
   tags = {"gyp-商超-超市商品"}
)
public class SupermarketProductController {
   private static final Logger log = LoggerFactory.getLogger(SupermarketProductController.class);
   @Autowired
   @Lazy
   private SupermarketProductService supermarketProductService;
   @Autowired
   @Lazy
   private ExportApi exportApi;
   @Autowired
   @Lazy
   private SupermarketApi supermarketApi;

   @ApiOperation(
      value = "分页查询超市商品",
      notes = "分页查询超市商品"
   )
   @PostMapping({"/page-product"})
   public AmountBaseVO<SupermarketProductPageVO> pageProduct(@RequestBody @Valid SupermarketProductPageDTO content) {
      return this.supermarketProductService.pageProduct(content);
   }

   @ApiOperation(
      value = "批量新增商品到超市",
      notes = "批量新增商品到超市"
   )
   @PostMapping({"/save-batch-product"})
   public void saveBatchProduct(@RequestBody @Valid List<SupermarketProductSaveDTO> content) {
      this.supermarketProductService.saveBatchProduct(content);
   }

   @ApiOperation(
      value = "修改单一超市商品信息",
      notes = "修改单一超市商品信息"
   )
   @PostMapping({"/edit-product"})
   public void editProduct(@RequestBody @Valid SupermarketProductEditDTO content) {
      this.supermarketProductService.editProduct(content);
   }

   @ApiOperation(
      value = "超市商品删除",
      notes = "超市商品删除根据超市商品明细id"
   )
   @PostMapping({"/delete-product"})
   public void deleteProduct(@RequestBody @Valid List<Long> content) {
      this.supermarketProductService.deleteProduct(content);
   }

   @ApiOperation(
      value = "获取未被添加到该超市的商品",
      notes = "获取未被添加到该超市的商品"
   )
   @PostMapping({"/list-unadd-product"})
   public List<SupermarketUnAddProductListVO> listUnAddProduct(@RequestBody @Valid SupermarketUnAddProductListDTO content) {
      return this.supermarketProductService.listUnAddProduct(content);
   }

   @ApiOperation("超市商品-导出")
   @PostMapping({"/export/product"})
   public AjaxResult exportMaterial(@RequestBody SupermarketProductPageDTO content, HttpServletResponse response) {
      try {
         this.exportApi.startExcelExportTaskByPage("超市商品", "超市商品", SupermarketProductPageVO.class, content.getExportCols(), content, null, () -> {
            return PageVO.of(this.supermarketProductService.exportMaterial(content));
         });
         return AjaxResult.success();
      } catch (Throwable var4) {
         throw var4;
      }
   }

   @ApiOperation(
      value = "分页获取未被添加到该超市的商品",
      notes = "分页获取未被添加到该超市的商品"
   )
   @PostMapping({"/page/unadd-product"})
   public PageVO<SupermarketUnAddProductListVO> pageUnAddProduct(@RequestBody @Valid SupermarketUnAddProductPageDTO content) {
      return PageVO.of(this.supermarketProductService.pageUnAddProduct(content));
   }
}
