package com.huimv.admin.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.admin.common.utils.Result;
import com.huimv.admin.entity.CameraArea;
import com.huimv.admin.entity.CameraBase;
import com.huimv.admin.service.ICameraAreaService;
import com.huimv.admin.service.ICameraBaseService;
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 author
 * @since 2023-02-16
 */
@CrossOrigin
@RestController
@RequestMapping("/admin/cameraArea")
public class CameraAreaController {
    @Autowired
    private ICameraAreaService cameraAreaService;

    @Autowired
    private ICameraBaseService 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();
        CameraBase camera = new CameraBase();
        if (StringUtils.isNotBlank(areaName)) {
            camera.setAreaName(areaName);
        }
        UpdateWrapper<CameraBase> 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);

    }


}
