package com.hys.app.controller.erp.use;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hys.app.framework.context.user.AdminUserContext;
import com.hys.app.framework.database.BaseQueryParam;
import com.hys.app.framework.database.WebPage;
import com.hys.app.model.erp.dos.ProductGroupDO;
import com.hys.app.model.erp.dto.*;
import com.hys.app.model.erp.vo.ProductStockVO;
import com.hys.app.model.erp.vo.ProductVO;
import com.hys.app.model.erp.vo.TransitVO;
import com.hys.app.model.support.LogClient;
import com.hys.app.model.support.validator.annotation.Log;
import com.hys.app.service.erp.ProductManager;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 产品相关API
 *
 * @author 张崧
 * 2023-11-30 16:06:44
 */
@RestController
@RequestMapping("/admin/erp/product")
@Api(tags = "产品相关API")
@Validated
public class ProductManagerController {

    @Autowired
    private ProductManager productManager;

    @ApiOperation(value = "分页列表")
    @GetMapping
    public WebPage<ProductVO> list(ProductQueryParams queryParams) {
        return productManager.list(queryParams);
    }

    @ApiOperation(value = "查询")
    @GetMapping("/{id}")
    public ProductVO getDetail(@PathVariable String id) {
        return productManager.getDetail(id);
    }


    @ApiOperation(value = "产品分组")
    @PostMapping("/productGroup")
    @Log(client = LogClient.admin, detail = "添加名称为[${productGroupDO.name}]的分组")
    public void productGroup(@RequestBody @Valid ProductGroupDO productGroupDO) {
        productManager.productGroup(productGroupDO);
    }

    @ApiOperation(value = "删除产品分组")
    @PostMapping("/remove/{id}")
    @Log(client = LogClient.admin, detail = "删除id为[${id}]的分组")
    public void removeProductGroup(@PathVariable(value = "id") Long id) {
        AdminUserContext.hasCompany();
        productManager.removeProductGroup(id);
    }


    @PostMapping("/update/{id}")
    @ApiOperation(value = "修改产品分组")
    @Log(client = LogClient.admin, detail = "修改id为[${id}]的分组")
    public void updateProductGroup(@PathVariable(value = "id") Long id, @RequestBody @Valid ProductGroupDO productGroupDO) {
        AdminUserContext.hasCompany();
        productManager.updateProductGroup(id, productGroupDO);
    }

    @ApiOperation(value = "获取产品分组")
    @GetMapping("/list")
    public Page<ProductGroupDO> page(ProductGroupQueryParams queryParams) {
        return productManager.getGroupPage(queryParams);
    }


    @ApiOperation(value = "获取产品分组详情")
    @GetMapping("/getById/{id}")
    public ProductGroupDO getById(@PathVariable(value = "id") Long id) {
        return productManager.getGroupById(id);
    }

    @ApiOperation(value = "根据分组信息获取选中的sku")
    @GetMapping("/getProductByGroup/{warehouseId}/{groupId}")
    public List<ProductStockVO> getProductByGroup(@PathVariable(value = "groupId") Long groupId, @PathVariable(value = "warehouseId") List<Long> warehouseId) {
        return productManager.getProductByGroup(groupId, warehouseId);
    }

    @ApiOperation(value = "设置库存预警")
    @PostMapping("/updateWarnStock/{stockId}/{warnNum}")
    public void updateWarnStock(@PathVariable(value = "stockId") Long stockId, @PathVariable(value = "warnNum") Integer warnNum) {
        productManager.updateWarnStock(stockId, warnNum);
    }

    @ApiOperation(value = "首页获取库存预警列表")
    @GetMapping("/getWarnStock/{warehouseId}")
    public List<ProductStockVO> getWarnStock(@PathVariable(value = "warehouseId") Long warehouseId) {

        return productManager.getWarnStock(warehouseId);
    }

    @ApiOperation(value = "获取获取在途明细")
    @GetMapping("/transitDetail/{stockId}")
    public List<TransitVO> transitDetail(@PathVariable(value = "stockId") Long stockId) {

        return productManager.transitDetail(stockId);
    }

    @ApiOperation(value = "排序")
    @PostMapping("/updateSort")
    @Log(client = LogClient.admin, detail = "对sku进行了排序")
    public void updateSort(@RequestBody  @Valid GoodsSortDto goodsSortDto) {
        AdminUserContext.hasCompany();
         productManager.updateSort(goodsSortDto.getData());
    }


}

