package com.huimv.video.video.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.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huimv.common.utils.Result;
import com.huimv.common.utils.ResultCode;
import com.huimv.video.video.entity.Camera;
import com.huimv.video.video.entity.CameraBrand;
import com.huimv.video.video.mapper.CameraBrandMapper;
import com.huimv.video.video.mapper.CameraMapper;
import com.huimv.video.video.service.ICameraBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author astupidcoder
 * @since 2021-09-13
 */
@Service
public class CameraBrandServiceImpl extends ServiceImpl<CameraBrandMapper, CameraBrand> implements ICameraBrandService {

    @Autowired
    private CameraMapper 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<Camera>().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, "删除成功");
    }
}
