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

import com.yc.ship.framework.common.pojo.CommonResult;
import com.yc.ship.framework.common.pojo.PageParam;
import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.common.util.object.BeanUtils;
import com.yc.ship.framework.excel.core.util.ExcelUtils;
import com.yc.ship.framework.operatelog.core.annotations.OperateLog;
import com.yc.ship.module.resource.controller.admin.channel.vo.ScenicChannelPageReqVO;
import com.yc.ship.module.resource.controller.admin.channel.vo.ScenicChannelRespVO;
import com.yc.ship.module.resource.controller.admin.channel.vo.ScenicChannelSaveReqVO;
import com.yc.ship.module.resource.controller.admin.vo.TreeNodeVO;
import com.yc.ship.module.resource.convert.channel.ChannelConvert;
import com.yc.ship.module.resource.dal.dataobject.channel.ScenicChannelDO;
import com.yc.ship.module.resource.dal.dataobject.site.ScenicSiteDO;
import com.yc.ship.module.resource.service.channel.ScenicChannelService;
import com.yc.ship.module.resource.service.site.ScenicSiteService;
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 com.yc.ship.framework.common.pojo.CommonResult.success;
import static com.yc.ship.framework.common.util.collection.CollectionUtils.convertList;
import static com.yc.ship.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 通道")
@RestController
@RequestMapping("/resource/scenic-channel")
@Validated
public class ScenicChannelController {

    @Resource
    private ScenicSiteService scenicSiteService;
    @Resource
    private ScenicChannelService scenicChannelService;

    @PostMapping("/create")
    @Operation(summary = "创建通道")
    @PreAuthorize("@ss.hasPermission('product:scenic-channel:create')")
    public CommonResult<Long> createScenicChannel(@Valid @RequestBody ScenicChannelSaveReqVO createReqVO) {
        return success(scenicChannelService.createScenicChannel(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新通道")
    @PreAuthorize("@ss.hasPermission('product:scenic-channel:update')")
    public CommonResult<Boolean> updateScenicChannel(@Valid @RequestBody ScenicChannelSaveReqVO updateReqVO) {
        scenicChannelService.updateScenicChannel(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除通道")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('product:scenic-channel:delete')")
    public CommonResult<Boolean> deleteScenicChannel(@RequestParam("id") Long id) {
        scenicChannelService.deleteScenicChannel(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得通道")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('product:scenic-channel:query')")
    public CommonResult<ScenicChannelRespVO> getScenicChannel(@RequestParam("id") Long id) {
        ScenicChannelDO scenicChannel = scenicChannelService.getScenicChannel(id);
        ScenicSiteDO siteDO = scenicSiteService.getScenicSite(scenicChannel.getSiteId());
        ScenicChannelRespVO channelRespVO = BeanUtils.toBean(scenicChannel, ScenicChannelRespVO.class);
        channelRespVO.setSiteType(siteDO.getSiteType());
        return success(channelRespVO);
    }

    @GetMapping("/page")
    @Operation(summary = "获得通道分页")
    @PreAuthorize("@ss.hasPermission('product:scenic-channel:query')")
    public CommonResult<PageResult<ScenicChannelRespVO>> getScenicChannelPage(@Valid ScenicChannelPageReqVO pageReqVO) {
        PageResult<ScenicChannelDO> pageResult = scenicChannelService.getScenicChannelPage(pageReqVO);
        Map<Long, ScenicSiteDO> siteMap = scenicSiteService.getSiteMap(
                convertList(pageResult.getList(), ScenicChannelDO::getSiteId));
        return success(new PageResult<>(ChannelConvert.INSTANCE.convertList(pageResult.getList(), siteMap),
                pageResult.getTotal()));
//        return success(BeanUtils.toBean(pageResult, ScenicChannelRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出通道 Excel")
    @PreAuthorize("@ss.hasPermission('product:scenic-channel:export')")
    @OperateLog(type = EXPORT,enable = false)
    public void exportScenicChannelExcel(@Valid ScenicChannelPageReqVO pageReqVO,
                                         HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ScenicChannelDO> list = scenicChannelService.getScenicChannelPage(pageReqVO).getList();
        Map<Long, ScenicSiteDO> siteMap = scenicSiteService.getSiteMap(
                convertList(list, ScenicChannelDO::getSiteId));
        List<ScenicChannelRespVO> excelList = ChannelConvert.INSTANCE.convertList(list, siteMap);
        // 导出 Excel
//        ExcelUtils.write(response, "通道数据.xls", "数据", ScenicChannelRespVO.class,
//                BeanUtils.toBean(list, ScenicChannelRespVO.class));
        ExcelUtils.write(response, "通道数据.xls", "数据", ScenicChannelRespVO.class, excelList);
    }

    @GetMapping({"/list-all-simple", "/simple-list"})
    @Operation(summary = "获取通道精简信息列表", description = "只包含被启用的通道，主要用于前端的下拉选项")
    public CommonResult<List<ScenicChannelRespVO>> getSimpleChannelList(@RequestParam("siteId") String siteId) {
        List<ScenicChannelDO> list = scenicChannelService.getChannelListByStatus(siteId);
        return success(BeanUtils.toBean(list, ScenicChannelRespVO.class));
    }

    @GetMapping("/checkChannelList")
    @Operation(summary = "获取检票通道列表", description = "站点和通道")
    public CommonResult<List<TreeNodeVO>> getCheckChannelList() {
        return success(scenicChannelService.getCheckChannelList());
    }

}
