package com.platform.yijia.service.impl;
import com.platform.yijia.dao.StationInfoMapper;
import com.platform.yijia.dao.StationOilPriceMapper;
import com.platform.yijia.dao.StationPayManageMapper;
import com.platform.yijia.param.request.StationRequest;
import com.platform.yijia.param.response.StationInfoResponse;
import com.platform.yijia.param.response.StationInfoVo;
import com.platform.yijia.pojo.*;
import com.platform.yijia.service.StationService;
import com.platform.yijia.utils.MapHelper;
import com.platform.yijia.utils.PageUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

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

@Service("stationService")
public class StationServiceImpl implements StationService {

    @Resource
    private StationInfoMapper stationInfoMapper;

    @Resource
    private StationOilPriceMapper stationOilPriceMapper;

    @Resource
    private StationPayManageMapper stationPayManageMapper;

    //查询油站配置信息
    @Override
    public StationPayManage selectStationPayManage(StationPayManage stationPayManage) {
        return stationPayManageMapper.selectStationPayManage(stationPayManage);
    }

    //获取油站信息
    @Override
    public StationInfo selectStationInfo(StationInfo stationInfo) {
        return stationInfoMapper.selectStationInfo(stationInfo);
    }

    //获取油站的打印机
    @Override
    public List<Map<String, Object>> getStationPrinterList(Integer stationId) {
        return stationInfoMapper.getStationPrinterList(stationId);
    }

    //查询油站油品价格
    @Override
    public StationOilPrice getStationOilPrice(StationOilPrice stationOilPrice) {
        return stationOilPriceMapper.getStationOilPrice(stationOilPrice);
    }

    //获取油站公告
    @Override
    public Map<String, Object> getStationNoticeInfo(Integer stationId) {
        return stationInfoMapper.getStationNoticeInfo(stationId);
    }

    /**
     * 获取油站轮播图
     * @param stationPic
     * @return
     */
    @Override
    public List<StationPic> getStationPicList(StationPic stationPic) {
        return stationInfoMapper.getStationPicList(stationPic);
    }

    @Override
    public StationInfoVo stationInfoList(StationRequest stationRequest) {
        //根据传过来的坐标查询数据
        List<StationInfoResponse> distanceAndResidueSeat = new ArrayList<>();
         StationInfoExample example=new StationInfoExample();
        if(StringUtils.isNotBlank(stationRequest.getStationName())){
            example.or().andStationNameLike(stationRequest.getStationName());

        }
        if(StringUtils.isNotBlank(stationRequest.getAppId())){
            example.or().andAppIdEqualTo(stationRequest.getAppId());
        }
        // 数据库查询门店数据
        List<StationInfo> stationInfolist = stationInfoMapper.selectByExample(example);
        // 入参坐标拼接
        String coordinate = stationRequest.getStationLatitude()+","+stationRequest.getStationLongitude();
        if(StringUtils.isNotBlank(stationRequest.getStationLatitude()) && StringUtils.isNotBlank(stationRequest.getStationLongitude())){
            //根据地址坐标计算距离
            List<StationInfoResponse> stationInfos = computeDistance(coordinate, stationInfolist);
            //根据距离排序
            distanceAndResidueSeat = getDistanceAndResidueSeat(stationInfos);
        }
        StationInfoVo stationInfoVo=new StationInfoVo();
        if(stationRequest.getPageSize()==0){
            stationInfoVo.setPageSize(10);
        }else{
            stationInfoVo.setPageSize(stationRequest.getPageSize());
        }
        stationInfoVo.setPageNum(stationRequest.getPageNum());
        if(distanceAndResidueSeat!=null&&distanceAndResidueSeat.size()>0){
            if( distanceAndResidueSeat.size()<=stationInfoVo.getPageNum()*stationInfoVo.getPageSize() || distanceAndResidueSeat.size()>stationInfoVo.getPageNum()*stationInfoVo.getPageSize()){
                List<StationInfoResponse> ss= PageUtil.splitList(distanceAndResidueSeat,stationRequest.getPageSize(),stationRequest.getPageNum());
                stationInfoVo.setStationInfoResponseList(ss);
                stationInfoVo.setTotal(ss.size());
            }
        }
        return stationInfoVo;
    }


    @Override
    public StationInfoVo gzhStationInfoList(StationRequest stationRequest) {
        //根据传过来的坐标查询数据
        List<StationInfoResponse> distanceAndResidueSeat = new ArrayList<>();
        StationInfoExample example=new StationInfoExample();
        if(StringUtils.isNotBlank(stationRequest.getStationName())){
            example.or().andStationNameLike(stationRequest.getStationName());

        }
        if(StringUtils.isNotBlank(stationRequest.getAppId())){
            example.or().andGzhAppIdEqualTo(stationRequest.getAppId());
        }
        // 数据库查询门店数据
        List<StationInfo> stationInfolist = stationInfoMapper.selectByExample(example);
        // 入参坐标拼接
        String coordinate = stationRequest.getStationLatitude()+","+stationRequest.getStationLongitude();
        if(StringUtils.isNotBlank(stationRequest.getStationLatitude()) && StringUtils.isNotBlank(stationRequest.getStationLongitude())){
            //根据地址坐标计算距离
            List<StationInfoResponse> stationInfos = computeDistance(coordinate, stationInfolist);
            //根据距离排序
            distanceAndResidueSeat = getDistanceAndResidueSeat(stationInfos);
        }
        StationInfoVo stationInfoVo=new StationInfoVo();
        if(stationRequest.getPageSize()==0){
            stationInfoVo.setPageSize(10);
        }else{
            stationInfoVo.setPageSize(stationRequest.getPageSize());
        }
        stationInfoVo.setPageNum(stationRequest.getPageNum());
        if(distanceAndResidueSeat!=null&&distanceAndResidueSeat.size()>0){
            if( distanceAndResidueSeat.size()<=stationInfoVo.getPageNum()*stationInfoVo.getPageSize() || distanceAndResidueSeat.size()>stationInfoVo.getPageNum()*stationInfoVo.getPageSize()){
                List<StationInfoResponse> ss= PageUtil.splitList(distanceAndResidueSeat,stationRequest.getPageSize(),stationRequest.getPageNum());
                stationInfoVo.setStationInfoResponseList(ss);
                stationInfoVo.setTotal(ss.size());
            }
        }
        return stationInfoVo;
    }


