package cn.estsh.i3plus.core.apiservice.controller;

import cn.estsh.i3plus.core.api.iservice.busi.ISysLogOperateService;
import cn.estsh.i3plus.platform.common.util.PlatformConstWords;
import cn.estsh.i3plus.pojo.base.common.Pager;
import cn.estsh.i3plus.pojo.base.enumutil.ResourceEnumUtil;
import cn.estsh.i3plus.pojo.platform.platbean.SysLogOperate;
import cn.estsh.impp.framework.base.controller.CoreBaseController;
import cn.estsh.impp.framework.boot.exception.ImppBusiException;
import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder;
import cn.estsh.impp.framework.boot.exception.ImppExceptionEnum;
import cn.estsh.impp.framework.boot.util.ResultBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description : 系统资源服务demo
 * @Reference :
 * @Author : alwaysfrin
 * @CreateDate : 2018-09-26 10:34
 * @Modify:
 **/
@RestController
@Api(description="系统服务demo")
@RequestMapping(PlatformConstWords.BASE_URL + "/demo-mongo-service")
public class DemoMongoController extends CoreBaseController{
    private static final Logger LOGGER = LoggerFactory.getLogger(DemoMongoController.class);

    @Autowired
    private ISysLogOperateService logOperateService;

    @PostMapping(value="/logoperate/insert")
    @ApiOperation(value="添加操作日志",notes = "添加操作日志，可用于国际化")
    public ResultBean insertLogOperate(SysLogOperate logOperate) {
        try {
            logOperateService.insertSysLogOperate(logOperate);
            return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        }catch(ImppBusiException busExcep){
            return ResultBean.fail(busExcep).build();
        }catch(Exception e){
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    /**
     * 修改系统语言，可用于国际化
     * @return
     */
    @DeleteMapping(value="/logoperate/delete/{id}")
    @ApiOperation(value="删除日志",notes = "删除日志")
    public ResultBean deleteLog(@PathVariable String id) {
        try {
            logOperateService.deleteSysLogOperateById(Long.parseLong(id));
            return ResultBean.success("删除日志成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        }catch(ImppBusiException busExcep){
            return ResultBean.fail(busExcep).build();
        }catch(Exception e){
            return ResultBean.fail(e.getMessage()).setCode(ImppExceptionEnum.SYSTEM_EXCEPTION.getCode());
        }
    }

    @GetMapping(value="/logoperate/query")
    @ApiOperation(value="查询日志",notes = "查询日志")
    public ResultBean queryLanguage(SysLogOperate logOperate,Pager pager) {
        try {
            List logList = logOperateService.listSysLogOperate();
            return ResultBean.success("操作成功").setResultList(logList).setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        }catch(ImppBusiException busExcep){
            return ResultBean.fail(busExcep).build();
        }catch(Exception e){
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping(value="/logoperate/operate-user-name")
    @ApiOperation(value="查询日志",notes = "查询日志")
    public ResultBean queryLanguage(String OperateUserName) {
        try {
            List<SysLogOperate> logList = logOperateService.listSysLogOperateByOperateUserName(OperateUserName);

            long count = logOperateService.countAllSysLogOperate();
            LOGGER.info("=log size= " + count);

            return ResultBean.success("操作成功").setResultList(logList).setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        }catch(ImppBusiException busExcep){
            return ResultBean.fail(busExcep).build();
        }catch(Exception e){
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping(value="/logoperate/get/{id}")
    @ApiOperation(value="根据id查询日志",notes = "根据id查询日志")
    public ResultBean getLogById(@PathVariable String id) {
        try {
            logOperateService.getSysLogOperateById(Long.parseLong(id));
            return ResultBean.success().setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        }catch(ImppBusiException busExcep){
            return ResultBean.fail(busExcep).build();
        }catch(Exception e){
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }
}
