package com.cf.imes.module.system.controller.admin.funds.purchase;

import com.cf.imes.framework.common.pojo.CommonResult;
import com.cf.imes.framework.common.pojo.PageParam;
import com.cf.imes.framework.common.pojo.PageResult;
import com.cf.imes.framework.excel.core.util.ExcelUtils;
import com.cf.imes.framework.operatelog.core.annotations.OperateLog;
import com.cf.imes.module.system.controller.admin.funds.organamount.vo.TotalSalesAmountRespVO;
import com.cf.imes.module.system.controller.admin.funds.purchase.vo.SalesDetailRespVO;
import com.cf.imes.module.system.controller.admin.funds.purchase.vo.SalesDetailsPageReqVO;
import com.cf.imes.module.system.service.funds.purchase.PurchaseService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

import static com.cf.imes.framework.common.pojo.CommonResult.success;
import static com.cf.imes.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

/**
 * @author 软件购买
 */
@Tag(name = "管理后台 - 销售明细管理")
@RestController
@RequestMapping("/system/sales")
@Validated
public class SalesDetailsController {

    @Resource
    private PurchaseService purchaseService;

    @GetMapping("/page")
    @Operation(summary = "获取销售明细分页")
    public CommonResult<PageResult<SalesDetailRespVO>> getManageSalesDetailsPage(@Valid SalesDetailsPageReqVO pageReqVO) {
        return success(purchaseService.getManageSalesDetailsPage(pageReqVO));
    }

    @GetMapping("/export")
    @Operation(summary = "导出充值明细")
    @OperateLog(type = EXPORT)
    public void exportSalesDetailsPage(@Valid SalesDetailsPageReqVO pageReqVO,
                                      HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SalesDetailRespVO> list = purchaseService.getManageSalesDetailsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "销售明细表.xls", "数据", SalesDetailRespVO.class, list);
    }

    @GetMapping("/total")
    @Operation(summary = "获取管理端销售总额信息")
    public CommonResult<TotalSalesAmountRespVO> getManageTotalSalesInfo() {
        return success(purchaseService.getTotalSales());
    }
}
