package cn.iocoder.yudao.module.mes.controller.admin.machine;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.mes.controller.admin.machine.vo.MachineComponentListReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.machine.vo.MachineComponentRespVO;
import cn.iocoder.yudao.module.mes.controller.admin.machine.vo.MachineComponentSaveReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.machine.vo.MachineTypeEnum;
import cn.iocoder.yudao.module.mes.dal.dataobject.machine.MachineComponentDO;
import cn.iocoder.yudao.module.mes.service.machine.MachineComponentService;
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 javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

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

    @Resource
    private MachineComponentService machineComponentService;

    @PostMapping("/create")
    @Operation(summary = "创建机台")
    @PreAuthorize("@ss.hasPermission('mes:machine-component:create')")
    public CommonResult<Long> createMachineComponent(@Valid @RequestBody MachineComponentSaveReqVO createReqVO) {
        return success(machineComponentService.createMachineComponent(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新机台")
    @PreAuthorize("@ss.hasPermission('mes:machine-component:update')")
    public CommonResult<Boolean> updateMachineComponent(@Valid @RequestBody MachineComponentSaveReqVO updateReqVO) {
        machineComponentService.updateMachineComponent(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得机台")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mes:machine-component:query')")
    public CommonResult<MachineComponentRespVO> getMachineComponent(@RequestParam("id") Long id) {
        MachineComponentDO machineComponent = machineComponentService.getMachineComponent(id);
        return success(BeanUtils.toBean(machineComponent, MachineComponentRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得机台列表")
    public CommonResult<List<MachineComponentRespVO>> getMachineComponentList(@Valid MachineComponentListReqVO listReqVO) {
        List<MachineComponentDO> list = machineComponentService.getMachineComponentList(listReqVO);
        return success(BeanUtils.toBean(list, MachineComponentRespVO.class));
    }
    @GetMapping("/listPipelineAndMachine")
    @Operation(summary = "获得产线和机台列表")
    public CommonResult<List<MachineComponentRespVO>> listPipelineAndMachine(@Valid MachineComponentListReqVO listReqVO) {
        List<Integer> listTypes = new ArrayList<>();
        listTypes.add(MachineTypeEnum.产线.getValue());
        listTypes.add(MachineTypeEnum.机台.getValue());
        listReqVO.setComponentTypes(listTypes);
        listReqVO.setComponentType(null);
        List<MachineComponentDO> list = machineComponentService.getMachineComponentList(listReqVO);
        return success(BeanUtils.toBean(list, MachineComponentRespVO.class));
    }
    @GetMapping("/export-excel")
    @Operation(summary = "导出机台 Excel")
    @PreAuthorize("@ss.hasPermission('mes:machine-component:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMachineComponentExcel(@Valid MachineComponentListReqVO listReqVO,
                                            HttpServletResponse response) throws IOException {
        List<MachineComponentDO> list = machineComponentService.getMachineComponentList(listReqVO);
        // 导出 Excel
        ExcelUtils.write(response, "机台.xls", "数据", MachineComponentRespVO.class,
                BeanUtils.toBean(list, MachineComponentRespVO.class));
    }
}