package com.fjhx.controller.stock;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fjhx.base.BaseIdEntity;
import com.fjhx.constants.StatusConstant;
import com.fjhx.entity.product.ProductInfo;
import com.fjhx.entity.stock.StockCombination;
import com.fjhx.params.stock.GetQuantityByWarehouseIdDto;
import com.fjhx.params.stock.StockCombinationPageDto;
import com.fjhx.params.stock.StockCombinationPageVo;
import com.fjhx.service.product.ProductInfoService;
import com.fjhx.service.stock.StockCombinationService;
import org.springblade.core.tool.api.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
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 java.util.List;
import java.util.Map;

/**
 * <p>
 * 产品组合记录 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2023-02-16
 */
@RestController
@RequestMapping("/stockCombination")
public class StockCombinationController {

    @Autowired
    private StockCombinationService stockCombinationService;

    @Autowired
    private ProductInfoService productInfoService;

    /**
     * 产品组合分页
     */
    @PostMapping("/page")
    public R page(@RequestBody StockCombinationPageDto dto) {
        Page<StockCombinationPageVo> result = stockCombinationService.getPage(dto);
        return R.success(result);
    }

    /**
     * 添加组合
     */
    @PostMapping("/add")
    public R add(@Validated @RequestBody StockCombination stockCombination) {
        stockCombinationService.add(stockCombination);
        return R.success();
    }

    /**
     * 获取可组合数量
     */
    @PostMapping("/getQuantityByWarehouseId")
    public R getQuantityByWarehouseId(@Validated @RequestBody GetQuantityByWarehouseIdDto getQuantityByWarehouseIdDto) {
        int quantity = stockCombinationService.getQuantityByWarehouseId(getQuantityByWarehouseIdDto);
        return R.success(quantity);
    }

    /**
     * 获取组合名称列表
     */
    @PostMapping("/getCombinationList")
    public R getCombinationList() {
        List<Map<String, Object>> result = productInfoService.listMaps(q -> q
                .select(BaseIdEntity::getId, ProductInfo::getName)
                .eq(ProductInfo::getCombination, StatusConstant.YES));
        return R.success(result);
    }

}

