package com.usky.fire.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.usky.common.core.bean.CommonPage;
import com.usky.common.mybatis.core.AbstractCrudService;
import com.usky.common.security.utils.SecurityUtils;
import com.usky.fire.domain.DemWaterSourceMaintain;
import com.usky.fire.mapper.DemWaterSourceMaintainMapper;
import com.usky.fire.service.DemWaterSourceMaintainService;
import com.usky.fire.service.util.OnlineMethod;
import com.usky.fire.service.vo.LocateInfo;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 天然水源信息表 服务实现类
 * </p>
 *
 * @author JCB
 * @since 2022-10-25
 */
@Service
public class DemWaterSourceMaintainServiceImpl extends AbstractCrudService<DemWaterSourceMaintainMapper, DemWaterSourceMaintain> implements DemWaterSourceMaintainService {

    @Override
    public CommonPage<DemWaterSourceMaintain> waterSourceMaintainList(String squadron, Integer id, Integer pageNum, Integer pageSize) {
        IPage<DemWaterSourceMaintain> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<DemWaterSourceMaintain> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(DemWaterSourceMaintain::getDeleteFlag, "0")
                .like(StringUtils.isNotBlank(squadron), DemWaterSourceMaintain::getSquadron, squadron)
                .eq(id != 0 && id != null, DemWaterSourceMaintain::getId, id)
                .orderByDesc(DemWaterSourceMaintain::getId);
        List<DemWaterSourceMaintain> list = this.list(queryWrapper);
        page = this.page(page, queryWrapper);
        return new CommonPage<>(page.getRecords(), page.getTotal(), pageSize, pageNum);
    }

    @Override
    public void addWaterSourceMaintain(DemWaterSourceMaintain demWaterSourceMaintain) {
        demWaterSourceMaintain.setCreator(SecurityUtils.getUsername());
        demWaterSourceMaintain.setCreateTime(LocalDateTime.now());
        demWaterSourceMaintain.setDeleteFlag("0");
        this.save(demWaterSourceMaintain);
    }

    @Override
    public void updateWaterSourceMaintain(DemWaterSourceMaintain demWaterSourceMaintain) {
        demWaterSourceMaintain.setUpdateTime(LocalDateTime.now());
        demWaterSourceMaintain.setUpdatePerson(SecurityUtils.getUsername());
        this.updateById(demWaterSourceMaintain);
    }

    @Override
    public void delWaterSourceMaintain(Integer id) {
        DemWaterSourceMaintain demWaterSourceMaintain = new DemWaterSourceMaintain();
        demWaterSourceMaintain.setId(id);
        demWaterSourceMaintain.setDeleteFlag("1");
        this.updateById(demWaterSourceMaintain);
    }

    @Override
    public List<Object> waterSourceMaintainScatterer(String streetTown) {
        List<Object> list2 = new ArrayList<>();
        IPage<DemWaterSourceMaintain> page = new Page<>(1, 500);
        LambdaQueryWrapper<DemWaterSourceMaintain> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(DemWaterSourceMaintain::getDeleteFlag, "0")
                .isNotNull(DemWaterSourceMaintain::getDescribe)
                .orderByDesc(DemWaterSourceMaintain::getId);
        List<DemWaterSourceMaintain> list = this.list(queryWrapper);
        if (CollectionUtils.isNotEmpty(list)) {
            String longitude = null;
            String latitude = null;
            for (int i = 0; i < list.size(); i++) {
                if (StringUtils.isBlank(list.get(i).getLongitude()) && StringUtils.isBlank(list.get(i).getLatitude())) {
                    List<LocateInfo> lonLat = OnlineMethod.getLonLat(list.get(i).getDescribe());
                    if (CollectionUtils.isNotEmpty(lonLat)) {
                        longitude = "" + lonLat.get(0).getLongitude();
                        latitude = "" + lonLat.get(0).getLatitude();
                    }
                    DemWaterSourceMaintain demWaterSourceMaintain = new DemWaterSourceMaintain();
                    demWaterSourceMaintain.setId(list.get(i).getId());
                    demWaterSourceMaintain.setLatitude(latitude);
                    demWaterSourceMaintain.setLongitude(longitude);
                    demWaterSourceMaintain.setUpdatePerson("程序修改");
                    demWaterSourceMaintain.setCreateTime(LocalDateTime.now());
                    this.updateById(demWaterSourceMaintain);
                    list.get(i).setLatitude(latitude);
                    list.get(i).setLongitude(longitude);
                }
                Map<String, Object> map = new HashMap<>();
                map.put("id", list.get(i).getId());
                map.put("detachment", list.get(i).getDetachment());
                map.put("squadron", list.get(i).getSquadron());
                map.put("describe", list.get(i).getDescribe());
                map.put("longitude", list.get(i).getLongitude());
                map.put("latitude", list.get(i).getLatitude());
                list2.add(map);
            }
        }
        return list2;
    }
}
