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

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.catalogue.vo.CataloguePageReqVO;
import com.yc.ship.module.miniapplet.controller.admin.catalogue.vo.CatalogueRespVO;
import com.yc.ship.module.miniapplet.controller.admin.catalogue.vo.CatalogueSaveReqVO;
import com.yc.ship.module.miniapplet.controller.admin.catalogue.vo.CatalogueSimpleRespVO;
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.service.catalogue.CatalogueService;
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("/applet/catalogue")
@Validated
public class CatalogueController {

    @Resource
    private CatalogueService catalogueService;

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

    @PutMapping("/update")
    @Operation(summary = "更新")
    @PreAuthorize("@ss.hasPermission('applet:catalogue:update')")
    public CommonResult<Boolean> updateCatalogue(@Valid @RequestBody CatalogueSaveReqVO updateReqVO) {
        catalogueService.updateCatalogue(updateReqVO);
        return success(true);
    }


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

    @GetMapping("/get")
    @Operation(summary = "获得")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('applet:catalogue:query')")
    public CommonResult<CatalogueRespVO> getCatalogue(@RequestParam("id") Long id, @RequestParam("level") String level) {
        CatalogueRespVO respVO = catalogueService.getCatalogueResp(id);
        return success(respVO);
    }

    @GetMapping("/page")
    @Operation(summary = "获得分页")
    @PreAuthorize("@ss.hasPermission('applet:catalogue:query')")
    public CommonResult<PageResult<CatalogueRespVO>> getCataloguePage(@Valid CataloguePageReqVO pageReqVO) {
        PageResult<CatalogueDO> pageResult = catalogueService.getCataloguePage(pageReqVO);
        List<CatalogueDO> catalogueList = catalogueService.getCatalogueList(convertList(pageResult.getList(), CatalogueDO::getParentId));
        Map<Long, CatalogueDO> catalogueMap = CollectionUtils.convertMap(catalogueList, CatalogueDO::getId);
        return success(new PageResult<>(CatalogueConvert.INSTANCE.convertCatalogueList(pageResult.getList(), catalogueMap), pageResult.getTotal()));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出 Excel")
    @PreAuthorize("@ss.hasPermission('applet:catalogue:export')")
    @OperateLog(type = EXPORT,enable = false)
    public void exportCatalogueExcel(@Valid CataloguePageReqVO pageReqVO, HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CatalogueDO> list = catalogueService.getCataloguePage(pageReqVO).getList();
        List<CatalogueDO> catalogueList = catalogueService.getCatalogueList(convertList(list, CatalogueDO::getParentId));
        Map<Long, CatalogueDO> catalogueMap = CollectionUtils.convertMap(catalogueList, CatalogueDO::getId);
        List<CatalogueRespVO> respList = CatalogueConvert.INSTANCE.convertCatalogueList(list, catalogueMap);
        // 导出 Excel
        ExcelUtils.write(response, "目录类型.xls", "数据", CatalogueRespVO.class, respList);
    }

    @GetMapping({"/getCatalogueTree"})
    @Operation(summary = "获取目录类型树", description = "只包含被启用的目录类型，主要用于前端的下拉选项")
    public CommonResult<List<Map<String, Object>>> getCatalogueTree() {
        List<Map<String, Object>> list = catalogueService.getCatalogueTree();
        return success(list);
    }

    @GetMapping({"/list-all-simple", "/simple-list"})
    @Operation(summary = "获取目录类型精简信息列表", description = "只包含被开启的目录类型，主要用于前端的下拉选项")
    public CommonResult<List<CatalogueSimpleRespVO>> getSimpleCatalogueList(@RequestParam("status") Integer status) {
        List<CatalogueDO> list = catalogueService.getCatalogueListByStatus(status == null ? 1 : status);
        return success(BeanUtils.toBean(list, CatalogueSimpleRespVO.class));
    }

}
