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

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.cooperate.vo.CooperatePageReqVO;
import com.yc.ship.module.otc.controller.admin.cooperate.vo.CooperateRespVO;
import com.yc.ship.module.otc.controller.admin.cooperate.vo.CooperateSaveReqVO;
import com.yc.ship.module.otc.dal.dataobject.cooperate.CooperateDO;
import com.yc.ship.module.otc.service.cooperate.CooperateService;
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("/cooperate")
@Validated
public class CooperateController {

    @Resource
    private CooperateService cooperateService;

    @PostMapping("/create")
    @Operation(summary = "创建合作旅行社")
    @PreAuthorize("@ss.hasPermission('otc:cooperate:create')")
    public CommonResult<Long> createCooperate(@Valid @RequestBody CooperateSaveReqVO createReqVO) {
        return success(cooperateService.createCooperate(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新合作旅行社")
    @PreAuthorize("@ss.hasPermission('otc:cooperate:update')")
    public CommonResult<Boolean> updateCooperate(@Valid @RequestBody CooperateSaveReqVO updateReqVO) {
        cooperateService.updateCooperate(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除合作旅行社")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('otc:cooperate:delete')")
    public CommonResult<Boolean> deleteCooperate(@RequestParam("id") Long id) {
        cooperateService.deleteCooperate(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得合作旅行社")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('otc:cooperate:query')")
    public CommonResult<CooperateRespVO> getCooperate(@RequestParam("id") Long id) {
        CooperateDO cooperate = cooperateService.getCooperate(id);
        return success(BeanUtils.toBean(cooperate, CooperateRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得合作旅行社分页")
//    @PreAuthorize("@ss.hasPermission('otc:cooperate:query')")
    public CommonResult<PageResult<CooperateRespVO>> getCooperatePage(@Valid CooperatePageReqVO pageReqVO) {
        PageResult<CooperateDO> pageResult = cooperateService.getCooperatePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CooperateRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出合作旅行社 Excel")
    @PreAuthorize("@ss.hasPermission('otc:cooperate:export')")
    @OperateLog(type = EXPORT,enable = false)
    public void exportCooperateExcel(@Valid CooperatePageReqVO pageReqVO,
                                     HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CooperateDO> list = cooperateService.getCooperatePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "合作旅行社.xls", "数据", CooperateRespVO.class,
                BeanUtils.toBean(list, CooperateRespVO.class));
    }

}
