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.TmOrderCargo;
import com.bs.tm.service.ITmOrderCargoService;
import javax.annotation.Resource;

/**
 * 承运货物信息Controller
 *
 * @author bs
 * @date 2024-02-07
 */
@Api(tags = "承运货物信息")
@RestController
@RequestMapping("/order/cargo")
public class TmOrderCargoController extends BaseController {
    @Resource
    private ITmOrderCargoService tmOrderCargoService;

    /**
     * 分页查询承运货物信息列表
     */
    @ApiOperation("分页查询承运货物信息列表")
    @GetMapping("/pageList")
    public TableDataInfo pageList(TmOrderCargo tmOrderCargo) {
        startPage();
        LambdaQueryWrapper<TmOrderCargo> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,tmOrderCargo);
        List<TmOrderCargo> list = tmOrderCargoService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 查询承运货物信息列表
     */
    @ApiOperation("查询承运货物信息列表")
    @GetMapping("/list")
    public AjaxResult list(TmOrderCargo tmOrderCargo) {
        LambdaQueryWrapper<TmOrderCargo> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,tmOrderCargo);
        List<TmOrderCargo> list = tmOrderCargoService.list(queryWrapper);
        return success(list);
    }

    /**
     * 导出承运货物信息列表
     */
    @ApiOperation("导出承运货物信息列表")
    @Log(title = "承运货物信息导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TmOrderCargo tmOrderCargo) {
        LambdaQueryWrapper<TmOrderCargo> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,tmOrderCargo);
        List<TmOrderCargo> list = tmOrderCargoService.list(queryWrapper);
        ExcelUtil<TmOrderCargo> util = new ExcelUtil<TmOrderCargo>(TmOrderCargo. class);
        util.exportExcel(response, list, "承运货物信息数据");
    }

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

    /**
     * 新增承运货物信息
     */
    @ApiOperation("新增承运货物信息")
    @Log(title = "承运货物信息新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TmOrderCargo tmOrderCargo) {
        return toAjax(tmOrderCargoService.save(tmOrderCargo));
    }

    /**
     * 修改承运货物信息
     */
    @ApiOperation("修改承运货物信息")
    @Log(title = "承运货物信息修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TmOrderCargo tmOrderCargo) {
        return toAjax(tmOrderCargoService.updateById(tmOrderCargo));
    }

    /**
     * 删除承运货物信息
     */
    @ApiOperation("删除承运货物信息")
    @Log(title = "承运货物信息删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{IDs}")
    public AjaxResult remove(@PathVariable List<Long> IDs) {
        return toAjax(tmOrderCargoService.removeBatchByIds(IDs));
    }

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


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

        //货物ID,从nc_cargo_source导入的数据时保留该信息
        if(Validator.isNotEmpty(tmOrderCargo.getCargoId())){
            queryWrapper.eq(TmOrderCargo::getCargoId,tmOrderCargo.getCargoId());
        }

        //订单ID
        if(Validator.isNotEmpty(tmOrderCargo.getOrderId())){
            queryWrapper.eq(TmOrderCargo::getOrderId,tmOrderCargo.getOrderId());
        }

        //货主代码
        if(Validator.isNotEmpty(tmOrderCargo.getCustId())){
            queryWrapper.eq(TmOrderCargo::getCustId,tmOrderCargo.getCustId());
        }

        //货物体积
        if(Validator.isNotEmpty(tmOrderCargo.getCargoVol())){
            queryWrapper.eq(TmOrderCargo::getCargoVol,tmOrderCargo.getCargoVol());
        }

        //货源类型
        if(Validator.isNotEmpty(tmOrderCargo.getCargoType())){
            queryWrapper.eq(TmOrderCargo::getCargoType,tmOrderCargo.getCargoType());
        }

        //货物信息
        if(Validator.isNotEmpty(tmOrderCargo.getCargoInfo())){
            queryWrapper.eq(TmOrderCargo::getCargoInfo,tmOrderCargo.getCargoInfo());
        }

        //货物重量
        if(Validator.isNotEmpty(tmOrderCargo.getCargoWeight())){
            queryWrapper.eq(TmOrderCargo::getCargoWeight,tmOrderCargo.getCargoWeight());
        }

        //货物价值
        if(Validator.isNotEmpty(tmOrderCargo.getCargoWorth())){
            queryWrapper.eq(TmOrderCargo::getCargoWorth,tmOrderCargo.getCargoWorth());
        }

        //货物件数
        if(Validator.isNotEmpty(tmOrderCargo.getCargoNum())){
            queryWrapper.eq(TmOrderCargo::getCargoNum,tmOrderCargo.getCargoNum());
        }

        //货物外包装
        if(Validator.isNotEmpty(tmOrderCargo.getCargoPack())){
            queryWrapper.eq(TmOrderCargo::getCargoPack,tmOrderCargo.getCargoPack());
        }

        //发货人
        if(Validator.isNotEmpty(tmOrderCargo.getDispatchUser())){
            queryWrapper.eq(TmOrderCargo::getDispatchUser,tmOrderCargo.getDispatchUser());
        }

        //发货地
        if(Validator.isNotEmpty(tmOrderCargo.getDispatchPlace())){
            queryWrapper.eq(TmOrderCargo::getDispatchPlace,tmOrderCargo.getDispatchPlace());
        }

        //发货地编码
        if(Validator.isNotEmpty(tmOrderCargo.getDispatchPlaceCode())){
            queryWrapper.eq(TmOrderCargo::getDispatchPlaceCode,tmOrderCargo.getDispatchPlaceCode());
        }

        //发货详细地址
        if(Validator.isNotEmpty(tmOrderCargo.getDispatchPlaceDetail())){
            queryWrapper.eq(TmOrderCargo::getDispatchPlaceDetail,tmOrderCargo.getDispatchPlaceDetail());
        }

        //发货地位置
        if(Validator.isNotEmpty(tmOrderCargo.getDispatchLocation())){
            queryWrapper.eq(TmOrderCargo::getDispatchLocation,tmOrderCargo.getDispatchLocation());
        }

        //发货人固定电话
        if(Validator.isNotEmpty(tmOrderCargo.getDispatchTel())){
            queryWrapper.eq(TmOrderCargo::getDispatchTel,tmOrderCargo.getDispatchTel());
        }

        //发货人手机号码
        if(Validator.isNotEmpty(tmOrderCargo.getDispatchMobile())){
            queryWrapper.eq(TmOrderCargo::getDispatchMobile,tmOrderCargo.getDispatchMobile());
        }

        //发货单位id,便于后期扩展(发货单位选择控件,统计)
        if(Validator.isNotEmpty(tmOrderCargo.getDispatchCustId())){
            queryWrapper.eq(TmOrderCargo::getDispatchCustId,tmOrderCargo.getDispatchCustId());
        }

        //收货人
        if(Validator.isNotEmpty(tmOrderCargo.getReceiveUser())){
            queryWrapper.eq(TmOrderCargo::getReceiveUser,tmOrderCargo.getReceiveUser());
        }

        //收货地
        if(Validator.isNotEmpty(tmOrderCargo.getReceivePlace())){
            queryWrapper.eq(TmOrderCargo::getReceivePlace,tmOrderCargo.getReceivePlace());
        }

        //收货地编码
        if(Validator.isNotEmpty(tmOrderCargo.getReceivePlaceCode())){
            queryWrapper.eq(TmOrderCargo::getReceivePlaceCode,tmOrderCargo.getReceivePlaceCode());
        }

        //收货详细地址
        if(Validator.isNotEmpty(tmOrderCargo.getReceivePlaceDetail())){
            queryWrapper.eq(TmOrderCargo::getReceivePlaceDetail,tmOrderCargo.getReceivePlaceDetail());
        }

        //收货地位置
        if(Validator.isNotEmpty(tmOrderCargo.getReceiveLocation())){
            queryWrapper.eq(TmOrderCargo::getReceiveLocation,tmOrderCargo.getReceiveLocation());
        }

        //收货方手机号码
        if(Validator.isNotEmpty(tmOrderCargo.getReceiveMobile())){
            queryWrapper.eq(TmOrderCargo::getReceiveMobile,tmOrderCargo.getReceiveMobile());
        }

        //收货方固定电话
        if(Validator.isNotEmpty(tmOrderCargo.getReceiveTel())){
            queryWrapper.eq(TmOrderCargo::getReceiveTel,tmOrderCargo.getReceiveTel());
        }

        //收货方单位ID
        if(Validator.isNotEmpty(tmOrderCargo.getReceiveCustId())){
            queryWrapper.eq(TmOrderCargo::getReceiveCustId,tmOrderCargo.getReceiveCustId());
        }

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

        //总重量
        if(Validator.isNotEmpty(tmOrderCargo.getTotalWeight())){
            queryWrapper.eq(TmOrderCargo::getTotalWeight,tmOrderCargo.getTotalWeight());
        }

        //总体积
        if(Validator.isNotEmpty(tmOrderCargo.getTotalVol())){
            queryWrapper.eq(TmOrderCargo::getTotalVol,tmOrderCargo.getTotalVol());
        }

        //单价
        if(Validator.isNotEmpty(tmOrderCargo.getPrice())){
            queryWrapper.eq(TmOrderCargo::getPrice,tmOrderCargo.getPrice());
        }

        //单价类型
        if(Validator.isNotEmpty(tmOrderCargo.getPriceType())){
            queryWrapper.eq(TmOrderCargo::getPriceType,tmOrderCargo.getPriceType());
        }

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

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

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

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

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

        }

}
