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

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import cn.iocoder.yudao.module.system.controller.admin.sys.vo.SysEvaluationTemplatePageReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.CheckInProcessSequenceDO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.SysEvaluationTemplatesDO;
import cn.iocoder.yudao.module.system.service.biz.SysEvaluationTemplatesService;
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.web.bind.annotation.*;

import javax.annotation.Resource;

import java.util.List;
import java.util.Objects;

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

@Tag(name = "管理后台 - 评估模板")
@RestController
@RequestMapping("/evaluationTemplates")
public class SysEvaluationTemplatesController {

    @Resource
    private SysEvaluationTemplatesService sysEvaluationTemplatesService;



    @PostMapping("/add")
    @Operation(summary = "创建评估模板")
    @PreAuthorize("@ss.hasPermission('evaluationTemplates:create')")
    @TenantIgnore
    public CommonResult<Boolean> add(@RequestBody SysEvaluationTemplatesDO templatesDO) {
        templatesDO.setTenantId(templatesDO.getTenantId() == null ? TenantContextHolder.getTenantId() : templatesDO.getTenantId());
        sysEvaluationTemplatesService.add(templatesDO);
        return success(true);
    }


    @GetMapping("/getEvaluationTemplateByPage")
    @Operation(summary = "评估模板列表")
    @TenantIgnore
    public CommonResult<PageResult<SysEvaluationTemplatesDO>> getEvaluationTemplateByPage(SysEvaluationTemplatePageReqVO reqVO){
        reqVO.setTenantIds(Objects.isNull(reqVO.getTenantIds()) ? new Long[]{TenantContextHolder.getTenantId()} : reqVO.getTenantIds());
        PageResult<SysEvaluationTemplatesDO> pageResult = sysEvaluationTemplatesService.getEvaluationTemplateByPage(reqVO);
        return success(BeanUtils.toBean(pageResult, SysEvaluationTemplatesDO.class));
    }


    @PostMapping("/getDetails")
    @Operation(summary = "评估模板详情")
    @Parameter(name = "id", description = "模板ID", required = true, example = "1024")
    @TenantIgnore
    public CommonResult<SysEvaluationTemplatesDO> getDetails(@RequestParam("id") Long id){
        return success(sysEvaluationTemplatesService.getDetails(id));
    }


    @PostMapping("/edit")
    @Operation(summary = "编辑评估模板")
    @TenantIgnore
    public CommonResult<Boolean> edit(@RequestBody SysEvaluationTemplatesDO templatesDO) {
        sysEvaluationTemplatesService.edit(templatesDO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除评估模板")
    @Parameter(name = "id", description = "模板ID", required = true, example = "1024")
    @TenantIgnore
    public CommonResult<Boolean> delete(@RequestParam("id") Long id) {
        sysEvaluationTemplatesService.delete(id);
        return success(true);
    }


    @PostMapping("/updateStatus")
    @Operation(summary = "修改评估模板状态")
    public CommonResult<Boolean> updateStatus(@RequestBody SysEvaluationTemplatesDO templatesDO){
        sysEvaluationTemplatesService.updateStatus(templatesDO);
        return success(true);
    }


    @GetMapping("/checkInProcessSeqInfo")
    @Operation(summary = "获取入住评估流程模板")
    public CommonResult<List<CheckInProcessSequenceDO>>  checkInProcessSeqInfo(@RequestParam(value = "orgType",required = false) Integer orgType){
        return success(sysEvaluationTemplatesService.checkInProcessSeqInfo(orgType));
    }
}
