package com.huimv.video.video.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.huimv.common.utils.Result;
import com.huimv.common.utils.ResultCode;
import com.huimv.video.video.entity.CameraFunction;
import com.huimv.video.video.entity.CameraFunctionArea;
import com.huimv.video.video.service.ICameraFuntcionAreaService;
import com.huimv.video.video.service.ICameraFuntcionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author astupidcoder
 * @since 2021-09-13
 */
@RestController
@RequestMapping("/cameraFunctionArea")
public class CameraFunctionAreaController {
    @Autowired
    private ICameraFuntcionAreaService cameraFuntcionAreaService;


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

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

    @PostMapping("/update")
    public Result update(@RequestBody CameraFunctionArea cameraFunctionArea) {
        cameraFuntcionAreaService.updateById(cameraFunctionArea);
        int brandName = cameraFuntcionAreaService.count(new QueryWrapper<CameraFunctionArea>().eq("function_name", cameraFunctionArea.getFunctionName()));
        if (brandName > 1) {
            return new Result(100001, "区域名称重复", false);
        }

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

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

        return cameraFuntcionAreaService.removeIds(ids);

    }

    @PostMapping("/addFunction")
    public Result addFunction(@RequestBody Map<String, String> params) {

        return cameraFuntcionAreaService.addFunction(params);

    }

    @PostMapping("/getFunctionById")
    public Result getFunctionByid(@RequestBody Map<String, String> params) {
        String id = params.get("id");
        CameraFunctionArea byId = cameraFuntcionAreaService.getById(id);
        String functionIds = byId.getFunctionIds();
        if (StringUtils.isNotBlank(functionIds)){
            return new Result(ResultCode.SUCCESS,functionIds.split(","));

        }
        return new Result(ResultCode.SUCCESS,null);

    }


}
