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

import com.cf.imes.framework.common.enums.CommonStatusEnum;
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.framework.organ.core.context.OrganContextHolder;
import com.cf.imes.module.system.controller.admin.machine.vo.*;
import com.cf.imes.module.system.dal.dataobject.dept.DeptDO;
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 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')")
    @OperateLog(logArgs = false)
    public CommonResult<Long> createCutting(@Validated(CreateGroup.class) @RequestBody CuttingSaveReqVO createReqVO) {
        return success(machineService.createCutting(createReqVO));
    }

    @PostMapping("/update-cutting")
    @Operation(summary = "更新机台")
    //@PreAuthorize("@ss.hasPermission('machine::update')")
    @OperateLog(logArgs = false)
    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('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 = "获得默认机台模板")
    @Parameter(name = "machineType", description = "机台类型", required = true, example = "1")
    //@PreAuthorize("@ss.hasPermission('machine::query')")
    public CommonResult<CuttingTemplateRespVO> getDefaultCutting(@RequestParam Integer machineType) {
        return success(machineTemplateService.getDefaultCutting(machineType));
    }

   /* @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));
    }*/


    @GetMapping("getMachineTree")
    @Operation(summary = "获得机台树")
    //@PreAuthorize("@ss.hasPermission('machine::query')")
    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) {
        organId = organId == null ? OrganContextHolder.getOrganId() : organId;
        List<MachineSimpleRespVO> list = machineService.simpleList(organId,type);
        return success(list);
    }

}