package com.ruoyi.ibs.draw.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.ibs.draw.domain.dto.draw.LayerEditInfoDTO;
import com.ruoyi.ibs.draw.domain.dto.draw.LayerGridDTO;
import com.ruoyi.ibs.draw.domain.dto.draw.LayerListDTO;
import com.ruoyi.ibs.draw.domain.entity.DrawGrid;
import com.ruoyi.ibs.draw.domain.entity.DrawLayer;
import com.ruoyi.ibs.draw.domain.entity.DrawLayerAudit;
import com.ruoyi.ibs.draw.domain.entity.DrawShape;
import com.ruoyi.ibs.draw.domain.vo.DrawLayerVO;
import com.ruoyi.ibs.draw.domain.vo.LayerRuleVO;
import com.ruoyi.ibs.draw.domain.vo.MapCustCountVO;
import com.ruoyi.ibs.draw.mapper.DrawGridMapper;
import com.ruoyi.ibs.draw.mapper.DrawLayerAuditMapper;
import com.ruoyi.ibs.draw.mapper.DrawLayerMapper;
import com.ruoyi.ibs.draw.mapper.DrawShapeMapper;
import com.ruoyi.ibs.draw.service.DrawLayerService;
import com.ruoyi.ibs.draw.service.DrawShapeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Author 吴凯程
 * @Date 2024/12/20
 **/
@Service
public class DrawLayerServiceImpl implements DrawLayerService {

    @Resource
    private DrawLayerMapper drawLayerMapper;

    @Resource
    private DrawShapeService drawShapeService;

    @Resource
    private DrawShapeMapper drawShapeMapper;

    @Resource
    private DrawGridMapper drawGridMapper;

    @Resource
    private DrawLayerAuditMapper drawLayerAuditMapper;


