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

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.checkchannel.vo.CheckChannelPageReqVO;
import com.yc.ship.module.resource.controller.admin.checkchannel.vo.CheckChannelRespVO;
import com.yc.ship.module.resource.controller.admin.checkchannel.vo.CheckChannelSaveReqVO;
import com.yc.ship.module.resource.dal.dataobject.checkchannel.CheckChannelDO;
import com.yc.ship.module.resource.service.checkchannel.CheckChannelService;
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/checkchannel")
@Validated
public class CheckChannelController {

    @Resource
    private CheckChannelService checkChannelService;

    @PostMapping("/create")
    @Operation(summary = "创建产品检票通道")
    @PreAuthorize("@ss.hasPermission('product:check-channel:create')")
    public CommonResult<Long> createCheckChannel(@Valid @RequestBody CheckChannelSaveReqVO createReqVO) {
        return success(checkChannelService.create(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新产品检票通道")
    @PreAuthorize("@ss.hasPermission('product:check-channel:update')")
    public CommonResult<Boolean> updateCheckChannel(@Valid @RequestBody CheckChannelSaveReqVO updateReqVO) {
        checkChannelService.update(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除产品检票通道")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('product:check-channel:delete')")
    public CommonResult<Boolean> deleteCheckChannel(@RequestParam("id") Long id) {
        checkChannelService.delete(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得产品检票通道")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('product:check-channel:query')")
    public CommonResult<CheckChannelRespVO> getCheckChannel(@RequestParam("id") Long id) {
        CheckChannelDO checkChannel = checkChannelService.getCheckChannel(id);
        return success(BeanUtils.toBean(checkChannel, CheckChannelRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得产品检票通道分页")
    @PreAuthorize("@ss.hasPermission('product:check-channel:query')")
    public CommonResult<PageResult<CheckChannelRespVO>> getCheckChannelPage(@Valid CheckChannelPageReqVO pageReqVO) {
        PageResult<CheckChannelDO> pageResult = checkChannelService.getCheckChannelPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CheckChannelRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出产品检票通道 Excel")
    @PreAuthorize("@ss.hasPermission('product:check-channel:export')")
    @OperateLog(type = EXPORT,enable = false)
    public void exportCheckChannelExcel(@Valid CheckChannelPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CheckChannelDO> list = checkChannelService.getCheckChannelPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "产品检票通道.xls", "数据", CheckChannelRespVO.class,
                        BeanUtils.toBean(list, CheckChannelRespVO.class));
    }

}