
package com.tmerclub.cloud.payment.controller.platform;

import com.tmerclub.cloud.api.payment.vo.AccountDetailVO;
import com.tmerclub.cloud.api.payment.vo.ShopAccountDetailVO;
import com.tmerclub.cloud.common.constant.Constant;
import com.tmerclub.cloud.common.database.dto.PageDTO;
import com.tmerclub.cloud.common.database.vo.PageVO;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.payment.dto.AccountSearchDTO;
import com.tmerclub.cloud.payment.service.PayInfoService;
import com.tmerclub.cloud.payment.service.RefundInfoService;
import com.tmerclub.cloud.payment.vo.PayInfoVO;
import com.tmerclub.cloud.payment.vo.RefundInfoVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * @author TRACK
 */
@RestController("platformAccountDetailController")
@RequestMapping("/p/accountDetail")
@Tag(name = "platform-账户详情")
public class AccountDetailController {
    @Autowired
    private PayInfoService payInfoService;

    @Autowired
    private RefundInfoService refundInfoService;

    @GetMapping("/getIncomeAccountDetail")
    @Operation(summary = "获取收入账户详情", description = "根据时间获取")
    public ServerResponseEntity<AccountDetailVO> getIncomeAccountDetail(AccountSearchDTO accountSearchDTO) {
        if (Objects.equals(accountSearchDTO.getShopId(), Constant.PLATFORM_SHOP_ID)) {
            AccountDetailVO platformIncomeAccountDetail = payInfoService.getPlatformIncomeAccountDetail(accountSearchDTO.getStartTime(), accountSearchDTO.getEndTime());
            return ServerResponseEntity.success(platformIncomeAccountDetail);
        }
        AccountDetailVO accountDetailVO = payInfoService.getIncomeAccountDetail(accountSearchDTO.getStartTime(), accountSearchDTO.getEndTime(), accountSearchDTO.getShopName());
        return ServerResponseEntity.success(accountDetailVO);
    }

    @GetMapping("/getRefundAccountDetail")
    @Operation(summary = "获取退款账户详情", description = "根据时间获取")
    public ServerResponseEntity<AccountDetailVO> getRefundAccountDetail(AccountSearchDTO accountSearchDTO) {
        if (Objects.equals(accountSearchDTO.getShopId(), Constant.PLATFORM_SHOP_ID)) {
            AccountDetailVO platformRefundAccountDetail = refundInfoService.getPlatformRefundAccountDetail(accountSearchDTO.getStartTime(), accountSearchDTO.getEndTime());
            return ServerResponseEntity.success(platformRefundAccountDetail);
        }
        AccountDetailVO accountDetailVO = refundInfoService.getRefundAccountDetail(accountSearchDTO.getStartTime(), accountSearchDTO.getEndTime(), accountSearchDTO.getShopName());
        return ServerResponseEntity.success(accountDetailVO);
    }

    @GetMapping("/getIncomeAccountDetailInfo")
    @Operation(summary = "分页获取收入账户详情", description = "分页获取收入账户详情")
    public ServerResponseEntity<PageVO<ShopAccountDetailVO>> getIncomeAccountDetailInfo(PageDTO pageDTO, AccountSearchDTO accountSearchDTO) {
        PageVO<ShopAccountDetailVO> accountDetailPage = payInfoService.pageIncomeAccountDetail(pageDTO, accountSearchDTO);
        return ServerResponseEntity.success(accountDetailPage);
    }

    @GetMapping("/getRefundAccountDetailInfo")
    @Operation(summary = "分页获取退款账户详情", description = "分页获取退款账户详情")
    public ServerResponseEntity<PageVO<ShopAccountDetailVO>> getRefundAccountDetailInfo(PageDTO pageDTO, AccountSearchDTO accountSearchDTO) {
        PageVO<ShopAccountDetailVO> accountDetailPage = refundInfoService.pageRefundAccountDetail(pageDTO, accountSearchDTO);
        return ServerResponseEntity.success(accountDetailPage);
    }

    @GetMapping("/getPayInfo")
    @Operation(summary = "分页获取指定店铺的收入结算明细", description = "根据分页参数和时间获取")
    public ServerResponseEntity<PageVO<PayInfoVO>> getPayInfo(PageDTO pageDTO, AccountSearchDTO accountSearchDTO) {
        PageVO<PayInfoVO> pageVO = payInfoService.getPayInfoPage(pageDTO, accountSearchDTO);
        return ServerResponseEntity.success(pageVO);
    }

    @GetMapping("/getRefundInfo")
    @Operation(summary = "分页获取指定店铺的退款结算明细", description = "根据分页参数和时间获取")
    public ServerResponseEntity<PageVO<RefundInfoVO>> getRefundInfo(PageDTO pageDTO, AccountSearchDTO accountSearchDTO) {
        PageVO<RefundInfoVO> pageVO = refundInfoService.getRefundInfoPage(pageDTO, accountSearchDTO);
        return ServerResponseEntity.success(pageVO);
    }

    @GetMapping("/soldExcel")
    @Operation(summary = "导出收入对账excel", description = "导出收入对账excel")
    public ServerResponseEntity<Void> soldExcel(HttpServletResponse response, AccountSearchDTO accountSearchDTO) {
        payInfoService.excelPayInfo(response, accountSearchDTO);
        return ServerResponseEntity.success();
    }

    @GetMapping("/refundSoldExcel")
    @Operation(summary = "导出退款对账excel", description = "导出退款对账excel")
    public ServerResponseEntity<Void> refundSoldExcel(HttpServletResponse response, AccountSearchDTO accountSearchDTO) {
        refundInfoService.excelRefundInfo(response, accountSearchDTO);
        return ServerResponseEntity.success();
    }
}
