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.common.util.object.BeanUtils;
import com.cf.imes.framework.es.core.valid.CreateGroup;
import com.cf.imes.framework.es.core.valid.UpdateGroup;
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.machine.MachineDO;
import com.cf.imes.module.system.service.machine.MachineService;
import com.cf.imes.module.system.validation.machine.MachineTypeValid;
import com.fasterxml.jackson.core.JsonProcessingException;
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 jakarta.annotation.Resource;
import jakarta.validation.Valid;
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;


@Tag(name = "管理后台 - 机台")
@RestController
@RequestMapping("/system/machine/")
@Validated
public class MachineController {

    @Resource
    private MachineService machineService;

    @PutMapping("/create-cutting")
    @Operation(summary = "创建机台")
    @PreAuthorize("@ss.hasAnyPermissions('system:machine:create','system:machine:duplet')")
    @OperateLog(logArgs = false,type = CREATE )
    public CommonResult<Long> createCutting(@Validated(CreateGroup.class) @RequestBody CuttingSaveReqVO createReqVO) throws JsonProcessingException {
        return success(machineService.createCutting(createReqVO));
    }

    @PostMapping("/update-cutting")
    @Operation(summary = "更新机台")
    @PreAuthorize("@ss.hasAnyPermissions('system:machine:update','system:machine:submit')")
    @OperateLog(logArgs = false,type = UPDATE)
    public CommonResult<Boolean> update(@Validated(UpdateGroup.class) @RequestBody CuttingSaveReqVO updateReqVO) {
        machineService.updateCutting(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete-cutting")
    @Operation(summary = "删除机台")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:machine:delete')")
    public CommonResult<Boolean> delete(@RequestParam("id") Long id) {
        machineService.deleteCutting(id);
        return success(true);
    }

    @DeleteMapping("/batch-delete-cutting")
    @Operation(summary = "批量删除机台")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:machine:delete')")
    public CommonResult<Boolean> batchDeleteCutting(@RequestParam("ids") List<Long> ids) {
        machineService.batchDeleteCutting(ids);
        return success(true);
    }

    @GetMapping("/get-cutting")
    @Operation(summary = "获得机台")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    //@PreAuthorize("@ss.hasPermission('machine::query')")
    public CommonResult<CuttingRespVO> get(@RequestParam("id") Long id) {
        CuttingRespVO respVO = machineService.getCutting(id);
        return success(respVO);
    }

    @GetMapping("/page")
    @Operation(summary = "获得机台分页")
    //@PreAuthorize("@ss.hasPermission('machine::query')")
    public CommonResult<PageResult<CuttingRespVO>> getPage(@Valid MachinePageReqVO pageReqVO) {
        PageResult<MachineDO> pageResult = machineService.getPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CuttingRespVO.class));
    }




    @GetMapping("getMachineTree")
    @Operation(summary = "获得机台树")
    @PreAuthorize("@ss.hasAnyPermissions('system:machine:query','system:base-setting')")
    public CommonResult<Map<Integer, List<MachineVO>>> getMachineTree(@RequestParam(required = false) Long type) {
        return success(machineService.getMachineTree(type));
    }

    @GetMapping(value = {"/list-all-simple", "/simple-list"})
    @Operation(summary = "获取机台精简信息列表", description = "主要用于前端的下拉选项")
    public CommonResult<List<MachineSimpleRespVO>> getSimpleDeptList(@RequestParam(required = false) Long organId,@RequestParam(required = false) Long type) {
        List<MachineSimpleRespVO> list = machineService.simpleList(type);
        return success(list);
    }



    @GetMapping("getBrandAndModel")
    @Operation(summary = "获得机台的品牌跟型号")
    //@PreAuthorize("@ss.hasPermission('machine::query')")
    public CommonResult<List<MachineBrandModelRespVO>> getBrandAndModel(@RequestParam @MachineTypeValid Integer type) {
        return success(machineService.getBrandAndModel(type));
    }


    @GetMapping("getMachineModelAll")
    @Operation(summary = "获得机台类型下所有机台的型号")
    //@PreAuthorize("@ss.hasPermission('machine::query')")
    public CommonResult<List<MachineSimpleRespVO>> getMachineModelAll(@RequestParam Integer type) {
        return success(machineService.getMachineModelAll(type));
    }


}