package cn.iocoder.yudao.module.pms.job;

import cn.iocoder.yudao.framework.common.util.number.NumberUtils;
import cn.iocoder.yudao.framework.datapermission.core.annotation.DataPermission;
import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.framework.tenant.core.job.TenantJob;
import cn.iocoder.yudao.module.pms.ThingsModelDTO;
import cn.iocoder.yudao.module.pms.controller.admin.TableDataInfo;
import cn.iocoder.yudao.module.pms.controller.admin.vo.IotDevicePageReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.IotDeviceDO;
import cn.iocoder.yudao.module.pms.dal.mysql.IotDeviceMapper;
import com.alibaba.fastjson.JSON;
import com.xingyuv.captcha.util.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

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

@Component
public class MapLngLatJob implements JobHandler {
    @Resource
    private IotDeviceMapper iotDeviceMapper;
    @Resource
    private RestTemplate restTemplate;
    @Value("${yanfan.url}")
    private String yanfanUrl;

    @Override
    @TenantIgnore
    @DataPermission(enable = false)
    public String execute(String param) throws Exception {
        IotDevicePageReqVO page = new IotDevicePageReqVO();
        List<IotDeviceDO> iotDeviceDOS = iotDeviceMapper.selectDataCollect(page);
        iotDeviceDOS.forEach(deviceDO -> {
            if (Objects.nonNull(deviceDO.getYfDeviceId())) {
                TableDataInfo tableDataInfo = restTemplate.getForObject(yanfanUrl + "/prod-api/iot/device/listThingsModel?deviceId="+deviceDO.getYfDeviceId()+"&pageNum=1&pageSize=200", TableDataInfo.class);
                if (Objects.isNull(tableDataInfo)) {
                    return;
                }
                List<ThingsModelDTO> rows = JSON.parseArray(JSON.toJSONString(tableDataInfo.getRows()), ThingsModelDTO.class);
                String lac = "";
                ThingsModelDTO lacDTO = rows.stream().filter(e -> "lac".equals(e.getIdentifier())).findFirst().orElse(null);
                if (Objects.nonNull(lacDTO)) {
                    lac = lacDTO.getValue();
                }
                String cellId = "";
                ThingsModelDTO cellDTO = rows.stream().filter(e -> "cell_id".equals(e.getIdentifier())).findFirst().orElse(null);
                if (Objects.nonNull(cellDTO)) {
                    cellId = cellDTO.getValue();
                }
                String mcc = "";
                ThingsModelDTO mccDTO = rows.stream().filter(e -> "mcc_mnc".equals(e.getIdentifier())).findFirst().orElse(null);
                if (Objects.nonNull(mccDTO)) {
                    mcc = mccDTO.getValue();
                }
                if (StringUtils.isNotBlank(cellId)&&StringUtils.isNotBlank(mcc)&&StringUtils.isNotBlank(lac)) {
                    int lacTen = NumberUtils.hexToDecimal(lac);
                    int cellTen = NumberUtils.hexToDecimal(cellId);
                    String result = restTemplate.getForObject("http://api.cellocation.com:84/cell/?coord=wgs84&output=csv&mcc=" + StringUtils.substring(mcc, 0, 3) + "&mnc=" + StringUtils.substring(mcc, 3) +
                            "&lac=" + lacTen + "&ci=" + cellTen, String.class);
                    if (StringUtils.isNotBlank(result)) {
                        List<String> list = Arrays.asList(result.split(","));
                        String lat = list.get(1);
                        String lng = list.get(2);
                        String location = list.get(4);
                        deviceDO.setLat(Double.parseDouble(lat));
                        deviceDO.setLng(Double.parseDouble(lng));
                        deviceDO.setLocation(location);
                        iotDeviceMapper.updateById(deviceDO);
                    }
                }
            }
        });
        return "";
    }

    public static void main(String[] args) {
        String abc = "46011";
        System.out.println(StringUtils.substring(abc, 3));
    }
}
