package com.cf.imes.module.system.controller.admin.machine;

import com.cf.imes.framework.common.pojo.CommonResult;
import com.cf.imes.framework.common.pojo.PageResult;
import com.cf.imes.module.system.controller.admin.machine.vo.*;
import com.cf.imes.module.system.dal.dataobject.machinetemplate.MachineTemplateDO;
import com.cf.imes.module.system.service.machine.MachineTemplateService;
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.stream.Collectors;

import static com.cf.imes.framework.common.pojo.CommonResult.success;

/**
 * @author Beal
 */
@RestController
@Tag(name = "机台模板授权")
@RequestMapping("/system/machine-template-auth")
public class MachineTemplateAuthController {

    @Resource
    private MachineTemplateService machineTemplateService;

    @PostMapping("organ-auth-template")
    @Operation(summary = "组织模板授权")
    @PreAuthorize("@ss.hasPermission('machine-template::query')")
    public CommonResult<Boolean> auth(@RequestBody MachineOrgAuthReq vo) {
        return success(machineTemplateService.auth(vo));
    }

    @GetMapping("organ-template-page")
    @Operation(summary = "组织模板授权分页列表")
    @PreAuthorize("@ss.hasPermission('machine-template::query')")
    public CommonResult<PageResult<OrganTemplateResp>> organTemplatePage(OrganTemplatePage page) {
        return success(machineTemplateService.organTemplatePage(page));
    }

    @GetMapping("organ-template-by-organId")
    @Operation(summary = "根据组织id查询组织模板授权")
    @PreAuthorize("@ss.hasPermission('machine-template::query')")
    @Parameter(name = "organId", description = "组织id", required = true, example = "1")
    public CommonResult<OrganTemplateResp> organTemplateByOrganId(@RequestParam Long organId ) {
        return success(machineTemplateService.organTemplateByOrganId(organId));
    }

    @GetMapping("page-template-machine-auth")
    @Operation(summary = "授权机台模板分页")
    @PreAuthorize("@ss.hasPermission('machine-template::query')")
    public CommonResult<PageResult<TemplateAutoResp>> pageTemplateAuth(MachinePageReqVO pageParam) {
        PageResult<MachineTemplateDO> page = machineTemplateService.page(pageParam);
        List<TemplateAutoResp> list = page.getList().stream().map(e->TemplateAutoResp.builder()
                .id(e.getId())
                .machineType(e.getMachineType())
                .templateName(e.getName())
                .isDefault(e.getIsDefault())
                .build()
        ).collect(Collectors.toList());

        return success(new PageResult<TemplateAutoResp>(list, page.getTotal()));
    }
}
