package com.nantian.cloud.jljc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nantian.cloud.jljc.entity.EcAbnormalVolatility;
import com.nantian.cloud.jljc.entity.EcAbnormalVolatilityInfo;
import com.nantian.cloud.jljc.entity.TReport;
import com.nantian.cloud.jljc.mapper.EcAbnormalVolatilityMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nantian.cloud.jljc.service.EcAbnormalVolatilityInfoService;
import com.nantian.cloud.jljc.service.EcAbnormalVolatilityService;
import com.nantian.sc.cloud.common.data.datascope.DataScope;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 企业能耗异常波动表 服务实现类
 * </p>
 *
 * @author stz
 * @since 2020-08-10
 */
@Service("ecAbnormalVolatilityService")
public class EcAbnormalVolatilityServiceImpl extends ServiceImpl<EcAbnormalVolatilityMapper, EcAbnormalVolatility> implements EcAbnormalVolatilityService {

    @Autowired
    private EcAbnormalVolatilityInfoService ecAbnormalVolatilityInfoService;

    @Override
    public IPage<EcAbnormalVolatility> getPageScope(Page page, EcAbnormalVolatility ecAbnormalVolatility) {
        return baseMapper.getPageScope(page,ecAbnormalVolatility,new DataScope());
    }

    @Override
    public void removeInfoByReport( String tableName, TReport tReport) {
        List<EcAbnormalVolatility> volatilities = baseMapper.selectList(new QueryWrapper<>(new EcAbnormalVolatility())
                .eq("del_flag", "1")
                .eq("epc", tReport.getEnterpriseCode())
                .eq("year", tReport.getYear()));
        if(volatilities != null && volatilities.size() > 0){ //主表有数据，查询其能报对应的子表信息
            List<EcAbnormalVolatilityInfo> infos = ecAbnormalVolatilityInfoService.list(new QueryWrapper<>(new EcAbnormalVolatilityInfo())
                    .eq("del_flag", "1")
                    .eq("abnormal_volatility_id", volatilities.get(0).getId())
                    .in(StringUtils.isNotEmpty(tableName), "data_table", tableName.split(",")));
            if(infos != null && infos.size() > 0){ //删除精确分析
                for (EcAbnormalVolatilityInfo info : infos) {
                    info.setDelFlag("4");
                }
                ecAbnormalVolatilityInfoService.updateBatchById(infos);
            }
        }
    }
}
