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

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.iotshelves.vo.IotShelvesPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotshelves.vo.IotShelvesRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotshelves.vo.IotShelvesSaveReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotstoragearea.vo.IotStorageAreaRespVO;
import cn.iocoder.yudao.module.pms.convert.shelves.IotShelvesConvert;
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.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.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-shelves")
@Validated
public class IotShelvesController {

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

    @PostMapping("/create")
    @Operation(summary = "创建PMS 货架")
    @PreAuthorize("@ss.hasPermission('pms:iot-shelves:create')")
    public CommonResult<Long> createIotShelves(@Valid @RequestBody IotShelvesSaveReqVO createReqVO) {
        return success(iotShelvesService.createIotShelves(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新PMS 货架")
    @PreAuthorize("@ss.hasPermission('pms:iot-shelves:update')")
    public CommonResult<Boolean> updateIotShelves(@Valid @RequestBody IotShelvesSaveReqVO updateReqVO) {
        iotShelvesService.updateIotShelves(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得PMS 货架")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('pms:iot-shelves:query')")
    public CommonResult<IotShelvesRespVO> getIotShelves(@RequestParam("id") Long id) {
        IotShelvesDO iotShelves = iotShelvesService.getIotShelves(id);
        return success(BeanUtils.toBean(iotShelves, IotShelvesRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得PMS 货架分页")
    @PreAuthorize("@ss.hasPermission('pms:iot-shelves:query')")
    public CommonResult<PageResult<IotShelvesRespVO>> getIotShelvesPage(@Valid IotShelvesPageReqVO pageReqVO) {
        PageResult<IotShelvesDO> pageResult = iotShelvesService.getIotShelvesPage(pageReqVO);
        // 拼接库存地点数据
        Map<Long, SapOrgRespDTO> storageLocationMap = sapOrgApi.getSapOrgMap(
                convertList(pageResult.getList(), IotShelvesDO::getStorageLocationId));
        // 拼接库区数据
        Map<Long, IotStorageAreaDO> storageAreaMap = iotStorageAreaService.getStorageAreaMap(
                convertList(pageResult.getList(), IotShelvesDO::getStorageAreaId));
        return success(new PageResult<>(IotShelvesConvert.INSTANCE.convertList(pageResult.getList(), storageLocationMap, storageAreaMap),
                pageResult.getTotal()));
    }

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

    @GetMapping("/export-excel")
    @Operation(summary = "导出PMS 货架 Excel")
    @PreAuthorize("@ss.hasPermission('pms:iot-shelves:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportIotShelvesExcel(@Valid IotShelvesPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IotShelvesDO> list = iotShelvesService.getIotShelvesPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "PMS 货架.xls", "数据", IotShelvesRespVO.class,
                        BeanUtils.toBean(list, IotShelvesRespVO.class));
    }

}