package com.yc.ship.module.miniapplet.controller.admin.content;

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.collection.CollectionUtils;
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.miniapplet.controller.admin.content.vo.ContentPageReqVO;
import com.yc.ship.module.miniapplet.controller.admin.content.vo.ContentRespVO;
import com.yc.ship.module.miniapplet.controller.admin.content.vo.ContentSaveReqVO;
import com.yc.ship.module.miniapplet.controller.admin.content.vo.ContentSimpleRespVO;
import com.yc.ship.module.miniapplet.convert.catalogue.CatalogueConvert;
import com.yc.ship.module.miniapplet.dal.dataobject.catalogue.CatalogueDO;
import com.yc.ship.module.miniapplet.dal.dataobject.content.ContentDO;
import com.yc.ship.module.miniapplet.service.catalogue.CatalogueService;
import com.yc.ship.module.miniapplet.service.content.ContentService;
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 java.util.stream.Collectors;

import static com.yc.ship.framework.common.exception.util.ServiceExceptionUtil.exception;
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;
import static com.yc.ship.module.miniapplet.enums.ErrorCodeConstants.*;


@Tag(name = "管理后台 - 目录内容")
@RestController
@RequestMapping("/applet/content")
@Validated
public class ContentController {

    @Resource
    private CatalogueService catalogueService;

    @Resource
    private ContentService contentService;

    @PostMapping("/create")
    @Operation(summary = "创建")
    @PreAuthorize("@ss.hasPermission('applet:content:create')")
    public CommonResult<Long> createContent(@Valid @RequestBody ContentSaveReqVO createReqVO) {
        return success(contentService.createContent(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新")
    @PreAuthorize("@ss.hasAnyPermissions('applet:content:update','applet:content:withdraw')")
    public CommonResult<Boolean> updateContent(@Valid @RequestBody ContentSaveReqVO updateReqVO) {
        contentService.updateContent(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('applet:content:delete')")
    public CommonResult<Boolean> deleteContent(@RequestParam("id") Long id) {
        contentService.deleteContent(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('applet:content:query')")
    public CommonResult<ContentRespVO> getContent(@RequestParam("id") Long id) {
        ContentDO content = contentService.getContent(id);
        return success(BeanUtils.toBean(content, ContentRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得分页")
    @PreAuthorize("@ss.hasPermission('applet:content:query')")
    public CommonResult<PageResult<ContentRespVO>> getContentPage(@Valid ContentPageReqVO pageReqVO) {
        PageResult<ContentDO> pageResult = contentService.getContentPage(pageReqVO);
        List<CatalogueDO> catalogueList = catalogueService.getCatalogueList(convertList(pageResult.getList(), ContentDO::getCatalogueId));
        Map<Long, CatalogueDO> catalogueMap = CollectionUtils.convertMap(catalogueList, CatalogueDO::getId);
        return success(new PageResult<>(CatalogueConvert.INSTANCE.convertContentList(pageResult.getList(), catalogueMap),
                pageResult.getTotal()));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出 Excel")
    @PreAuthorize("@ss.hasPermission('applet:content:export')")
    @OperateLog(type = EXPORT,enable = false)
    public void exportContentExcel(@Valid ContentPageReqVO pageReqVO, HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ContentDO> list = contentService.getContentPage(pageReqVO).getList();
        List<CatalogueDO> catalogueList = catalogueService.getCatalogueList(convertList(list, ContentDO::getCatalogueId));
        Map<Long, CatalogueDO> catalogueMap = CollectionUtils.convertMap(catalogueList, CatalogueDO::getId);
        List<ContentRespVO> respList = CatalogueConvert.INSTANCE.convertContentList(list, catalogueMap);
        // 导出 Excel
        ExcelUtils.write(response, "内容.xls", "数据", ContentRespVO.class,
                respList);
    }


    @GetMapping({"/list-all-simple", "/simple-list"})
    @Operation(summary = "获取内容精简信息列表", description = "只包含被开启内容精简信息，主要用于前端的下拉选项")
    public CommonResult<List<ContentSimpleRespVO>> getSimpleContentList(@RequestParam(value = "catalogueId", required = false) Long catalogueId, @RequestParam(value = "status", required = false) Integer status) {
        List<ContentDO> list = contentService.getContentList(catalogueId, status == null ? 1 : status);
        return success(BeanUtils.toBean(list, ContentSimpleRespVO.class));
    }
}
