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

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.framework.tenant.core.context.TenantContextHolder;
import com.yc.ship.module.resource.controller.admin.guidegroup.vo.GuideGroupPageReqVO;
import com.yc.ship.module.resource.controller.admin.guidegroup.vo.GuideGroupRespVO;
import com.yc.ship.module.resource.controller.admin.guidegroup.vo.GuideGroupSaveReqVO;
import com.yc.ship.module.resource.controller.admin.guidegroup.vo.GuideGroupSimpleRespVO;
import com.yc.ship.module.resource.controller.admin.vo.SimpleListReqVO;
import com.yc.ship.module.resource.dal.dataobject.guidegroup.GuideGroupDO;
import com.yc.ship.module.resource.enums.CommonStatusEnum;
import com.yc.ship.module.resource.service.guidegroup.GuideGroupService;
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 static com.yc.ship.framework.common.pojo.CommonResult.success;
import static com.yc.ship.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;


@Tag(name = "管理后台 - 导游分组")
@RestController
@RequestMapping("/resource/guide-group")
@Validated
public class GuideGroupController {

    @Resource
    private GuideGroupService guideGroupService;

    @PostMapping("/create")
    @Operation(summary = "创建导游分组")
    @PreAuthorize("@ss.hasPermission('dispatch:guide-group:create')")
    public CommonResult<String> createGuideGroup(@Valid @RequestBody GuideGroupSaveReqVO createReqVO) {
        return success(guideGroupService.createGuideGroup(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新导游分组")
    @PreAuthorize("@ss.hasPermission('dispatch:guide-group:update')")
    public CommonResult<Boolean> updateGuideGroup(@Valid @RequestBody GuideGroupSaveReqVO updateReqVO) {
        guideGroupService.updateGuideGroup(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除导游分组")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('dispatch:guide-group:delete')")
    public CommonResult<Boolean> deleteGuideGroup(@RequestParam("id") String id) {
        guideGroupService.deleteGuideGroup(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得导游分组")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('dispatch:guide-group:query')")
    public CommonResult<GuideGroupRespVO> getGuideGroup(@RequestParam("id") String id) {
        GuideGroupDO guideGroup = guideGroupService.getGuideGroup(id);
        return success(BeanUtils.toBean(guideGroup, GuideGroupRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得导游分组分页")
    @PreAuthorize("@ss.hasPermission('dispatch:guide-group:query')")
    public CommonResult<PageResult<GuideGroupRespVO>> getGuideGroupPage(@Valid GuideGroupPageReqVO pageReqVO) {
        PageResult<GuideGroupDO> pageResult = guideGroupService.getGuideGroupPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, GuideGroupRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出导游分组 Excel")
    @PreAuthorize("@ss.hasPermission('dispatch:guide-group:export')")
    @OperateLog(type = EXPORT)
    public void exportGuideGroupExcel(@Valid GuideGroupPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<GuideGroupDO> list = guideGroupService.getGuideGroupPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "导游分组.xls", "数据", GuideGroupRespVO.class,
                        BeanUtils.toBean(list, GuideGroupRespVO.class));
    }
    @GetMapping(value = {"/list-all-simple", "/simple-list"})
    @Operation(summary = "获取导游精简信息列表", description = "列表数据以树结构展示")
    public CommonResult<List<GuideGroupSimpleRespVO>> getSimpleGroupList(@RequestParam("isTree") String isTree) {
        List<GuideGroupDO> list = guideGroupService.getGroupList(
            SimpleListReqVO.builder()
                .tenantId(TenantContextHolder.getTenantId())
                .status(CommonStatusEnum.DISABLE.getStatus()).build());
        List<GuideGroupSimpleRespVO> respList = BeanUtils.toBean(list, GuideGroupSimpleRespVO.class);
        return success(respList);
    }

}
