package com.zksy.utils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zksy.base.domain.EquipmentMaintain;
import com.zksy.base.domain.EquipmentStatus;
import com.zksy.base.service.EquipmentMaintainService;
import com.zksy.base.service.EquipmentStatusService;
import com.zksy.common.core.domain.entity.SysUser;
import com.zksy.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
//
@Component
public class DevicePhoneFetchUtil {

    //换了一种方式注入，待测试
    @Autowired
    private  EquipmentMaintainService equipmentMaintainService;
    @Autowired
    private  EquipmentStatusService equipmentStatusService;
    @Autowired
    private  ISysUserService sysUserService;

//    private final EquipmentMaintainService equipmentMaintainService;
//    private final EquipmentStatusService equipmentStatusService;
//    private final ISysUserService sysUserService;
//
//    public DevicePhoneFetchUtil(EquipmentMaintainService equipmentMaintainService,
//                                EquipmentStatusService equipmentStatusService,
//                                ISysUserService sysUserService) {
//        this.equipmentMaintainService = equipmentMaintainService;
//        this.equipmentStatusService = equipmentStatusService;
//        this.sysUserService = sysUserService;
//    }

    /**
     * 根据设备ID获取所有关联手机号
     * @param deviceId 设备ID
     * @return 去重后的有效手机号列表
     */
    public List<String> getPhoneListByDeviceId(String deviceId) {
        Set<String> phoneSet = new HashSet<>();

        //获取维护表中的手机号
        addAllMaintainPersonPhones(phoneSet, deviceId);
        //获取用户表的手机号
        addUserPhoneByStatus(phoneSet, deviceId);
        return phoneSet.stream()
                .filter(this::isValidPhone)
                .collect(Collectors.toList());
    }

    /**
     * 收集该设备所有维护记录中的维护人手机号
     */
    private void addAllMaintainPersonPhones(Set<String> phoneSet, String deviceId) {
        LambdaQueryWrapper<EquipmentMaintain> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EquipmentMaintain::getEquipmentId, deviceId);

        List<EquipmentMaintain> maintainList = equipmentMaintainService.list(queryWrapper);
        if (maintainList.isEmpty()) {
            return;
        }
        maintainList.forEach(maintain -> {
            String phone = String.valueOf(maintain.getMaintainPersonPhone());
            phoneSet.add(phone);
        });
    }

    /**
     * 从设备状态表关联用户表获取负责人手机号
     */
    private void addUserPhoneByStatus(Set<String> phoneSet, String deviceId) {

        LambdaQueryWrapper<EquipmentStatus> statusWrapper = new LambdaQueryWrapper<>();
        statusWrapper.eq(EquipmentStatus::getEquipmentId, deviceId);
        EquipmentStatus equipmentStatus = equipmentStatusService.getOne(statusWrapper);

        if (equipmentStatus == null || equipmentStatus.getUserId() == null) {
            return;
        }
        SysUser sysUser = sysUserService.selectUserById(equipmentStatus.getUserId());

        //0代表存在的用户
        if (sysUser != null && sysUser.getPhonenumber() != null && sysUser.getDelFlag().equals("0")) {
            phoneSet.add(sysUser.getPhonenumber());
        }
    }

    /**
     * 校验手机号格式（11位数字）
     */
    private boolean isValidPhone(String phone) {
        return phone != null && phone.matches("^1[3-9]\\d{9}$");
    }
}
