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.domain.CkStockChange;
import com.bs.jxc.service.ICkStockChangeService;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
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-10
 */
@Api(tags = "库存变动记录")
@RestController
@RequestMapping("/jxc/ckchange")
public class CkStockChangeController extends BaseController {
    @Resource
    private ICkStockChangeService ckStockChangeService;

    /**
     * 分页查询库存变动记录列表
     */
    @ApiOperation("分页查询库存变动记录列表")
    @GetMapping("/pageList")
    public TableDataInfo pageList(CkStockChange ckStockChange) {
        startPage();
        MPJLambdaWrapper<CkStockChange> queryWrapper = new MPJLambdaWrapper();
        queryWrapper.select(CkStockChange::getCustId,CkStockChange::getId,CkStockChange::getChangeType,CkStockChange::getCargoId,
                CkStockChange::getHsCode,  CkStockChange::getCargoName, CkStockChange::getStockId,
                CkStockChange::getStockName, CkStockChange::getBillNo,CkStockChange::getBillId, CkStockChange::getChangeNum, CkStockChange::getBeforeNum,
                CkStockChange::getAfterNum, CkStockChange::getChangeWt, CkStockChange::getBeforeWt, CkStockChange::getAfterWt, CkStockChange::getChangeVol,
                CkStockChange::getBeforeVol, CkStockChange::getAfterVol, CkStockChange::getChangeValue, CkStockChange::getBeforeValue, CkStockChange::getAfterValue,
                CkStockChange::getChangeDate)  // 查询 CkStockChange 所有字段
                .select(CkBillStock::getStockCode)  // 查询关联表 CkBillStock 的 stockCode 字段
                .leftJoin(CkBillStock.class, CkBillStock::getId, CkStockChange::getStockId);
        queryWrapper
                .like(ckStockChange.getCustId() != null, CkStockChange::getCustId, ckStockChange.getCustId())
                .eq(Validator.isNotEmpty(ckStockChange.getChangeType()), CkStockChange::getChangeType, ckStockChange.getChangeType())
                .eq(ckStockChange.getCargoId() != null, CkStockChange::getCargoId, ckStockChange.getCargoId())
                .like(Validator.isNotEmpty(ckStockChange.getHsCode()), CkStockChange::getHsCode, ckStockChange.getHsCode())
                .like(Validator.isNotEmpty(ckStockChange.getCargoName()), CkStockChange::getCargoName, ckStockChange.getCargoName())
                .eq(ckStockChange.getStockId() != null, CkStockChange::getStockId, ckStockChange.getStockId())
                .like(Validator.isNotEmpty(ckStockChange.getStockName()), CkStockChange::getStockName, ckStockChange.getStockName())
                .like(Validator.isNotEmpty(ckStockChange.getBillNo()), CkStockChange::getBillNo, ckStockChange.getBillNo())
                .eq(ckStockChange.getBillId() != null, CkStockChange::getBillId, ckStockChange.getBillId())
                .eq(ckStockChange.getChangeNum() != null, CkStockChange::getChangeNum, ckStockChange.getChangeNum())
                .eq(ckStockChange.getBeforeNum() != null, CkStockChange::getBeforeNum, ckStockChange.getBeforeNum())
                .eq(ckStockChange.getAfterNum() != null, CkStockChange::getAfterNum, ckStockChange.getAfterNum())
                .eq(ckStockChange.getChangeDate() != null, CkStockChange::getChangeDate, ckStockChange.getChangeDate())
                .between(null != ckStockChange.getBeginDate(), CkStockChange::getChangeDate, ckStockChange.getBeginDate(), ckStockChange.getEndDate())
                .eq(Validator.isNotEmpty(ckStockChange.getRemark()), CkStockChange::getRemark, ckStockChange.getRemark())
                .eq(Validator.isNotEmpty(ckStockChange.getDelFlag()), CkStockChange::getDelFlag, ckStockChange.getDelFlag())
                .eq(Validator.isNotEmpty(ckStockChange.getStockCode()), CkBillStock::getStockCode, ckStockChange.getStockCode())
                .eq(Validator.isNotEmpty(ckStockChange.getTenantId()), CkStockChange::getTenantId, ckStockChange.getTenantId());

        List<CkStockChange> list = ckStockChangeService.selectJoinList(CkStockChange.class,queryWrapper);
        return getDataTable(list);
    }

