package com.rf.psychological.opLog.rest;

import com.alibaba.fastjson.JSONObject;
import com.rf.psychological.opLog.dao.SysLogEntity;
import com.rf.psychological.security.SafetyProcess;
import com.rf.psychological.opLog.service.SysLogService;
import com.rf.psychological.utils.Constant;
import com.rf.psychological.utils.HttpStatus;
import com.rf.psychological.utils.Result;
import com.rf.psychological.base.rest.BaseController;
import com.rf.psychological.utils.Utils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author lpf
 * @description:
 * @date 2022/1/1310:50
 */
@Slf4j
@RestController
@RequestMapping("/syslog")
@Api(tags = "操作日志管理")
public class SysLogController extends BaseController {

    @Autowired
    private SysLogService sysLogService;


    /**
     *
     * @param institutionNo 机构编号
     * @param searchKey 操作名称
     * @param beginTime 开始时间，格式yyyy-MM-dd
     * @param endTime 结束时间，格式yyyy-MM-dd
     * @param searchUserName 操作者姓名
     * @param type 0---表示普通用户，1--表示管理员，2--表示超级管理员,""空时表示查机构下的所有操作
     * @param beginNum 起始页
     * @param pageSize 每页显示个数
     * @return
     */
    @SafetyProcess
    @GetMapping("/find")
    @ApiOperation("查看操作日志")
    public Result getSysLog(String institutionNo, String searchKey, String beginTime, String endTime, String searchUserName, String type, int beginNum, int pageSize){
        if (pageSize == 0) {
            pageSize = 10;
        }
        Date beginDate, endDate;
        try {

            beginDate = Utils.date2time(beginTime, Constant.DATE_TRANS_FLAG_START);
            endDate = Utils.date2time(endTime,Constant.DATE_TRANS_FLAG_END);
        } catch (ParseException e) {
            log.error("日期格式转换错误："+e.getLocalizedMessage());
            e.printStackTrace();
            return fail(HttpStatus.SERVER_EXCEPTION,null,"服务器内部错误");
        }

        Page<SysLogEntity> byInstitutionNo = sysLogService.findByInstitutionNo(institutionNo, searchKey, beginDate, endDate, searchUserName, type, beginNum, pageSize);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("sysLogEntities", byInstitutionNo.getContent());
        jsonObject.put("num", byInstitutionNo.getTotalElements());
        return success(jsonObject);
    }


}
