package com.huimv.admin.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.huimv.admin.common.utils.Result;
import com.huimv.admin.common.utils.ResultCode;
import com.huimv.admin.entity.CameraFunctionArea;
import com.huimv.admin.entity.vo.TreeBasePigpen;
import com.huimv.admin.entity.vo.TreeCameraFuntionArea;
import com.huimv.admin.mapper.CameraFunctionAreaMapper;
import com.huimv.admin.mapper.CameraFunctionMapper;
import com.huimv.admin.service.ICameraFunctionAreaService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2023-02-17
 */
@Service
public class CameraFunctionAreaServiceImpl extends ServiceImpl<CameraFunctionAreaMapper, CameraFunctionArea> implements ICameraFunctionAreaService {
    @Autowired
    private CameraFunctionAreaMapper cameraFunctionAreaMapper;

    @Autowired
    private CameraFunctionMapper cameraFunctionMapper;

    @Override
    public Result cameraList(Map<String, Object> params) {
        String functionaName = (String) params.get("functionaName");
        String farmId = (String) params.get("farmId");
        if (farmId.equals("21")){
            farmId = "22";
        }
        QueryWrapper<CameraFunctionArea> wrapper = new QueryWrapper<>();
        wrapper.eq("farm_id",farmId);
        wrapper.like(StringUtils.isNotBlank(functionaName),"function_name",functionaName);
        List<CameraFunctionArea> list = this.list(wrapper);
        //将结果List改为树
        List<TreeCameraFuntionArea> treeBasePigpens = parseBizBaseArea(list);
        return new Result(ResultCode.SUCCESS,treeBasePigpens);
    }

    private List<TreeCameraFuntionArea> parseBizBaseArea(List<CameraFunctionArea> list) {
        //构建需要展示的树形节点结构
        Map<String, TreeCameraFuntionArea> nodeMap = list.stream().map(cameraFunctionArea -> {
            TreeCameraFuntionArea  baseVo = new TreeCameraFuntionArea();
            baseVo.setId(cameraFunctionArea.getId()+"");
            baseVo.setFarmId(cameraFunctionArea.getFarmId());
            baseVo.setParentId(cameraFunctionArea.getParentId());
            baseVo.setCamearIds(cameraFunctionArea.getCamearIds());
            baseVo.setFunctionName(cameraFunctionArea.getFunctionName());
            baseVo.setFunctionIds(cameraFunctionArea.getFunctionIds());
            baseVo.setFunctionType(cameraFunctionArea.getFunctionType());
            baseVo.setRemark(cameraFunctionArea.getRemark());
            baseVo.setChildNode(new ArrayList<>());
            return baseVo;
        }).collect(Collectors.toMap(TreeCameraFuntionArea::getId, b -> b,(k1, k2)->k1));
        //创建数组存父亲节点
        ArrayList<TreeCameraFuntionArea> roots = new ArrayList<>();
        //构建树形
        nodeMap.values().forEach(item->{
            String parentCode = item.getParentId()+"";
            if (nodeMap.get(parentCode) == null){
                //父节点为空，说明当前节点就已经是父节点了 将该节点存起来
                roots.add(item);
            }else {
                //父节点 不为空，说明有父节点，拿到该节点的父节点的孩子节点（就是我自己),将该节点存起来
                nodeMap.get(parentCode).getChildNode().add(item);
            }
        });
        return roots;
    }

    @Override
    @Transactional
    public Result removeIds(List<Integer> ids) {
        for (Integer id : ids) {
            CameraFunctionArea byId = this.getById(id);
            if (StringUtils.isNotBlank(byId.getFunctionIds()) ) {
                return new Result(10001, "删除失败," + byId.getFunctionName() + "仍然存在区域", false);
            }
        }
        this.removeByIds(ids);
        return new Result(10000, "删除成功", true);
    }

    @Override
    public Result addFunction(Map<String, String> params) {
        String functionAreaId = params.get("functionAreaId");
        String functionIds = params.get("functionIds");
        CameraFunctionArea cameraFunctionArea = new CameraFunctionArea();
        cameraFunctionArea.setId(Integer.parseInt(functionAreaId));
        cameraFunctionArea.setFunctionIds(functionIds);
        this.updateById(cameraFunctionArea);
        return new Result(10000, "添加成功", true);
    }
}
