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

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.mapper.goods.GoodsSnMapper;
import com.hys.app.model.erp.dos.GoodsSnDO;
import com.hys.app.model.erp.dos.ProductDO;
import com.hys.app.model.erp.dto.GoodsSnDTO;
import com.hys.app.model.erp.dto.GoodsSnQueryParams;
import com.hys.app.model.erp.vo.GoodsSnVO;
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.GoodsSnManager;
import com.hys.app.util.ValidateUtil;
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-12-26 15:56:58
 */
@RestController
@RequestMapping("/admin/erp/goods/sn")
@Api(tags = "商品序列号相关api")
@Validated
public class GoodsSnController {

    @Autowired
    private GoodsSnManager goodsSnManager;

    @Autowired
    private  GoodsSnMapper goodsSnMapper;

    @ApiOperation(value = "获取所有有sn的sku")
    @GetMapping("/getSnProduct")
    public Page<ProductVO> getSnProduct(GoodsSnQueryParams queryParams) {
        return goodsSnManager.getSnProduct(queryParams);
    }

    @ApiOperation(value = "添加")
    @PostMapping("/createSnCode")
    @Log(client = LogClient.admin, detail = "新增序列号")
    public void createSnCode(@RequestBody @Valid GoodsSnDTO goodsDTO) {
        AdminUserContext.hasCompany();
        goodsSnManager.createSnCode(goodsDTO);
    }

    @ApiOperation(value = "添加")
    @PostMapping("/updateSnCode/{id}/{snCode}")
    @Log(client = LogClient.admin, detail = "修改序列号[${snCode}]")
    public void updateSnCode(@PathVariable Long id, @PathVariable String snCode) {
        goodsSnManager.updateSnCode(id,snCode);
    }


    @ApiOperation(value = "获取详情")
    @GetMapping("/getDetail/{id}")
    public GoodsSnVO getDetail(@PathVariable Long id) {
        return goodsSnManager.getDetail(id);
    }


    @ApiOperation(value = "分页列表")
    @GetMapping
    public WebPage<GoodsSnVO> list(GoodsSnQueryParams queryParams) {
        return goodsSnManager.list(queryParams);
    }


    @ApiOperation(value = "删除序列号")
    @PostMapping("/removeById/{snId}")
    @Log(client = LogClient.admin, detail = "删除id为[${snId}]的序列号")
    public void removeById(@PathVariable Long snId) {
        GoodsSnDO snDO = ValidateUtil.getOne(goodsSnMapper, GoodsSnDO::getId, snId, "序列号");
        AdminUserContext.compareCompany(snDO.getCompanyId());
        goodsSnManager.removeById(snId);
    }
}

