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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuchen.saas.park.api.dto.property.PropertyMeterQueryDto;
import com.yuchen.saas.park.api.entity.park.PmgBatParkInfo;
import com.yuchen.saas.park.api.entity.property.PropertyMeter;
import com.yuchen.saas.park.api.vo.property.PropertyHouseMeterPageVo;
import com.yuchen.saas.park.api.vo.property.PropertyMeterGroupVo;
import com.yuchen.saas.park.api.vo.property.PropertyMeterPageVo;
import com.yuchen.saas.park.service.mapper.property.PropertyMeterMapper;
import com.yuchen.saas.park.service.service.PmgBatParkInfoService;
import com.yuchen.saas.park.service.service.property.IPropertyMeterService;
import com.yuchen.saas.parking.charge.api.vo.alarm.WarningInfoVO;
import com.yuchen.saas.payment.api.util.PageConverterUtils;
import org.nest.springwrap.core.log.exception.ServiceException;
import org.nest.springwrap.core.mp.base.BaseServiceImpl;
import org.nest.springwrap.core.mp.support.Condition;
import org.nest.springwrap.core.mp.support.NestPage;
import org.nest.springwrap.core.tool.utils.Func;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 仪表类型 服务实现类
 * </p>
 *
 * @author cxj
 * @since 2023-11-21
 */
@Service
public class PropertyMeterServiceImpl extends BaseServiceImpl<PropertyMeterMapper, PropertyMeter> implements IPropertyMeterService {

    @Resource
    private PmgBatParkInfoService pmgBatParkInfoService;

    @Override
    public NestPage<PropertyMeterPageVo> getPage(PropertyMeterQueryDto dto) {
        LambdaQueryWrapper<PropertyMeter> queryWrapper=Wrappers.lambdaQuery();
        queryWrapper.eq(Func.isNotEmpty(dto.getMeterName()),PropertyMeter::getMeterName,dto.getMeterName())
                .eq(PropertyMeter::getParkId,dto.getParkId())
                .orderByDesc(PropertyMeter::getCreateTime);
        IPage<PropertyMeter> page = this.page(Condition.getPage(dto), queryWrapper);
        IPage<PropertyMeterPageVo> result = new Page<>();
        Func.copy(page, result);
        List<PropertyMeterPageVo> list = page.getRecords().stream().map(a -> {
            PropertyMeterPageVo vo = Func.copy(a, PropertyMeterPageVo.class);
            if (Func.isNotEmpty(a.getParkId())) {
                PmgBatParkInfo parkInfo = pmgBatParkInfoService.getById(a.getParkId());
                if (Func.isNotEmpty(parkInfo)) {
                    vo.setParkName(parkInfo.getParkName());
                }
            }
            return vo;
        }).collect(Collectors.toList());
        result.setRecords(list);
        return NestPage.of(result);
    }

    @Override
    public Boolean del(Long id) {
        //todo 仪表别使用的话需要把仪表被使用回填
        PropertyMeter dto=getById(id);
        if(Func.isEmpty(dto)){
            throw new ServiceException("删除对象不存在!");
        }
        if(Func.isNotEmpty(dto.getIsUse())&&dto.getIsUse()==1){
            throw new ServiceException("仪表已被使用,不能进行删除!");
        }
        return deleteLogic(Arrays.asList(id));
    }

    @Override
    public List<PropertyMeterGroupVo> listByGroupByParkId(Long parkId) {
        LambdaQueryWrapper<PropertyMeter> queryWrapper=Wrappers.lambdaQuery();
        queryWrapper.eq(PropertyMeter::getParkId,parkId)
                .orderByDesc(PropertyMeter::getCreateTime);
        List<PropertyMeter> list = list(queryWrapper);
        List<PropertyMeterGroupVo> result=new ArrayList<>(list.size()*2);
        if(Func.isEmpty(list)){
            return result;
        }
        for (PropertyMeter item:list) {
            PropertyMeterGroupVo publicMeter = Func.copy(item, PropertyMeterGroupVo.class);
            publicMeter.setSourceType(2);
            publicMeter.setSourceMeterName("公摊"+item.getMeterName());
            result.add(publicMeter);
            PropertyMeterGroupVo houseMeter = Func.copy(item, PropertyMeterGroupVo.class);
            houseMeter.setSourceType(1);
            houseMeter.setSourceMeterName("家庭用"+item.getMeterName());
            result.add(houseMeter);
        }
        return result;
    }
}
