/*
 * Author: wangjian
 * 生成controller
 */
package com.tofly.gpsboot.controller.plan;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tofly.common.core.entity.ResultRespone;
import com.tofly.common.log.annotation.ToFlyAppLog;
import com.tofly.common.oauth.exception.ToflyDeniedException;
import com.tofly.gpsboot.service.car.CarBaseinfoService;
import com.tofly.gpsboot.service.paln.TfYwpnPlanCartouserService;
import com.tofly.pojo.car.CarBaseinfo;
import com.tofly.pojo.plan.TfYwpnPlanCartouser;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.web.bind.annotation.ModelAttribute;

import java.util.Arrays;
/**
 * 任务派发车辆人员绑定关联表
 *
 * @author JF
 * @date Thu Apr 06 00:00:00 CST 2023
 */
@RestController
@AllArgsConstructor
@RequestMapping("/tfywpnplancartouser")
@Api(tags="任务派发车辆人员绑定关联接口")
public class TfYwpnPlanCartouserController {

    private final TfYwpnPlanCartouserService tfYwpnPlanCartouserService;
    private final CarBaseinfoService carBaseinfoService;

//    /**
//    * 分页查询
//    * @param page 分页对象
//    * @param tfYwpnPlanCartouser ${comments}
//    * @return
//    */
//    @GetMapping("/page" )
//    @ApiOperation(value = "分页查询")
//    public ResultRespone getTfYwpnPlanCartouserPage(Page page, @ModelAttribute TfYwpnPlanCartouser tfYwpnPlanCartouser) {
//      return ResultRespone.success(tfYwpnPlanCartouserService.page(page, Wrappers.query(tfYwpnPlanCartouser)));
//    }

    /**
     * 列表查询
     * @param tfYwpnPlanCartouser 车辆人员绑定
     * @return
     */
    @GetMapping("/list" )
    @ApiOperation(value = "列表查询")
    public ResultRespone getTfYwpnPlanCartouserPage(@ModelAttribute TfYwpnPlanCartouser tfYwpnPlanCartouser) {
        return ResultRespone.success(tfYwpnPlanCartouserService.list(Wrappers.query(tfYwpnPlanCartouser)));
    }


//    /**
//     * 通过id查询${comments}
//     * @param id id
//     * @return ResultRespone
//     */
//    @GetMapping("/{id}" )
//    @ApiOperation(value = "通过ID查询")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "BigDecimal")
//    })
//    public ResultRespone getById(@PathVariable("id" ) Long id) {
//      return ResultRespone.success(tfYwpnPlanCartouserService.getById(id));
//    }

    /**
     * 新增/修改车辆人员绑定
     * @param tfYwpnPlanCartouser 车辆人员绑定
     * @return ResultRespone
     */
    @ToFlyAppLog(title="新增/修改车辆人员绑定" )
    @ApiOperation(value = "新增/修改车辆人员绑定")
    @PostMapping("/saveOrUpdate")
    public ResultRespone saveOrUpdate(@RequestBody TfYwpnPlanCartouser tfYwpnPlanCartouser) {
        CarBaseinfo carBaseinfo = new CarBaseinfo();
        carBaseinfo.setId(tfYwpnPlanCartouser.getCarId());
        carBaseinfo.setIsBind(tfYwpnPlanCartouser.getIsBind());
        carBaseinfoService.update(carBaseinfo,new QueryWrapper<CarBaseinfo>().lambda().eq(CarBaseinfo::getId,carBaseinfo.getId()));

        TfYwpnPlanCartouser planCartouser = tfYwpnPlanCartouserService.getOne(new QueryWrapper<TfYwpnPlanCartouser>().lambda()
                .eq(TfYwpnPlanCartouser::getCarId,tfYwpnPlanCartouser.getCarId())
                .eq(TfYwpnPlanCartouser::getPlanChildId,tfYwpnPlanCartouser.getPlanChildId())
                .eq(TfYwpnPlanCartouser::getUserId,tfYwpnPlanCartouser.getUserId()));
        boolean res;
        if(planCartouser != null){
            tfYwpnPlanCartouser.setId(planCartouser.getId());
            res = tfYwpnPlanCartouserService.updateById(tfYwpnPlanCartouser);
        } else {
            res = tfYwpnPlanCartouserService.save(tfYwpnPlanCartouser);
        }
        return ResultRespone.success(res);
    }

//    /**
//     * 修改车辆人员绑定
//     * @param tfYwpnPlanCartouser 车辆人员绑定
//     * @return ResultRespone
//     */
//    @ToFlyAppLog(title="修改车辆人员绑定" )
//    @ApiOperation(value = "修改车辆人员绑定")
//    @PutMapping
//    public ResultRespone updateById(@RequestBody TfYwpnPlanCartouser tfYwpnPlanCartouser) {
//        TfYwpnPlanCartouser planCartouser = tfYwpnPlanCartouserService.getOne(new QueryWrapper<TfYwpnPlanCartouser>().lambda()
//                .eq(TfYwpnPlanCartouser::getCarId,tfYwpnPlanCartouser.getCarId())
//                .eq(TfYwpnPlanCartouser::getPlanChildId,tfYwpnPlanCartouser.getPlanChildId())
//                .eq(TfYwpnPlanCartouser::getUserId,tfYwpnPlanCartouser.getUserId()));
//        if(planCartouser!=null){
//            throw new ToflyDeniedException("当前子计划的用户已绑定车辆");
//        }
//        if (StringUtils.isNotBlank(tfYwpnPlanCartouser.getIsBind())) {
//            CarBaseinfo carBaseinfo = new CarBaseinfo();
//            carBaseinfo.setId(tfYwpnPlanCartouser.getCarId());
//            carBaseinfo.setIsBind(tfYwpnPlanCartouser.getIsBind());
//            carBaseinfoService.update(carBaseinfo,new QueryWrapper<CarBaseinfo>().lambda().eq(CarBaseinfo::getId,carBaseinfo.getId()));
//        }
//      return ResultRespone.success(tfYwpnPlanCartouserService.updateById(tfYwpnPlanCartouser));
//    }

//    /**
//     * 通过id删除${comments}
//     * @param id id
//     * @return ResultRespone
//     */
//    @ToFlyAppLog(title="通过id删除${comments}" )
//    @ApiOperation(value = "通过ID删除${comments}")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "BigDecimal")
//    })
//    @DeleteMapping("/{id}" )
//    public ResultRespone removeById(@PathVariable Long id) {
//      return ResultRespone.success(tfYwpnPlanCartouserService.removeById(id));
//    }
//
//    /**
//     * 通过id批量删除${comments}
//     * @param ids id
//     * @return ResultRespone
//     */
//    @ToFlyAppLog(title="通过id批量删除${comments}" )
//    @ApiOperation(value = "通过ID批量删除${comments}")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "ids", value = "批量删除，多个id以逗号隔开", required = true, dataType = "String")
//    })
//    @DeleteMapping("/deleteByIds" )
//    public ResultRespone removeById(String ids) {
//        return ResultRespone.success(tfYwpnPlanCartouserService.removeByIds(Arrays.asList(ids.split(","))));
//    }
}
