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.framework.operatelog.core.annotations.OperateLog;
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.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import java.util.List;
import java.util.Map;

import static com.cf.imes.framework.common.pojo.CommonResult.success;
import static com.cf.imes.framework.operatelog.core.enums.OperateTypeEnum.CREATE;
import static com.cf.imes.framework.operatelog.core.enums.OperateTypeEnum.UPDATE;

/**
 * @author there
 */
@RestController
@RequestMapping("/system/machine-template")
@Tag(name = "机台模板")
@Validated
public class MachineTemplateController {

    @Resource
    private MachineTemplateService machineTemplateService;


    @GetMapping("getMachineTemplateTree")
    @Operation(summary = "获得机台模板树")
    @PreAuthorize("@ss.hasPermission('machine-template::query')")
    public CommonResult<Map<Integer, List<MachineTemplateRespVO>>> getMachineTemplateTree() {
        return success(machineTemplateService.getMachineTemplateTree());
    }


    @GetMapping("page-template-machine")
    @Operation(summary = "机台模板分页")
    @PreAuthorize("@ss.hasPermission('machine-template::query')")
    public CommonResult<PageResult<MachineTemplateDO>> pageTemplateById(MachinePageReqVO pageParam) {
        return success(machineTemplateService.page(pageParam));
    }


    @PutMapping("insert-template-machine")
    @Operation(summary = "新增机台模板配置")
    @PreAuthorize("@ss.hasAnyPermissions('machine-template::create','machine-template::duplet')")
    @OperateLog(logArgs = false,type = CREATE)
    public CommonResult<Long> createCuttingTemplate(@RequestBody CuttingTemplateSaveReqVO reqVO) {
        return success(machineTemplateService.createCuttingTemplate(reqVO));
    }

    @PostMapping("update-template-machine")
    @Operation(summary = "修改机台模板配置")
    @PreAuthorize("@ss.hasAnyPermissions('machine-template::update','baseConfig:machine:submit')")
    @OperateLog(logArgs = false,type = UPDATE)
    public CommonResult<Boolean> updateCuttingTemplate(@RequestBody CuttingTemplateSaveReqVO reqVO) {
        return success(machineTemplateService.updateCuttingTemplate(reqVO));
    }

    @GetMapping("get-template-machine")
    @Operation(summary = "获取机台模板配置")
//    @PreAuthorize("@ss.hasPermission('machine-template::query')")
    public CommonResult<CuttingTemplateRespVO> getCuttingTemplateById(@RequestParam("id") String id) {
        return success(machineTemplateService.getCuttingTemplateById(id));
    }

    @DeleteMapping("delete-template-machine")
    @Operation(summary = "删除机台模板配置")
    @PreAuthorize("@ss.hasPermission('machine-template::delete')")
    public CommonResult<Boolean> deleteCutting(@RequestParam("id") Long id) {
        return success(machineTemplateService.deleteCuttingTemplate(id));
    }

    @DeleteMapping("batch-delete-cutting")
    @Operation(summary = "删除品牌即其下的机台模板配置")
    @PreAuthorize("@ss.hasPermission('machine-template::delete')")
    public CommonResult<Boolean> batchDeleteCutting(@RequestBody MachineTemplateBrandReqVO reqVO) {
        return success(machineTemplateService.batchDeleteCuttingTemplate(reqVO));
    }


    @PutMapping("insert-template-machine-brand")
    @Operation(summary = "新增机台模板品牌")
    @OperateLog(type = CREATE)
    @PreAuthorize("@ss.hasPermission('machine-template::insert-brand')")
    public CommonResult<Boolean> createMachineTemplateBrand(@RequestBody MachineTemplateBrandReqVO reqVO) {
        machineTemplateService.createMachineTemplateBrand(reqVO);
        return success(true);
    }


    @PutMapping("update-template-machine-brand")
    @Operation(summary = "修改机台品牌名称")
    @PreAuthorize("@ss.hasPermission('machine-template::create')")
    public CommonResult<Boolean> updateMachineTemplateBrand(@RequestBody MachineTemplateBrandReqVO reqVO) {
        machineTemplateService.updateMachineTemplateBrand(reqVO);
        return success(true);
    }


}
