package com.huimv.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.mapper.CameraBaseMapper;
import com.huimv.admin.mapper.CameraBrandMapper;
import com.huimv.admin.service.ICameraBrandService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2023-02-16
 */
@Service
public class CameraBrandServiceImpl extends ServiceImpl<CameraBrandMapper, CameraBrand> implements ICameraBrandService {
    @Autowired
    private CameraBaseMapper cameraMapper;

    @Override
    public Result listPage(Map<String, Object> params) {
        Integer current = (Integer) params.get("current");
        Integer size = (Integer) params.get("size");
        String brandName = (String) params.get("brandName");
        Integer farmId = (Integer) params.get("farmId");
        IPage<CameraBrand> list = page(new Page<>(current, size), new QueryWrapper<CameraBrand>().like(StringUtils.isNotBlank(brandName), "brand_name", brandName).eq("farm_id", farmId));
        return new Result(ResultCode.SUCCESS, list);
    }

    @Override
    public Result removeIds(List<Integer> ids) {
        for (Integer id : ids) {
            Integer integer = cameraMapper.selectCount(new QueryWrapper<CameraBase>().eq("brand_id", id));
            CameraBrand byId = this.getById(id);
            if (integer != 0) {
                return new Result(ResultCode.FAIL, "有" + byId.getBrandName() + "的摄像头在使用,删除失败");
            }
        }
        this.removeByIds(ids);
        return new Result(ResultCode.SUCCESS, "删除成功");
    }

}
