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

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.storetradelog.vo.StoreTradeLogPageReqVO;
import com.yc.ship.module.otc.controller.admin.storetradelog.vo.StoreTradeLogRespVO;
import com.yc.ship.module.otc.controller.admin.storetradelog.vo.StoreTradeLogSaveReqVO;
import com.yc.ship.module.otc.dal.dataobject.storetradelog.StoreTradeLogDO;
import com.yc.ship.module.otc.service.store.StoreService;
import com.yc.ship.module.otc.service.storetradelog.StoreTradeLogService;
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/store-trade-log")
@Validated
public class StoreTradeLogController {

    @Resource
    private StoreService storeService;

    @Resource
    private StoreTradeLogService storeTradeLogService;

    @PostMapping("/create")
    @Operation(summary = "创建旅行社门店交易记录：余额充值、预付购票、预付退款、余额购票")
    @PreAuthorize("@ss.hasPermission('otc:store-trade-log:create')")
    public CommonResult<Long> createStoreTradeLog(@Valid @RequestBody StoreTradeLogSaveReqVO createReqVO) {
        return success(storeTradeLogService.createStoreTradeLog(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新旅行社门店交易记录：余额充值、预付购票、预付退款、余额购票")
    @PreAuthorize("@ss.hasPermission('otc:store-trade-log:update')")
    public CommonResult<Boolean> updateStoreTradeLog(@Valid @RequestBody StoreTradeLogSaveReqVO updateReqVO) {
        storeTradeLogService.updateStoreTradeLog(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除旅行社门店交易记录：余额充值、预付购票、预付退款、余额购票")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('otc:store-trade-log:delete')")
    public CommonResult<Boolean> deleteStoreTradeLog(@RequestParam("id") Long id) {
        storeTradeLogService.deleteStoreTradeLog(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得旅行社门店交易记录：余额充值、预付购票、预付退款、余额购票")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('otc:store-trade-log:query')")
    public CommonResult<StoreTradeLogRespVO> getStoreTradeLog(@RequestParam("id") Long id) {
        StoreTradeLogRespVO storeTradeLog = storeTradeLogService.getStoreTradeLog(id);
        return success(storeTradeLog);
    }

    @GetMapping("/page")
    @Operation(summary = "获得旅行社门店交易记录：余额充值、预付购票、预付退款、余额购票分页")
    @PreAuthorize("@ss.hasPermission('otc:store-trade-log:query')")
    public CommonResult<PageResult<StoreTradeLogRespVO>> getStoreTradeLogPage(@Valid StoreTradeLogPageReqVO pageReqVO) {
        PageResult<StoreTradeLogDO> pageResult = storeTradeLogService.getStoreTradeLogPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, StoreTradeLogRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出旅行社门店交易记录：余额充值、预付购票、预付退款、余额购票 Excel")
    @PreAuthorize("@ss.hasPermission('otc:store-trade-log:export')")
    @OperateLog(type = EXPORT,enable = false)
    public void exportStoreTradeLogExcel(@Valid StoreTradeLogPageReqVO pageReqVO,
                                         HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<StoreTradeLogDO> list = storeTradeLogService.getStoreTradeLogPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "旅行社门店交易记录.xls", "数据", StoreTradeLogRespVO.class,
                BeanUtils.toBean(list, StoreTradeLogRespVO.class));
    }

}
