package com.bs.tm.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 cn.hutool.core.lang.Validator;
import com.bs.tm.domain.TmCargoDetail;
import com.bs.tm.service.ITmCargoDetailService;
import javax.annotation.Resource;

/**
 * 货源明细Controller
 *
 * @author bs
 * @date 2024-02-07
 */
@Api(tags = "货源明细")
@RestController
@RequestMapping("/cargo/detail")
public class TmCargoDetailController extends BaseController {
    @Resource
    private ITmCargoDetailService tmCargoDetailService;

    /**
     * 分页查询货源明细列表
     */
    @ApiOperation("分页查询货源明细列表")
    @GetMapping("/pageList")
    public TableDataInfo pageList(TmCargoDetail tmCargoDetail) {
        startPage();
        LambdaQueryWrapper<TmCargoDetail> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,tmCargoDetail);
        List<TmCargoDetail> list = tmCargoDetailService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 查询货源明细列表
     */
    @ApiOperation("查询货源明细列表")
    @GetMapping("/list")
    public AjaxResult list(TmCargoDetail tmCargoDetail) {
        LambdaQueryWrapper<TmCargoDetail> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,tmCargoDetail);
        List<TmCargoDetail> list = tmCargoDetailService.list(queryWrapper);
        return success(list);
    }

    /**
     * 导出货源明细列表
     */
    @ApiOperation("导出货源明细列表")
    @Log(title = "货源明细导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TmCargoDetail tmCargoDetail) {
        LambdaQueryWrapper<TmCargoDetail> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,tmCargoDetail);
        List<TmCargoDetail> list = tmCargoDetailService.list(queryWrapper);
        ExcelUtil<TmCargoDetail> util = new ExcelUtil<TmCargoDetail>(TmCargoDetail. class);
        util.exportExcel(response, list, "货源明细数据");
    }

    /**
     * 获取货源明细详细信息
     */
    @ApiOperation("获取货源明细详细信息")
    @GetMapping(value = "/{ID}")
    public AjaxResult getInfo(@PathVariable("ID") Long ID) {
        return success(tmCargoDetailService.getById(ID));
    }

    /**
     * 新增货源明细
     */
    @ApiOperation("新增货源明细")
    @Log(title = "货源明细新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TmCargoDetail tmCargoDetail) {
        return toAjax(tmCargoDetailService.save(tmCargoDetail));
    }

    /**
     * 修改货源明细
     */
    @ApiOperation("修改货源明细")
    @Log(title = "货源明细修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TmCargoDetail tmCargoDetail) {
        return toAjax(tmCargoDetailService.updateById(tmCargoDetail));
    }

    /**
     * 删除货源明细
     */
    @ApiOperation("删除货源明细")
    @Log(title = "货源明细删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{IDs}")
    public AjaxResult remove(@PathVariable List<Long> IDs) {
        return toAjax(tmCargoDetailService.removeBatchByIds(IDs));
    }

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


        //id
        if(Validator.isNotEmpty(tmCargoDetail.getId())){
            queryWrapper.eq(TmCargoDetail::getId,tmCargoDetail.getId());
        }

        //货源ID
        if(Validator.isNotEmpty(tmCargoDetail.getCargoId())){
            queryWrapper.eq(TmCargoDetail::getCargoId,tmCargoDetail.getCargoId());
        }

        //货物明细名称
        if(Validator.isNotEmpty(tmCargoDetail.getDetailName())){
            queryWrapper.eq(TmCargoDetail::getDetailName,tmCargoDetail.getDetailName());
        }

        //货物明细类型
        if(Validator.isNotEmpty(tmCargoDetail.getDetailType())){
            queryWrapper.eq(TmCargoDetail::getDetailType,tmCargoDetail.getDetailType());
        }

        //货物明细重量
        if(Validator.isNotEmpty(tmCargoDetail.getDetailWeight())){
            queryWrapper.eq(TmCargoDetail::getDetailWeight,tmCargoDetail.getDetailWeight());
        }

        //货物明细价值
        if(Validator.isNotEmpty(tmCargoDetail.getDetailWorth())){
            queryWrapper.eq(TmCargoDetail::getDetailWorth,tmCargoDetail.getDetailWorth());
        }

        //货物明细体积
        if(Validator.isNotEmpty(tmCargoDetail.getDetailVol())){
            queryWrapper.eq(TmCargoDetail::getDetailVol,tmCargoDetail.getDetailVol());
        }

        //货物明细件数
        if(Validator.isNotEmpty(tmCargoDetail.getDetailNum())){
            queryWrapper.eq(TmCargoDetail::getDetailNum,tmCargoDetail.getDetailNum());
        }

        //货物外包装
        if(Validator.isNotEmpty(tmCargoDetail.getDetailPack())){
            queryWrapper.eq(TmCargoDetail::getDetailPack,tmCargoDetail.getDetailPack());
        }

        //创建人
        if(Validator.isNotEmpty(tmCargoDetail.getCreateBy())){
            queryWrapper.eq(TmCargoDetail::getCreateBy,tmCargoDetail.getCreateBy());
        }

        //创建时间
        if(Validator.isNotEmpty(tmCargoDetail.getCreateTime())){
            queryWrapper.eq(TmCargoDetail::getCreateTime,tmCargoDetail.getCreateTime());
        }

        //修改人
        if(Validator.isNotEmpty(tmCargoDetail.getUpdateBy())){
            queryWrapper.eq(TmCargoDetail::getUpdateBy,tmCargoDetail.getUpdateBy());
        }

        //修改时间
        if(Validator.isNotEmpty(tmCargoDetail.getUpdateTime())){
            queryWrapper.eq(TmCargoDetail::getUpdateTime,tmCargoDetail.getUpdateTime());
        }

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

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

        }

}
