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

import com.yc.ship.framework.common.enums.CommonStatusEnum;
import com.yc.ship.framework.common.pojo.CommonResult;
import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.common.util.collection.CollectionUtils;
import com.yc.ship.framework.common.util.collection.MapUtils;
import com.yc.ship.framework.common.util.object.BeanUtils;
import com.yc.ship.framework.ip.core.utils.AreaUtils;
import com.yc.ship.module.resource.controller.admin.scenic.vo.ResourceScenicPageReqVO;
import com.yc.ship.module.resource.controller.app.scenic.vo.AppResourceScenicRespVO;
import com.yc.ship.module.resource.dal.dataobject.dock.ResourceDockDO;
import com.yc.ship.module.resource.dal.dataobject.scenic.ResourceScenicDO;
import com.yc.ship.module.resource.service.dock.ResourceDockService;
import com.yc.ship.module.resource.service.scenic.ResourceScenicService;
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.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

import static com.yc.ship.framework.common.pojo.CommonResult.success;

@Tag(name = "景区 APP - 景区接口")
@RestController
@RequestMapping("/app/resource/scenic")
@Validated
public class AppResourceScenicController {
    @Resource
    private ResourceScenicService scenicService;

    @Resource
    private ResourceDockService dockService;
    @GetMapping("/page")
    @Operation(summary = "获得景区管理分页")
    public CommonResult<PageResult<AppResourceScenicRespVO>> getScenicPage(@Valid ResourceScenicPageReqVO pageReqVO) {
        pageReqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
        PageResult<ResourceScenicDO> pageResult = scenicService.getScenicPage(pageReqVO);
        PageResult<AppResourceScenicRespVO> page = BeanUtils.toBean(pageResult, AppResourceScenicRespVO.class);
        List<AppResourceScenicRespVO> list = page.getList();
        List<Long> dockIds = CollectionUtils.convertList(list, AppResourceScenicRespVO::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("/get")
    @Operation(summary = "获得景区详情")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<AppResourceScenicRespVO> getScenic(@RequestParam("id") Long id) {
        ResourceScenicDO scenic = scenicService.getScenic(id);
        return success(BeanUtils.toBean(scenic, AppResourceScenicRespVO.class));
    }
}
