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

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.iotstoragearea.vo.IotStorageAreaPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotstoragearea.vo.IotStorageAreaRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotstoragearea.vo.IotStorageAreaSaveReqVO;
import cn.iocoder.yudao.module.pms.convert.iotstoragearea.IotStorageAreaConvert;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotstoragearea.IotStorageAreaDO;
import cn.iocoder.yudao.module.pms.service.iotstoragearea.IotStorageAreaService;
import cn.iocoder.yudao.module.system.api.saporg.SapOrgApi;
import cn.iocoder.yudao.module.system.api.saporg.dto.SapOrgRespDTO;
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.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

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

@Tag(name = "管理后台 - PMS 库区")
@RestController
@RequestMapping("/pms/iot-storage-area")
@Validated
public class IotStorageAreaController {

    @Resource
    private IotStorageAreaService iotStorageAreaService;
    @Resource
    private SapOrgApi sapOrgApi;

    @PostMapping("/create")
    @Operation(summary = "创建PMS 库区")
    @PreAuthorize("@ss.hasPermission('pms:iot-storage-area:create')")
    public CommonResult<Long> createIotStorageArea(@Valid @RequestBody IotStorageAreaSaveReqVO createReqVO) {
        return success(iotStorageAreaService.createIotStorageArea(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新PMS 库区")
    @PreAuthorize("@ss.hasPermission('pms:iot-storage-area:update')")
    public CommonResult<Boolean> updateIotStorageArea(@Valid @RequestBody IotStorageAreaSaveReqVO updateReqVO) {
        iotStorageAreaService.updateIotStorageArea(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除PMS 库区")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('pms:iot-storage-area:delete')")
    public CommonResult<Boolean> deleteIotStorageArea(@RequestParam("id") Long id) {
        iotStorageAreaService.deleteIotStorageArea(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得PMS 库区")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('pms:iot-storage-area:query')")
    public CommonResult<IotStorageAreaRespVO> getIotStorageArea(@RequestParam("id") Long id) {
        IotStorageAreaDO iotStorageArea = iotStorageAreaService.getIotStorageArea(id);
        return success(BeanUtils.toBean(iotStorageArea, IotStorageAreaRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得PMS 库区分页")
    @PreAuthorize("@ss.hasPermission('pms:iot-storage-area:query')")
    public CommonResult<PageResult<IotStorageAreaRespVO>> getIotStorageAreaPage(@Valid IotStorageAreaPageReqVO pageReqVO) {
        PageResult<IotStorageAreaDO> pageResult = iotStorageAreaService.getIotStorageAreaPage(pageReqVO);
        // 拼接库存地点数据
        Map<Long, SapOrgRespDTO> storageLocationMap = sapOrgApi.getSapOrgMap(
                convertList(pageResult.getList(), IotStorageAreaDO::getStorageLocationId));
        // return success(BeanUtils.toBean(pageResult, IotStorageAreaRespVO.class));
        return success(new PageResult<>(IotStorageAreaConvert.INSTANCE.convertList(pageResult.getList(), storageLocationMap),
                pageResult.getTotal()));
    }

    @GetMapping(value = {"/list-all-simple", "simple-list"})
    @Operation(summary = "获取库存地点区域全列表", description = "只包含被开启的数据，主要用于前端的下拉选项")
    public CommonResult<List<IotStorageAreaRespVO>> getSimpleStorageAreaList() {
        // 获得库存地点区域列表，只要开启状态的
        List<IotStorageAreaDO> list = iotStorageAreaService.getStorageAreaListByStatus(Collections.singleton(CommonStatusEnum.ENABLE.getStatus()));
        // 排序后，返回给前端
        list.sort(Comparator.comparing(IotStorageAreaDO::getSort));
        return success(BeanUtils.toBean(list, IotStorageAreaRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出PMS 库区 Excel")
    @PreAuthorize("@ss.hasPermission('pms:iot-storage-area:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportIotStorageAreaExcel(@Valid IotStorageAreaPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IotStorageAreaDO> list = iotStorageAreaService.getIotStorageAreaPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "PMS 库区.xls", "数据", IotStorageAreaRespVO.class,
                        BeanUtils.toBean(list, IotStorageAreaRespVO.class));
    }

}