package com.project.zcustom.service.service.property.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.zcustom.controller.core.PageParamVo;
import com.project.zcustom.controller.core.StringUtils;
import com.project.zcustom.domain.property.PlatPropertyList;

import com.project.zcustom.mapper.property.PlatPropertyListMapper;
import com.project.zcustom.service.service.property.IPlatPropertyListService;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 驿站列Service业务层处理
 *
 * @author change
 * @date 2024-10-21
 */
@Service
public class PlatPropertyListServiceImpl extends ServiceImpl<PlatPropertyListMapper, PlatPropertyList> implements IPlatPropertyListService {

    /**
     * 组装查询条件
     */
    private LambdaQueryWrapper<PlatPropertyList> getLambdaQueryWrapper(PlatPropertyList platPropertyList) {
        LambdaQueryWrapper<PlatPropertyList> lqw = new LambdaQueryWrapper<PlatPropertyList>();
        if (StringUtils.isNotBlank(platPropertyList.getAppOrg())) {
            lqw.eq(PlatPropertyList::getAppOrg, platPropertyList.getAppOrg());
        }
        if (StringUtils.isNotBlank(platPropertyList.getStationName())) {
            lqw.like(PlatPropertyList::getStationName, platPropertyList.getStationName());
        }
        if (StringUtils.isNotBlank(platPropertyList.getStationAddress())) {
            lqw.eq(PlatPropertyList::getStationAddress, platPropertyList.getStationAddress());
        }
        return lqw;
    }

    /**
     * 分页查询驿站列列表
     *
     * @param platPropertyList 驿站列
     * @return 驿站列集合
     */
    @Override
    public IPage<PlatPropertyList> pagingPlatPropertyListList(PlatPropertyList platPropertyList) {
        IPage<PlatPropertyList> page = new Page<>(platPropertyList.getPageNum(), platPropertyList.getPageSize());
        LambdaQueryWrapper<PlatPropertyList> lqw = getLambdaQueryWrapper(platPropertyList);
        // 排序 默认时间倒序
        lqw.orderByDesc(PlatPropertyList::getCreateTime);
        return baseMapper.selectPage(page, lqw);
    }

    /**
     * 分页查询驿站列列表
     *
     * @param lqw
     * @return 驿站列集合
     */
    @Override
    public IPage<PlatPropertyList> pagingPlatPropertyListList(PageParamVo pageParamVo, LambdaQueryWrapper<PlatPropertyList> lqw) {
        IPage<PlatPropertyList> page = new Page<>(pageParamVo.getPageNum(), pageParamVo.getPageSize());
        // 排序 默认时间倒序
        lqw.orderByDesc(PlatPropertyList::getCreateTime);
        return baseMapper.selectPage(page, lqw);
    }

    /**
     * 查询驿站列列表
     *
     * @param platPropertyList 驿站列
     * @return 驿站列
     */
    @Override
    public List<PlatPropertyList> selectPlatPropertyListList(PlatPropertyList platPropertyList) {
        LambdaQueryWrapper<PlatPropertyList> lqw = getLambdaQueryWrapper(platPropertyList);
        // 排序 默认时间倒序
        lqw.orderByDesc(PlatPropertyList::getCreateTime);
        return baseMapper.selectList(lqw);
    }

    /**
     * 查询驿站列列表
     *
     * @param platPropertyList 驿站列
     * @return 驿站列
     */
    @Override
    public List<PlatPropertyList> selectPlatPropertyListList(PlatPropertyList platPropertyList, Long limitNum) {
        LambdaQueryWrapper<PlatPropertyList> lqw = getLambdaQueryWrapper(platPropertyList);
        // 排序 默认时间倒序
        lqw.orderByDesc(PlatPropertyList::getCreateTime);
        lqw.last("limit " + limitNum);
        return baseMapper.selectList(lqw);
    }

    /**
     * 根据指定字段查询信息
     *
     * @param field      字段名称
     * @param filedValue 字段值
     * @return 驿站列
     */
    @Override
    public PlatPropertyList getPlatPropertyListByField(String field, String filedValue) {
        QueryWrapper<PlatPropertyList> lqw = new QueryWrapper<PlatPropertyList>();
        lqw.eq(field, filedValue);
        lqw.last("limit 1");
        return this.getOne(lqw, false);
    }

    /**
     * 根据指定字段查询信息列表
     *
     * @param field      字段名称
     * @param filedValue 字段值
     * @return 驿站列
     */
    @Override
    public List<PlatPropertyList> selectPlatPropertyListListByField(String field, String filedValue, Long limitNum) {
        QueryWrapper<PlatPropertyList> lqw = new QueryWrapper<PlatPropertyList>();
        lqw.eq(field, filedValue);
        lqw.last("limit " + limitNum);
        return this.list(lqw);
    }

    /**
     * 根据指定字段查询信息列表
     *
     * @param field      字段名称
     * @param filedValue 字段值
     * @return 驿站列
     */
    @Override
    public List<PlatPropertyList> selectPlatPropertyListListByField(String field, String filedValue) {
        QueryWrapper<PlatPropertyList> lqw = new QueryWrapper<PlatPropertyList>();
        lqw.eq(field, filedValue);
        lqw.orderByDesc("station_num");
        return this.list(lqw);
    }

    /**
     * 判断指定字段值是否存在
     *
     * @param field      字段名称
     * @param filedValue 字段值
     */
    @Override
    public void checkFieldExist(String field, String filedValue, String tips, Long id) {
        QueryWrapper<PlatPropertyList> lqw = new QueryWrapper<PlatPropertyList>();
        lqw.eq(field, filedValue);
        lqw.ne("id", id);
        if (this.getOne(lqw, false) != null) {
            throw new RuntimeException(tips + "已存在");
        }
    }

    /**
     *  真删接口
     */
    @Override
    public void reallyDeletePlatPropertyList(LambdaQueryWrapper<PlatPropertyList> lqw){
        baseMapper.reallyDeleteByWrapper(lqw);
    }

    /**
     * 单位列表
     *
     * @param appOrg 字段名称
     * @return 服务信息
     */
    @Override
    public List<Map<String,Object>> selectServerListByAppOrg(String appOrg){
        QueryWrapper<PlatPropertyList> lqw = new QueryWrapper<PlatPropertyList>();
        if(!Objects.equals(appOrg, "0000")){
            lqw.eq("app_org", appOrg);
        }
        lqw.orderByDesc("station_num");
        lqw.last("limit 100");//最多查询100条数据
        List<PlatPropertyList> list = this.list(lqw);
        if(list.isEmpty()){
            return new ArrayList<>();
        }
        return list.stream().map(item -> {
            Map<String,Object> map = new HashMap<>();
            map.put("stationName", item.getStationName());
            map.put("stationAddress", item.getStationAddress());
            map.put("stationNum", item.getStationNum());
            return map;
        }).collect(Collectors.toList());
    }
}
