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

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.guide.vo.GuidePageReqVO;
import com.yc.ship.module.resource.controller.admin.guide.vo.GuideRespVO;
import com.yc.ship.module.resource.controller.admin.guide.vo.GuideSaveReqVO;
import com.yc.ship.module.resource.controller.admin.guide.vo.GuideSimpleRespVO;
import com.yc.ship.module.resource.dal.dataobject.guide.GuideDO;
import com.yc.ship.module.resource.enums.UseStatusEnum;
import com.yc.ship.module.resource.service.guide.GuideService;
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")
@Validated
public class GuideController {

    @Resource
    private GuideService guideService;

    @PostMapping("/create")
    @Operation(summary = "创建导游管理")
    @PreAuthorize("@ss.hasPermission('dispatch:guide:create')")
    public CommonResult<String> createGuide(@Valid @RequestBody GuideSaveReqVO createReqVO) {
        return success(guideService.createGuide(createReqVO));
    }

    @PostMapping("/creates")
    @Operation(summary = "创建导游管理")
    @PreAuthorize("@ss.hasPermission('dispatch:guide:create')")
    public CommonResult<String> createGuide(@Valid @RequestBody List<GuideSaveReqVO> dataList) {
        dataList.forEach(guideSaveReqVO -> guideService.createGuide(guideSaveReqVO));
        return success(null);
    }

    @PutMapping("/update")
    @Operation(summary = "更新导游管理")
    @PreAuthorize("@ss.hasPermission('dispatch:guide:update')")
    public CommonResult<Boolean> updateGuide(@Valid @RequestBody GuideSaveReqVO updateReqVO) {
        guideService.updateGuide(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得导游管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('dispatch:guide:query')")
    public CommonResult<GuideRespVO> getGuide(@RequestParam("id") String id) {
        GuideDO guide = guideService.getGuide(id);
        return success(BeanUtils.toBean(guide, GuideRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得导游管理分页")
//    @PreAuthorize("@ss.hasPermission('dispatch:guide:query')")
    public CommonResult<PageResult<GuideRespVO>> getGuidePage(@Valid GuidePageReqVO pageReqVO) {
        PageResult<GuideDO> pageResult = guideService.getGuidePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, GuideRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出导游管理 Excel")
    @PreAuthorize("@ss.hasPermission('dispatch:guide:export')")
    @OperateLog(type = EXPORT)
    public void exportGuideExcel(@Valid GuidePageReqVO pageReqVO,
                                 HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<GuideDO> list = guideService.getGuidePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "导游管理.xls", "数据", GuideRespVO.class,
                BeanUtils.toBean(list, GuideRespVO.class));
    }


    @GetMapping(value = {"/list-all-simple", "/simple-list"})
    @Operation(summary = "获取导游精简信息列表", description = "列表数据")
    public CommonResult<List<GuideSimpleRespVO>> getSimpleGuideList() {
        List<GuideDO> list = guideService.getGuideList(
                GuideSimpleRespVO.builder()
                        .deleted(UseStatusEnum.ENABLE.getStatus())
                        .useStatus(UseStatusEnum.ENABLE.getStatus()).build()
        );
        return success(BeanUtils.toBean(list, GuideSimpleRespVO.class));
    }

}
