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

/**
 * 货源定向发布Controller
 *
 * @author bs
 * @date 2024-02-07
 */
@Api(tags = "货源定向发布")
@RestController
@RequestMapping("/cargo/target")
public class TmCargoTargetController extends BaseController {
    @Resource
    private ITmCargoTargetService tmCargoTargetService;

    /**
     * 分页查询货源定向发布列表
     */
    @ApiOperation("分页查询货源定向发布列表")
    @GetMapping("/pageList")
    public TableDataInfo pageList(TmCargoTarget tmCargoTarget) {
        startPage();
        LambdaQueryWrapper<TmCargoTarget> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,tmCargoTarget);
        List<TmCargoTarget> list = tmCargoTargetService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 查询货源定向发布列表
     */
    @ApiOperation("查询货源定向发布列表")
    @GetMapping("/list")
    public AjaxResult list(TmCargoTarget tmCargoTarget) {
        LambdaQueryWrapper<TmCargoTarget> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,tmCargoTarget);
        List<TmCargoTarget> list = tmCargoTargetService.list(queryWrapper);
        return success(list);
    }

    /**
     * 导出货源定向发布列表
     */
    @ApiOperation("导出货源定向发布列表")
    @Log(title = "货源定向发布导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TmCargoTarget tmCargoTarget) {
        LambdaQueryWrapper<TmCargoTarget> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,tmCargoTarget);
        List<TmCargoTarget> list = tmCargoTargetService.list(queryWrapper);
        ExcelUtil<TmCargoTarget> util = new ExcelUtil<TmCargoTarget>(TmCargoTarget. class);
        util.exportExcel(response, list, "货源定向发布数据");
    }

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

    /**
     * 新增货源定向发布
     */
    @ApiOperation("新增货源定向发布")
    @Log(title = "货源定向发布新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TmCargoTarget tmCargoTarget) {
        return toAjax(tmCargoTargetService.save(tmCargoTarget));
    }

    /**
     * 修改货源定向发布
     */
    @ApiOperation("修改货源定向发布")
    @Log(title = "货源定向发布修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TmCargoTarget tmCargoTarget) {
        return toAjax(tmCargoTargetService.updateById(tmCargoTarget));
    }

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

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


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

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

        //承运方客户代码
        if(Validator.isNotEmpty(tmCargoTarget.getCarrierCustCode())){
            queryWrapper.eq(TmCargoTarget::getCarrierCustCode,tmCargoTarget.getCarrierCustCode());
        }

        //状态
        if(Validator.isNotEmpty(tmCargoTarget.getStatus())){
            queryWrapper.eq(TmCargoTarget::getStatus,tmCargoTarget.getStatus());
        }

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

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

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

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

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

        }

}
