package com.lightinit.hsdatashow.impl.api;

import com.lightinit.hsdatashow.entity.WaterSupplyAggregate;
import com.lightinit.hsdatashow.entity.WaterSupplyAggregateExample;
import com.lightinit.hsdatashow.mapper.WaterSupplyAggregateMapper;
import com.lightinit.hsdatashow.service.api.IWaterApiService;
import com.lightinit.hsdatashow.spring.DynamicDataContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @author WangYao
 * @date 2018/7/19 14:05
 * @description 水务运行API业务实现
 */
@Service
public class WaterApiImpl implements IWaterApiService {

    @Autowired
    private WaterSupplyAggregateMapper waterSupplyAggregateMapper;

    public void addWaterSupplyAggregate(int year, int value){
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        WaterSupplyAggregate entity1 = new WaterSupplyAggregate();
        entity1.setDataYear(year);
        entity1.setCostProportion(BigDecimal.valueOf(value));
        entity1.setCreatedAt(new Date());
        waterSupplyAggregateMapper.insert(entity1);
    }

    public void randomAddWaterSupplyAggregate(int years){
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        WaterSupplyAggregateExample example1 = new WaterSupplyAggregateExample();
//        WaterSupplyAggregateExample.Criteria criteria1 = example1.createCriteria();
        List<WaterSupplyAggregate> entityList1 = waterSupplyAggregateMapper.selectByExample(example1);
        Integer minYear = entityList1.stream().mapToInt(q -> q.getDataYear()).min().getAsInt();
        for (int i = 0; i < years; i++) {
            int randomValue = (int)(Math.random() * 100 + 1);
            addWaterSupplyAggregate(minYear - 1 - i, randomValue);
        }
    }
}
