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

/**
 * 称重作业Controller
 *
 * @author bs
 * @date 2025-01-12
 */
@Api(tags = "称重作业")
@RestController
@RequestMapping("/tm/vehicle/weight")
public class TmVehicleWeightController extends BaseController {
    @Resource
    private ITmVehicleWeightService tmVehicleWeightService;

    /**
     * 分页查询称重作业列表
     */
    @ApiOperation("分页查询称重作业列表")
    @GetMapping("/pageList")
    public TableDataInfo pageList(TmVehicleWeight tmVehicleWeight) {
        startPage();
        LambdaQueryWrapper<TmVehicleWeight> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,tmVehicleWeight);
        List<TmVehicleWeight> list = tmVehicleWeightService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 查询称重作业列表
     */
    @ApiOperation("查询称重作业列表")
    @GetMapping("/list")
    public AjaxResult list(TmVehicleWeight tmVehicleWeight) {
        LambdaQueryWrapper<TmVehicleWeight> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,tmVehicleWeight);
        List<TmVehicleWeight> list = tmVehicleWeightService.list(queryWrapper);
        return success(list);
    }

    /**
     * 导出称重作业列表
     */
    @ApiOperation("导出称重作业列表")
    @Log(title = "称重作业导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TmVehicleWeight tmVehicleWeight) {
        LambdaQueryWrapper<TmVehicleWeight> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,tmVehicleWeight);
        List<TmVehicleWeight> list = tmVehicleWeightService.list(queryWrapper);
        ExcelUtil<TmVehicleWeight> util = new ExcelUtil<TmVehicleWeight>(TmVehicleWeight. class);
        util.exportExcel(response, list, "称重作业数据");
    }

    /**
     * 获取称重作业详细信息
     */
    @ApiOperation("获取称重作业详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(tmVehicleWeightService.getById(id));
    }

    /**
     * 新增称重作业
     */
    @ApiOperation("新增称重作业")
    @Log(title = "称重作业新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TmVehicleWeight tmVehicleWeight) {
        return toAjax(tmVehicleWeightService.save(tmVehicleWeight));
    }

    /**
     * 修改称重作业
     */
    @ApiOperation("修改称重作业")
    @Log(title = "称重作业修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TmVehicleWeight tmVehicleWeight) {
        return toAjax(tmVehicleWeightService.updateById(tmVehicleWeight));
    }

    /**
     * 删除称重作业
     */
    @ApiOperation("删除称重作业")
    @Log(title = "称重作业删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        return toAjax(tmVehicleWeightService.removeBatchByIds(ids));
    }

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


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

        //流水号
        if(Validator.isNotEmpty(tmVehicleWeight.getSeqNo())){
            queryWrapper.eq(TmVehicleWeight::getSeqNo,tmVehicleWeight.getSeqNo());
        }

        //主机编号
        if(Validator.isNotEmpty(tmVehicleWeight.getHostNo())){
            queryWrapper.eq(TmVehicleWeight::getHostNo,tmVehicleWeight.getHostNo());
        }

        //数据类型
        if(Validator.isNotEmpty(tmVehicleWeight.getDataType())){
            queryWrapper.eq(TmVehicleWeight::getDataType,tmVehicleWeight.getDataType());
        }

        //数据产生日期
        if(Validator.isNotEmpty(tmVehicleWeight.getDataDate())){
            queryWrapper.eq(TmVehicleWeight::getDataDate,tmVehicleWeight.getDataDate());
        }

        //数据量
        if(Validator.isNotEmpty(tmVehicleWeight.getWorkWt())){
            queryWrapper.eq(TmVehicleWeight::getWorkWt,tmVehicleWeight.getWorkWt());
        }

        //发送日期
        if(Validator.isNotEmpty(tmVehicleWeight.getSendTime())){
            queryWrapper.eq(TmVehicleWeight::getSendTime,tmVehicleWeight.getSendTime());
        }

        //接收日期
        if(Validator.isNotEmpty(tmVehicleWeight.getReceiveTime())){
            queryWrapper.eq(TmVehicleWeight::getReceiveTime,tmVehicleWeight.getReceiveTime());
        }

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

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

        //创建部门
        if(Validator.isNotEmpty(tmVehicleWeight.getCreateDept())){
            queryWrapper.eq(TmVehicleWeight::getCreateDept,tmVehicleWeight.getCreateDept());
        }

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

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

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

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

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

        }

}
