package com.rongwei.bscommon.sys.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.rongwei.bsentity.domain.EquEnergyMeterCenterDosageDayDo;
import com.rongwei.bsentity.domain.EquEnergyMeterDosageDayDo;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface EquEnergyMeterCenterDosageDayDao extends BaseMapper<EquEnergyMeterCenterDosageDayDo> {

    @Delete("TRUNCATE  table equ_energy_meter_center_dosage_day")
    void deleteData();

    @Select("SELECT\n" +
            "\tUUID() AS 'ID',\n" +
            "\tb.CENTERID,\n" +
            "\tb.CENTERDAY,\n" +
            "\tb.DOSAGE,\n" +
            "\tb.TENANTID,\n" +
            "\tb.OWNEDFACTORY \n" +
            "FROM\n" +
            "\tequ_energy_meter_center a\n" +
            "\tLEFT JOIN (\n" +
            "\tSELECT\n" +
            "\t\ta.CENTERID,\n" +
            "\t\tb.CENTERDAY,\n" +
            "\t\tb.TENANTID,\n" +
            "\t\tb.OWNEDFACTORY,\n" +
            "\t\tSUM(\n" +
            "\t\tCASE\n" +
            "\t\t\t\tWHEN b.DOSAGE IS NULL THEN\n" +
            "\t\t\tNULL \n" +
            "\t\t\t\tWHEN a.JSMETHOD = '+' THEN\n" +
            "\t\t\t\tb.DOSAGE * a.XS \n" +
            "\t\t\t\tWHEN a.JSMETHOD = '-' THEN\n" +
            "\t\t\t\tb.DOSAGE * a.XS *- 1 ELSE NULL \n" +
            "\t\t\tEND \n" +
            "\t\t\t) 'DOSAGE' \n" +
            "\t\tFROM\n" +
            "\t\t\tequ_energy_meter_center_detail a\n" +
            "\t\t\tINNER JOIN equ_energy_meter_dosage_day b ON a.METERID = b.METERID \n" +
            "\t\t \tWHERE a.DELETED='0'\n" +
            "\t\tGROUP BY\n" +
            "\t\t\ta.CENTERID,\n" +
            "\t\t\tb.CENTERDAY \n" +
            "\t\t) b ON a.ID = b.CENTERID \n" +
            "WHERE\n" +
            "\tb.CENTERDAY IS NOT NULL\n" +
            "\tand\n" +
            "\ta.`STATUS`='enable' and a.DELETED='0'")
    List<EquEnergyMeterCenterDosageDayDo> calRecordDataToCenterDosageDay();
}
