package com.hzs.third.pay.controller.manage;

import cn.hutool.core.bean.BeanUtil;
import com.hzs.common.core.annotation.AccessPermissions;
import com.hzs.common.core.enums.*;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.core.utils.poi.ExcelUtil;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.core.web.page.TableDataInfo;
import com.hzs.common.domain.third.pay.TOnlineRefund;
import com.hzs.common.domain.third.pay.ext.TOnlineRefundExt;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.third.pay.param.OnlineRefundParam;
import com.hzs.third.pay.service.IRefundService;
import com.hzs.third.pay.service.ITOnlineRefundService;
import com.hzs.third.pay.vo.OnlineRefundVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * @Description: 退款明细
 * @Author: jiang chao
 * @Time: 2023/8/16 18:50
 * @Classname: OnlineRefundController
 * @PackageName: com.hzs.third.pay.controller.manage
 */
@RestController
@RequestMapping("/manage/online-refund")
public class OnlineRefundController extends BaseController {

    @Autowired
    private ITOnlineRefundService itOnlineRefundService;
    @Autowired
    private IRefundService iRefundService;

    /**
     * 退款列表
     *
     * @param param 查询入参
     * @return
     */
    @AccessPermissions("OnlineRefundDetails")
    @GetMapping("/refund-list")
    public TableDataInfo refundList(OnlineRefundParam param) {
        DateUtils.handTimeStartAndEnd(param.getCreationTime());
        DateUtils.handTimeStartAndEnd(param.getFinishTime());

        // 返回结果
        List<OnlineRefundVO> resultList = new ArrayList<>();
        param.setSystemType(SecurityUtils.getSystemType());
        startPage();
        List<TOnlineRefundExt> list = itOnlineRefundService.queryList(param, SecurityUtils.getPkCountry());
        if (StringUtils.isNotEmpty(list)) {
            resultList = BeanUtil.copyToList(list, OnlineRefundVO.class);
        }

        TableDataInfo tableDataInfo = getDataTable(list);
        tableDataInfo.setRows(resultList);
        return tableDataInfo;
    }

    /**
     * 退款导出
     *
     * @param response
     * @param param    查询入参
     */
    @PostMapping("/refund-export")
    public void refundExport(HttpServletResponse response, OnlineRefundParam param) {
        DateUtils.handTimeStartAndEnd(param.getCreationTime());
        DateUtils.handTimeStartAndEnd(param.getFinishTime());
        param.setSystemType(SecurityUtils.getSystemType());
        // 返回结果
        List<OnlineRefundVO> resultList = new ArrayList<>();

        List<TOnlineRefundExt> list = itOnlineRefundService.queryList(param, SecurityUtils.getPkCountry());

        if (StringUtils.isNotEmpty(list)) {
            // 获取需要翻译的枚举翻译
            resultList = BeanUtil.copyToList(list, OnlineRefundVO.class);

            for (OnlineRefundVO onlineRefundVO : resultList) {
                onlineRefundVO.setPayBusinessTypeVal(EPayBusinessType.getLabelByValue(onlineRefundVO.getPayBusinessType()));
                onlineRefundVO.setRefundStatusVal(ERefundStatus.getLabelByValue(onlineRefundVO.getRefundStatus()));
                onlineRefundVO.setRefundChannelVal(EPayChannel.getLabelByValue(onlineRefundVO.getRefundChannel()));
            }
        }

        ExcelUtil<OnlineRefundVO> util = new ExcelUtil<>(OnlineRefundVO.class);
        util.exportExcel(response, resultList, "退款明细导出");
    }

    /**
     * 查询退款状态
     *
     * @param pkId 退款ID
     * @return
     */
//    @GetMapping("/query-status")
//    public AjaxResult queryStatus(@RequestParam Long pkId) {
//        TOnlineRefund tOnlineRefund = itOnlineRefundService.getById(pkId);
//        if (ERefundStatus.REFUNDED.getValue() == tOnlineRefund.getRefundStatus()) {
//            // 已经退款成功
//            return AjaxResult.error("当前数据已退款");
//        }
//
//        String str;
//
//        if (EPayChannel.HUIFU.getValue() == tOnlineRefund.getRefundChannel()) {
//            // 新汇付退款
//            str = iRefundService.queryHuifuRefundHandle(tOnlineRefund);
//        } else {
//            return AjaxResult.error("暂不支持该数据查询");
//        }
//
//        if (null == str) {
//            return AjaxResult.success();
//        }
//        return AjaxResult.error(str);
//    }

}
