package com.ruoyi.ibs.custmap.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.ibs.custmap.domain.dto.CustMapCircleDTO;
import com.ruoyi.ibs.custmap.domain.entity.CustInfoRadar;
import com.ruoyi.ibs.custmap.domain.entity.CustMapFavorite;
import com.ruoyi.ibs.custmap.domain.vo.CustMapVO;
import com.ruoyi.ibs.custmap.mapper.CustMapFavoriteMapper;
import com.ruoyi.ibs.custmap.mapper.CustMapMapper;
import com.ruoyi.ibs.custmap.service.CustMapSelectService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author 吴凯程
 * @Date 2025/5/15
 **/

@Service
public class CustMapSelectServiceImpl implements CustMapSelectService {

    @Resource
    private CustMapMapper custMapMapper;

    @Resource
    private CustMapFavoriteMapper custMapFavoriteMapper;

    /**
     * @param
     * @return
     */
    @Override
    public List<CustMapVO> selectCustListByCircle(CustMapCircleDTO custMapCircleDTO) {
        custMapCircleDTO.setDept(SecurityUtils.getHeadId());
        custMapCircleDTO.setPageIndex((custMapCircleDTO.getNum() - 1) * custMapCircleDTO.getSize());
        custMapCircleDTO.setLatRange(custMapCircleDTO.getDist() / 111.0);
        custMapCircleDTO.setLngRange(custMapCircleDTO.getDist() / (111.0 * Math.cos(Math.toRadians(custMapCircleDTO.getTargetLat()))));
        List<CustMapVO> list = custMapMapper.selectCustMapListByCircle(custMapCircleDTO);
        for (CustMapVO custMapVO : list) {
            LambdaQueryWrapper<CustMapFavorite> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(CustMapFavorite::getUserName, SecurityUtils.getUsername())
                    .eq(CustMapFavorite::getRadarId, custMapVO.getId());
            custMapVO.setIsFavorite(custMapFavoriteMapper.selectCount(queryWrapper) > 0);
        }
        return list;
    }

    /**
     * @param custMapCircleDTO
     * @return
     */
    @Override
    public Long countCustMapListByCircle(CustMapCircleDTO custMapCircleDTO) {
        custMapCircleDTO.setDept(SecurityUtils.getHeadId());
        custMapCircleDTO.setLatRange(custMapCircleDTO.getDist() / 111.0);
        custMapCircleDTO.setLngRange(custMapCircleDTO.getDist() / (111.0 * Math.cos(Math.toRadians(custMapCircleDTO.getTargetLat()))));
        return custMapMapper.countCustMapListByCircle(custMapCircleDTO);
    }

    /**
     * @param
     * @return
     */
    @Override
    public CustInfoRadar selectCustInfoRadarById(Integer id) {
        return custMapMapper.selectById(id);
    }



}
