package com.yc.ship.module.trade.controller.admin.bill;

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.trade.controller.admin.order.vo.order.TradeOrderPageReqVO;
import com.yc.ship.module.trade.controller.admin.order.vo.order.TradeOrderRespVO;
import com.yc.ship.module.trade.dal.dataobject.bill.BillDO;
import com.yc.ship.module.trade.dal.dataobject.bill.BillStoreDO;
import com.yc.ship.module.trade.service.bill.BillService;
import com.yc.ship.module.trade.controller.admin.bill.vo.BillPageReqVO;
import com.yc.ship.module.trade.controller.admin.bill.vo.BillRespVO;
import com.yc.ship.module.trade.controller.admin.bill.vo.BillSaveReqVO;
import com.yc.ship.module.trade.controller.admin.bill.vo.StoreBillRespVO;
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("/ota/bill")
@Validated
public class BillController {

    @Resource
    private BillService billService;

    @PostMapping("/create")
    @Operation(summary = "通过时间自动创建旅行社账单")
//    @PreAuthorize("@ss.hasPermission('ota:bill:create')")
    public CommonResult<Long> createBill(@Valid @RequestBody BillSaveReqVO createReqVO) {
        return success(billService.createBill(createReqVO));
    }

    @PostMapping("/createOrderBill")
    @Operation(summary = "手动创建旅行社账单")
//    @PreAuthorize("@ss.hasPermission('ota:bill:create')")
    public CommonResult<Long> createBillOrder(@Valid @RequestBody BillSaveReqVO createReqVO) {
        return success(billService.createBillOrder(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新旅行社账单")
//    @PreAuthorize("@ss.hasPermission('ota:bill:update')")
    public CommonResult<Boolean> updateBill(@Valid @RequestBody BillSaveReqVO updateReqVO) {
        billService.updateBill(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除旅行社账单")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ota:bill:delete')")
    public CommonResult<Boolean> deleteBill(@RequestParam("id") Long id) {
        billService.deleteBill(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得旅行社账单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('ota:bill:query')")
    public CommonResult<BillRespVO> getBill(@RequestParam("id") Long id) {
        BillDO bill = billService.getBill(id);
        return success(BeanUtils.toBean(bill, BillRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得账单分页")
//    @PreAuthorize("@ss.hasPermission('ota:bill:query')")
    public CommonResult<PageResult<BillRespVO>> getBillPage(@Valid BillPageReqVO pageReqVO) {
        PageResult<BillRespVO> pageResult = billService.getBillPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BillRespVO.class));
    }

    @PostMapping("/pageStore")
    @Operation(summary = "获得门店账单分页")
//    @PreAuthorize("@ss.hasPermission('ota:bill:query')")
    public CommonResult<PageResult<BillRespVO>> getBillStorePage(@Valid @RequestBody BillPageReqVO pageReqVO) {
        PageResult<BillStoreDO> pageResult = billService.getBillStorePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BillRespVO.class));
    }

    @GetMapping("/export-order-bill-excel")
    @Operation(summary = "导出订单账单 Excel")
    @OperateLog(type = EXPORT,enable = false)
    public void exportOrderBillExcel(@Valid TradeOrderPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TradeOrderRespVO> orders = billService.getOrderPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "旅行社账单.xls", "数据", TradeOrderRespVO.class,
                        BeanUtils.toBean(orders, TradeOrderRespVO.class));
    }

    @GetMapping("/export-bill-excel")
    @Operation(summary = "导出旅行社账单 Excel")
    @OperateLog(type = EXPORT,enable = false)
    public void exportBillExcel(@Valid BillPageReqVO pageReqVO,
                                HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BillRespVO> billList = billService.getBillPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "旅行社账单.xls", "数据", BillRespVO.class, billList);
    }


    @GetMapping("/export-store-bill-excel")
    @Operation(summary = "导出门店账单 Excel")
    @OperateLog(type = EXPORT,enable = false)
    public void exportStoreBillExcel(@Valid BillPageReqVO pageReqVO,
                                HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BillStoreDO> billList = billService.getBillStorePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "门店账单.xls", "数据", StoreBillRespVO.class,
                BeanUtils.toBean(billList, StoreBillRespVO.class));
    }

    @GetMapping("/getBillOrderList")
    @Operation(summary = "获得账单绑定订单信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<List<TradeOrderRespVO>> getOrderList(@RequestParam("id") Long id,@RequestParam("type") Integer type) {
        List<TradeOrderRespVO> orders = billService.getBillOrderList(id,type);
        return success(BeanUtils.toBean(orders, TradeOrderRespVO.class));
    }

    @GetMapping("/getOrderPage")
    @Operation(summary = "获得旅行社订单信息")
    public CommonResult<PageResult<TradeOrderRespVO>> getOrderPage(TradeOrderPageReqVO pageReqVO) {
        PageResult<TradeOrderRespVO> orders = billService.getOrderPage(pageReqVO);
        return success(BeanUtils.toBean(orders, TradeOrderRespVO.class));
    }

    @GetMapping("/getStoreBillById")
    @Operation(summary = "通过旅行社账单获得门店账单信息")
    public CommonResult<List<BillRespVO>> getStoreBillById(@Valid Long id) {
        List<BillStoreDO> orders = billService.getStoreBillById(id);
        return success(BeanUtils.toBean(orders, BillRespVO.class));
    }
}