package com.huatek.busi.service.impl.dataoutput;

import java.time.LocalDate;
import java.util.List;

import com.huatek.busi.dao.DataOutputSimulateEntityDao;
import com.huatek.busi.dao.DataOutputWarningEntityDao;
import com.huatek.busi.dto.SimulateResultDto;
import com.huatek.busi.entity.DataOutputEntitySimulation;
import com.huatek.busi.entity.DataOutputEntityWarning;
import com.huatek.busi.vo.warning.BusiEqmWarningTaskVo;
import com.huatek.busi.where.SimulateResultSelectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.base.Objects;
import com.huatek.busi.common.SiteConstants.SiteTreeEnum;
import com.huatek.busi.dao.DataOutputEntityDao;
import com.huatek.busi.dto.DataOutputEntityDto;
import com.huatek.busi.entity.DataOutputEntity;
import com.huatek.busi.mapper.DataOutputEntityMapper;
import com.huatek.busi.service.dataoutput.DataOutputEntityService;
import com.huatek.busi.service.impl.dataoutput.helper.DataOutputExecutors;
import com.huatek.busi.service.impl.dataoutput.helper.DataOutputUtil;
import com.huatek.busi.vo.dataoutput.DataOutputEntityVo;
import com.huatek.busi.vo.dataoutput.OutputEquipmentVo;
import com.huatek.torch.frame.exception.OwlException;

/**
 * 数据输出记录 ServiceImpl
 *
 * @author anker_cheng
 * @date Oct 12, 2019 9:57:59 AM
 */
@Component
@Service(interfaceClass = DataOutputEntityService.class)
@Transactional(rollbackFor = Exception.class)
public class DataOutputEntityServiceImpl implements DataOutputEntityService {

    @Autowired
    private DataOutputEntityMapper dataOutputEntityMapper;
    @Autowired
    private DataOutputEntityDao dataOutputEntityDao;

    @Autowired
    private DataOutputSimulateEntityDao dataOutputSimulateEntityDao;

    @Autowired
    private DataOutputWarningEntityDao dataOutputWarningEntityDao;

    @Autowired
    private DataOutputExecutors dataOutputExecutors;

    @Value("${dataoutput.base.path}")
    private String basePath;

    @Override
    public String getBasePath() {
        return basePath;
    }

    @Override
    public PageInfo<DataOutputEntityVo> findDataOutputEntityPage(DataOutputEntityDto dataOutputEntityDto) {
        return PageHelper.startPage(dataOutputEntityDto.getPage(), dataOutputEntityDto.getLimit())
            .doSelectPageInfo(() -> dataOutputEntityMapper.selectDataOutputEntityPage(dataOutputEntityDto));
    }

    @Override
    public DataOutputEntityVo getDataOutputEntityVo(String id) {
        return dataOutputEntityMapper.getDataOutputEntityVo(id);
    }


    @Override
    public SimulateResultDto getSimulateResultDto(String mainId) {
        return dataOutputEntityMapper.getSimulateResultDto(mainId);
    }

    @Override
    public int deleteByDay(LocalDate day) {
        int count = dataOutputEntityMapper.deleteByDay(day);
//        if(count > 0) {
//            //删除文件
//            DataOutputUtil.deleteFile(basePath, day);
//        }
        return count;
    }

    @Override
    public List<OutputEquipmentVo> findOutputEquips(String mainEquipmentType) {
        if (Objects.equal(SiteTreeEnum.BYQ_EQM.getValue(), mainEquipmentType)) {
            return dataOutputEntityMapper.transformerList();
        }
        if (Objects.equal(SiteTreeEnum.GIS_HL_EQM.getValue(), mainEquipmentType)) {
            return dataOutputEntityMapper.gisList();
        }
        if (Objects.equal(SiteTreeEnum.BLQ_HL_EQM.getValue(), mainEquipmentType)) {
            return dataOutputEntityMapper.arresterList();
        }
        if (Objects.equal(SiteTreeEnum.OTHER_EQM.getValue(), mainEquipmentType)) {
            return dataOutputEntityMapper.otherList();
        }
        if (Objects.equal(SiteTreeEnum.GEN_EQM.getValue(), mainEquipmentType)) {
            return dataOutputEntityMapper.generatorList();
        }
        if (Objects.equal(SiteTreeEnum.CW_HL_EQM.getValue(), mainEquipmentType)) {
            return dataOutputEntityMapper.cwgList();
        }
        if (Objects.equal(SiteTreeEnum.DL_EQM.getValue(), mainEquipmentType)) {
            return dataOutputEntityMapper.dlList();
        }
        throw new OwlException("参数错误");
    }

    @Override
    public List<OutputEquipmentVo> findOutputMainEquips(String mainEquipmentType) {
        return dataOutputEntityMapper.findOutputMainEquips(mainEquipmentType);
    }

    @Override
    public int saveOrUpdate(DataOutputEntity entity) {
        return dataOutputEntityDao.saveOrUpdate(entity);
    }

    @Override
    public void doOutputSimulate(SimulateResultDto vo, LocalDate day) throws Exception {
        // 生成记录并保存
        DataOutputEntitySimulation entity = DataOutputUtil.newEntitySimulate(vo, day);
        if(entity!=null)
        {
            dataOutputSimulateEntityDao.saveOrUpdate(entity);
        	 // 查数据、写文件
            dataOutputExecutors.doOutputSimulate(basePath, entity.getFilePath(), entity.getFileName(), vo, LocalDate.now());
        }

    }

    @Override
    public void doOutputWarning(BusiEqmWarningTaskVo vo, LocalDate day) throws Exception {
        // 生成记录并保存
        DataOutputEntityWarning entity = DataOutputUtil.newEntityWarning(vo, day);
        if(entity!=null)
        {
            dataOutputWarningEntityDao.saveOrUpdate(entity);
            // 查数据、写文件
            dataOutputExecutors.doOutputWarning(basePath, entity.getFilePath(), entity.getFileName(), vo, LocalDate.now());
        }
    }

    @Override
    public void doOutput(OutputEquipmentVo vo, LocalDate day) throws Exception {
        // 生成记录并保存
        DataOutputEntity entity = DataOutputUtil.newEntity(vo, day);
        if(entity!=null)
        {
            dataOutputEntityDao.saveOrUpdate(entity);
            // 查数据、写文件
            dataOutputExecutors.doOutput(basePath, entity.getFilePath(), entity.getFileName(), vo, LocalDate.now());
        }

    }

    @Override
    public List<BusiEqmWarningTaskVo> findWarnList() {
        return dataOutputEntityMapper.findWarinList();
    }


}