package com.huimv.admin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ObjectUtil;
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.CameraArea;
import com.huimv.admin.entity.CameraBase;
import com.huimv.admin.entity.EnvDevice;
import com.huimv.admin.entity.hkwsdto.RegionDetailDto;
import com.huimv.admin.entity.hkwsdto.UnitDto1;
import com.huimv.admin.entity.zengxindto.DataShacks;
import com.huimv.admin.mapper.CameraAreaMapper;
import com.huimv.admin.mapper.CameraBaseMapper;
import com.huimv.admin.service.ICameraAreaService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
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;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2023-02-16
 */
@Service
public class CameraAreaServiceImpl extends ServiceImpl<CameraAreaMapper, CameraArea> implements ICameraAreaService {
    @Autowired
    private CameraBaseMapper 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");
        String  farmId = (String) 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<CameraBase>().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<CameraBase>().eq("area_id", cameraArea.getId())));
            endList.add(map);
        }


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

    @Override
    public void sync(Map<String, Integer> params,List<UnitDto1> unitDto1) {
        Integer farmId = params.get("farmId");
        QueryWrapper<CameraArea> wrapper = new QueryWrapper<>();
        //TODO 正康猪业 先这样做
        wrapper.eq("farm_id",farmId);
        List<CameraArea> huatong = this.list(wrapper);
        List<String> haikangAreaId = unitDto1.stream().map(UnitDto1::getRegionUuid).collect(Collectors.toList());
        List<String> huatongAreaId = huatong.stream().map(CameraArea::getRegionUuid).collect(Collectors.toList());
        CopyOnWriteArrayList<String> haikangDeviceIdCopy = ListUtil.toCopyOnWriteArrayList(haikangAreaId);

        CopyOnWriteArrayList<String> huatongDeviceIdCopy = ListUtil.toCopyOnWriteArrayList(huatongAreaId);
        //新增的设备
        haikangDeviceIdCopy.removeAll(huatongAreaId);
        //TODO 新增 先循环，想不出来
        if (haikangDeviceIdCopy.size() >0){
            for (String s : haikangDeviceIdCopy) {
                for (UnitDto1 dto1 : unitDto1) {
                    if (s.equals(dto1.getRegionUuid())){
                        CameraArea cameraArea  = new CameraArea();
                        BeanUtil.copyProperties(dto1,cameraArea);
                        cameraArea.setAreaName(dto1.getName());
                        cameraArea.setFarmId(farmId);
                        cameraArea.setPublicUrl("61.153.248.106:6713");
                        this.save(cameraArea);
                    }
                }

            }
        }
        //需要删除的设备
        huatongDeviceIdCopy.removeAll(haikangAreaId);
        if (huatongDeviceIdCopy.size() >0){
            this.remove(new QueryWrapper<CameraArea>().in("region_uuid",huatongDeviceIdCopy));
        }
    }

    @Override
    public void NewSync(Map<String, Integer> params, List<RegionDetailDto> regionDetailDto) {
        Integer farmId = params.get("farmId");
        //同步，先删除
        QueryWrapper<CameraArea> wrapper = new QueryWrapper<>();
        //TODO 浦江猪业(新平台) 先这样做
        wrapper.eq("farm_id",farmId);
        List<CameraArea> huatong = this.list(wrapper);
        List<String> HikAreaId = regionDetailDto.stream().map(RegionDetailDto::getIndexCode).collect(Collectors.toList());
        List<String> HtAreaId = huatong.stream().map(CameraArea::getRegionUuid).collect(Collectors.toList());
        CopyOnWriteArrayList<String> HikArea = ListUtil.toCopyOnWriteArrayList(HikAreaId);
        CopyOnWriteArrayList<String> HtArea = ListUtil.toCopyOnWriteArrayList(HtAreaId);
        //新增的设备
        HikArea.removeAll(HtAreaId);
        //TODO 新增
        if (HikArea.size() >0){
            for (String s : HikArea) {
                for (RegionDetailDto dto : regionDetailDto) {
                    if (s.equals(dto.getIndexCode())){
                        CameraArea cameraArea = new CameraArea();
                        cameraArea.setAreaName(dto.getName());
                        cameraArea.setCreateTime(dto.getCreateTime());
                        cameraArea.setUpdateTime(dto.getUpdateTime());
                        cameraArea.setRegionUuid(dto.getIndexCode());
                        cameraArea.setParentUuid(dto.getParentIndexCode());
                        cameraArea.setFarmId(farmId);
//                        cameraArea.setPublicUrl("61.153.248.109");
                        cameraArea.setPublicUrl("www.htzj.fun");
                        this.save(cameraArea);
                    }
                }
            }
        }
        //需要删除的设备
        HtArea.removeAll(HikArea);
        if (HtArea.size() >0){
            this.remove(new QueryWrapper<CameraArea>().in("region_uuid",HtArea));
        }

    }


}
