package com.ruoyi.ucp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.ucp.entity.PointInfo;
import com.ruoyi.ucp.mapper.PointInfoMapper;
import com.ruoyi.ucp.service.IPointInfoService;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author gfhd
 * @since 2024-03-01
 */
@Service
//@DataSource(DataSourceType.SLAVE)
public class PointInfoServiceImpl extends ServiceImpl<PointInfoMapper, PointInfo> implements IPointInfoService {

    @Override
    @Cacheable(value = "pointinfo", key = "#pointInfo.institutionType + ':' + #pointInfo.stationId + ':' + #pointInfo.projectId + ':' + #pointInfo.lineId + ':' + #pointInfo.turbineId + ':' + #pointInfo.uniformCode")
    public List<PointInfo> getByEntity(PointInfo pointInfo) {
        LambdaQueryWrapper<PointInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(pointInfo.getInstitutionType() != null, PointInfo::getInstitutionType, pointInfo.getInstitutionType());
        if (pointInfo.getTurbineId() != null) {
            if ("all".equals(pointInfo.getTurbineId()))
                wrapper.ne(PointInfo::getTurbineId, "");
            else
                wrapper.eq(PointInfo::getTurbineId, pointInfo.getTurbineId());
        } else if (pointInfo.getLineId() != null) {
            if ("all".equals(pointInfo.getLineId()))
                wrapper.ne(PointInfo::getLineId, "").eq(PointInfo::getTurbineId, "");
            else
                wrapper.eq(PointInfo::getLineId, pointInfo.getLineId());
        } else if (pointInfo.getProjectId() != null) {
            if ("all".equals(pointInfo.getProjectId()))
                wrapper.ne(PointInfo::getProjectId, "").eq(PointInfo::getLineId, "")
                        .eq(PointInfo::getTurbineId, "");
            else
                wrapper.eq(PointInfo::getProjectId, pointInfo.getProjectId());
        } else if (pointInfo.getStationId() != null) {
            if ("all".equals(pointInfo.getStationId()))
                wrapper.ne(PointInfo::getStationId, "").eq(PointInfo::getProjectId, "")
                        .eq(PointInfo::getLineId, "").eq(PointInfo::getTurbineId, "");
            else
                wrapper.eq(PointInfo::getStationId, pointInfo.getStationId());
        }
        wrapper.eq(pointInfo.getUniformCode() != null, PointInfo::getUniformCode, pointInfo.getUniformCode());
        return baseMapper.selectList(wrapper);
    }
}
