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

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import com.huatek.busi.common.util.DateUtil;
import com.huatek.busi.vo.routinetest.TransformerTestVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huatek.busi.dao.GisOfflineExperimentReportDao;
import com.huatek.busi.dto.GisOfflineExperimentReportDto;
import com.huatek.busi.entity.GisOfflineExperimentReport;
import com.huatek.busi.entity.TransformerOfflineExperimentReport;
import com.huatek.busi.mapper.GisOfflineExperimentReportMapper;
import com.huatek.busi.service.gis.GisOfflineExperimentReportService;
import com.huatek.busi.vo.routinetest.TransformerTestPageVo;
import com.huatek.busi.vo.routinetest.gis.ActionFeaturePart;
import com.huatek.busi.vo.routinetest.gis.DcResistorPart;
import com.huatek.busi.vo.routinetest.gis.GisOfflineExperimentReportVo;
import com.huatek.busi.vo.routinetest.gis.LowVoltageActionPart;
import com.huatek.busi.vo.routinetest.gis.Sf6GasWaterContentPart;
import com.huatek.busi.vo.routinetest.gis.Sf6RelayAccuracyPart;
import com.huatek.busi.vo.routinetest.transformer.WindingBushingDCResistance;
import com.huatek.frame.base.util.BeanUtils;
import com.huatek.torch.sql.beans.LocalUser;
import com.huatek.torch.util.tools.UserUtil;

/**
 * Gis离线试验报告Service实现
 *
 * @author anker_cheng
 * @date 2019/08/24
 */
@Component
@Service(interfaceClass = GisOfflineExperimentReportService.class)
@Transactional(rollbackFor = Exception.class)
public class GisOfflineExperimentReportServiceImpl implements GisOfflineExperimentReportService {
    @Autowired
    private GisOfflineExperimentReportDao gisOfflineExperimentReportDao;
    @Autowired
    private GisOfflineExperimentReportMapper gisOfflineExperimentReportMapper;

    private final String[] specialColumns = new String[]{"brandPart", "lowVoltageActionPart", "dcResistorPart",
            "actionFeaturePart", "sf6RelayAccuracyPart", "sf6GasWaterContentPart"};

