/**
 *
 */
package com.huatek.busi.controller;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Map;

import com.huatek.busi.entity.SoundVibrationParams;
import com.huatek.busi.entity.TransformerEvaluationResults;
import com.huatek.busi.service.transformer.SoundVibrationParamsService;
import com.huatek.torch.util.uuid.Uuid;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.huatek.busi.service.transformer.TransformerEvaluationResultsService;
import com.huatek.busi.vo.TransformerEvaluationResultsVo;
import com.huatek.torch.frame.tools.ConstantUtil;
import com.huatek.torch.frame.tools.ResultUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * @ClassName: TransformerEvaluationResultsController
 * @FullClassPath: com.huatek.busi.controller.TransformerEvaluationResultsController
 * @Description: 变压器评测控制器.
 * @author: Arno
 * @date: Sep 12, 2019 11:17:08 AM
 * @version: 1.0
 */
@Api(tags = {"变压器评测结果数据"})
@RestController
@RequestMapping("/api/transformerEvaluationResults")
public class TransformerEvaluationResultsController {

    @Reference(check = false)
    private TransformerEvaluationResultsService transformerEvaluationResultsService;

    @Reference(check = false)
    private SoundVibrationParamsService soundVibrationParamsService;

    /**
     * @Title: page
     * @Description: 变压器例行试验列表分页
     * @createDate: Aug 28, 2019 5:35:41 PM
     * @param    page
     * @param    limit
     * @return Map<String, Object>
     */
    @ApiOperation(value = "变压器评测结果列表分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mainTreeId", value = "站点树ID", required = false),
            @ApiImplicitParam(name = "startTime", value = "开始时间", required = false),
            @ApiImplicitParam(name = "endTime", value = "结束时间", required = false),
            @ApiImplicitParam(name = "page", value = "当前页", required = false),
            @ApiImplicitParam(name = "limit", value = "每页大小", required = false)
    })
    @GetMapping(value = "/page")
    public Map<String, Object> page(@RequestParam(value = "mainTreeId", required = true) String mainTreeId,
                                    @RequestParam(value = "startTime", required = false) String startTime,
                                    @RequestParam(value = "endTime", required = false) String endTime,
                                    @RequestParam(value = "page", required = false) Integer page,
                                    @RequestParam(value = "type", required = false) String type ,
                                    @RequestParam(value = "limit", required = false) Integer limit) {

        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate start = null;
        LocalDate end = null;


        if (StringUtils.isNotBlank(startTime)) {
            start = LocalDate.parse(startTime, df);
        }

        if (StringUtils.isNotBlank(endTime)) {
            end = LocalDate.parse(endTime, df).plusDays(1);
        }

        PageInfo<TransformerEvaluationResultsVo> pageInfo = transformerEvaluationResultsService.page(mainTreeId, start, end, page, limit, type);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, pageInfo.getTotal(), "", pageInfo.getList());
    }

    /**
     * 同步增加数据
     *
     * @param arrayList
     * @return
     */
    @PostMapping(value = "/add")
    public Map<String, Object> add(@RequestBody ArrayList<Map<String, Object>> arrayList) {
        for (Map<String, Object> objectMap : arrayList) {
            TransformerEvaluationResults entity = new TransformerEvaluationResults();
            entity.setAmplitude((String) objectMap.get("amplitude"));
            entity.setMainTreeId((String) objectMap.get("mainTreeId"));
            transformerEvaluationResultsService.save(entity);

            //保存声级震动数据
            SoundVibrationParams params = new SoundVibrationParams();
            params.setId(Uuid.getUUID());
            params.setMainTreeId((String) objectMap.get("mainTreeId"));
            params.setHighPoint((String) objectMap.get("highPoint"));
            params.setLowPoint((String) objectMap.get("lowPoint"));
            params.setCenterPoint((String) objectMap.get("centerPoint"));
            params.setNoisePoint((String) objectMap.get("noisePoint"));
            params.setCreateTime(LocalDateTime.now());
            soundVibrationParamsService.save(params);
        }
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "新增成功", "");
    }

}
