package cn.iocoder.yudao.module.pms.controller.admin.iotdevicematerial;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.pms.controller.admin.iotdevicematerial.vo.IotDeviceMaterialPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotdevicematerial.vo.IotDeviceMaterialRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotdevicematerial.vo.IotDeviceMaterialSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotdevicematerial.IotDeviceMaterialDO;
import cn.iocoder.yudao.module.pms.service.iotdevicematerial.IotDeviceMaterialService;
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 = "管理后台 - PMS 设备BOM物料关联")
@RestController
@RequestMapping("/pms/iot-device-material")
@Validated
public class IotDeviceMaterialController {

    @Resource
    private IotDeviceMaterialService iotDeviceMaterialService;

    @PostMapping("/create")
    @Operation(summary = "创建PMS 设备BOM物料关联")
    @PreAuthorize("@ss.hasPermission('pms:iot-device-material:create')")
    public CommonResult<Long> createIotDeviceMaterial(@Valid @RequestBody IotDeviceMaterialSaveReqVO createReqVO) {
        return success(iotDeviceMaterialService.createIotDeviceMaterial(createReqVO));
    }

    @PostMapping("/addMaterials")
    @Operation(summary = "批量 创建PMS 设备BOM挂载物料关联")
    @PreAuthorize("@ss.hasPermission('pms:iot-device-material:create')")
    public CommonResult<Long> addMaterials(@Valid @RequestBody List<IotDeviceMaterialSaveReqVO> materials) {
        return success(iotDeviceMaterialService.addMaterials(materials));
    }

    @PutMapping("/update")
    @Operation(summary = "更新PMS 设备BOM物料关联")
    @PreAuthorize("@ss.hasPermission('pms:iot-device-material:update')")
    public CommonResult<Boolean> updateIotDeviceMaterial(@Valid @RequestBody IotDeviceMaterialSaveReqVO updateReqVO) {
        iotDeviceMaterialService.updateIotDeviceMaterial(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除PMS 设备BOM物料关联")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('pms:iot-device-material:delete')")
    public CommonResult<Boolean> deleteIotDeviceMaterial(@RequestParam("id") Long id) {
        iotDeviceMaterialService.deleteIotDeviceMaterial(id);
        return success(true);
    }

    @DeleteMapping("/deleteDeviceMaterial")
    @Operation(summary = "删除PMS 设备BOM挂载物料关联")
    @Parameter(name = "bomNodeId", description = "BOM节点id", required = true)
    @Parameter(name = "code", description = "物料编码", required = true)
    @PreAuthorize("@ss.hasPermission('pms:iot-device-material:delete')")
    public CommonResult<Boolean> deleteDeviceMaterial(@RequestParam("bomNodeId") Long bomNodeId, @RequestParam("code") String code) {
        iotDeviceMaterialService.deleteDeviceMaterial(bomNodeId, code);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得PMS 设备BOM物料关联")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('pms:iot-device-material:query')")
    public CommonResult<IotDeviceMaterialRespVO> getIotDeviceMaterial(@RequestParam("id") Long id) {
        IotDeviceMaterialDO iotDeviceMaterial = iotDeviceMaterialService.getIotDeviceMaterial(id);
        return success(BeanUtils.toBean(iotDeviceMaterial, IotDeviceMaterialRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得PMS 设备BOM物料关联分页")
    @PreAuthorize("@ss.hasPermission('pms:iot-device-material:query')")
    public CommonResult<PageResult<IotDeviceMaterialRespVO>> getIotDeviceMaterialPage(@Valid IotDeviceMaterialPageReqVO pageReqVO) {
        PageResult<IotDeviceMaterialDO> pageResult = iotDeviceMaterialService.getIotDeviceMaterialPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IotDeviceMaterialRespVO.class));
    }

    @GetMapping("/list-by-bom-id")
    @Operation(summary = "根据设备分类bom节点id获取物料列表")
    @PreAuthorize("@ss.hasPermission('pms:iot-device-material:query')")
    public CommonResult<PageResult<IotDeviceMaterialRespVO>> getDeviceMaterialsByBomId(@Valid IotDeviceMaterialPageReqVO reqVO) {
        PageResult<IotDeviceMaterialDO> deviceTemplateAttrs = iotDeviceMaterialService.getDeviceMaterialsByBomId(reqVO);
        return success(buildDeviceMaterialVOPageResult(deviceTemplateAttrs));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出PMS 设备BOM物料关联 Excel")
    @PreAuthorize("@ss.hasPermission('pms:iot-device-material:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportIotDeviceMaterialExcel(@Valid IotDeviceMaterialPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IotDeviceMaterialDO> list = iotDeviceMaterialService.getIotDeviceMaterialPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "PMS 设备BOM物料关联.xls", "数据", IotDeviceMaterialRespVO.class,
                        BeanUtils.toBean(list, IotDeviceMaterialRespVO.class));
    }

    private PageResult<IotDeviceMaterialRespVO> buildDeviceMaterialVOPageResult(PageResult<IotDeviceMaterialDO> pageResult) {
        if (CollUtil.isEmpty(pageResult.getList())) {
            return PageResult.empty(pageResult.getTotal());
        }
        // 2. 开始拼接
        return BeanUtils.toBean(pageResult, IotDeviceMaterialRespVO.class);
    }

}