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.CameraArea;
import com.huimv.video.video.entity.CameraFunction;
import com.huimv.video.video.entity.CameraFunctionArea;
import com.huimv.video.video.mapper.CameraAreaMapper;
import com.huimv.video.video.mapper.CameraFunctionAreaMapper;
import com.huimv.video.video.mapper.CameraMapper;
import com.huimv.video.video.service.ICameraAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author astupidcoder
 * @since 2021-09-13
 */
@Service
public class CameraAreaServiceImpl extends ServiceImpl<CameraAreaMapper, CameraArea> implements ICameraAreaService {

    @Autowired
    private CameraMapper cameraMapper;


    @Override
    public Result cameraList(Map<String, Object> params) {

        Integer current = (Integer) params.get("current");
        Integer size = (Integer) params.get("size");
        String areaName = (String) params.get("areaName");
        Integer farmId = (Integer) params.get("farmId");

        IPage<CameraArea> list = page(new Page<>(current, size), new QueryWrapper<CameraArea>().like(StringUtils.isNotBlank(areaName), "area_name", areaName).eq("farm_id", farmId));
        return new Result(ResultCode.SUCCESS, list);
    }

    @Override
    public Result removeIds(List<Integer> ids) {
        for (Integer id : ids) {
            CameraArea byId = this.getById(id);

            Integer areaId = cameraMapper.selectCount(new QueryWrapper<Camera>().eq("area_id", id));
            if (areaId > 0) {
                return new Result(10001, "删除失败,区域" + byId.getAreaName() + "仍然存在摄像头", false);
            }
        }
        this.removeByIds(ids);
        return new Result(10000, "删除成功", true);
    }

    @Override
    public Result lsitAndCamera(Integer farmId) {
        List endList = new ArrayList();

        List<CameraArea> cameraAreaList = this.list(new QueryWrapper<CameraArea>().eq("farm_id", farmId));

        for (CameraArea cameraArea : cameraAreaList) {
            Map map = new HashMap();
            map.put("cameraName", cameraArea.getAreaName());
            map.put("id", cameraArea.getId());
            map.put("cameraList", cameraMapper.selectList(new QueryWrapper<Camera>().eq("area_id", cameraArea.getId())));
            endList.add(map);
        }


        return new Result(ResultCode.SUCCESS, endList);
    }


}