package com.energy.manage.service.service.wavedatatransfer.impl;

import com.energy.manage.common.po.wavedatatransfer.WaveDataTransferPo;
import com.energy.manage.common.po.wavetransferhistory.WaveDataTransferHistoryPo;
import com.energy.manage.service.domain.dto.wavedatatransfer.WaveDataTransferDto;
import com.energy.manage.service.domain.vo.wavedatatransfer.WaveDataTransferVo;
import com.energy.manage.service.mappers.wavedatatransfer.WaveDataTransferMapper;
import com.energy.manage.service.mappers.wavedatatransferhistory.WaveDataTransferHistoryMapper;
import com.energy.manage.service.service.wavedatatransfer.WaveDatatransferServce;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author chy
 * @date 2024/11/7 16:23
 * @desc
 */
@Service
public class WaveDatatransferServceImpl implements WaveDatatransferServce {

    @Autowired
    private WaveDataTransferMapper waveDataTransferMapper;

    @Autowired
    private WaveDataTransferHistoryMapper waveDataTransferHistoryMapper;

    /**
     * 查询数据转换列表
     * @param waveDatatransferDto
     * @return
     */
    @Override
    public List<WaveDataTransferVo> queryDataTransfer(WaveDataTransferDto waveDatatransferDto) {
        PageHelper.startPage(waveDatatransferDto.getPageNum(), waveDatatransferDto.getPageSize());
        return waveDataTransferMapper.selectByCondition(waveDatatransferDto);
    }

    /**
     * 新增之前查重
     * @param waveDatatransferDto
     * @return
     */
    @Override
    public Boolean beforAddDataTransferQuery(WaveDataTransferDto waveDatatransferDto) {
        return waveDataTransferMapper.selectCountByCondition(waveDatatransferDto) > 0;
    }

    /**
     * 新增/覆盖数据转换
     * @param waveDatatransferPo
     * @return
     */
    @Override
    public int addDataTransfer(WaveDataTransferPo waveDatatransferPo, Integer loginUserId){
        WaveDataTransferPo exsitPo = waveDataTransferMapper.selectOne(waveDatatransferPo.setTransferStatus(-1));
        //设置更新人
        waveDatatransferPo.setUpdateBy(loginUserId);
        if(exsitPo != null){
            waveDatatransferPo.setUpdateTime(new Date());
            return waveDataTransferMapper.update(waveDatatransferPo.setId(exsitPo.getId()));
        }
        //设置创建人
        waveDatatransferPo.setCreateBy(loginUserId);
        return waveDataTransferMapper.insertSelective(waveDatatransferPo);
    }

    /**
     * 编辑数据转换
     *
     * @param waveDatatransferPo
     * @param loginUserId
     * @return
     */
    @Override
    public int editDataTransfer(WaveDataTransferPo waveDatatransferPo, Integer loginUserId) {
//        waveDatatransferPo.setUpdateBy(loginUserId);
//        return waveDataTransferMapper.updateByPrimaryKeySelective(waveDatatransferPo);
        return 0;
    }

    /**
     * 数据转换、重新转换
     * @param waveDatatransferDto
     * @return
     */
    @Override
    public int dataTransfer(WaveDataTransferDto waveDatatransferDto) {
        WaveDataTransferPo po = new WaveDataTransferPo();
        po.setId(waveDatatransferDto.getId());
        WaveDataTransferPo exsitPo = waveDataTransferMapper.selectOne(po);
        if(waveDatatransferDto.getRetransfer() == 1){
            WaveDataTransferHistoryPo historyPo = new WaveDataTransferHistoryPo();
            BeanUtils.copyProperties(exsitPo,historyPo);
            historyPo.setId(null);
            waveDataTransferHistoryMapper.insertSelective(historyPo);
        }
        exsitPo.setTransferStatus(0);
        exsitPo.setTransferAddr(waveDatatransferDto.getTransferAddr());
        exsitPo.setTransferStartTime(new Date());
        return waveDataTransferMapper.update(exsitPo);
    }

}
