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

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tofly.api.DepartmentClient;
import com.tofly.common.core.entity.ResultRespone;
import com.tofly.gpsboot.service.car.CarBaseinfoService;
import com.tofly.pojo.car.CarBaseinfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 车辆基本信息管理
 *
 * @author Zhangyu
 * @date Mon Apr 27 00:00:00 GMT+08:00 2020
 */
@RestController
@AllArgsConstructor
@RequestMapping("/carbaseinfo")
@Api(tags="车辆基本信息管理接口")
public class CarBaseinfoController {

    private final CarBaseinfoService carBaseinfoService;

    private final DepartmentClient departmentClient;

    /**
    * 分页查询
    * @param page 分页对象
    * @param carBaseinfo 车辆基本信息管理
    * @return
    */
    @GetMapping("/page" )
    @ApiOperation(value = "分页查询")
    public ResultRespone getCarBaseinfoPage(Page page, @ModelAttribute CarBaseinfo carBaseinfo) {
//        Long deptId = carBaseinfo.getDepartmentId();
//        List<Long> deptIdList = null;
//        if (deptId != null) {
//            deptIdList = departmentClient.getDepartmentPage(deptId,"1").getResult()
//                    .stream().map(Department::getId).collect(Collectors.toList());
//        }
//        carBaseinfo.setDepartmentId(null);
//        List<String> commonCars = carBaseinfoService.getCommonCarInfo();
//        List<Long> finalDeptIdList = deptIdList;
        List<Long> deptIdList = null;
        if (StringUtils.isNotBlank(carBaseinfo.getDeptIds())) {
            deptIdList = Arrays.stream(carBaseinfo.getDeptIds().split(",")).map(Long::parseLong).collect(Collectors.toList());
        }
        return ResultRespone.success(carBaseinfoService.page(page, Wrappers.query(carBaseinfo).lambda()
                .in(CollUtil.isNotEmpty(deptIdList), CarBaseinfo::getDepartmentId, deptIdList)));
            //    .and(wrapper-> wrapper.in(CollUtil.isNotEmpty(finalDeptIdList), CarBaseinfo::getDepartmentId, finalDeptIdList)
//                .and(wrapper-> wrapper.in(CollUtil.isNotEmpty(finalDeptIdList), CarBaseinfo::getDepartmentId, finalDeptIdList)
//                        .or()
//                .in(CollUtil.isNotEmpty(commonCars),CarBaseinfo::getCarNumber, commonCars)));
    }


    /**
     * 通过id查询车辆基本信息管理
     * @param id id
     * @return ResultRespone
     */
    @GetMapping("/{id}" )
    @ApiOperation(value = "通过ID查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "Long")
    })
    public ResultRespone getById(@PathVariable("id" ) Long id) {
      return ResultRespone.success(carBaseinfoService.getById(id));
    }

    /**
     * 新增车辆基本信息管理
     * @param carBaseinfo 车辆基本信息管理
     * @return ResultRespone
     *//*
    @ToFlyAppLog(title="新增车辆基本信息管理" )
    @ApiOperation(value = "新增车辆基本信息管理")
    @PostMapping
    public ResultRespone save(@RequestBody CarBaseinfo carBaseinfo) {
      return ResultRespone.success(carBaseinfoService.save(carBaseinfo));
    }*/

    /**
     * 修改车辆基本信息管理
     * @param carBaseinfo 车辆基本信息管理
     * @return ResultRespone
     *//*
    @ToFlyAppLog(title="修改车辆基本信息管理" )
    @ApiOperation(value = "修改车辆基本信息管理")
    @PutMapping
    public ResultRespone updateById(@RequestBody CarBaseinfo carBaseinfo) {
      return ResultRespone.success(carBaseinfoService.updateById(carBaseinfo));
    }*/

   /* *//**
     * 通过id删除车辆基本信息管理
     * @param id id
     * @return ResultRespone
     *//*
    @ToFlyAppLog(title="通过id删除车辆基本信息管理" )
    @ApiOperation(value = "通过ID删除车辆基本信息管理")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "Long")
    })
    @DeleteMapping("/{id}" )
    public ResultRespone removeById(@PathVariable Long id) {
      return ResultRespone.success(carBaseinfoService.removeById(id));
    }

    *//**
     * 通过id批量删除车辆基本信息管理
     * @param ids id
     * @return ResultRespone
     *//*
    @ToFlyAppLog(title="通过id批量删除车辆基本信息管理" )
    @ApiOperation(value = "通过ID批量删除车辆基本信息管理")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "批量删除，多个id以逗号隔开", required = true, dataType = "String")
    })
    @DeleteMapping("/deleteByIds" )
    public ResultRespone removeById(String ids) {
        return ResultRespone.success(carBaseinfoService.removeByIds(Arrays.asList(ids.split(","))));
    }*/
}
