package com.sooka.sponest.monitor.LabelManagement.service.impl;


import com.alibaba.nacos.common.utils.CollectionUtils;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.sooka.sponest.monitor.LabelManagement.domain.CentermonitorCamera;
import com.sooka.sponest.monitor.LabelManagement.domain.CentermonitorSetLabel;
import com.sooka.sponest.monitor.LabelManagement.domain.LabelType;
import com.sooka.sponest.monitor.LabelManagement.mapper.CentermonitorSetLabelServiceMapper;
import com.sooka.sponest.monitor.LabelManagement.service.CentermonitorSetLabelService;
import com.sooka.sponest.monitor.remoteapi.service.center.system.RemoteService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CentermonitorSetLabelServicempl implements CentermonitorSetLabelService {

    @Resource
    private CentermonitorSetLabelServiceMapper centermonitorSetLabelServiceMapper;
    @Resource
    private RemoteService remoteService;

    /**
     * 查询标签列表
     */
    @Override
    public List<CentermonitorSetLabel> getLabel(CentermonitorSetLabel centermonitorSetLabel) {
        return centermonitorSetLabelServiceMapper.getLabel(centermonitorSetLabel);
    }

    /**
     * 新增标签
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult addLabel(CentermonitorSetLabel centermonitorSetLabel) {
        String one = null;
        String two = centermonitorSetLabelServiceMapper.getLabelsByParentId(centermonitorSetLabel.getParentId());
        if (StringUtils.isNotEmpty(two)) {
            one = centermonitorSetLabelServiceMapper.getLabelsByParentId(two);
        }
        if (!centermonitorSetLabel.getParentId().equals("0") && StringUtils.isNotEmpty(one) && !one.equals("0")) {
            return AjaxResult.error("标签层级只能添加到三级");
        } else {
            if (centermonitorSetLabel.getParentId().equals("0")) {
                centermonitorSetLabel.setLevel("1");
            } else if (two.equals("0")) {
                centermonitorSetLabel.setLevel("2");
            } else {
                centermonitorSetLabel.setLevel("3");
            }
            if (StringUtils.isNotEmpty(centermonitorSetLabel.getImgUrl())) {
                centermonitorSetLabel.setImgUrl(centermonitorSetLabel.getImgUrl().substring(centermonitorSetLabel.getImgUrl().indexOf("group1")));
            }
            int r = centermonitorSetLabelServiceMapper.addLabel(centermonitorSetLabel);
            for (int i = 0; i < centermonitorSetLabel.getLabelTypeList().size(); i++) {
                LabelType labelType = centermonitorSetLabel.getLabelTypeList().get(i);
                labelType.setLabelId(centermonitorSetLabel.getId());
                r = centermonitorSetLabelServiceMapper.insertlabelType(labelType);
            }
            return AjaxResult.success("新增成功！");
        }
    }

    /**
     * 修改标签
     */
    @Override
    public AjaxResult UpdateLabel(CentermonitorSetLabel centermonitorSetLabel) {
        String one = null;
        String two = centermonitorSetLabelServiceMapper.getLabelsByParentId(centermonitorSetLabel.getParentId());
        if (StringUtils.isNotEmpty(two)) {
            one = centermonitorSetLabelServiceMapper.getLabelsByParentId(two);
        }
        if (!centermonitorSetLabel.getParentId().equals("0") && StringUtils.isNotEmpty(one) && !one.equals("0")) {
            return AjaxResult.error("标签层级只能添加到三级");
        } else {
            if (centermonitorSetLabel.getParentId().equals("0")) {
                centermonitorSetLabel.setLevel("1");
            } else if (two.equals("0")) {
                centermonitorSetLabel.setLevel("2");
            } else {
                centermonitorSetLabel.setLevel("3");
            }
            String ids = centermonitorSetLabel.getId();
            int id = Integer.parseInt(ids);
            centermonitorSetLabelServiceMapper.deletelabeltype(id);
            if (StringUtils.isNotEmpty(centermonitorSetLabel.getImgUrl())) {
                centermonitorSetLabel.setImgUrl(centermonitorSetLabel.getImgUrl().substring(centermonitorSetLabel.getImgUrl().indexOf("group1")));
            }
            int r = centermonitorSetLabelServiceMapper.UpdateLabel(centermonitorSetLabel);
            for (int i = 0; i < centermonitorSetLabel.getLabelTypeList().size(); i++) {
                LabelType labelType = centermonitorSetLabel.getLabelTypeList().get(i);
                labelType.setLabelId(centermonitorSetLabel.getId());
                r = centermonitorSetLabelServiceMapper.insertlabelType(labelType);
            }
            return AjaxResult.success("修改成功！");
        }
    }

    /**
     * 查询设置标签修改页
     */
    @Override
    public CentermonitorSetLabel getLabels(Integer id) {
        R<String> url = remoteService.selectConfigKey_fileUrl();
        CentermonitorSetLabel camera = centermonitorSetLabelServiceMapper.getLabels(id);
        if (ObjectUtils.isNotEmpty(camera.getImgUrl())) {
            camera.setImgUrl(url.getData() + camera.getImgUrl());
        }
        camera.setLabelTypeList(centermonitorSetLabelServiceMapper.SelectByIdlabelType(id));
        return camera;
    }

    /**
     * 设置标签页删除
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public AjaxResult deletelabel(Integer id) {
        CentermonitorSetLabel label = centermonitorSetLabelServiceMapper.getCameraByLabelIdAndParentId(String.valueOf(id));
        if (ObjectUtils.isNotEmpty(label) && label.getLevel().equals("2")) {
            CentermonitorSetLabel centermonitorSetLabel = centermonitorSetLabelServiceMapper.getCameraByLabelIdAndParentId(String.valueOf(label.getId()));
            if (ObjectUtils.isNotEmpty(centermonitorSetLabel)) {
                int count = centermonitorSetLabelServiceMapper.getCentermonitorLabelMappingLabelId(centermonitorSetLabel.getId());
                if (count > 0) {
                    return AjaxResult.error("该标签下三级标签已绑定设备，不允许删除");
                }
            }
        } else if (ObjectUtils.isNotEmpty(label) && label.getLevel().equals("3")) {
            int count = centermonitorSetLabelServiceMapper.getCentermonitorLabelMappingLabelId(label.getId());
            if (count > 0) {
                return AjaxResult.error("该标签下三级标签已绑定设备，不允许删除");
            }
        }
        CentermonitorSetLabel camera = centermonitorSetLabelServiceMapper.getLabels(id);
        if (camera.getLevel().equals("1")) {
            //删除二级标签
            List<CentermonitorSetLabel> list = centermonitorSetLabelServiceMapper.getCameraByParentId(String.valueOf(id));
            if (CollectionUtils.isNotEmpty(list)) {
                for (CentermonitorSetLabel item : list) {
                    centermonitorSetLabelServiceMapper.deletelabelByParentId(Integer.valueOf(item.getId()));
                }
            }
        }
        //删除三级标签
        centermonitorSetLabelServiceMapper.deletelabelByParentId(id);
        int resultLabel = centermonitorSetLabelServiceMapper.deletelabel(id);
        centermonitorSetLabelServiceMapper.deletelabeltype(id);
        if (resultLabel > 0) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error("该标签已绑定设备，不允许删除");
        }
    }

    /**
     * 设置标签页查询
     */
    @Override
    public List<CentermonitorSetLabel> Searchtags(String name) {
        return centermonitorSetLabelServiceMapper.Searchtags(name);
    }

    /**
     * 查询摄像头列表
     */
    @Override
    public List<CentermonitorSetLabel> getdevice(CentermonitorSetLabel centermonitorSetLabel) {
        return centermonitorSetLabelServiceMapper.getdevice(centermonitorSetLabel);
    }

    @Override
    public AjaxResult addCamera(CentermonitorCamera centermonitorCamera) {
        centermonitorSetLabelServiceMapper.addCamera(centermonitorCamera.getCentermonitorCameraDTOList(), centermonitorCamera.getLabelId());
        return AjaxResult.success();
    }

    @Override
    public List<CentermonitorSetLabel> getCameraByLabelId(CentermonitorSetLabel centermonitorSetLabel) {
        return centermonitorSetLabelServiceMapper.getCameraByLabelId(centermonitorSetLabel);
    }

    @Override
    public Integer removeCameraByLabelId(CentermonitorSetLabel centermonitorSetLabel) {
        return centermonitorSetLabelServiceMapper.removeCameraByLabelId(centermonitorSetLabel);
    }

    /**
     * 查询全部标签接口
     */
    @Override
    public List<CentermonitorSetLabel> queryLabels(CentermonitorSetLabel centermonitorSetLabel) {
        return centermonitorSetLabelServiceMapper.queryLabels(centermonitorSetLabel);
    }

    @Override
    public int updatePresetPoint(CentermonitorSetLabel centermonitorSetLabel) {
        if (StringUtils.isNotEmpty(centermonitorSetLabel.getPresetPointCode())) {
            return centermonitorSetLabelServiceMapper.updatePresetPoint(centermonitorSetLabel);
        } else {
            return 1;
        }
    }

    @Override
    public CentermonitorSetLabel getCameraByIdAndDeviceId(CentermonitorSetLabel centermonitorSetLabel) {
        return centermonitorSetLabelServiceMapper.getCameraByIdAndDeviceId(centermonitorSetLabel);
    }
}



