package com.yc.ship.module.resource.controller.admin.ship;

import com.yc.ship.framework.common.util.collection.CollectionUtils;
import com.yc.ship.framework.common.util.collection.MapUtils;
import com.yc.ship.module.resource.dal.dataobject.shiptype.ResourceShipTypeDO;
import com.yc.ship.module.resource.dal.dataobject.supplier.ProductSupplierDO;
import com.yc.ship.module.resource.service.shiptype.ResourceShipTypeService;
import com.yc.ship.module.resource.service.supplier.ProductSupplierService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.yc.ship.framework.common.pojo.PageParam;
import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.common.pojo.CommonResult;
import com.yc.ship.framework.common.util.object.BeanUtils;
import static com.yc.ship.framework.common.pojo.CommonResult.success;

import com.yc.ship.framework.excel.core.util.ExcelUtils;

import com.yc.ship.framework.apilog.core.annotation.ApiAccessLog;
import static com.yc.ship.framework.apilog.core.enums.OperateTypeEnum.*;

import com.yc.ship.module.resource.controller.admin.ship.vo.*;
import com.yc.ship.module.resource.dal.dataobject.ship.ResourceShipDO;
import com.yc.ship.module.resource.service.ship.ResourceShipService;

@Tag(name = "管理后台 - 资源管理-游轮管理")
@RestController
@RequestMapping("/resource/ship")
@Validated
public class ResourceShipController {

    @Resource
    private ResourceShipService shipService;

    @Resource
    private ResourceShipTypeService shipTypeService;

    @Resource
    private ProductSupplierService supplierService;

    @PostMapping("/create")
    @Operation(summary = "创建资源管理-游轮管理")
    @PreAuthorize("@ss.hasPermission('resource:ship:create')")
    public CommonResult<Long> createShip(@Valid @RequestBody ResourceShipSaveReqVO createReqVO) {
        return success(shipService.createShip(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新资源管理-游轮管理")
    @PreAuthorize("@ss.hasPermission('resource:ship:update')")
    public CommonResult<Boolean> updateShip(@Valid @RequestBody ResourceShipSaveReqVO updateReqVO) {
        shipService.updateShip(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除资源管理-游轮管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('resource:ship:delete')")
    public CommonResult<Boolean> deleteShip(@RequestParam("id") Long id) {
        shipService.deleteShip(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得资源管理-游轮管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('resource:ship:query')")
    public CommonResult<ResourceShipRespVO> getShip(@RequestParam("id") Long id) {
        ResourceShipDO ship = shipService.getShip(id);
        return success(BeanUtils.toBean(ship, ResourceShipRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得资源管理-游轮管理分页")
    @PreAuthorize("@ss.hasPermission('resource:ship:query')")
    public CommonResult<PageResult<ResourceShipRespVO>> getShipPage(@Valid ResourceShipPageReqVO pageReqVO) {
        PageResult<ResourceShipDO> pageResult = shipService.getShipPage(pageReqVO);
        PageResult<ResourceShipRespVO> page = BeanUtils.toBean(pageResult, ResourceShipRespVO.class);
        List<ResourceShipRespVO> list = page.getList();
        List<Long> typeIds = CollectionUtils.convertList(list, ResourceShipRespVO::getShipTypeId);
        List<ResourceShipTypeDO> typeList = shipTypeService.getList(typeIds);
        Map<Long, ResourceShipTypeDO> longResourceShipTypeDOMap = CollectionUtils.convertMap(typeList, ResourceShipTypeDO::getId);

        //供应商名称
        List<Long> supplierIds = CollectionUtils.convertList(list, ResourceShipRespVO::getSupplierId);
        List<ProductSupplierDO> supplierList = supplierService.getList(supplierIds);
        Map<Long, ProductSupplierDO> longProductSupplierDOMap = CollectionUtils.convertMap(supplierList, ProductSupplierDO::getId);

        list.stream().forEach(item -> {
            MapUtils.findAndThen(longResourceShipTypeDOMap, item.getShipTypeId(), shipType -> item.setShipTypeName(shipType.getName()));
            MapUtils.findAndThen(longProductSupplierDOMap, item.getSupplierId(), supplier -> item.setSupplierName(supplier.getName()));
        });
        return success(page);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出资源管理-游轮管理 Excel")
    @PreAuthorize("@ss.hasPermission('resource:ship:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportShipExcel(@Valid ResourceShipPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ResourceShipDO> list = shipService.getShipPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "资源管理-游轮管理.xls", "数据", ResourceShipRespVO.class,
                        BeanUtils.toBean(list, ResourceShipRespVO.class));
    }
    @GetMapping("/list-simple")
    @Operation(summary = "获得资源管理-游轮列表")
    public CommonResult<List<ResourceShipRespVO>> getShipListSimple() {
        List<ResourceShipDO> list = shipService.getListSimple();
        return success(BeanUtils.toBean(list, ResourceShipRespVO.class));
    }

}