package com.tuoheng.admin.service.iotequipment.query;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tuoheng.admin.conver.IotEquipmentConverMapper;
import com.tuoheng.admin.entity.IotEquipment;
import com.tuoheng.admin.entity.RoadInformation;
import com.tuoheng.admin.entity.Section;
import com.tuoheng.admin.entity.User;
import com.tuoheng.admin.enums.DataPermissionEnum;
import com.tuoheng.admin.enums.MarkEnum;
import com.tuoheng.admin.mapper.DeptMapper;
import com.tuoheng.admin.mapper.IotEquipmentMapper;
import com.tuoheng.admin.mapper.RoadInformationMapper;
import com.tuoheng.admin.mapper.SectionMapper;
import com.tuoheng.admin.request.iotequipment.QueryIotEquipmentListRequest;
import com.tuoheng.admin.request.iotequipment.QueryIotEquipmentPageListRequest;
import com.tuoheng.admin.utils.CurrentUserUtil;
import com.tuoheng.admin.vo.IotEquipmentVo;
import com.tuoheng.common.core.utils.JsonResult;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

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

/**
 * 物联设备 业务类
 *
 * @author wanjing
 * @since 2023-11-25
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class QueryEditIotEquipmentListService {

    private final DeptMapper deptMapper;

    private final IotEquipmentMapper iotEquipmentMapper;

    public JsonResult getList(QueryIotEquipmentListRequest request) {
        User user = CurrentUserUtil.getUserInfo();
        JsonResult result = this.check(request);
        if (0 != result.getCode()) {
            log.info("查询物联设备列表：校验失败：{}", result.getMsg());
            return result;
        }
        // 获取部门Id的查询范围
        List<String> deptIdList = this.getDeptIdList(user);
        request.setDeptIdList(deptIdList);
        request.setTenantId(user.getTenantId());
        List<IotEquipment> iotEquipmentList = iotEquipmentMapper.selectList(request);
        List<IotEquipmentVo> iotEquipmentVoList = this.buildIotEquipmentVoList(iotEquipmentList);
        return JsonResult.success(iotEquipmentVoList);
    }

    /**
     * 检查参数
     *
     * @param request
     * @return
     */
    private JsonResult check(QueryIotEquipmentListRequest request) {
        return JsonResult.success();
    }

    /**
     * 获取部门Id的查询范围
     *
     * @param user
     * @return
     */
    private List<String> getDeptIdList(User user) {
        List<String> deptIdList = new ArrayList<>();
        if (DataPermissionEnum.ALL.getCode() == user.getDataPermission()) {
            return null;
        } else if (DataPermissionEnum.DEPT_AND_SUB_DEPT.getCode() == user.getDataPermission()) {
            deptIdList = deptMapper.selectAllChildListById(user.getDeptId());
        } else if (DataPermissionEnum.DEPT.getCode() == user.getDataPermission()) {
            deptIdList.add(user.getDeptId());
        }
        return deptIdList;
    }

    private List<IotEquipmentVo> buildIotEquipmentVoList(List<IotEquipment> iotEquipmentList) {
        if (CollectionUtil.isEmpty(iotEquipmentList)) {
            return null;
        }
        List<IotEquipmentVo> iotEquipmentVoList = IotEquipmentConverMapper.INSTANCE.fromIotEquipmentToIotEquipmentVoList(iotEquipmentList);
        return iotEquipmentVoList;
    }
}
