package cn.iocoder.yudao.module.system.controller.admin.worklog;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.system.controller.admin.user.vo.user.UserRespVO;
import cn.iocoder.yudao.module.system.controller.admin.worklog.vo.rule.LogRulePageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.worklog.vo.rule.LogRuleRespVO;
import cn.iocoder.yudao.module.system.controller.admin.worklog.vo.rule.LogRuleSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.worklog.LogRuleDO;
import cn.iocoder.yudao.module.system.service.user.AdminUserService;
import cn.iocoder.yudao.module.system.service.worklog.LogRuleService;
import cn.iocoder.yudao.module.system.service.worklog.LogUseService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 日志规则")
@RestController
@RequestMapping("/system/log-rule")
@Validated
public class LogRuleController {

    @Resource
    private LogRuleService logRuleService;

    @Resource
    private LogUseService logUseService;

    @Resource
    private AdminUserService userService;

    @PostMapping("/create")
    @Operation(summary = "创建日志规则")
    @PreAuthorize("@ss.hasPermission('system:log-rule:create')")
    public CommonResult<Long> createLogRule(@Valid @RequestBody LogRuleSaveReqVO createReqVO) {
        return success(logRuleService.createLogRule(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新日志规则")
    @PreAuthorize("@ss.hasPermission('system:log-rule:update')")
    public CommonResult<Boolean> updateLogRule(@Valid @RequestBody LogRuleSaveReqVO updateReqVO) {
        logRuleService.updateLogRule(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除日志规则")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:log-rule:delete')")
    public CommonResult<Boolean> deleteLogRule(@RequestParam("id") Long id) {
        logRuleService.deleteLogRule(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得日志规则")
    @Parameter(name = "formId", description = "日志模板编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:log-rule:query')")
    public CommonResult<LogRuleRespVO> getLogRule(@RequestParam("formId") Long formId) {

        LogRuleDO logRule = logRuleService.getLogRule(formId);

        return success(BeanUtils.toBean(logRule, LogRuleRespVO.class));
    }

    @GetMapping("/get-userInfo")
    @Operation(summary = "获得规则内模板使用者信息")
    @Parameter(name = "formId", description = "模板编号", required = true, example = "1")
    public CommonResult<List<UserRespVO>> getLogUseInfo(@RequestParam("formId") Long formId) {

        //获取模板使用者部门编号组
        List<Long> deptIds = logUseService.getDeptByFormId(formId);
        //获取模板使用者编号组
        List<Long> userIds = logUseService.getUserByFormId(formId);

        //根据部门编号组 获取用户信息
        List<UserRespVO> userInfo = userService.getUserByDeptIds(deptIds);

        return success(userInfo.stream().filter(user -> userIds.contains(user.getId())).collect(Collectors.toList()));
    }

    @GetMapping("/page")
    @Operation(summary = "获得日志规则分页")
    @PreAuthorize("@ss.hasPermission('system:log-rule:query')")
    public CommonResult<PageResult<LogRuleRespVO>> getLogRulePage(@Valid LogRulePageReqVO pageReqVO) {
        PageResult<LogRuleDO> pageResult = logRuleService.getLogRulePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, LogRuleRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出日志规则 Excel")
    @PreAuthorize("@ss.hasPermission('system:log-rule:export')")
    @OperateLog(type = EXPORT)
    public void exportLogRuleExcel(@Valid LogRulePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<LogRuleDO> list = logRuleService.getLogRulePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "日志规则.xls", "数据", LogRuleRespVO.class,
                        BeanUtils.toBean(list, LogRuleRespVO.class));
    }

}