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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tofly.common.core.entity.ResultRespone;
import com.tofly.common.core.util.StringUtil;
import com.tofly.common.log.annotation.ToFlyAppLog;
import com.tofly.xrtygis.entity.TfCameraManage;
import com.tofly.xrtygis.entity.dto.TfCameraManageSearchDto;
import com.tofly.xrtygis.mapper.TfCameraTypeMapper;
import com.tofly.xrtygis.service.TfCameraManageService;
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.beanutils.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 摄像头管理
 *
 * @author admin
 * @date Fri Mar 11 00:00:00 CST 2022
 */
@RestController
@AllArgsConstructor
@RequestMapping("/tfcameramanage")
@Api(tags="摄像头管理接口")
public class TfCameraManageController {

    private final  TfCameraManageService tfCameraManageService;

    private  final TfCameraTypeMapper tfCameraTypeMapper;

    /**
    * 分页查询
    * @param page 分页对象
    * @param tfCameraManageSearchDto 摄像头管理
    * @return
    */
    @GetMapping("/page" )
    @ApiOperation(value = "分页查询")
    @ToFlyAppLog(title="分页查询" )
    public ResultRespone getTfCameraManagePage(Page page, @ModelAttribute TfCameraManageSearchDto tfCameraManageSearchDto) {
        try{
            TfCameraManage tfCameraManage = new TfCameraManage();
            String likeStr = tfCameraManageSearchDto.getLikeStr();
            BeanUtils.copyProperties(tfCameraManage,tfCameraManageSearchDto);
            List<String> finalList=new ArrayList<>();
            if(StringUtil.isNotEmpty(tfCameraManageSearchDto.getTypeIds())){
                finalList.addAll(Arrays.asList(tfCameraManageSearchDto.getTypeIds().split(",")));
            }

            LambdaQueryWrapper<TfCameraManage> queryWrapper= new QueryWrapper<TfCameraManage>().lambda()
                    .in(finalList.size()>0, TfCameraManage::getTypeId, finalList)
                    .eq(StringUtil.isNotEmpty(tfCameraManage.getCode()),TfCameraManage::getCode,tfCameraManage.getCode())
                    .like(StringUtil.isNotEmpty(tfCameraManage.getName()),TfCameraManage::getName,tfCameraManage.getName())
                    .like(StringUtil.isNotEmpty(tfCameraManage.getModel()),TfCameraManage::getModel,tfCameraManage.getModel())
                    .like(StringUtil.isNotEmpty(tfCameraManage.getManufactor()),TfCameraManage::getManufactor,tfCameraManage.getManufactor())
                    .like(StringUtil.isNotEmpty(tfCameraManage.getInstallationUnit()),TfCameraManage::getInstallationUnit,tfCameraManage.getInstallationUnit())
                    .like(StringUtil.isNotEmpty(tfCameraManage.getPhone()),TfCameraManage::getPhone,tfCameraManage.getPhone())
                    .eq(tfCameraManage.getWaterId()!=null && tfCameraManage.getWaterId()>0,TfCameraManage::getWaterId,tfCameraManage.getWaterId())
                    .eq(StringUtil.isNotEmpty(tfCameraManage.getBelongStatus()),TfCameraManage::getBelongStatus,tfCameraManage.getBelongStatus())
                    .like(StringUtil.isNotEmpty(tfCameraManage.getLeader()),TfCameraManage::getLeader,tfCameraManage.getLeader())
                    .and(StringUtil.isNotBlank(likeStr), k -> k.like(TfCameraManage::getName,likeStr).or().like(TfCameraManage::getLeader,likeStr));

            return ResultRespone.success(tfCameraManageService.page(page, queryWrapper));
        }catch (Exception e){
            e.printStackTrace();
            return ResultRespone.failed();
        }
    }


    /**
     * 通过id查询摄像头管理
     * @param waterId id
     * @return ResultRespone
     */
    @GetMapping("/{waterId}" )
    @ApiOperation(value = "通过ID查询")
    @ToFlyAppLog(title="通过ID查询" )
    @ApiImplicitParams({
            @ApiImplicitParam(name = "waterId", value = "ID", required = true, dataType = "Long")
    })
    public ResultRespone getById(@PathVariable("waterId" ) Long waterId) {
        try{
            return ResultRespone.success(tfCameraManageService.getById(waterId));
        }catch (Exception e){
            e.printStackTrace();
            return ResultRespone.failed();
        }
    }

    /**
     * 新增摄像头管理
     * @param tfCameraManage 摄像头管理
     * @return ResultRespone
     */
    @ToFlyAppLog(title="新增摄像头管理" )
    @ApiOperation(value = "新增摄像头管理")
    @PostMapping
    public ResultRespone save(@RequestBody TfCameraManage tfCameraManage) {
        try{
            return ResultRespone.success(tfCameraManageService.save(tfCameraManage));
        }catch (Exception e){
            e.printStackTrace();
            return ResultRespone.failed();
        }
    }

    /**
     * 修改摄像头管理
     * @param tfCameraManage 摄像头管理
     * @return ResultRespone
     */
    @ToFlyAppLog(title="修改摄像头管理" )
    @ApiOperation(value = "修改摄像头管理")
    @PutMapping
    public ResultRespone updateById(@RequestBody TfCameraManage tfCameraManage) {
        try{
            return ResultRespone.success(tfCameraManageService.updateById(tfCameraManage));
        }catch (Exception e){
            e.printStackTrace();
            return ResultRespone.failed();
        }
    }

    /**
     * 通过id删除摄像头管理
     * @param waterId id
     * @return ResultRespone
     */
    @ToFlyAppLog(title="通过id删除摄像头管理" )
    @ApiOperation(value = "通过ID删除摄像头管理")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "waterId", value = "ID", required = true, dataType = "Long")
    })
    @DeleteMapping("/{waterId}" )
    public ResultRespone removeById(@PathVariable Long waterId) {
        try{
            return ResultRespone.success(tfCameraManageService.removeById(waterId));
        }catch (Exception e){
            e.printStackTrace();
            return ResultRespone.failed();
        }
    }

    /**
     * 通过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) {
        try{
            return ResultRespone.success(tfCameraManageService.removeByIds(Arrays.asList(ids.split(","))));
        }catch (Exception e){
            e.printStackTrace();
            return ResultRespone.failed();
        }
    }


    /**
     * 导入设备和对应的指标数据
     * @return
     */
    @ToFlyAppLog(title="导入摄像头数据" )
    @ApiOperation(value = "导入摄像头数据")
    @PostMapping("/import")
    public ResultRespone ImportData(@Validated @RequestParam(value = "file",required = true) MultipartFile file){
        return ResultRespone.success(tfCameraManageService.ImportData(file));
    }

}
