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

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hys.app.framework.context.user.AdminUserContext;
import com.hys.app.framework.database.WebPage;
import com.hys.app.model.erp.dos.ProductDO;
import com.hys.app.model.erp.dto.ProductDTO;
import com.hys.app.model.erp.dto.ProductGroupQueryParams;
import com.hys.app.model.erp.dto.ProductQueryParams;
import com.hys.app.model.erp.vo.ProductVO;
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;

/**
 * 产品相关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 IPage<ProductDO> list(ProductQueryParams queryParams) {
        AdminUserContext.hasSuperAdminQuery(queryParams.getCompanyId());
        return productManager.list(queryParams);
    }


    @ApiOperation(value = "新增产品")
    @PostMapping("/addProduct")
    @Log(client = LogClient.admin, detail = "添加名称为[${productDTO.name}]的商品信息")
    public void productGroup(@RequestBody @Valid ProductDTO productDTO) {
        AdminUserContext.hasCompany();
        productManager.addProduct(productDTO);
    }

    @ApiOperation(value = "修改产品")
    @PostMapping("/updateProduct/{id}")
    @Log(client = LogClient.admin, detail = "修改id为[${id}]的商品信息")
    public void updateProduct(@RequestBody @Valid ProductDTO productDTO, @PathVariable Long id) {
        AdminUserContext.hasCompany();
        productManager.updateProduct(productDTO, id);
    }

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

    @ApiOperation(value = "获取详情")
    @PostMapping("/getProduct/{id}")
    public void getProduct(@PathVariable Long id) {
        productManager.getById(id);
    }

    @ApiOperation(value = "获取列表")
    @PostMapping("/getProductList")
    public IPage<ProductDO> getProductList(ProductQueryParams queryParams) {
        AdminUserContext.hasSuperAdminQuery(queryParams.getCompanyId());
        return productManager.page(new Page<>(queryParams.getPageNo(), queryParams.getPageSize()),
                new LambdaQueryWrapper<ProductDO>()
                        .eq(ProductDO::getCompanyId, AdminUserContext.getCompanyId())
                        .and(StrUtil.isNotBlank(queryParams.getKeyword()), item -> item
                                .like(ProductDO::getName, queryParams.getName())
                                .or()
                                .like(ProductDO::getName, queryParams.getBrand())
                                .or()
                                .like(ProductDO::getName, queryParams.getSn())));
    }

    @ApiOperation(value = "设置库存预警")
    @PostMapping("/updateWarnStock")
    @Log(client = LogClient.admin, detail = "新增入库获取所有的sku信息")
    public void updateWarnStock( @RequestBody  ProductGroupQueryParams productGroupQueryParams) {
        AdminUserContext.hasCompany();
        productManager.updateWarnStock(productGroupQueryParams.getStockId(), productGroupQueryParams.getWarnNum());
    }

}

