package com.yuchen.saas.park.service.service.property;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.lang.generator.SnowflakeGenerator;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuchen.saas.park.api.dto.property.PropertyBuildingsLevelQryDto;
import com.yuchen.saas.park.api.entity.property.PropertyBuildingsLevel;
import com.yuchen.saas.park.service.mapper.property.PropertyBuildingsLevelMapper;
import org.nest.springwrap.core.mp.base.BaseServiceImpl;
import org.nest.springwrap.core.tool.constant.NestConstant;
import org.nest.springwrap.core.tool.utils.DigestUtil;
import org.nest.springwrap.core.tool.utils.Func;
import org.springframework.stereotype.Service;

import java.sql.Array;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class PropertyBuildingsLevelService extends BaseServiceImpl<PropertyBuildingsLevelMapper, PropertyBuildingsLevel> {
    public List<PropertyBuildingsLevel> getAll() {

        LambdaQueryWrapper<PropertyBuildingsLevel> qw = new LambdaQueryWrapper<>();
        List<PropertyBuildingsLevel> propertyBuildingsLevelList = baseMapper.selectList(qw);

        return propertyBuildingsLevelList;
    }

    public boolean savePropertyBuildingsLevel(PropertyBuildingsLevel propertyBuildingsLevel) {
        //this.verifySaveFeeMode(communityInfo);
        this.assembleSaveDto(propertyBuildingsLevel);
        return this.saveOrUpdate(propertyBuildingsLevel);

    }

    private void assembleSaveDto(PropertyBuildingsLevel propertyBuildingsLevel) {
        if (Func.isEmpty(propertyBuildingsLevel.getId())) {
            propertyBuildingsLevel.setCreateTime(new Date());
        }
    }
    public List<PropertyBuildingsLevel> getLevelsByBuildingId(Long buildingId) {

        LambdaQueryWrapper<PropertyBuildingsLevel> qw = new LambdaQueryWrapper<>();
        qw.eq(PropertyBuildingsLevel::getBuildingId, buildingId);
        List<PropertyBuildingsLevel> propertyBuildingsLevelList = baseMapper.selectList(qw);
        if (Func.isNotEmpty(propertyBuildingsLevelList)){
            propertyBuildingsLevelList=propertyBuildingsLevelList.stream()
                    .sorted(Comparator.comparingInt(e->Integer.parseInt(e.getLevelNum())))
                    .collect(Collectors.toList());
        }
        return propertyBuildingsLevelList;
    }


    public List<PropertyBuildingsLevel> getLevelsByBuildingAndUnitId(Long buildingId, Long buildingUnitId) {

        LambdaQueryWrapper<PropertyBuildingsLevel> qw = new LambdaQueryWrapper<>();
        qw.eq(PropertyBuildingsLevel::getBuildingId, buildingId);
        qw.eq(PropertyBuildingsLevel::getBuildingUnitId, buildingUnitId);
        List<PropertyBuildingsLevel> propertyBuildingsLevelList = baseMapper.selectList(qw);

        return propertyBuildingsLevelList;
    }

    private LambdaQueryWrapper<PropertyBuildingsLevel> getPageQueryParam(PropertyBuildingsLevelQryDto req) {
        LambdaQueryWrapper<PropertyBuildingsLevel> qw = new LambdaQueryWrapper<>();
        if (Func.isNotEmpty(req.getBuildingId())) {
            qw.eq(PropertyBuildingsLevel::getBuildingId, req.getBuildingId());
        }
        if (Func.isNotEmpty(req.getBuildingUnitId())) {
            qw.eq(PropertyBuildingsLevel::getBuildingUnitId, req.getBuildingUnitId());
        }

        qw.eq(PropertyBuildingsLevel::getIsDeleted, NestConstant.DB_NOT_DELETED);
        return qw;
    }

    public Page<PropertyBuildingsLevel> selectPage(PropertyBuildingsLevelQryDto req) {
        Page<PropertyBuildingsLevel> page = new Page<>(req.getCurrent(), req.getSize());
        baseMapper.propertyBuildingsLvlPage(page, this.getPageQueryParam(req));
        return page;
    }




}

