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

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.system.controller.admin.worklog.vo.form.*;
import cn.iocoder.yudao.module.system.convert.worklog.LogFormConvert;
import cn.iocoder.yudao.module.system.dal.dataobject.worklog.LogFormDO;
import cn.iocoder.yudao.module.system.dal.dataobject.worklog.LogRuleDO;
import cn.iocoder.yudao.module.system.service.worklog.LogFormService;
import cn.iocoder.yudao.module.system.service.worklog.LogRuleService;
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.validation.Valid;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 日志动态模板")
@RestController
@RequestMapping("/system/worklog/form")
@Validated
public class LogFormController {

    @Resource
    private LogFormService formService;

    @Resource
    private LogRuleService logRuleService;

    @PostMapping("/create")
    @Operation(summary = "创建动态模板")
    @PreAuthorize("@ss.hasPermission('worklog:form:create')")
    public CommonResult<Long> createForm(@Valid @RequestBody LogFormCreateReqVO createReqVO) {
        return success(formService.createForm(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新动态模板")
    @PreAuthorize("@ss.hasPermission('worklog:form:update')")
    public CommonResult<Boolean> updateForm(@Valid @RequestBody LogFormUpdateReqVO updateReqVO) {
        formService.updateForm(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除动态模板")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('worklog:form:delete')")
    public CommonResult<Boolean> deleteForm(@RequestParam("id") Long id) {
        formService.deleteForm(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得动态模板")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('worklog:form:query')")
    public CommonResult<LogFormRespVO> getForm(@RequestParam("id") Long id) {

        LogFormDO form = formService.getForm(id);
        LogFormRespVO logFormRespVO = LogFormConvert.INSTANCE.convert(form);

        if (form != null) {

            //获取规则信息
            LogRuleDO logRuleDO = logRuleService.getLogRule(form.getId());

            //设置日志类型
            logFormRespVO.setType(logRuleDO.getType());
        }

        return success(logFormRespVO);
    }

    @GetMapping("/list-all-simple")
    @Operation(summary = "获得动态模板的精简列表", description = "用于表单下拉框")
    public CommonResult<List<LogFormSimpleRespVO>> getSimpleForms() {
        List<LogFormDO> list = formService.getFormList();
        return success(LogFormConvert.INSTANCE.convertList2(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得动态模板分页")
    public CommonResult<PageResult<LogFormRespVO>> getFormPage(@Valid LogFormPageReqVO pageVO) {
        PageResult<LogFormDO> pageResult = formService.getFormPage(pageVO);
        return success(LogFormConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/page-by-rule")
    @Operation(summary = "获得我可以使用的模板分页")
    public CommonResult<PageResult<LogFormRespVO>> getFormPageByRule(@Valid LogFormPageReqVO pageVO) {

        PageResult<LogFormDO> pageResult = formService.getFormPageByRule(pageVO);
        return success(LogFormConvert.INSTANCE.convertPage(pageResult));
    }
}
