package com.huimv.video.video.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.huimv.common.utils.Result;
import com.huimv.video.video.entity.Camera;
import com.huimv.video.video.entity.CameraArea;
import com.huimv.video.video.service.ICameraAreaService;
import com.huimv.video.video.service.ICameraService;
import com.huimv.video.video.utils.IpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author astupidcoder
 * @since 2021-09-13
 */
@RestController
@RequestMapping("/cameraArea")
public class CameraAreaController {
    @Autowired
    private ICameraAreaService cameraAreaService;

    @Autowired
    private ICameraService cameraService;


    @PostMapping("/list")
    public Result list(@RequestBody Map<String, Object> params) {
        return cameraAreaService.cameraList(params);
    }

    @PostMapping("/add")
    public Result add(@RequestBody CameraArea cameraArea) {
        int brandName = cameraAreaService.count(new QueryWrapper<CameraArea>().eq("area_name", cameraArea.getAreaName()).eq("farm_id", cameraArea.getFarmId()));
        if (brandName > 0) {
            return new Result(100001, "区域名称重复", false);
        }
        cameraAreaService.save(cameraArea);
        return new Result(10000, "添加成功", true);
    }

    @PostMapping("/update")
    public Result update(@RequestBody CameraArea cameraArea) {
        cameraAreaService.updateById(cameraArea);
        int brandName = cameraAreaService.count(new QueryWrapper<CameraArea>().eq("area_name", cameraArea.getAreaName()));
        if (brandName > 1) {
            return new Result(100001, "区域名称重复", false);
        }

        String areaName = cameraArea.getAreaName();
        Camera camera = new Camera();
        if (StringUtils.isNotBlank(areaName)) {
            camera.setAreaName(areaName);
        }
        UpdateWrapper<Camera> wrapper = new UpdateWrapper<>();
        wrapper.eq("area_id", cameraArea.getId());
        cameraService.update(camera, wrapper);


        return new Result(10000, "修改成功", true);
    }

    @GetMapping("/remove")
    public Result remove(@RequestParam(value = "ids") List<Integer> ids) {

        return cameraAreaService.removeIds(ids);

    }


    @GetMapping("/lsitAndCamera")
    public Result lsitAndCamera(@RequestParam(value = "farmId") Integer farmId) {

        return cameraAreaService.lsitAndCamera(farmId);

    }

    @GetMapping("/getUrl")
    public String getUrl(@RequestParam(value = "farmId") Integer farmId, HttpServletRequest request) {

        String ipAddr = IpUtil.getIpAddr(request);
        CameraArea cameraArea = new CameraArea();
        cameraArea.setPublicUrl(ipAddr + ":9080");
        if (0 == farmId){
            cameraArea.setId(67);
            cameraAreaService.updateById(cameraArea);
        }else if (10000 == farmId){
            cameraArea.setId(87);
            cameraAreaService.updateById(cameraArea);
        }else {
            cameraAreaService.update(cameraArea, new QueryWrapper<CameraArea>().eq("farm_id", farmId).ne("id",87));
        }

        return "成功";

    }


}
