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

import com.yc.ship.framework.common.util.collection.CollectionUtils;
import com.yc.ship.framework.common.util.collection.MapUtils;
import com.yc.ship.framework.ip.core.utils.AreaUtils;
import com.yc.ship.module.resource.dal.dataobject.dock.ResourceDockDO;
import com.yc.ship.module.resource.service.dock.ResourceDockService;
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.scenic.vo.*;
import com.yc.ship.module.resource.dal.dataobject.scenic.ResourceScenicDO;
import com.yc.ship.module.resource.service.scenic.ResourceScenicService;

@Tag(name = "管理后台 - 景区管理")
@RestController
@RequestMapping("/resource/scenic")
@Validated
public class ResourceScenicController {

    @Resource
    private ResourceScenicService scenicService;

    @Resource
    private ResourceDockService dockService;

    @PostMapping("/create")
    @Operation(summary = "创建景区管理")
    @PreAuthorize("@ss.hasPermission('resource:scenic:create')")
    public CommonResult<Long> createScenic(@Valid @RequestBody ResourceScenicSaveReqVO createReqVO) {
        return success(scenicService.createScenic(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新景区管理")
    @PreAuthorize("@ss.hasPermission('resource:scenic:update')")
    public CommonResult<Boolean> updateScenic(@Valid @RequestBody ResourceScenicSaveReqVO updateReqVO) {
        scenicService.updateScenic(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除景区管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('resource:scenic:delete')")
    public CommonResult<Boolean> deleteScenic(@RequestParam("id") Long id) {
        scenicService.deleteScenic(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得景区管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('resource:scenic:query')")
    public CommonResult<ResourceScenicRespVO> getScenic(@RequestParam("id") Long id) {
        ResourceScenicDO scenic = scenicService.getScenic(id);
        return success(BeanUtils.toBean(scenic, ResourceScenicRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得景区管理分页")
    @PreAuthorize("@ss.hasPermission('resource:scenic:query')")
    public CommonResult<PageResult<ResourceScenicRespVO>> getScenicPage(@Valid ResourceScenicPageReqVO pageReqVO) {
        PageResult<ResourceScenicDO> pageResult = scenicService.getScenicPage(pageReqVO);
        PageResult<ResourceScenicRespVO> page = BeanUtils.toBean(pageResult, ResourceScenicRespVO.class);
        List<ResourceScenicRespVO> list = page.getList();
        List<Long> dockIds = CollectionUtils.convertList(list, ResourceScenicRespVO::getDockId);
        List<ResourceDockDO> dockList = dockService.getList(dockIds);
        Map<Long, ResourceDockDO> longResourceDockDOMap = CollectionUtils.convertMap(dockList, ResourceDockDO::getId);
        list.stream().forEach(item -> {
            MapUtils.findAndThen(longResourceDockDOMap, item.getDockId(), dock -> item.setDockName(dock.getName()));
            if(item.getCountyId() != null) {
                String area = AreaUtils.format(item.getCountyId());
                item.setArea(area);
            }else if(item.getCityId() != null) {
                String area = AreaUtils.format(item.getCityId());
                item.setArea(area);
            }else if(item.getProvinceId() != null) {
                String area = AreaUtils.format(item.getProvinceId());
                item.setArea(area);
            }
        });
        return success(page);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出景区管理 Excel")
    @PreAuthorize("@ss.hasPermission('resource:scenic:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportScenicExcel(@Valid ResourceScenicPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ResourceScenicDO> list = scenicService.getScenicPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "景区管理.xls", "数据", ResourceScenicRespVO.class,
                        BeanUtils.toBean(list, ResourceScenicRespVO.class));
    }

    @GetMapping("/enableList")
    @Operation(summary = "获得启用的景区列表")
    public CommonResult<List<ResourceScenicRespVO>> getEnableScenicList() {
        List<ResourceScenicDO> list = scenicService.getEnableScenicList();
        return success(BeanUtils.toBean(list, ResourceScenicRespVO.class));
    }
    @GetMapping("/list-simple")
    @Operation(summary = "获得资源管理-景区列表")
    public CommonResult<List<ResourceScenicRespVO>> getScenicListSimple() {
        List<ResourceScenicDO> list = scenicService.getListSimple();
        return success(BeanUtils.toBean(list, ResourceScenicRespVO.class));
    }

}