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

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

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.ArresterOfflineExperimentReportDao;
import com.huatek.busi.dto.ArresterOfflineExperimentReportDto;
import com.huatek.busi.entity.ArresterOfflineExperimentReport;
import com.huatek.busi.mapper.ArresterOfflineExperimentReportMapper;
import com.huatek.busi.service.arrester.ArresterOfflineExperimentReportService;
import com.huatek.busi.vo.ArresterOfflineExperimentReportVo;
import com.huatek.busi.vo.routinetest.arrester.ArresterOfflineExperimentReportObj;
import com.huatek.busi.vo.routinetest.arrester.DCReferenceVoltage;
import com.huatek.busi.vo.routinetest.arrester.DischargeCountingAction;
import com.huatek.busi.vo.routinetest.arrester.InsulationResistance;
import com.huatek.frame.base.util.BeanUtils;
import com.huatek.torch.sql.beans.LocalUser;
import com.huatek.torch.util.tools.UserUtil;

/**
 * 避雷器离线实现报告
 *
 * @author buck_guo
 * @date 2019/08/23
 */
@Component
@Service(interfaceClass = ArresterOfflineExperimentReportService.class)
@Transactional(rollbackFor = Exception.class)
public class ArresterOfflineExperimentReportServiceImpl implements ArresterOfflineExperimentReportService {
    @Autowired
    private ArresterOfflineExperimentReportDao arresterOfflineExperimentReportDao;

    @Autowired
    private ArresterOfflineExperimentReportMapper arresterOfflineExperimentReportMapper;

    private final String[] specialColumns = new String[]{"insulationResistance", "dcReferenceVoltage", "dischargeCountingAction"};


    @Override
    public int saveArresterOfflineExperimentReport(String token, ArresterOfflineExperimentReportObj arresterOfflineExperimentReportObj) {

        //将以下三个对象转化为json字符串
        InsulationResistance insulationResistance = arresterOfflineExperimentReportObj.getInsulationResistance();
        DCReferenceVoltage dcReferenceVoltage = arresterOfflineExperimentReportObj.getDcReferenceVoltage();
        DischargeCountingAction dischargeCountingAction = arresterOfflineExperimentReportObj.getDischargeCountingAction();


        String insulationJsonStr = JSONObject.toJSONString(insulationResistance);
        String dcReferenceJsonStr = JSONObject.toJSONString(dcReferenceVoltage);
        String dischargeJsonStr = JSONObject.toJSONString(dischargeCountingAction);

        ArresterOfflineExperimentReport arresterOfflineExperimentReport = new ArresterOfflineExperimentReport();

        BeanUtils.copyProperties(arresterOfflineExperimentReportObj, arresterOfflineExperimentReport, specialColumns);

        arresterOfflineExperimentReport.setInsulationResistance(insulationJsonStr);
        arresterOfflineExperimentReport.setDcReferenceVoltage(dcReferenceJsonStr);
        arresterOfflineExperimentReport.setDischargeCountingAction(dischargeJsonStr);

//        arresterOfflineExperimentReport.setConclusionNotes(arresterOfflineExperimentReportObj.getConclusionNotes());
//        arresterOfflineExperimentReport.setChecker(arresterOfflineExperimentReportObj.getChecker());
//        arresterOfflineExperimentReport.setExperimenter(arresterOfflineExperimentReportObj.getExperimenter());
//        arresterOfflineExperimentReport.setSignature1(arresterOfflineExperimentReportObj.getSignature1());
//        arresterOfflineExperimentReport.setSignature2(arresterOfflineExperimentReportObj.getSignature2());
//
//        arresterOfflineExperimentReport.setCreateTime(LocalDateTime.now());
        LocalUser user = UserUtil.getLocalUser(token);
        if (StringUtils.isEmpty(arresterOfflineExperimentReportObj.getId())) {
            arresterOfflineExperimentReport.setCreateTime(LocalDateTime.now());
            if (user != null) {
                arresterOfflineExperimentReport.setCreateUser(user.getUid());
            }
        } else {
            arresterOfflineExperimentReport.setModifiedTime(LocalDateTime.now());
            if (user != null) {
                arresterOfflineExperimentReport.setModifiedUser(user.getUid());
            }
        }
        return arresterOfflineExperimentReportDao.saveOrUpdate(arresterOfflineExperimentReport);
    }

    @Override
    public PageInfo<ArresterOfflineExperimentReportVo>
    queryList(ArresterOfflineExperimentReportDto arresterOfflineExperimentReportDto) {
        PageInfo<ArresterOfflineExperimentReportVo> pageInfo = PageHelper.startPage(arresterOfflineExperimentReportDto.getPage(), arresterOfflineExperimentReportDto.getLimit())
                .doSelectPageInfo(() -> arresterOfflineExperimentReportMapper.queryList(arresterOfflineExperimentReportDto));

        pageInfo.getList().stream().forEach(arresterOfflineExperimentReportVo -> {
            if (arresterOfflineExperimentReportVo.getExperimentDate() != null) {
                arresterOfflineExperimentReportVo.setTestTime(new SimpleDateFormat("yyyy-MM-dd").format(arresterOfflineExperimentReportVo.getExperimentDate()));
            }
        });
        return pageInfo;
    }


    @Override
    public ArresterOfflineExperimentReportObj selectByPrimaryKey(String id) {

        ArresterOfflineExperimentReport arresterOfflineExperimentReport = arresterOfflineExperimentReportDao.get(id);
        ArresterOfflineExperimentReportObj vo = new ArresterOfflineExperimentReportObj();


        BeanUtils.copyProperties(arresterOfflineExperimentReport, vo, specialColumns);
        vo.setInsulationResistance(
                JSONObject.parseObject(arresterOfflineExperimentReport.getInsulationResistance(), InsulationResistance.class));
        vo.setDcReferenceVoltage(
                JSONObject.parseObject(arresterOfflineExperimentReport.getDcReferenceVoltage(), DCReferenceVoltage.class));
        vo.setDischargeCountingAction(
                JSONObject.parseObject(arresterOfflineExperimentReport.getDischargeCountingAction(), DischargeCountingAction.class));
        vo.setExperimentDate(arresterOfflineExperimentReport.getExperimentDate());

        return vo;
    }

    @Override
    public ArresterOfflineExperimentReportObj getByTreeId(String mainTreeId) {
        ArresterOfflineExperimentReportVo report = arresterOfflineExperimentReportMapper.getByTreeId(mainTreeId);
        if (report == null) {
            return null;
        }
        ArresterOfflineExperimentReportObj vo = new ArresterOfflineExperimentReportObj();
        BeanUtils.copyProperties(report, vo, specialColumns);
        vo.setInsulationResistance(
                JSONObject.parseObject(report.getInsulationResistance(), InsulationResistance.class));
        vo.setDcReferenceVoltage(JSONObject.parseObject(report.getDcReferenceVoltage(), DCReferenceVoltage.class));
        vo.setDischargeCountingAction(
                JSONObject.parseObject(report.getDischargeCountingAction(), DischargeCountingAction.class));
        return vo;
    }
}
