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

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.iotcargolocation.vo.IotCargoLocationPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotcargolocation.vo.IotCargoLocationRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotcargolocation.vo.IotCargoLocationSaveReqVO;
import cn.iocoder.yudao.module.pms.convert.cargolocation.IotCargoLocationConvert;
import cn.iocoder.yudao.module.pms.convert.shelves.IotShelvesConvert;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotcargolocation.IotCargoLocationDO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotshelves.IotShelvesDO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotstoragearea.IotStorageAreaDO;
import cn.iocoder.yudao.module.pms.service.iotcargolocation.IotCargoLocationService;
import cn.iocoder.yudao.module.pms.service.iotshelves.IotShelvesService;
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.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-cargo-location")
@Validated
public class IotCargoLocationController {

    @Resource
    private IotCargoLocationService iotCargoLocationService;
    @Resource
    private SapOrgApi sapOrgApi;
    @Resource
    private IotStorageAreaService iotStorageAreaService;
    @Resource
    private IotShelvesService iotShelvesService;

    @PostMapping("/create")
    @Operation(summary = "创建PMS 货位")
    @PreAuthorize("@ss.hasPermission('pms:iot-cargo-location:create')")
    public CommonResult<Long> createIotCargoLocation(@Valid @RequestBody IotCargoLocationSaveReqVO createReqVO) {
        return success(iotCargoLocationService.createIotCargoLocation(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新PMS 货位")
    @PreAuthorize("@ss.hasPermission('pms:iot-cargo-location:update')")
    public CommonResult<Boolean> updateIotCargoLocation(@Valid @RequestBody IotCargoLocationSaveReqVO updateReqVO) {
        iotCargoLocationService.updateIotCargoLocation(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除PMS 货位")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('pms:iot-cargo-location:delete')")
    public CommonResult<Boolean> deleteIotCargoLocation(@RequestParam("id") Long id) {
        iotCargoLocationService.deleteIotCargoLocation(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得PMS 货位")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('pms:iot-cargo-location:query')")
    public CommonResult<IotCargoLocationRespVO> getIotCargoLocation(@RequestParam("id") Long id) {
        IotCargoLocationDO iotCargoLocation = iotCargoLocationService.getIotCargoLocation(id);
        return success(BeanUtils.toBean(iotCargoLocation, IotCargoLocationRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得PMS 货位分页")
    @PreAuthorize("@ss.hasPermission('pms:iot-cargo-location:query')")
    public CommonResult<PageResult<IotCargoLocationRespVO>> getIotCargoLocationPage(@Valid IotCargoLocationPageReqVO pageReqVO) {
        PageResult<IotCargoLocationDO> pageResult = iotCargoLocationService.getIotCargoLocationPage(pageReqVO);
        // 组装 库存地点 库区 货架 信息
        // 拼接库存地点数据
        Map<Long, SapOrgRespDTO> storageLocationMap = sapOrgApi.getSapOrgMap(
                convertList(pageResult.getList(), IotCargoLocationDO::getStorageLocationId));
        // 拼接库区数据
        Map<Long, IotStorageAreaDO> storageAreaMap = iotStorageAreaService.getStorageAreaMap(
                convertList(pageResult.getList(), IotCargoLocationDO::getStorageAreaId));
        // 拼接货架数据
        Map<Long, IotShelvesDO> shelvesMap = iotShelvesService.getShelvesMap(
                convertList(pageResult.getList(), IotCargoLocationDO::getShelvesId));
        return success(new PageResult<>(IotCargoLocationConvert.INSTANCE.convertList(pageResult.getList(),
                storageLocationMap, storageAreaMap, shelvesMap), pageResult.getTotal()));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出PMS 货位 Excel")
    @PreAuthorize("@ss.hasPermission('pms:iot-cargo-location:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportIotCargoLocationExcel(@Valid IotCargoLocationPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IotCargoLocationDO> list = iotCargoLocationService.getIotCargoLocationPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "PMS 货位.xls", "数据", IotCargoLocationRespVO.class,
                        BeanUtils.toBean(list, IotCargoLocationRespVO.class));
    }

}