package com.base.biz.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.base.biz.aop.PageInfo;
import com.base.biz.mapper.SceneBasisTargetMapper;
import com.base.biz.mapper.SceneTargetPointMapper;
import com.base.biz.model.PointDTO;
import com.base.biz.model.dto.request.QueryTargetListDTO;
import com.base.biz.model.dto.response.TargetPointDTO;
import com.base.biz.model.dto.response.SceneBasisTargetDTO;
import com.base.biz.model.entity.SceneBasisTarget;
import com.base.biz.model.entity.SceneTargetPoint;
import com.base.biz.model.enums.TargetTypeEnums;
import com.base.orbit.utils.NonEmptyUtils;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author cxl
 * @since 2022-02-24
 */
@Service
public class SceneBasisTargetService {

    @Resource
    SceneBasisTargetMapper sceneBasisTargetMapper;

    @Resource
    SceneTargetPointMapper sceneTargetPointMapper;


    public PageInfo<SceneBasisTargetDTO> getAllTargetList(int page, int size) {
        Page<SceneBasisTarget> res = (Page<SceneBasisTarget>) sceneBasisTargetMapper.selectPage(new Page<>(page, size), new QueryWrapper<>());
        List<SceneBasisTargetDTO> list = new ArrayList<>();
        res.getRecords().forEach(e -> {
            SceneBasisTargetDTO sceneBasisTargetDTO = new SceneBasisTargetDTO();
            BeanUtils.copyProperties(e, sceneBasisTargetDTO);
            list.add(sceneBasisTargetDTO);
        });
        PageInfo<SceneBasisTargetDTO> pageInfo = new PageInfo<>();
        pageInfo.setPage((int) res.getPages());
        pageInfo.setPageSize((int) res.getSize());
        pageInfo.setList(list);
        pageInfo.setTotal(res.getTotal());
        return pageInfo;
    }

    public SceneBasisTargetDTO getTargetByCode(String code) {
        SceneBasisTarget sceneBasisTarget = sceneBasisTargetMapper.getTargetByCode(code);
        NonEmptyUtils.objectNotNull(sceneBasisTarget);

        SceneBasisTargetDTO sceneBasisTargetDTO = new SceneBasisTargetDTO();
        BeanUtils.copyProperties(sceneBasisTarget, sceneBasisTargetDTO);
        return sceneBasisTargetDTO;
    }

    public List<SceneBasisTargetDTO> getTargetByCodes(List<String> targetCodes) {
        List<SceneBasisTargetDTO> res = new ArrayList<>();
        List<SceneBasisTarget> list = sceneBasisTargetMapper.getTargetByCodes(targetCodes);

        list.forEach(e -> {
            SceneBasisTargetDTO sceneBasisTargetDTO = new SceneBasisTargetDTO();
            BeanUtils.copyProperties(e, sceneBasisTargetDTO);
            res.add(sceneBasisTargetDTO);
        });
        return res;
    }


    public PageInfo<TargetPointDTO> getTargetPointPage(QueryTargetListDTO queryTargetListDTO) {
        PageHelper.startPage(queryTargetListDTO.getPage(), queryTargetListDTO.getPageSize());
        List<TargetPointDTO> targetList = sceneTargetPointMapper.getTargetByVo(queryTargetListDTO);
        if (CollectionUtils.isEmpty(targetList)) {
            return PageInfo.listOf(targetList);
        }
        List<String> targetIds = targetList.stream().map(TargetPointDTO::getTargetId).collect(Collectors.toList());
        List<SceneTargetPoint> sceneTargetPoints = getPointsByTargetIds(targetIds);
        targetList.forEach(target -> {
            String targetId = target.getTargetId();
            List<SceneTargetPoint> sceneTargetPointList = sceneTargetPoints.stream().filter(e -> targetId.equals(e.getTargetId())).collect(Collectors.toList());
            List<PointDTO> pointDTOList = sceneTargetPointsCastToDTO(sceneTargetPointList);
            target.setType(TargetTypeEnums.getTargetTypeEnums(StringUtils.isEmpty(target.getType()) ? "-1" : target.getType()).getValue());
            target.setPointDTOList(pointDTOList);
        });
        com.github.pagehelper.PageInfo<TargetPointDTO> pageInfo = new com.github.pagehelper.PageInfo<>(targetList);
        return PageInfo.of(pageInfo);
    }

    public List<SceneTargetPoint> getPointsByTargetIds(List<String> targetIds) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.in("target_id", targetIds);
        List<SceneTargetPoint> sceneTargetPoints = sceneTargetPointMapper.selectList(queryWrapper);
        return sceneTargetPoints;
    }

    public List<PointDTO> sceneTargetPointsCastToDTO(List<SceneTargetPoint> sceneTargetPoints) {
        List<PointDTO> pointDTOList = new ArrayList<>();
        sceneTargetPoints.forEach(sceneTargetPoint -> {
            PointDTO pointDTO = new PointDTO();
            pointDTO.setPointId(sceneTargetPoint.getId());
            pointDTO.setLat(sceneTargetPoint.getLatitude().doubleValue());
            pointDTO.setLng(sceneTargetPoint.getLongitude().doubleValue());
            pointDTO.setSequence(sceneTargetPoint.getSequence());
            pointDTOList.add(pointDTO);
        });
        return pointDTOList;
    }

}