    /**
     * @param layerName
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addLayer(String layerName, String layerColor) {
        isLayerNameValid(layerName);
//        LambdaQueryWrapper<DrawLayer> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(DrawLayer::getLayerColor, layerColor)
//                .eq(DrawLayer::getDeptId, SecurityUtils.getDeptId());
//        if (drawLayerMapper.selectCount(queryWrapper) > 0) {
//            throw new ServiceException("该颜色已被使用");
//        }
        DrawLayer drawLayer = new DrawLayer();
        drawLayer.setLayerName(layerName);
        drawLayer.setLayerColor(layerColor);
        drawLayerMapper.insert(drawLayer);
        return "特色图层创建成功";
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String editLayerInfo(LayerEditInfoDTO layerEditInfoDTO) {
        DrawLayer drawLayer = drawLayerMapper.selectById(layerEditInfoDTO.getLayerId());
        if (Objects.isNull(drawLayer)) {
            throw new ServiceException("该特色图层不存在");
        }
        if (!drawLayer.getLayerName().equals(layerEditInfoDTO.getLayerName())) {
            isLayerNameValid(layerEditInfoDTO.getLayerName());
            drawLayer.setLayerName(layerEditInfoDTO.getLayerName());
        }
        if (!drawLayer.getLayerColor().equals(layerEditInfoDTO.getLayerColor())) {
            if (!checkLayerColorValid(layerEditInfoDTO.getLayerColor())) {
                throw new ServiceException("该颜色已被使用");
            }
            drawLayer.setLayerColor(layerEditInfoDTO.getLayerColor());
        }
        drawLayerMapper.updateById(drawLayer);
        return "特色区域信息修改成功";
    }

    @Override
    public boolean checkLayerNameValid(String layerName) {
        LambdaQueryWrapper<DrawLayer> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DrawLayer::getLayerName, layerName)
                .eq(DrawLayer::getDeptId, SecurityUtils.getDeptId());
        return drawLayerMapper.selectCount(queryWrapper) < 1;
    }

    /**
     * @param layerColor
     * @return
     */
    @Override
    public boolean checkLayerColorValid(String layerColor) {
        LambdaQueryWrapper<DrawLayer> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DrawLayer::getLayerColor, layerColor)
                .eq(DrawLayer::getDeptId, SecurityUtils.getDeptId());
        return drawLayerMapper.selectCount(queryWrapper) < 1;
    }

    private void isLayerNameValid(String layerName) {
        LambdaQueryWrapper<DrawLayer> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DrawLayer::getLayerName, layerName)
                .eq(DrawLayer::getDeptId, SecurityUtils.getDeptId());
        if (drawLayerMapper.selectCount(queryWrapper) > 0) {
            throw new ServiceException("图层名称已存在");
        }
    }

    /**
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String deleteLayer(List<Long> deleteIds) {
        deleteIds.forEach(layerId -> {
            DrawLayer drawLayer = drawLayerMapper.selectById(layerId);
            if (Objects.isNull(drawLayer)) {
                throw new ServiceException("该特色图层不存在:" + layerId);
            }
            LambdaQueryWrapper<DrawShape> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(DrawShape::getLayerId, layerId);
            List<Long> shapeIds = drawShapeMapper.selectList(queryWrapper).stream().map(DrawShape::getShapeId).collect(Collectors.toList());
            //删除该图层下所有待审批的消息
            drawLayerAuditMapper.delete(new LambdaQueryWrapper<DrawLayerAudit>().eq(DrawLayerAudit::getLayerId,layerId));
            drawShapeService.deleteShape(shapeIds);
            drawLayerMapper.deleteById(layerId);
        });
        return "图层删除成功";
    }

    /**
     * @param
     * @return
     */
    @Override
    public List<DrawLayerVO> getDrawLayerList(LayerListDTO layerListDTO) {
//        LambdaQueryWrapper<DrawLayer> queryWrapper = new LambdaQueryWrapper<>();
////        queryWrapper.eq(DrawLayer::getDeptId, SecurityUtils.getDeptId());
//        //改为同一机构下查看所有图层
//        queryWrapper.likeRight(DrawLayer::getDeptId, SecurityUtils.getDeptId().toString().substring(0,3));
//        if (Objects.nonNull(layerListDTO.getLayerName())) {
//            queryWrapper.like(DrawLayer::getLayerName, layerListDTO.getLayerName());
//        }
//        queryWrapper.orderByDesc(DrawLayer::getLayerId);
//        List<DrawLayer> drawLayers = drawLayerMapper.selectList(queryWrapper);
//        List<DrawLayerVO> collect = drawLayers.stream().map(DrawLayerVO::new).collect(Collectors.toList());


        String deptHead = SecurityUtils.getDeptId().toString().substring(0, 3);
        //  增加：disabled  如果图层下无图形，则高亮
        return drawLayerMapper.getDrawLayerList(deptHead, SecurityUtils.getDeptId(),layerListDTO.getLayerName());
    }

    /**
     * @param opsDept
     * @return
     */
    @Override
    public List<DrawLayerVO> getValidDrawLayerList(String opsDept) {
        String deptHead = SecurityUtils.getDeptId().toString().substring(0, 3);
        return drawLayerMapper.getValidDrawLayerList(deptHead, opsDept, SecurityUtils.getDeptId(), SecurityUtils.getUsername());
    }

    @Override
    public List<DrawGrid> getDrawGridList(LayerGridDTO layerGridDTO) {
        layerGridDTO.setDeptId(SecurityUtils.getDeptId());
        return drawGridMapper.getGridListByOpsAndLayer(layerGridDTO);
    }

    @Override
    public List<LayerRuleVO> getLayerList(String opsDept) {
        String deptId = SecurityUtils.getDeptId().toString().substring(0,3);
        HashMap<String,String> paramMap = new HashMap<>();
        paramMap.put("opsDept",opsDept);
        paramMap.put("deptId",deptId);
        List<LayerRuleVO> list = drawLayerMapper.getLayerList(paramMap);
        return list;
    }

    @Override
    public List<MapCustCountVO> queryBusinessFreqList() {
        return drawLayerMapper.queryBusinessFreqList();
    }


}
