package com.huimv.admin.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huimv.admin.common.utils.Result;
import com.huimv.admin.common.utils.ResultCode;
import com.huimv.admin.entity.BaseSpecificVideo;
import com.huimv.admin.entity.CameraBase;
import com.huimv.admin.service.IBaseSpecificVideoService;
import com.huimv.admin.service.ICameraBaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author astupidcoder
 * @since 2021-12-20
 */
@RestController
@RequestMapping("/baseSpecificVideo")
@CrossOrigin
public class BaseSpecificVideoController {
    @Autowired
    IBaseSpecificVideoService baseSpecificVideoService;

    @Autowired
    ICameraBaseService cameraService;

    @PostMapping("/add")
    public Result add(@RequestBody BaseSpecificVideo baseFarmX) {
        CameraBase byId = cameraService.getOne(new QueryWrapper<CameraBase>().eq("camera_uuid",baseFarmX.getVideoId()));
        baseFarmX.setVideoName(byId.getCameraName());
        baseFarmX.setVideoIds(byId.getId());
        baseFarmX.setUrl(byId.getUrl());
        baseSpecificVideoService.save(baseFarmX);
        return new Result(ResultCode.SUCCESS);
    }


    @GetMapping("/remove")
    public Result remove(@RequestParam(name = "id") Integer id) {
        baseSpecificVideoService.removeById(id);
        return new Result(ResultCode.SUCCESS);
    }

    @PostMapping("/update")
    public Result update(@RequestBody BaseSpecificVideo baseFarmX) {
        CameraBase byId = cameraService.getById(baseFarmX.getVideoId());
        baseFarmX.setVideoName(byId.getCameraName());
        baseSpecificVideoService.updateById(baseFarmX);
        return new Result(ResultCode.SUCCESS);
    }

    @GetMapping("/list")
    public Result list(@RequestParam(name = "farmId") Integer farmId,
                       @RequestParam(name = "type") String type) {
        List<BaseSpecificVideo> farm_id = baseSpecificVideoService.list(new QueryWrapper<BaseSpecificVideo>().eq("farm_id", farmId)
                .eq(StringUtils.isNotBlank(type), "type", type
                ));
        for (BaseSpecificVideo baseSpecificVideo : farm_id) {
            CameraBase byId = cameraService.getOne(new QueryWrapper<CameraBase>().eq("camera_uuid", baseSpecificVideo.getVideoId()));
            baseSpecificVideo.setVideoIds(byId.getId());
        }
        return new Result(ResultCode.SUCCESS, farm_id);
    }

    @PostMapping("/page")
    public Result page(@RequestBody Map<String, String> paramsMap) {
        String farmId = paramsMap.get("farmId");
        String type = paramsMap.get("type");
        String pageNum = paramsMap.get("pageNum");
        String pageSize = paramsMap.get("pageSize");
        if ("".equals(pageNum) || null == pageNum) {
            pageNum = "1";
        }
        if ("".equals(pageSize) || null == pageSize) {
            pageSize = "10";
        }
        Page<BaseSpecificVideo> page = new Page<>(Integer.parseInt(pageNum), Integer.parseInt(pageSize));
//        List<BaseSpecificVideo> farm_id = baseSpecificVideoService.list(new QueryWrapper<BaseSpecificVideo>().eq("farm_id", farmId)
//                .eq(StringUtils.isNotBlank(type), "type", type));
        Page<BaseSpecificVideo> page1 = baseSpecificVideoService.page(page, new QueryWrapper<BaseSpecificVideo>()
                .eq("farm_id", farmId).eq(StringUtils.isNotBlank(type), "type", type));
        for (BaseSpecificVideo baseSpecificVideo : page1.getRecords()) {
            CameraBase byId = cameraService.getOne(new QueryWrapper<CameraBase>().eq("camera_uuid", baseSpecificVideo.getVideoId()));
            baseSpecificVideo.setVideoIds(byId.getId());
        }
        return new Result(ResultCode.SUCCESS, page1);
    }
    @GetMapping("/listSpecif")
    public Result listSpecif(@RequestParam(name = "farmId") Integer farmId,
                             @RequestParam(name = "type") Integer type) throws Exception {

        return new Result(ResultCode.SUCCESS, baseSpecificVideoService.listSpecif(farmId, type));
    }


}