    /**
     * 查询库存变动记录列表
     */
    @ApiOperation("查询库存变动记录列表")
    @GetMapping("/list")
    public AjaxResult list(CkStockChange ckStockChange) {
        LambdaQueryWrapper<CkStockChange> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,ckStockChange);
        List<CkStockChange> list = ckStockChangeService.list(queryWrapper);
        return success(list);
    }

    /**
     * 导出库存变动记录列表
     */
    @ApiOperation("导出库存变动记录列表")
    @Log(title = "库存变动记录导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CkStockChange ckStockChange) {
        LambdaQueryWrapper<CkStockChange> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,ckStockChange);
        List<CkStockChange> list = ckStockChangeService.list(queryWrapper);
        ExcelUtil<CkStockChange> util = new ExcelUtil<CkStockChange>(CkStockChange. class);
        util.exportExcel(response, list, "库存变动记录数据");
    }

    /**
     * 获取库存变动记录详细信息
     */
    @ApiOperation("获取库存变动记录详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(ckStockChangeService.getById(id));
    }

    /**
     * 新增库存变动记录
     */
    @ApiOperation("新增库存变动记录")
    @Log(title = "库存变动记录新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CkStockChange ckStockChange) {
        return toAjax(ckStockChangeService.save(ckStockChange));
    }

    /**
     * 修改库存变动记录
     */
    @ApiOperation("修改库存变动记录")
    @Log(title = "库存变动记录修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CkStockChange ckStockChange) {
        return toAjax(ckStockChangeService.updateById(ckStockChange));
    }

    /**
     * 删除库存变动记录
     */
    @ApiOperation("删除库存变动记录")
    @Log(title = "库存变动记录删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        return toAjax(ckStockChangeService.removeBatchByIds(ids));
    }

    /**
      * 条件设置
      */
    private void condition (LambdaQueryWrapper<CkStockChange> queryWrapper,CkStockChange ckStockChange){




        //客户代码
        if(ckStockChange.getCustId() != null){
            queryWrapper.like(CkStockChange::getCustId,ckStockChange.getCustId());
        }

        //变动类型
        if(Validator.isNotEmpty(ckStockChange.getChangeType())){
            queryWrapper.eq(CkStockChange::getChangeType,ckStockChange.getChangeType());
        }

        //货品ID
        if(ckStockChange.getCargoId() != null){
            queryWrapper.eq(CkStockChange::getCargoId,ckStockChange.getCargoId());
        }

        //HS代码
        if(Validator.isNotEmpty(ckStockChange.getHsCode())){
            queryWrapper.like(CkStockChange::getHsCode,ckStockChange.getHsCode());
        }

        //货品名称
        if(Validator.isNotEmpty(ckStockChange.getCargoName())){
            queryWrapper.like(CkStockChange::getCargoName,ckStockChange.getCargoName());
        }

        //仓库
        if(ckStockChange.getStockId() != null){
            queryWrapper.eq(CkStockChange::getStockId,ckStockChange.getStockId());
        }

        //仓库名称
        if(Validator.isNotEmpty(ckStockChange.getStockName())){
            queryWrapper.like(CkStockChange::getStockName,ckStockChange.getStockName());
        }

        //批号
        if(Validator.isNotEmpty(ckStockChange.getBillNo())){
            queryWrapper.like(CkStockChange::getBillNo,ckStockChange.getBillNo());
        }

        //变动单号
        if(ckStockChange.getBillId() != null){
            queryWrapper.eq(CkStockChange::getBillId,ckStockChange.getBillId());
        }

        //变动数量
        if(ckStockChange.getChangeNum() != null){
            queryWrapper.eq(CkStockChange::getChangeNum,ckStockChange.getChangeNum());
        }

        //变动前
        if(ckStockChange.getBeforeNum() != null){
            queryWrapper.eq(CkStockChange::getBeforeNum,ckStockChange.getBeforeNum());
        }

        //变动后
        if(ckStockChange.getAfterNum() != null){
            queryWrapper.eq(CkStockChange::getAfterNum,ckStockChange.getAfterNum());
        }

        //变动日期
        if(ckStockChange.getChangeDate() != null){
            queryWrapper.eq(CkStockChange::getChangeDate,ckStockChange.getChangeDate());
        }

        if (null != ckStockChange.getBeginDate()) {
            queryWrapper.between(CkStockChange::getChangeDate, ckStockChange.getBeginDate(), ckStockChange.getEndDate());
        }

        //备注
        if(Validator.isNotEmpty(ckStockChange.getRemark())){
            queryWrapper.eq(CkStockChange::getRemark,ckStockChange.getRemark());
        }

        //删除标志（0代表存在 2代表删除）
        if(Validator.isNotEmpty(ckStockChange.getDelFlag())){
            queryWrapper.eq(CkStockChange::getDelFlag,ckStockChange.getDelFlag());
        }


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

        }

}