    @Override
    public PageInfo<GisOfflineExperimentReportVo>
    findGisOfflineExperimentReportList(GisOfflineExperimentReportDto gisOfflineExperimentReportDto) {
        PageInfo<GisOfflineExperimentReportVo> pageInfo = PageHelper.startPage(gisOfflineExperimentReportDto.getPage(), gisOfflineExperimentReportDto.getLimit())
                .doSelectPageInfo(() -> gisOfflineExperimentReportMapper
                        .findGisOfflineExperimentReportList(gisOfflineExperimentReportDto));
        pageInfo.getList().stream().forEach(gisOfflineExperimentReportVo -> {
            GisOfflineExperimentReport find = gisOfflineExperimentReportDao.get(gisOfflineExperimentReportVo.getId());
            LowVoltageActionPart part = JSONObject.parseObject(find.getLowVoltageActionPart(), LowVoltageActionPart.class);
            if (part.getTestDate() != null) {
                gisOfflineExperimentReportVo.setTestTime(part.getTestDate().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
            }
        });
        return pageInfo;
    }

    @Override
    public GisOfflineExperimentReportVo getGisOfflineExperimentReport(String id) {
        GisOfflineExperimentReport gisOfflineExperimentReport = gisOfflineExperimentReportDao.get(id);
        if (gisOfflineExperimentReport == null) {
            return null;
        }
        GisOfflineExperimentReportVo vo = new GisOfflineExperimentReportVo();
        BeanUtils.copyProperties(gisOfflineExperimentReport, vo, specialColumns);
        vo.setLowVoltageActionPart(
                JSONObject.parseObject(gisOfflineExperimentReport.getLowVoltageActionPart(), LowVoltageActionPart.class));
        vo.setDcResistorPart(
                JSONObject.parseObject(gisOfflineExperimentReport.getDcResistorPart(), DcResistorPart.class));
        vo.setActionFeaturePart(
                JSONObject.parseObject(gisOfflineExperimentReport.getActionFeaturePart(), ActionFeaturePart.class));
        vo.setSf6RelayAccuracyPart(
                JSONObject.parseObject(gisOfflineExperimentReport.getSf6RelayAccuracyPart(), Sf6RelayAccuracyPart.class));
        vo.setSf6GasWaterContentPart(JSONObject.parseObject(gisOfflineExperimentReport.getSf6GasWaterContentPart(),
                Sf6GasWaterContentPart.class));
        return vo;
    }

    @Override
    public synchronized int saveGisOfflineExperimentReport(String token, GisOfflineExperimentReportDto param) {
        // 用户
        LocalUser user = UserUtil.getLocalUser(token);
        // 保存实体
        GisOfflineExperimentReport gisOfflineExperimentReport = new GisOfflineExperimentReport();
        BeanUtils.copyProperties(param, gisOfflineExperimentReport, specialColumns);
        if (StringUtils.isEmpty(gisOfflineExperimentReport.getId())) {
            gisOfflineExperimentReport.setCreatedTime(LocalDateTime.now());
            if (user != null) {
                gisOfflineExperimentReport.setCreator(user.getUid());
            }
        } else {
            gisOfflineExperimentReport.setModifiedTime(LocalDateTime.now());
            if (user != null) {
                gisOfflineExperimentReport.setModifier(user.getUid());
            }
        }
        gisOfflineExperimentReport.setLowVoltageActionPart(JSONObject.toJSONString(param.getLowVoltageActionPart()));
        gisOfflineExperimentReport.setDcResistorPart(JSONObject.toJSONString(param.getDcResistorPart()));
        gisOfflineExperimentReport.setActionFeaturePart(JSONObject.toJSONString(param.getActionFeaturePart()));
        gisOfflineExperimentReport.setSf6RelayAccuracyPart(JSONObject.toJSONString(param.getSf6RelayAccuracyPart()));
        gisOfflineExperimentReport
                .setSf6GasWaterContentPart(JSONObject.toJSONString(param.getSf6GasWaterContentPart()));
        // 设置试验日期
        setGisOfflineTime(param, gisOfflineExperimentReport);

        return gisOfflineExperimentReportDao.saveOrUpdate(gisOfflineExperimentReport);
    }

    private void setGisOfflineTime(GisOfflineExperimentReportDto param, GisOfflineExperimentReport report){

        if (param.getLowVoltageActionPart() != null) {
            report.setTestTime(DateUtil.toLocalDateTime(param.getLowVoltageActionPart().getTestDate()));
            return;
        }
        if (param.getDcResistorPart() != null) {
            report.setTestTime(DateUtil.toLocalDateTime(param.getDcResistorPart().getTestDate()));
            return;
        }
        if (param.getActionFeaturePart() != null) {
            report.setTestTime(DateUtil.toLocalDateTime(param.getActionFeaturePart().getTestDate()));
            return;
        }
        if (param.getSf6RelayAccuracyPart() != null) {
            report.setTestTime(DateUtil.toLocalDateTime(param.getSf6RelayAccuracyPart().getTestDate()));
            return;
        }
        if (param.getSf6GasWaterContentPart() != null) {
            report.setTestTime(DateUtil.toLocalDateTime(param.getSf6GasWaterContentPart().getTestDate()));
        }
    }

    @Override
    public GisOfflineExperimentReportVo getByTreeId(String mainTreeId) {
        GisOfflineExperimentReport gisOfflineExperimentReport = gisOfflineExperimentReportMapper.getByTreeId(mainTreeId);
        if (gisOfflineExperimentReport == null) {
            return null;
        }
        GisOfflineExperimentReportVo vo = new GisOfflineExperimentReportVo();
        BeanUtils.copyProperties(gisOfflineExperimentReport, vo, specialColumns);
        vo.setLowVoltageActionPart(
                JSONObject.parseObject(gisOfflineExperimentReport.getLowVoltageActionPart(), LowVoltageActionPart.class));
        vo.setDcResistorPart(
                JSONObject.parseObject(gisOfflineExperimentReport.getDcResistorPart(), DcResistorPart.class));
        vo.setActionFeaturePart(
                JSONObject.parseObject(gisOfflineExperimentReport.getActionFeaturePart(), ActionFeaturePart.class));
        vo.setSf6RelayAccuracyPart(
                JSONObject.parseObject(gisOfflineExperimentReport.getSf6RelayAccuracyPart(), Sf6RelayAccuracyPart.class));
        vo.setSf6GasWaterContentPart(JSONObject.parseObject(gisOfflineExperimentReport.getSf6GasWaterContentPart(),
                Sf6GasWaterContentPart.class));
        return vo;
    }

}