    /**
     * 根据地址坐标计算距离
     * @return
     */
    public List<StationInfoResponse> computeDistance(String coordinate, List<StationInfo> stationInfos){
        List<StationInfoResponse> stationInfoList = new ArrayList<>();
        if(stationInfos!=null && stationInfos.size() > 0){
            for (StationInfo stationInfo : stationInfos) {
                StationInfoResponse stationInfoResponse =new StationInfoResponse();
                String longitude = stationInfo.getStationLongitude();
                String latitude = stationInfo.getStationLatitude();
                if(StringUtils.isNotBlank(longitude) && StringUtils.isNotBlank(latitude) && StringUtils.isNotBlank(coordinate)){
                    String distanceData = latitude+","+longitude;
                    double distance = MapHelper.GetPointDistance(coordinate, distanceData);
                    Double format = Double.parseDouble(String.format("%.1f", distance));
                    //计算距离 展示距离小于5公里以内的站
                    //if(format<=5){
                        stationInfoResponse.setStationId(stationInfo.getStationId());
                        stationInfoResponse.setStationName(stationInfo.getStationName());
                        stationInfoResponse.setStationAddress(stationInfo.getStationAddress());
                        stationInfoResponse.setOilGunNum(stationInfo.getOilGunNum());
                        stationInfoResponse.setContacts(stationInfo.getContacts());
                        stationInfoResponse.setPhone(stationInfo.getPhone());
                        stationInfoResponse.setStationGroupId(stationInfo.getStationGroupId());
                        stationInfoResponse.setStationGroupName(stationInfo.getStationGroupName());
                        stationInfoResponse.setStationPic(stationInfo.getStationPic());
                        stationInfoResponse.setStationLatitude(stationInfo.getStationLatitude());
                        stationInfoResponse.setStationLongitude(stationInfo.getStationLongitude());
                        stationInfoResponse.setDistance(format);
                        stationInfoResponse.setMno(stationInfo.getMno());
                        stationInfoList.add(stationInfoResponse);
                    //}
                }
            }
        }
        return stationInfoList;
    }

    /**
     * 根据距离排序
     * @return
     */
    public static List<StationInfoResponse> getDistanceAndResidueSeat(List<StationInfoResponse> list) {
        boolean exchange = false;
        for (int i = 0; i < list.size(); i++) {
            for (int j = list.size() - 2; j >= i; j--) {
                //循环根据距离进行排序
                if (list.get(j + 1).getDistance() < list.get(j).getDistance()) {
                    StationInfoResponse stationInfoResponse = list.get(j + 1);
                    list.set(j + 1, list.get(j));
                    list.set(j, stationInfoResponse);
                    exchange = true;
                }
            }
            if (!exchange)
                break;
        }
        return list;
    }

    /**
     * 查询油站列表
     * @param stationInfo
     * @return
     */
    @Override
    public List<StationInfo> getStationList(StationInfo stationInfo) {
        StationInfoExample example = new StationInfoExample();
        if (StringUtils.isNotBlank(stationInfo.getGzhAppId())){
            example.or().andGzhAppIdEqualTo(stationInfo.getGzhAppId());
        }
        List<StationInfo> stationInfolist = stationInfoMapper.selectByExample(example);
        return stationInfolist;
    }

    /**
     * 添加油站信息
     */
    @Override
    public void AddStationInfo(StationInfo stationInfo) {
        stationInfoMapper.insert(stationInfo);
    }

    /**
     * 修改油站信息
     * @param stationInfo
     */
    @Override
    public void updateStationInfo(StationInfo stationInfo) {
        StationInfoExample example = new StationInfoExample();
        stationInfoMapper.updateByExample(stationInfo,example);
    }

    /**
     * 删除油站信息
     * @param stationInfo
     */
    @Override
    public void deleteStationInfo(StationInfo stationInfo) {
        StationInfoExample example = new StationInfoExample();
        example.or().andStationIdEqualTo(stationInfo.getStationId());
        stationInfoMapper.deleteByExample(example);
    }

    //获取该油站的优惠方式
    @Override
    public String getStationDiscountWay(String stationId) {
        return stationInfoMapper.getStationDiscountWay(stationId);
    }

    //获取小程序APPID和APPSECRET
    @Override
    public Map<String, String> getStationAppIdAndAppSecret(Integer stationId) {
        return stationInfoMapper.getStationAppIdAndAppSecret(stationId);
    }

}
