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

import lombok.extern.slf4j.Slf4j;
import cn.wise.sc.energy.power.plant.business.domain.AbstractEntity;
import cn.wise.sc.energy.power.plant.business.domain.vo.EntityVo;
import cn.wise.sc.energy.power.plant.business.service.IBaseService;
import cn.wise.sc.energy.power.plant.common.core.bean.EntityQuery;
import cn.wise.sc.energy.power.plant.common.core.bean.BaseResponse;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

/**
 * @description: 基础公共控制器
 * @author: qh
 * @create: 2020-07-20 18:18
 **/
@Slf4j
public class BaseController<IService extends IBaseService> {

    IService iService;

    @GetMapping("{id}")
    public BaseResponse<AbstractEntity> getByID(@PathVariable("id") Object id) {
        try {
            AbstractEntity entity = iService.getByID(id);
            return BaseResponse.okData(entity);
        } catch (Exception ex) {
            log.error(ex.getMessage());
            return BaseResponse.errorMsg("查询失败!");
        }
    }

    @DeleteMapping("{id}")
    public BaseResponse<AbstractEntity> delByID(@PathVariable("id") Object id) {
        try {
            iService.delByID(id);
            return BaseResponse.okMsg("删除成功");
        } catch (Exception ex) {
            log.error(ex.getMessage());
            return BaseResponse.errorMsg("删除失败失败!");
        }
    }

    @PostMapping
    public BaseResponse<EntityVo> addEntity(@RequestBody EntityQuery entityQuery) {
        try {
            EntityVo baseEntityVo = iService.add(entityQuery);
            return BaseResponse.okData(baseEntityVo);
        } catch (Exception ex) {
            log.error(ex.getMessage());
            return BaseResponse.errorMsg("删除失败失败!");
        }
    }

}
