package com.ruoyi.web.controller.mch;

import com.fasterxml.jackson.annotation.JsonView;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.JsonViewProfile;
import com.ruoyi.common.utils.ServiceUtil;
import com.ruoyi.ss.transactionBill.domain.TransactionBillQuery;
import com.ruoyi.ss.transactionBill.domain.vo.TransactionBillVO;
import com.ruoyi.ss.transactionBill.service.TransactionAssembler;
import com.ruoyi.ss.transactionBill.service.TransactionBillService;
import com.ruoyi.ss.transactionBill.service.TransactionBillValidator;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;

/**
 * @author wjh
 * 2024/7/29
 */
@RestController
@RequestMapping("/mch/bill")
public class MchTransactionBillController extends BaseController {

    @Autowired
    private TransactionBillService transactionBillService;

    @Autowired
    private TransactionBillValidator transactionBillValidator;

    @Autowired
    private TransactionAssembler transactionAssembler;

    @ApiOperation("商户查询统计信息")
    @GetMapping("/count")
    public AjaxResult count(TransactionBillQuery query)
    {
        query.setMchId(getUserId());
        return success(transactionBillService.selectCount(query));
    }

    @ApiOperation("收款人获取订单详情")
    @GetMapping("/{billId}")
    @JsonView(JsonViewProfile.AppMch.class)
    public AjaxResult detail(@PathVariable Long billId) {
        TransactionBillVO bill = transactionBillService.selectSmTransactionBillByBillId(billId);
        if (!transactionBillValidator.isMch(bill, getUserId()) && !transactionBillValidator.isAgent(bill, getUserId())) {
            return success();
        }
        transactionAssembler.assembleChannelName(Collections.singletonList(bill));
        return AjaxResult.success(bill);
    }

}

