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.common.utils.ResultCode;
import com.huimv.admin.entity.CameraBase;
import com.huimv.admin.entity.CameraBrand;
import com.huimv.admin.service.ICameraBaseService;
import com.huimv.admin.service.ICameraBrandService;
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("/cameraBrand")
public class CameraBrandController {
    @Autowired
    private ICameraBrandService cameraBrandService;
    @Autowired
    private ICameraBaseService cameraService;

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

    @PostMapping("/add")
    public Result add(@RequestBody CameraBrand cameraBrand) {
        int brandName = cameraBrandService.count(new QueryWrapper<CameraBrand>().eq("brand_name", cameraBrand.getBrandName()).eq("farm_id", cameraBrand.getFarmId()));
        if (brandName > 0) {
            return new Result(10001, "品牌名称重复", false);
        }

        cameraBrandService.save(cameraBrand);
        return new Result(10000, "添加成功", true);
    }

    @PostMapping("/update")
    public Result update(@RequestBody CameraBrand cameraArea) {
        cameraBrandService.updateById(cameraArea);
        int brandName = cameraBrandService.count(new QueryWrapper<CameraBrand>().eq("brand_name", cameraArea.getBrandName()));
        if (brandName > 1) {
            return new Result(10001, "品牌名称重复", false);
        }
        String areaName = cameraArea.getBrandName();
        CameraBase camera = new CameraBase();
        if (StringUtils.isNotBlank(areaName)) {
            camera.setBrandName(areaName);
        }
        UpdateWrapper<CameraBase> wrapper = new UpdateWrapper<>();
        wrapper.eq("brand_id", cameraArea.getId());
        cameraService.update(camera, wrapper);

        return new Result(ResultCode.SUCCESS, "修改成功");
    }

    @GetMapping("/remove")
    public Result remove(@RequestParam(value = "ids") List<Integer> ids) {
        return cameraBrandService.removeIds(ids);
    }

}
