package com.bs.jxc.controller;

import cn.hutool.core.lang.Validator;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bs.common.annotation.Log;
import com.bs.common.core.controller.BaseController;
import com.bs.common.core.domain.AjaxResult;
import com.bs.common.core.page.TableDataInfo;
import com.bs.common.enums.BusinessType;
import com.bs.common.utils.poi.ExcelUtil;
import com.bs.jxc.domain.CkBillStock;
import com.bs.jxc.service.ICkBillStockService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 出入库单据明细的出库明细Controller
 *
 * @author bs
 * @date 2023-07-16
 */
@Api(tags = "出入库单据明细的出库明细")
@RestController
@RequestMapping("/jxc/billstock")
public class CkBillStockController extends BaseController {
    @Resource
    private ICkBillStockService ckBillStockService;

    /**
     * 分页查询出入库单据明细的出库明细列表
     */
    @ApiOperation("分页查询出入库单据明细的出库明细列表")
    @GetMapping("/pageList")
    public TableDataInfo pageList(CkBillStock ckBillStock) {
        startPage();
        LambdaQueryWrapper<CkBillStock> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,ckBillStock);
        List<CkBillStock> list = ckBillStockService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 查询出入库单据明细的出库明细列表
     */
    @ApiOperation("查询出入库单据明细的出库明细列表")
    @GetMapping("/list")
    public AjaxResult list(CkBillStock ckBillStock) {
        LambdaQueryWrapper<CkBillStock> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,ckBillStock);
        List<CkBillStock> list = ckBillStockService.list(queryWrapper);
        return success(list);
    }

    /**
     * 导出出入库单据明细的出库明细列表
     */
    @ApiOperation("导出出入库单据明细的出库明细列表")
    @Log(title = "出入库单据明细的出库明细导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CkBillStock ckBillStock) {
        LambdaQueryWrapper<CkBillStock> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,ckBillStock);
        List<CkBillStock> list = ckBillStockService.list(queryWrapper);
        ExcelUtil<CkBillStock> util = new ExcelUtil<CkBillStock>(CkBillStock. class);
        util.exportExcel(response, list, "出入库单据明细的出库明细数据");
    }

    /**
     * 获取出入库单据明细的出库明细详细信息
     */
    @ApiOperation("获取出入库单据明细的出库明细详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(ckBillStockService.getById(id));
    }

    /**
     * 新增出入库单据明细的出库明细
     */
    @ApiOperation("新增出入库单据明细的出库明细")
    @Log(title = "出入库单据明细的出库明细新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CkBillStock ckBillStock) {
        return toAjax(ckBillStockService.save(ckBillStock));
    }

    /**
     * 修改出入库单据明细的出库明细
     */
    @ApiOperation("修改出入库单据明细的出库明细")
    @Log(title = "出入库单据明细的出库明细修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CkBillStock ckBillStock) {
        return toAjax(ckBillStockService.updateById(ckBillStock));
    }

    /**
     * 删除出入库单据明细的出库明细
     */
    @ApiOperation("删除出入库单据明细的出库明细")
    @Log(title = "出入库单据明细的出库明细删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        return toAjax(ckBillStockService.removeBatchByIds(ids));
    }

    /**
      * 条件设置
      */
    private void condition (LambdaQueryWrapper<CkBillStock> queryWrapper,CkBillStock ckBillStock){
        //入库库场ID
        if(ckBillStock.getStockId()!=null){
            queryWrapper.eq(CkBillStock::getStockId,ckBillStock.getStockId());
        }

        //入库库场代码
        if(Validator.isNotEmpty(ckBillStock.getStockCode())){
            queryWrapper.eq(CkBillStock::getStockCode,ckBillStock.getStockCode());
        }

        //入库库场名称
        if(Validator.isNotEmpty(ckBillStock.getStockName())){
            queryWrapper.eq(CkBillStock::getStockName,ckBillStock.getStockName());
        }

        //租户ID
        if(Validator.isNotEmpty(ckBillStock.getTenantId())){
            queryWrapper.eq(CkBillStock::getTenantId,ckBillStock.getTenantId());
        }

        }

}
