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

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
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.iotmaterialgroup.vo.*;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotmaterialgroup.IotMaterialGroupDO;
import cn.iocoder.yudao.module.pms.service.iotmaterialgroup.IotMaterialGroupService;
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 功能优化 物料组")
@RestController
@RequestMapping("/rq/iot-material-group")
@Validated
public class IotMaterialGroupController {

    @Resource
    private IotMaterialGroupService iotMaterialGroupService;

    @PostMapping("/create")
    @Operation(summary = "创建PMS 功能优化 物料组")
    @PreAuthorize("@ss.hasPermission('rq:iot-material-group:create')")
    public CommonResult<Long> createIotMaterialGroup(@Valid @RequestBody IotMaterialGroupSaveReqVO createReqVO) {
        return success(iotMaterialGroupService.createIotMaterialGroup(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新PMS 功能优化 物料组")
    @PreAuthorize("@ss.hasPermission('rq:iot-material-group:update')")
    public CommonResult<Boolean> updateIotMaterialGroup(@Valid @RequestBody IotMaterialGroupSaveReqVO updateReqVO) {
        iotMaterialGroupService.updateIotMaterialGroup(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除PMS 功能优化 物料组")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('rq:iot-material-group:delete')")
    public CommonResult<Boolean> deleteIotMaterialGroup(@RequestParam("id") Long id) {
        iotMaterialGroupService.deleteIotMaterialGroup(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得PMS 功能优化 物料组")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('rq:iot-material-group:query')")
    public CommonResult<IotMaterialGroupRespVO> getIotMaterialGroup(@RequestParam("id") Long id) {
        IotMaterialGroupDO iotMaterialGroup = iotMaterialGroupService.getIotMaterialGroup(id);
        return success(BeanUtils.toBean(iotMaterialGroup, IotMaterialGroupRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得PMS 功能优化 物料组分页")
    @PreAuthorize("@ss.hasPermission('rq:iot-material-group:query')")
    public CommonResult<PageResult<IotMaterialGroupRespVO>> getIotMaterialGroupPage(@Valid IotMaterialGroupPageReqVO pageReqVO) {
        PageResult<IotMaterialGroupDO> pageResult = iotMaterialGroupService.getIotMaterialGroupPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IotMaterialGroupRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获取物料组列表")
    @PreAuthorize("@ss.hasPermission('rq:iot-material-group:query')")
    public CommonResult<List<IotMaterialGroupRespVO>> getMaterialGroupList(IotMaterialGroupListReqVO reqVO) {
        List<IotMaterialGroupDO> list = iotMaterialGroupService.getMaterialGroupList(reqVO);
        return success(BeanUtils.toBean(list, IotMaterialGroupRespVO.class));
    }

    @GetMapping(value = {"/list-all-simple", "/simple-list"})
    @Operation(summary = "获取物料组精简信息列表", description = "只包含被开启的物料组，主要用于前端的下拉选项")
    public CommonResult<List<MaterialGroupSimpleRespVO>> getSimpleMaterialGroupList() {
        List<IotMaterialGroupDO> list = iotMaterialGroupService.getMaterialGroupList(
                new IotMaterialGroupListReqVO().setStatus(CommonStatusEnum.ENABLE.getStatus()));
        return success(BeanUtils.toBean(list, MaterialGroupSimpleRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出PMS 功能优化 物料组 Excel")
    @PreAuthorize("@ss.hasPermission('rq:iot-material-group:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportIotMaterialGroupExcel(@Valid IotMaterialGroupPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IotMaterialGroupDO> list = iotMaterialGroupService.getIotMaterialGroupPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "PMS 功能优化 物料组.xls", "数据", IotMaterialGroupRespVO.class,
                        BeanUtils.toBean(list, IotMaterialGroupRespVO.class));
    }

}