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

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.organization.vo.*;
import cn.iocoder.yudao.module.mes.dal.dataobject.organization.OrganizationDO;
import cn.iocoder.yudao.module.mes.service.organization.OrganizationService;
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.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/organization")
@Validated
public class OrganizationController {

    @Resource
    private OrganizationService organizationService;

    @PostMapping("/create")
    @Operation(summary = "创建产线工位")
    //@PreAuthorize("@ss.hasPermission('mes:organization:create')")
    public CommonResult<Long> createOrganization(@Valid @RequestBody OrganizationSaveReqVO createReqVO) {
        return success(organizationService.createOrganization(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新产线工位")
    //@PreAuthorize("@ss.hasPermission('mes:organization:update')")
    public CommonResult<Boolean> updateOrganization(@Valid @RequestBody OrganizationSaveReqVO updateReqVO) {
        organizationService.updateOrganization(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除产线工位")
    @Parameter(name = "id", description = "编号", required = true)
    //@PreAuthorize("@ss.hasPermission('mes:organization:delete')")
    public CommonResult<Boolean> deleteOrganization(@RequestParam("id") Long id) {
        organizationService.deleteOrganization(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得产线工位")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    //@PreAuthorize("@ss.hasPermission('mes:organization:query')")
    public CommonResult<OrganizationRespVO> getOrganization(@RequestParam("id") Long id) {
        OrganizationDO organization = organizationService.getOrganization(id);
        return success(BeanUtils.toBean(organization, OrganizationRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得产线工位列表")
    //@PreAuthorize("@ss.hasPermission('mes:organization:query')")
    public CommonResult<List<OrganizationRespVO>> getOrganizationList(@Valid OrganizationListReqVO listReqVO) {
        List<OrganizationDO> list = organizationService.getOrganizationList(listReqVO);
        return success(organizationService.buildVOList(list));
    }
    @GetMapping("/listOrgWorker")
    @Operation(summary = "获得派工工位列表")
    //@PreAuthorize("@ss.hasPermission('mes:organization:query')")
    public CommonResult<List<OrganizationRespVO>> getListOrgWorker(@Valid OrganizationListReqVO listReqVO) {
        List<OrganizationDO> list = organizationService.getOrganizationList(listReqVO);
        return success(organizationService.buildWorkerVOList(list));
    }
    @GetMapping("/export-excel")
    @Operation(summary = "导出产线工位 Excel")
    //@PreAuthorize("@ss.hasPermission('mes:organization:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportOrganizationExcel(@Valid OrganizationListReqVO listReqVO,
                                        HttpServletResponse response) throws IOException {
        List<OrganizationDO> list = organizationService.getOrganizationList(listReqVO);
        // 导出 Excel
        ExcelUtils.write(response, "产线工位.xls", "数据", OrganizationRespVO.class,
                organizationService.buildVOList(list));
    }


    @GetMapping("/deviceParameterAnalysis")
    @Operation(summary = "设备运行参数分析")
    @PreAuthorize("@ss.hasPermission('iot:device:query')")
    public CommonResult<List<LineAnalysisTreeDTO.LineNode>> deviceParameterAnalysis(@RequestParam(value = "keyword", required = false) String keyword,
                                                                                    @RequestParam(value = "showDevices") Integer showDevices
                                                                                    ) {
        List<LineAnalysisTreeDTO.LineNode> list = organizationService.deviceParameterAnalysis(keyword,showDevices);
        return success(list);
    }

//
//    @GetMapping("/getParametersById")
//    @Operation(summary = "根据产线Id获取点位参数")
//    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    //@PreAuthorize("@ss.hasPermission('mes:organization:query')")
//    public CommonResult<List<DeviceParametersDTO>> getParametersById(@RequestParam("id") Long id) {
//        List<DeviceParametersDTO> organization = organizationService.getDeviceParametersByOrganizationId(id);
//        return success(organization);
//    }


}