package cn.wise.sc.energy.power.plant.business.controller;

import cn.hutool.core.util.StrUtil;
import cn.wise.sc.energy.power.plant.business.domain.CaseAnalysisInfo;
import cn.wise.sc.energy.power.plant.business.domain.CharacterParamInfo;
import cn.wise.sc.energy.power.plant.business.domain.EventInfo;
import cn.wise.sc.energy.power.plant.business.domain.PageQuery;
import cn.wise.sc.energy.power.plant.business.domain.vo.EntityVo;
import cn.wise.sc.energy.power.plant.business.repository.EventInfoRepository;
import cn.wise.sc.energy.power.plant.business.service.IEventInfoService;
import cn.wise.sc.energy.power.plant.business.utils.BeanUtilsExt;
import cn.wise.sc.energy.power.plant.common.core.bean.BaseResponse;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @description: 报警事件
 * @author: qh
 * @create: 2020-09-03 14:21
 **/
@CrossOrigin
@RestController
@RequestMapping("eventinfo/")
public class EventInfoController {

    final
    IEventInfoService iEventInfoService;

    @Autowired
    EventInfoRepository repository;

    public EventInfoController(IEventInfoService iEventInfoService) {
        this.iEventInfoService = iEventInfoService;
    }

    @GetMapping
    public BaseResponse<List<EntityVo>> listByState(Integer state) {
        List<EntityVo> entityVos = iEventInfoService.listByState(state);
        if (entityVos.size() == 0){
            return BaseResponse.errorMsg("没找着!");
        }else {
            return BaseResponse.okData(entityVos);
        }
    }

    @PostMapping("/page")
    public BaseResponse<Page<EventInfo>> page(@RequestBody PageQuery page) {

        return iEventInfoService.page(page);
    }

    @ApiOperation("更新事件状态")
    @PostMapping("/configUpdate")
    public BaseResponse<Boolean> interaction(@RequestBody EventInfo info) {

        EventInfo config;
        if (info.getId() == null) {
            config = repository.save(info);
        } else {
            config = repository.getOne(info.getId());
            BeanUtils.copyProperties(info, config, BeanUtilsExt.getNullPropertyNames(info));
            config = repository.save(config);
        }
        if (config.getId() != null) {
            return BaseResponse.okData(true);
        } else {
            return BaseResponse.okData(false);
        }

    }

}
