package com.rongwei.bscommon.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rongwei.bscommon.sys.dao.EquEnergyMeterDosageMonthDao;
import com.rongwei.bscommon.sys.service.EquEnergyMeterDosageMonthService;
import com.rongwei.bsentity.domain.EquEnergyMeterDosageMonthDo;
import com.rongwei.rwcommon.utils.SecurityUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class EquEnergyMeterDosageMonthSetviceImpl extends
        ServiceImpl<EquEnergyMeterDosageMonthDao, EquEnergyMeterDosageMonthDo> implements EquEnergyMeterDosageMonthService {

    @Autowired
    EquEnergyMeterDosageMonthDao equEnergyMeterDosageMonthDao;
    @Autowired
    EquEnergyMeterDosageMonthService equEnergyMeterDosageMonthService;

    @Override
    public void calDayToMonth() {
        //1.查询月表中已存在的数据
        List<EquEnergyMeterDosageMonthDo> list = equEnergyMeterDosageMonthDao.queryDaySum();

        List<EquEnergyMeterDosageMonthDo> saveList =new ArrayList<>();
        List<EquEnergyMeterDosageMonthDo> updateList =new ArrayList<>();
        final UpdateWrapper<EquEnergyMeterDosageMonthDo>[] updateWrapper = new UpdateWrapper[]{null};


        list.forEach(v->{
            if (StringUtils.isNotBlank(v.getId())){
                updateList.add(v);
                updateWrapper[0] =new UpdateWrapper<>();
                this.setUpdateWrapper( updateWrapper[0],v);
                equEnergyMeterDosageMonthService.update(v, updateWrapper[0]);
            }else {
                v.setId(SecurityUtil.getUUID());

                saveList.add(v);
            }
        });

        if (!saveList.isEmpty()){
            equEnergyMeterDosageMonthService.saveBatch(saveList);
        }


    }


    public void setUpdateWrapper(UpdateWrapper<EquEnergyMeterDosageMonthDo> updateWrapper, EquEnergyMeterDosageMonthDo equEnergyMeterDosageMonthDo){
        updateWrapper.eq("id",equEnergyMeterDosageMonthDo.getId())
                .set("day1",equEnergyMeterDosageMonthDo.getDay1())
                .set("day2",equEnergyMeterDosageMonthDo.getDay2())
                .set("day3",equEnergyMeterDosageMonthDo.getDay3())
                .set("day4",equEnergyMeterDosageMonthDo.getDay4())
                .set("day5",equEnergyMeterDosageMonthDo.getDay5())
                .set("day6",equEnergyMeterDosageMonthDo.getDay6())
                .set("day7",equEnergyMeterDosageMonthDo.getDay7())
                .set("day8",equEnergyMeterDosageMonthDo.getDay8())
                .set("day9",equEnergyMeterDosageMonthDo.getDay9())
                .set("day10",equEnergyMeterDosageMonthDo.getDay10())
                .set("day11",equEnergyMeterDosageMonthDo.getDay11())
                .set("day12",equEnergyMeterDosageMonthDo.getDay12())
                .set("day13",equEnergyMeterDosageMonthDo.getDay13())
                .set("day14",equEnergyMeterDosageMonthDo.getDay14())
                .set("day15",equEnergyMeterDosageMonthDo.getDay15())
                .set("day16",equEnergyMeterDosageMonthDo.getDay16())
                .set("day17",equEnergyMeterDosageMonthDo.getDay17())
                .set("day18",equEnergyMeterDosageMonthDo.getDay18())
                .set("day19",equEnergyMeterDosageMonthDo.getDay19())
                .set("day20",equEnergyMeterDosageMonthDo.getDay20())
                .set("day21",equEnergyMeterDosageMonthDo.getDay21())
                .set("day22",equEnergyMeterDosageMonthDo.getDay22())
                .set("day23",equEnergyMeterDosageMonthDo.getDay23())
                .set("day24",equEnergyMeterDosageMonthDo.getDay24())
                .set("day25",equEnergyMeterDosageMonthDo.getDay25())
                .set("day26",equEnergyMeterDosageMonthDo.getDay26())
                .set("day27",equEnergyMeterDosageMonthDo.getDay27())
                .set("day28",equEnergyMeterDosageMonthDo.getDay28())
                .set("day29",equEnergyMeterDosageMonthDo.getDay29())
                .set("day30",equEnergyMeterDosageMonthDo.getDay30())
                .set("day31",equEnergyMeterDosageMonthDo.getDay31());
    }
}
