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

import com.cf.imes.framework.es.core.valid.UpdateGroup;
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.service.machine.MachineTemplateService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import javax.validation.*;
import com.cf.imes.framework.common.pojo.PageResult;
import com.cf.imes.framework.common.pojo.CommonResult;
import com.cf.imes.framework.common.util.object.BeanUtils;

import java.util.List;
import java.util.Map;

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


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

    @Resource
    private MachineService machineService;

    @Resource
    private MachineTemplateService machineTemplateService;

    @PutMapping("/create-cutting")
    @Operation(summary = "创建开料机台")
    @PreAuthorize("@ss.hasPermission('machine::create')")
    public CommonResult<Long> createCutting(@Valid @RequestBody CuttingSaveReqVO createReqVO) {
        return success(machineService.createCutting(createReqVO));
    }

    @PostMapping("/update-cutting")
    @Operation(summary = "更新开料机台")
    @PreAuthorize("@ss.hasPermission('machine::update')")
    public CommonResult<Boolean> update( @RequestBody  @Validated(UpdateGroup.class) CuttingSaveReqVO updateReqVO) {
        machineService.updateCutting(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete-cutting")
    @Operation(summary = "删除开料机台")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('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('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("getDefaultCutting")
    @Operation(summary = "获得默认开料机台模板")
    @PreAuthorize("@ss.hasPermission('machine::query')")
    public CommonResult<CuttingTemplateRespVO> getDefaultCutting() {
        return success(machineTemplateService.getDefaultCutting());
    }

    @GetMapping("getDefaultDrill")
    @Operation(summary = "获得默认钻孔机台模板")
    @PreAuthorize("@ss.hasPermission('machine::query')")
    public CommonResult<DrillTemplateRespVO> getDefaultDrill() {
        return success(machineTemplateService.getDefaultDrill());
    }

    /*@GetMapping("/export-excel")
    @Operation(summary = "导出机台 Excel")
    @PreAuthorize("@ss.hasPermission('machine::export')")
    @OperateLog(type = EXPORT)
    public void exportExcel(@Valid MachinePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MachineDO> list = Service.getPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "机台.xls", "数据", MachineRespVO.class,
                        BeanUtils.toBean(list, MachineRespVO.class));
    }*/

    @PutMapping("/create-drill")
    @Operation(summary = "创建钻孔机台")
    @PreAuthorize("@ss.hasPermission('machine::create')")
    public CommonResult<Long> createDrill(@Valid @RequestBody DrillSaveReqVO createReqVO) {
        return success(machineService.createDrill(createReqVO));
    }

    @PostMapping("/update-drill")
    @Operation(summary = "更新钻孔机台")
    @PreAuthorize("@ss.hasPermission('machine::update')")
    public CommonResult<Boolean> updateDrill( @RequestBody  @Validated(UpdateGroup.class) DrillSaveReqVO updateReqVO) {
        machineService.updateDrill(updateReqVO);
        return success(true);
    }

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

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

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

    @GetMapping("getMachineTree")
    @Operation(summary = "获得机台树")
    @PreAuthorize("@ss.hasPermission('machine::query')")
    public CommonResult<Map<Integer, List<MachineVO>>> getMachineTree() {
        return success(machineService.getMachineTree());
    }


}