package com.ho.business.mapper;


import com.ho.business.entity.IndustrialElecPrice;
import com.ho.business.vo.req.industrialElec.IndustrialElecPriceReqVo;
import com.ho.business.vo.resp.industrialElec.IndustrialElecPriceRspVo;
import org.apache.ibatis.annotations.*;
import java.util.List;

@Mapper
public interface IndustrialElecPriceMapper {

    @SelectProvider(type = IndustrialElecPriceMapper.ElecPriceProvider.class, method = "pageList")
    List<IndustrialElecPrice> selectPriceByCondition(IndustrialElecPriceReqVo industrialElecPriceReqVo);


    class ElecPriceProvider {
        public String pageList(IndustrialElecPriceReqVo industrialElecPriceReqVo) {
            String sql = "SELECT * FROM industrial_elec_price where 1=1";
            if (industrialElecPriceReqVo.getRegionId()!= null) {
                sql += " and region_id=#{regionId}";
            }
            if (industrialElecPriceReqVo.getElectricityType()!= null) {
                sql += " and electricity_type=#{electricityType}";
            }
            if (industrialElecPriceReqVo.getCustomerType()!= null) {
                sql += " and customer_type=#{customerType}";
            }
            if (industrialElecPriceReqVo.getVoltageLevel()!= null) {
                sql += " and voltage_level=#{voltageLevel}";
            }
            if (industrialElecPriceReqVo.getExecutionTime()!= null) {
                sql += " and  execution_time=#{executionTime}";
            }
            return sql;
        }
    }


    @Insert("insert into industrial_elec_price(electricity_type,customer_type,voltage_level,non_time_price,feed_peak_price,feed_regular_price,feed_low_valley_price,line_peak_price," +
            "line_low_valley_price,line_regular_price,proxy_price,line_loss_price,transmission_price,running_price,add_price,peak_price,peak_price_interval,spike_price,spike_price_interval," +
            "regular_price,regular_price_interval,low_valley_price,low_valley_price_interval,deep_valley_price,deep_valley_price_interval,demand_price,capacity_price,execution_time," +
            "region_id,create_time)" +
            " values(#{electricityType},#{customerType},#{voltageLevel},#{nonTimePrice},#{feedPeakPrice},#{feedRegularPrice},#{feedLowValleyPrice},#{linePeakPrice}," +
            "#{lineLowValleyPrice},#{lineRegularPrice},#{proxyPrice},#{lineLossPrice},#{transmissionPrice},#{runningPrice},#{addPrice},#{peakPrice},#{peakPriceInterval},#{spikePrice},#{spikePriceInterval}," +
            "#{regularPrice},#{regularPriceInterval},#{lowValleyPrice},#{lowValleyPriceInterval},#{deepValleyPrice},#{deepValleyPriceInterval},#{demandPrice},#{capacityPrice},#{executionTime}," +
            "#{regionId},#{createTime})")
    int insert(IndustrialElecPrice industrialElecPrice);

    @Delete("delete from industrial_elec_price where id=#{id}")
    int deleteById(@Param("id") Long id);

    @Update("update industrial_elec_price set electricity_type=#{electricityType},customer_type=#{customerType},voltage_level=#{voltageLevel},non_time_price=#{nonTimePrice},feed_peak_price=#{feedPeakPrice},feed_regular_price=#{feedRegularPrice},feed_low_valley_price=#{feedLowValleyPrice},line_peak_price=#{linePeakPrice}," +
            "line_low_valley_price=#{lineLowValleyPrice},line_regular_price=#{lineRegularPrice},proxy_price=#{proxyPrice},line_loss_price=#{lineLossPrice},transmission_price=#{transmissionPrice},running_price=#{runningPrice},add_price=#{addPrice},peak_price=#{peakPrice},peak_price_interval=#{peakPriceInterval},spike_price=#{spikePrice},spike_price_interval=#{spikePriceInterval}," +
            "regular_price=#{regularPrice},regular_price_interval=#{regularPriceInterval},low_valley_price=#{lowValleyPrice},low_valley_price_interval=#{lowValleyPriceInterval},deep_valley_price=#{deepValleyPrice},deep_valley_price_interval=#{deepValleyPriceInterval},demand_price=#{demandPrice},capacity_price=#{capacityPrice},execution_time=#{executionTime}," +
            "region_id=#{regionId},update_time=#{updateTime}  where id=#{id}")
    int updateById(IndustrialElecPrice industrialElecPrice);

    @Select("select * from industrial_elec_price where electricity_type=#{electricityType} and customer_type=#{customerType} and voltage_level=#{voltageLevel} and region_id=#{regionId} and execution_time=#{executionTime}")
    IndustrialElecPrice findByCondition(IndustrialElecPriceReqVo industrialElecPriceReqVo);

    @Select("select * from industrial_elec_price where id=#{id}")
    IndustrialElecPrice findById(@Param("id")Long id);

    @Select("select peak_price,spike_price,regular_price,low_valley_price,deep_valley_price,execution_time from industrial_elec_price where region_id=#{regionId} and electricity_type=#{electricityType} and customer_type=#{customerType}" +
            " and voltage_level=#{voltageLevel} and execution_time>#{executionStartTime} and execution_time<=#{executionEndTime} order by execution_time")
    List<IndustrialElecPriceRspVo> findElecPriceListByDate(@Param("regionId")Integer regionId, @Param("electricityType")Integer electricityType,@Param("customerType") Integer customerType, @Param("voltageLevel")Integer voltageLevel,@Param("executionStartTime") String executionStartTime,@Param("executionEndTime")String executionEndTime);

    @Insert({
            "<script>",
            "insert into industrial_elec_price(electricity_type,customer_type,voltage_level,non_time_price,feed_peak_price,feed_regular_price,feed_low_valley_price,line_peak_price," +
                    "line_low_valley_price,line_regular_price,proxy_price,line_loss_price,transmission_price,running_price,add_price,peak_price,peak_price_interval,spike_price,spike_price_interval,"+
                    "regular_price,regular_price_interval,low_valley_price,low_valley_price_interval,deep_valley_price,deep_valley_price_interval,demand_price,capacity_price,execution_time,"+
                    "region_id,create_time) values ",
            "<foreach collection='industrialElecPriceList' item='item' index='index' separator=','>",
            "(#{item.electricityType},#{item.customerType},#{item.voltageLevel},#{item.nonTimePrice},#{item.feedPeakPrice},#{item.feedRegularPrice},#{item.feedLowValleyPrice},#{item.linePeakPrice},"+
                    "#{item.lineLowValleyPrice},#{item.lineRegularPrice},#{item.proxyPrice},#{item.lineLossPrice},#{item.transmissionPrice},#{item.runningPrice},#{item.addPrice},#{item.peakPrice},#{item.peakPriceInterval},#{item.spikePrice},#{item.spikePriceInterval},"+
                    "#{item.regularPrice},#{item.regularPriceInterval},#{item.lowValleyPrice},#{item.lowValleyPriceInterval},#{item.deepValleyPrice},#{item.deepValleyPriceInterval},#{item.demandPrice},#{item.capacityPrice},#{item.executionTime}," +
                    "#{item.regionId},#{item.createTime})",
            "</foreach>",
            "</script>"
    })
    int insertCollectList(@Param(value="industrialElecPriceList") List<IndustrialElecPrice> industrialElecPriceList);

    @Delete("delete from industrial_elec_price where region_id=#{regionId} and execution_time=#{executionTime}")
    int deleteByRegionIdAndDate(@Param("regionId") Integer regionId,@Param("executionTime") String executionTime);
}
