package com.hzs.system.sys.controller.manage;

import cn.hutool.core.collection.CollectionUtil;
import com.hzs.common.core.annotation.AccessPermissions;
import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.enums.EOperationBusiness;
import com.hzs.common.core.enums.EOperationMethod;
import com.hzs.common.core.enums.EOperationModule;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.core.utils.poi.ExcelUtil;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.core.web.page.TableDataInfo;
import com.hzs.common.domain.system.sys.ext.SysOperationLogExt;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.system.sys.param.OperationLogParam;
import com.hzs.system.sys.service.ISysOperationLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * Description: 操作日志相关
 * Author: jiang chao
 * Time: 2022/9/27 15:10
 * Classname: SysOperationLogController
 * PackageName: com.hzs.system.sys.controller.manage
 */
@RestController
@RequestMapping("/manage/operation-log")
public class SysOperationLogController extends BaseController {

    @Autowired
    private ISysOperationLogService iSysOperationLogService;

    /**
     * 查询自己操作列表
     *
     * @param param 查询日志入参
     * @return
     */
    @AccessPermissions("operationRecord")
    @Log(module = EOperationModule.OPERATION_LOG, business = EOperationBusiness.OPERATION_LOG, method = EOperationMethod.SELECT)
    @GetMapping("/list-self")
    public TableDataInfo listSelf(OperationLogParam param) {
        param.setOperationTime(DateUtils.handTimeStartAndEnd(param.getOperationTime()));
        param.setUserId(SecurityUtils.getUserId());

        startPage();
        return getDataTable(iSysOperationLogService.queryList(param, SecurityUtils.getPkCountry()));
    }

    /**
     * 查询列表
     *
     * @param param 查询日志入参
     * @return
     */
    @AccessPermissions("operationRecord")
    @Log(module = EOperationModule.OPERATION_LOG, business = EOperationBusiness.OPERATION_LOG, method = EOperationMethod.SELECT)
    @GetMapping("/list")
    public TableDataInfo list(OperationLogParam param) {
        param.setOperationTime(DateUtils.handTimeStartAndEnd(param.getOperationTime()));

        startPage();
        return getDataTable(iSysOperationLogService.queryList(param, SecurityUtils.getPkCountry()));
    }

    /**
     * 导出
     *
     * @param response
     * @param param
     */
    @Log(module = EOperationModule.OPERATION_LOG, business = EOperationBusiness.OPERATION_LOG, method = EOperationMethod.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OperationLogParam param) {
        param.setOperationTime(DateUtils.handTimeStartAndEnd(param.getOperationTime()));

        List<SysOperationLogExt> list = iSysOperationLogService.queryList(param, SecurityUtils.getPkCountry());
        ExcelUtil<SysOperationLogExt> util = new ExcelUtil<>(SysOperationLogExt.class);
        util.exportExcel(response, list, "操作记录");
    }

    /**
     * 操作模块
     *
     * @return
     */
    @GetMapping("/module-type")
    public AjaxResult moduleType() {
        List<String> enumEntityList = new ArrayList<>();
        for (EOperationModule value : EOperationModule.values()) {
            if (StringUtils.isNotEmpty(value.getLabel())) {
                enumEntityList.add(value.getLabel());
            }
        }
        return AjaxResult.success(enumEntityList);
    }

    /**
     * 操作方法
     *
     * @return
     */
    @GetMapping("/method-type")
    public AjaxResult methodType() {
        List<String> enumEntityList = new ArrayList<>();
        for (EOperationMethod value : EOperationMethod.values()) {
            if (StringUtils.isNotEmpty(value.getLabel())) {
                enumEntityList.add(value.getLabel());
            }
        }
        return AjaxResult.success(enumEntityList);
    }


    /**
     * @description: 批量删除操作日志
     * @author: zhang jing
     * @date: 2024/1/30 11:29
     * @param: [syslog]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @PostMapping("/del-syslog")
    public AjaxResult delSyslog(@RequestBody SysOperationLogExt syslog) {
        if (CollectionUtil.isNotEmpty(syslog.getPkIdList())) {
            syslog.setPkModified(SecurityUtils.getUserId());
            iSysOperationLogService.delBatchSyslog(syslog);
        }
        return AjaxResult.success();
    }

}
