package com.huimv.admin.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.huimv.admin.common.utils.Result;
import com.huimv.admin.common.utils.ResultCode;
import com.huimv.admin.entity.CameraBase;
import com.huimv.admin.entity.CameraFunctionArea;
import com.huimv.admin.service.ICameraBaseService;
import com.huimv.admin.service.ICameraFunctionAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

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

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

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

    @PostMapping("/add")
    @Transactional
    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);
//        String camearIds = cameraFunctionArea.getCamearIds();
//        if (StringUtils.isNotBlank(camearIds)){
//            for (String s : camearIds.split(",")) {
//                CameraBase cameraBase = new CameraBase();
//                cameraBase.setId(Integer.parseInt(s));
//                cameraBase.setFunctionId(cameraFunctionArea.getId());
//                cameraBaseService.updateById(cameraBase);
//            }
//        }
        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);
    }


}
