/*
 * Author: wangjian
 * 生成ServiceImpl
 */
package com.tofly.watermeter.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tofly.watermeter.config.ComConstans;
import com.tofly.watermeter.custom.dto.ForeignReadInfoDto;
import com.tofly.watermeter.custom.dto.ForeignReadUpdateDto;
import com.tofly.watermeter.custom.dto.SupplyYhYfDto;
import com.tofly.watermeter.custom.service.ReadMeterService;
import com.tofly.watermeter.entity.Readupdate;
import com.tofly.watermeter.service.ReadupdateService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.tofly.watermeter.mapper.ReadupdateMapper;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 水量变更历史表
 *
 * @author wangjieliu
 * @date Mon Dec 20 00:00:00 CST 2021
 */
@Slf4j
@Service("readupdateService")
@AllArgsConstructor
public class ReadupdateServiceImpl extends ServiceImpl<ReadupdateMapper, Readupdate> implements ReadupdateService {

    private final ReadMeterService readMeterService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveReadupdate(ForeignReadInfoDto dto) {
        //Readupdate
        if(CollUtil.isNotEmpty(dto.getDatas())){
            List<SupplyYhYfDto> dtos = new ArrayList<>();
            for (ForeignReadUpdateDto r:dto.getDatas()) {
                Readupdate readupdate = new Readupdate();
                BeanUtils.copyProperties(r,readupdate);
                readupdate.setSourceType(ComConstans.STR_ONE);
                //保存变更数据
                this.save(readupdate);
                SupplyYhYfDto supplyYhYfDto = new SupplyYhYfDto();
                supplyYhYfDto.setYhbh(r.getYhbh());
                supplyYhYfDto.setCwyf(r.getMonth());
                dtos.add(supplyYhYfDto);

            }
            //重新同步用水量数据
            return readMeterService.supplyQfcbqfmxls(dtos);
        }
        return false;
    }
}
