package com.usky.fire.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usky.common.mybatis.core.AbstractCrudService;
import com.usky.fire.domain.BaseCompany;
import com.usky.fire.domain.BaseCompanyAttach1;
import com.usky.fire.mapper.BaseCompanyAttach1Mapper;
import com.usky.fire.mapper.BaseCompanyMapper;
import com.usky.fire.service.BaseCompanyAttach1Service;
import com.usky.fire.service.util.OnlineMethod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 联网单位信息附表1 服务实现类
 * </p>
 *
 * @author JCB
 * @since 2022-09-20
 */
@Service
public class BaseCompanyAttach1ServiceImpl extends AbstractCrudService<BaseCompanyAttach1Mapper, BaseCompanyAttach1> implements BaseCompanyAttach1Service {

    @Autowired
    private BaseCompanyMapper baseCompanyMapper;

    @Override
    public void streetTownList() {
        List<String> streetTownList = new ArrayList<>();
        streetTownList.add("华漕镇");
        streetTownList.add("新虹街道");
        streetTownList.add("七宝镇");
        streetTownList.add("虹桥镇");
        streetTownList.add("古美路街道");
        streetTownList.add("莘庄镇");
        streetTownList.add("梅陇镇");
        streetTownList.add("颛桥镇");
        streetTownList.add("马桥镇");
        streetTownList.add("江川路街道");
        streetTownList.add("吴泾镇");
        streetTownList.add("浦锦街道");
        streetTownList.add("浦江镇");
        LambdaQueryWrapper<BaseCompany> queryWrapper1 = Wrappers.lambdaQuery();
        queryWrapper1.select(BaseCompany::getId, BaseCompany::getCompanyId)
                .eq(BaseCompany::getEnable, 0)
                .notIn(BaseCompany::getStreetTown,streetTownList);
        List<BaseCompany> baseCompanies = baseCompanyMapper.selectList(queryWrapper1);
        if (CollectionUtils.isNotEmpty(baseCompanies)){
            List<String> companyIdList = new ArrayList<>();
            for (int i = 0; i < baseCompanies.size(); i++) {
                companyIdList.add(baseCompanies.get(i).getCompanyId());
            }

            LambdaQueryWrapper<BaseCompanyAttach1> queryWrapper = Wrappers.lambdaQuery();
            queryWrapper.select(BaseCompanyAttach1::getId, BaseCompanyAttach1::getLatitude, BaseCompanyAttach1::getLongitude, BaseCompanyAttach1::getCompanyId)
//                    .ne(BaseCompanyAttach1::getLatitude, "")
//                    .ne(BaseCompanyAttach1::getLongitude, "")
                    .in(BaseCompanyAttach1::getCompanyId,companyIdList);
            List<BaseCompanyAttach1> list = this.list(queryWrapper);

            if (CollectionUtils.isNotEmpty(list)) {
                for (int i = 0; i < 11; i++) {
                    if (StringUtils.isNotBlank(list.get(i).getLongitude()) && StringUtils.isNotBlank(list.get(i).getLatitude())) {
                        String location = list.get(i).getLongitude() + "," + list.get(i).getLatitude();
                        String streetTown = OnlineMethod.getStreetTown(location);
                        System.out.println(streetTown);
                        if (StringUtils.isNotBlank(streetTown)) {
                            for (int j = 0; j < baseCompanies.size(); j++) {
                                if (list.get(i).getCompanyId().equals(baseCompanies.get(j).getCompanyId())) {
                                    BaseCompany baseCompany = new BaseCompany();
                                    baseCompany.setId(baseCompanies.get(j).getId());
                                    baseCompany.setStreetTown(streetTown);
                                    baseCompanyMapper.updateById(baseCompany);
                                }
                            }
                        }
                    }
                }
            }
        }
    }

}
