package com.yc.ship.module.otc.controller.admin.otclevel;

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.otc.controller.admin.otclevel.vo.OtcLevelPageReqVO;
import com.yc.ship.module.otc.controller.admin.otclevel.vo.OtcLevelRespVO;
import com.yc.ship.module.otc.controller.admin.otclevel.vo.OtcLevelSaveReqVO;
import com.yc.ship.module.otc.dal.dataobject.otclevel.OtcLevelDO;
import com.yc.ship.module.otc.service.otclevel.OtcLevelService;
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("/otc/level")
@Validated
public class OtcLevelController {

    @Resource
    private OtcLevelService levelService;

    @PostMapping("/create")
    @Operation(summary = "创建旅行社等级管理")
    @PreAuthorize("@ss.hasPermission('otc:level:create')")
    public CommonResult<Long> createLevel(@Valid @RequestBody OtcLevelSaveReqVO createReqVO) {
        return success(levelService.createLevel(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新旅行社等级管理")
    @PreAuthorize("@ss.hasPermission('otc:level:update')")
    public CommonResult<Boolean> updateLevel(@Valid @RequestBody OtcLevelSaveReqVO updateReqVO) {
        levelService.updateLevel(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除旅行社等级管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('otc:level:delete')")
    public CommonResult<Boolean> deleteLevel(@RequestParam("id") Long id) {
        levelService.deleteLevel(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得旅行社等级管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('otc:level:query')")
    public CommonResult<OtcLevelRespVO> getLevel(@RequestParam("id") Long id) {
        OtcLevelDO level = levelService.getLevel(id);
        return success(BeanUtils.toBean(level, OtcLevelRespVO.class));
    }

    @GetMapping("/getLevelList")
    @Operation(summary = "获得旅行社等级管理列表-选择")
    public CommonResult<List<OtcLevelRespVO>> getLevelList() {
        List<OtcLevelDO> level = levelService.getLevelList();
        return success(BeanUtils.toBean(level, OtcLevelRespVO.class));
    }

    @GetMapping("/getLevelByTravlId")
    @Operation(summary = "获得旅行社等级")
    public CommonResult<OtcLevelRespVO> getLevelByTravlId(@RequestParam("travlId") Long travlId) {
        OtcLevelDO level = levelService.getLevelByTravlId(travlId);
        return success(BeanUtils.toBean(level, OtcLevelRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得旅行社等级管理分页")
    @PreAuthorize("@ss.hasPermission('otc:level:query')")
    public CommonResult<PageResult<OtcLevelRespVO>> getLevelPage(@Valid OtcLevelPageReqVO pageReqVO) {
        PageResult<OtcLevelDO> pageResult = levelService.getLevelPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, OtcLevelRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出旅行社等级管理 Excel")
    @PreAuthorize("@ss.hasPermission('otc:level:export')")
    @OperateLog(type = EXPORT)
    public void exportLevelExcel(@Valid OtcLevelPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<OtcLevelDO> list = levelService.getLevelPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "旅行社等级管理.xls", "数据", OtcLevelRespVO.class,
                        BeanUtils.toBean(list, OtcLevelRespVO.class));
    }

}