package com.fufan.mtsaas.modules.base.log.controller;


import com.alibaba.fastjson.JSONObject;
import com.fufan.mtsaas.modules.base.log.bo.request.LogQueryRequest;
import com.fufan.mtsaas.modules.base.log.bo.request.LogSaveRequest;
import com.fufan.mtsaas.modules.base.log.bo.request.LoginLogQueryRequest;
import com.fufan.mtsaas.modules.base.log.model.TLoginLog;
import com.fufan.mtsaas.modules.base.log.model.TSysLog;
import com.fufan.mtsaas.modules.base.log.service.TSysLogSvc;
import com.fufan.mtsaas.modules.base.log.service.TSysLoginLogSvc;
import com.fufan.mtsaas.modules.common.bo.request.DataRequest;
import com.fufan.mtsaas.modules.common.bo.request.ResultPack;
import com.fufan.mtsaas.modules.common.bo.response.RequestPojo;
import com.fufan.mtsaas.modules.common.bo.response.Result;
import com.fufan.mtsaas.util.JSonUtil;
import com.fufan.mtsaas.util.ParamUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * <p>
 * 系统操作日志表
 * </p>
 *
 * @author Liby
 * @since 2022-03-16
 */
@Api(tags = "MTSaas：系统操作日志表", description = "SysLog对象接口")
@RestController
@RequestMapping("/SYS/SysLog")
public class TSysLogCtrl {
    @Autowired
    private TSysLogSvc targetService;

    @Autowired
    private TSysLoginLogSvc loginLogService;
//    @Autowired
//    private RedissonCommand redissonCommand;
//    public  ExecutorService activityService =
//            new ThreadPoolExecutor(
//                    1,
//                    1,
//                    0L,
//                    TimeUnit.MILLISECONDS,
//                    new ArrayBlockingQueue<Runnable>(10),
//                    new ThreadFactoryBuilder().setNameFormat("activity" + "-Service-%d").build(),
//                    new ThreadPoolExecutor.AbortPolicy());
    /**
     * 分页查询系统操作日志表
     * */
    @ApiOperation(value = "分页查询所有系统操作日志表，根据需要传入分页参数：pageSize每页数量(默认为10)，currentPage当前页数(默认为1)", httpMethod = "POST", produces = "application/json")
    @PostMapping(value ="/doQueryPage")
    public Result<TSysLog> doQueryPage(@RequestBody @ApiIgnore DataRequest dataRequest, LogQueryRequest logQueryRequest){
        ResultPack resultPack = new ResultPack();
        RequestPojo requestPojo = ParamUtil.doChange();

        JSONObject mapdata = dataRequest.getData().getParam();
        LogQueryRequest map = JSonUtil.toBean(mapdata.toJSONString(), LogQueryRequest.class);

        return resultPack.doResultPack(targetService.doQueryPage(map),requestPojo.getIntfId(),requestPojo.getAppKey());
    }

    /**
     * 分页查询系统登录日志表
     */
    @ApiOperation(value = "分页查询所有系统登录作日志表，根据需要传入分页参数：pageSize每页数量(默认为10)，currentPage当前页数(默认为1)", httpMethod = "POST", produces = "application/json")
    @PostMapping(value = "/doLoginQueryPage")
    public Result<TLoginLog> doLoginQueryPage(@RequestBody @ApiIgnore DataRequest dataRequest, LoginLogQueryRequest logQueryRequest) {
        ResultPack resultPack = new ResultPack();
        RequestPojo requestPojo = ParamUtil.doChange();

        JSONObject mapdata = dataRequest.getData().getParam();
        LoginLogQueryRequest map = JSonUtil.toBean(mapdata.toJSONString(), LoginLogQueryRequest.class);

        return resultPack.doResultPack(loginLogService.doQueryPage(map), requestPojo.getIntfId(), requestPojo.getAppKey());
    }

    /**
     * 新增
     * */
    @ApiOperation(value = "新增系统操作日志表", httpMethod = "POST", produces = "application/json")
    @RequestMapping(method = RequestMethod.POST)
    public Result<TSysLog> doSave(@RequestBody @ApiIgnore DataRequest dataRequest, LogSaveRequest logSaveRequest){
        ResultPack resultPack = new ResultPack();
        RequestPojo requestPojo = ParamUtil.doChange();

        JSONObject mapdata = dataRequest.getData().getParam();
        LogSaveRequest map = JSonUtil.toBean(mapdata.toJSONString(), LogSaveRequest.class);

        return resultPack.doResultPack(targetService.doSave(map,requestPojo),requestPojo.getIntfId(),requestPojo.getAppKey());
    }


//    /**
//     * test
//     * */
//    @ApiOperation(value = "test", httpMethod = "POST", produces = "application/json")
//    @RequestMapping(method = RequestMethod.POST,path = "test")
//    public Result<TSysLog> test(@RequestBody @ApiIgnore DataRequest dataRequest, LogSaveRequest logSaveRequest){
//       Result result = new Result<>();
//        RLock rLock =  redissonCommand.getFairLock("test");
//        if(redissonCommand.tryLock(rLock)){
//            try {
//                System.out.println("666666");
//                activityService.execute(()->{
//                    RLock rLock2 =  redissonCommand.getFairLock("test");
//                    if(redissonCommand.tryLock2(rLock2)){
//                        System.out.println(777777);
//                    }
//                });
//
//            }catch (Exception e){}finally {
////            rLock.unlock();
//            }
//        }
//
//
//        return result;
//    }

}
