package com.ruoyi.web.controller.ss;

import java.util.Collections;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.ss.transactionBill.domain.TransactionBillQuery;
import com.ruoyi.ss.transactionBill.domain.dto.EndUseDTO;
import com.ruoyi.ss.transactionBill.domain.vo.TransactionBillVO;
import com.ruoyi.ss.transactionBill.domain.dto.BillRefundDTO;
import com.ruoyi.ss.transactionBill.domain.dto.WithdrawApprovalDTO;
import com.ruoyi.ss.transactionBill.service.TransactionAssembler;
import com.ruoyi.ss.transactionBill.service.TransactionBillConverter;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.ss.transactionBill.service.TransactionBillService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 充值记录Controller
 *
 * @author 邱贞招
 * @date 2024-02-21
 */
@RestController
@RequestMapping("/system/bill")
public class SmTransactionBillController extends BaseController
{
    @Autowired
    private TransactionBillService transactionBillService;

    @Autowired
    private TransactionBillConverter transactionBillConverter;

    @Autowired
    private TransactionAssembler transactionAssembler;

    /**
     * 查询充值记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:bill:list')")
    @GetMapping("/list")
    public TableDataInfo list(TransactionBillQuery smTransactionBill)
    {
        startPage();
        List<TransactionBillVO> list = transactionBillService.selectSmTransactionBillList(smTransactionBill);
        transactionAssembler.assembleTotalUse(list);
        return getDataTable(list);
    }

    /**
     * 导出充值记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:bill:export')")
    @Log(title = "充值记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TransactionBillQuery smTransactionBill)
    {
        List<TransactionBillVO> list = transactionBillService.selectSmTransactionBillList(smTransactionBill);
        ExcelUtil<TransactionBillVO> util = new ExcelUtil<TransactionBillVO>(TransactionBillVO.class);
        util.exportExcel(response, list, "充值记录数据");
    }

    /**
     * 获取充值记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:bill:query')")
    @GetMapping(value = "/{billId}")
    public AjaxResult getInfo(@PathVariable("billId") Long billId)
    {
        TransactionBillVO bill = transactionBillService.selectSmTransactionBillByBillId(billId);
        List<TransactionBillVO> list = Collections.singletonList(bill);
        transactionAssembler.assembleTotalUse(list);
        return success(bill);
    }

    @PreAuthorize("@ss.hasPermi('system:bill:query')")
    @GetMapping("/count")
    public AjaxResult count(TransactionBillQuery dto)
    {
        return success(transactionBillService.selectCount(dto));
    }

    @ApiOperation("同意提现申请")
    @PreAuthorize("@ss.hasPermi('system:bill:approval')")
    @PutMapping("/withdraw/pass")
    @Log(title = "同意提现申请", businessType = BusinessType.UPDATE)
    public AjaxResult passWithdraw(@RequestBody @Validated WithdrawApprovalDTO dto)
    {
        return success(transactionBillService.passWithdraw(dto));
    }

    @ApiOperation("拒绝提现申请")
    @PreAuthorize("@ss.hasPermi('system:bill:approval')")
    @PutMapping("/withdraw/reject")
    @Log(title = "拒绝提现申请", businessType = BusinessType.UPDATE)
    public AjaxResult rejectWithdraw(@RequestBody @Validated WithdrawApprovalDTO dto)
    {
        return success(transactionBillService.rejectWithdraw(dto));
    }

//    @ApiOperation("提现打款")
//    @PreAuthorize("@ss.hasPermi('system:bill:pay')")
//    @PutMapping("/withdraw/pay/{billId}")
//    @Log(title = "提现打款", businessType = BusinessType.UPDATE)
//    public AjaxResult payWithdraw(@PathVariable Long billId)
//    {
//        return success(transactionBillService.payWithdraw(billId));
//    }

    @ApiModelProperty("刷新支付结果")
    @PreAuthorize("@ss.hasPermi('system:bill:edit')")
    @GetMapping("/refreshPayResult/{billId}")
    @Log(title = "刷新支付结果", businessType = BusinessType.UPDATE)
    public AjaxResult refreshPayResult(@PathVariable Long billId)
    {
        transactionBillService.refreshPayResult(billId);
        return success();
    }

    @ApiModelProperty("手动设备充值")
    @GetMapping("/rechargeDevice/{billId}")
    @Log(title = "手动设备充值", businessType = BusinessType.UPDATE)
    public AjaxResult rechargeDevice(@PathVariable Long billId) {
        return toAjax(transactionBillService.rechargeDevice(billId));
    }

    // 订单退款
    @PutMapping("/refund")
    @Log(title = "订单退款", businessType = BusinessType.OTHER)
    @PreAuthorize("@ss.hasPermi('system:bill:refund')")
    public AjaxResult refund(@RequestBody @Validated BillRefundDTO dto) {
        TransactionBillVO bill = transactionBillService.selectSmTransactionBillByBillId(dto.getBillId());
        if (bill == null) {
            return error("订单不存在");
        }
        LoginUser loginUser = getLoginUser();
        dto.setUserName(loginUser.getUsername());
        dto.setUserType(loginUser.getLoginType().getType());
        dto.setUserId(getUserId());
        dto.setRefundReason(String.format("充值订单退款:%s", bill.getBillNo()));
        return toAjax(transactionBillService.refund(dto));
    }

    // 结束订单
    @PutMapping("/close")
    @Log(title = "结束订单", businessType = BusinessType.OTHER)
    @PreAuthorize("@ss.hasPermi('system:bill:close')")
    public AjaxResult close(@RequestBody @Validated EndUseDTO dto) {
        return success(transactionBillService.endUse(transactionBillConverter.toEndUseBO(dto), true));
    }

    // 修复订单数据
    @PutMapping("/{billId}/fix")
    @Log(title = "修复订单数据", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('system:bill:fix')")
    public AjaxResult fix(@PathVariable Long billId) {
        return toAjax(transactionBillService.fix(billId));
    }

    @PutMapping("/fixAll")
    @Log(title = "修复所有订单数据", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('system:bill:fix')")
    public AjaxResult fixAll() {
        return toAjax(transactionBillService.fixAll());
    }

}
