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.PageQuery;
import cn.wise.sc.energy.power.plant.business.repository.CaseAnalysisInfoRepository;
import cn.wise.sc.energy.power.plant.business.service.ICaseAnalysisInfoService;
import cn.wise.sc.energy.power.plant.business.utils.BeanUtilsExt;
import cn.wise.sc.energy.power.plant.common.core.bean.BaseResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.CrossOrigin;
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.time.LocalDateTime;

/**
 * @author neo.shu
 * @since 2020/9/12 15:29
 */

@CrossOrigin
@Validated
@RestController
@RequestMapping("caseanalysis/")
public class CaseAnalysisInfoController {

    @Autowired
    CaseAnalysisInfoRepository caseRepository;
    @Autowired
    ICaseAnalysisInfoService caseAnalysisInfoService;


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

        if (StrUtil.isBlank(page.getPlantId())) {
            return BaseResponse.errorMsg("电厂id不能为空！");
        }
        return caseAnalysisInfoService.page(page, page.getPlantId());
    }

    @PostMapping("/newOrUpdate")
    @Transactional(propagation = Propagation.REQUIRED)
    public BaseResponse<Boolean> add(@RequestBody CaseAnalysisInfo info) {
        CaseAnalysisInfo caseAnalysisInfo;
        if (info.getCaseId() == null || "".equals(info.getCaseId().trim())) {
            info.setCaseId(null);
            caseAnalysisInfo = caseRepository.save(info);
        } else {
            caseAnalysisInfo = caseRepository.getOne(info.getCaseId());
            BeanUtils.copyProperties(info, caseAnalysisInfo, BeanUtilsExt.getNullPropertyNames(info));
            caseAnalysisInfo.setUpdateTime(LocalDateTime.now());
            caseAnalysisInfo = caseRepository.save(caseAnalysisInfo);
        }
        if (caseAnalysisInfo.getId() != null) {
            return BaseResponse.okData(true);
        } else {
            return BaseResponse.okData(false);
        }
    }

    @PostMapping("/del")
    public BaseResponse<String> del(String id) {
        CaseAnalysisInfo caseAnalysisInfo = caseRepository.getOne(id);
        caseAnalysisInfo.setValid((short) 1);
        caseAnalysisInfo.setUpdateTime(LocalDateTime.now());
        caseAnalysisInfo = caseRepository.save(caseAnalysisInfo);
        if (caseAnalysisInfo != null) {
            return BaseResponse.okData("删除成功");
        } else {
            return BaseResponse.okData("删除失败");
        }
    }

}
