package com.bs.pm.controller;

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

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
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.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang3.StringUtils;
import com.bs.pm.domain.PmExchangeGoods;
import com.bs.pm.service.IPmExchangeGoodsService;
import javax.annotation.Resource;

/**
 * 换购商品Controller
 *
 * @author bs
 * @date 2025-10-05
 */
@Api(tags = "换购商品")
@RestController
@RequestMapping("/exchange/goods")
public class PmExchangeGoodsController extends BaseController {
    @Resource
    private IPmExchangeGoodsService pmExchangeGoodsService;

    /**
     * 分页查询换购商品列表
     */
    @ApiOperation("分页查询换购商品列表")
    @GetMapping("/pageList")
    public TableDataInfo pageList(PmExchangeGoods pmExchangeGoods) {
        startPage();
        LambdaQueryWrapper<PmExchangeGoods> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,pmExchangeGoods);
        List<PmExchangeGoods> list = pmExchangeGoodsService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 查询换购商品列表
     */
    @ApiOperation("查询换购商品列表")
    @GetMapping("/list")
    public AjaxResult list(PmExchangeGoods pmExchangeGoods) {
        LambdaQueryWrapper<PmExchangeGoods> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,pmExchangeGoods);
        List<PmExchangeGoods> list = pmExchangeGoodsService.list(queryWrapper);
        return success(list);
    }

    /**
     * 导出换购商品列表
     */
    @ApiOperation("导出换购商品列表")
    @Log(title = "换购商品导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PmExchangeGoods pmExchangeGoods) {
        LambdaQueryWrapper<PmExchangeGoods> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,pmExchangeGoods);
        List<PmExchangeGoods> list = pmExchangeGoodsService.list(queryWrapper);
        ExcelUtil<PmExchangeGoods> util = new ExcelUtil<PmExchangeGoods>(PmExchangeGoods. class);
        util.exportExcel(response, list, "换购商品数据");
    }

    /**
     * 获取换购商品详细信息
     */
    @ApiOperation("获取换购商品详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(pmExchangeGoodsService.getById(id));
    }

    /**
     * 新增换购商品
     */
    @ApiOperation("新增换购商品")
    @Log(title = "换购商品新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PmExchangeGoods pmExchangeGoods) {
        return toAjax(pmExchangeGoodsService.save(pmExchangeGoods));
    }

    /**
     * 修改换购商品
     */
    @ApiOperation("修改换购商品")
    @Log(title = "换购商品修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PmExchangeGoods pmExchangeGoods) {
        return toAjax(pmExchangeGoodsService.updateById(pmExchangeGoods));
    }

    /**
     * 删除换购商品
     */
    @ApiOperation("删除换购商品")
    @Log(title = "换购商品删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        return toAjax(pmExchangeGoodsService.removeBatchByIds(ids));
    }

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


        //id
        if(null != (pmExchangeGoods.getId())){
            queryWrapper.eq(PmExchangeGoods::getId,pmExchangeGoods.getId());
        }

        //商品名称
        if(StringUtils.isNotEmpty(pmExchangeGoods.getGoodsName())){
            queryWrapper.eq(PmExchangeGoods::getGoodsName,pmExchangeGoods.getGoodsName());
        }

        //商品编码
        if(StringUtils.isNotEmpty(pmExchangeGoods.getGoodsCode())){
            queryWrapper.eq(PmExchangeGoods::getGoodsCode,pmExchangeGoods.getGoodsCode());
        }

        //商品规格
        if(StringUtils.isNotEmpty(pmExchangeGoods.getGoodsSepc())){
            queryWrapper.eq(PmExchangeGoods::getGoodsSepc,pmExchangeGoods.getGoodsSepc());
        }

        //换购类型
        if(StringUtils.isNotEmpty(pmExchangeGoods.getExchangeType())){
            queryWrapper.eq(PmExchangeGoods::getExchangeType,pmExchangeGoods.getExchangeType());
        }

        //所需积分
        if(null != (pmExchangeGoods.getRequiredPoint())){
            queryWrapper.eq(PmExchangeGoods::getRequiredPoint,pmExchangeGoods.getRequiredPoint());
        }

        //所需金额
        if(null != (pmExchangeGoods.getRequiredAmount())){
            queryWrapper.eq(PmExchangeGoods::getRequiredAmount,pmExchangeGoods.getRequiredAmount());
        }

        //库存数量
        if(null != (pmExchangeGoods.getStockQuantity())){
            queryWrapper.eq(PmExchangeGoods::getStockQuantity,pmExchangeGoods.getStockQuantity());
        }

        //库存预警值
        if(null != (pmExchangeGoods.getWarningStock())){
            queryWrapper.eq(PmExchangeGoods::getWarningStock,pmExchangeGoods.getWarningStock());
        }

        //商品图片
        if(null != (pmExchangeGoods.getGoodsImage())){
            queryWrapper.eq(PmExchangeGoods::getGoodsImage,pmExchangeGoods.getGoodsImage());
        }

        //商品详情
        if(StringUtils.isNotEmpty(pmExchangeGoods.getGoodsDesc())){
            queryWrapper.eq(PmExchangeGoods::getGoodsDesc,pmExchangeGoods.getGoodsDesc());
        }

        //状态
        if(StringUtils.isNotEmpty(pmExchangeGoods.getStatus())){
            queryWrapper.eq(PmExchangeGoods::getStatus,pmExchangeGoods.getStatus());
        }

        //备注
        if(StringUtils.isNotEmpty(pmExchangeGoods.getRemark())){
            queryWrapper.eq(PmExchangeGoods::getRemark,pmExchangeGoods.getRemark());
        }

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

        //创建部门
        if(null != (pmExchangeGoods.getCreateDept())){
            queryWrapper.eq(PmExchangeGoods::getCreateDept,pmExchangeGoods.getCreateDept());
        }

        //创建人
        if(null != (pmExchangeGoods.getCreateBy())){
            queryWrapper.eq(PmExchangeGoods::getCreateBy,pmExchangeGoods.getCreateBy());
        }

        //创建时间
        if(null != (pmExchangeGoods.getCreateTime())){
            queryWrapper.eq(PmExchangeGoods::getCreateTime,pmExchangeGoods.getCreateTime());
        }

        //修改人
        if(null != (pmExchangeGoods.getUpdateBy())){
            queryWrapper.eq(PmExchangeGoods::getUpdateBy,pmExchangeGoods.getUpdateBy());
        }

        //修改时间
        if(null != (pmExchangeGoods.getUpdateTime())){
            queryWrapper.eq(PmExchangeGoods::getUpdateTime,pmExchangeGoods.getUpdateTime());
        }

        //租户ID
        if(StringUtils.isNotEmpty(pmExchangeGoods.getTenantId())){
            queryWrapper.eq(PmExchangeGoods::getTenantId,pmExchangeGoods.getTenantId());
        }

        //${column.columnComment}
        if(StringUtils.isNotEmpty(pmExchangeGoods.getColumn21())){
            queryWrapper.eq(PmExchangeGoods::getColumn21,pmExchangeGoods.getColumn21());
        }

        }

}
