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.constant.EnumsPrefixConstants;
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.page.TableDataInfo;
import com.hzs.common.domain.third.pay.ext.TOnlineRefundExt;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.common.service.ITransactionCommonService;
import com.hzs.third.pay.param.OnlineRefundParam;
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.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * 退款明细
 */
@RestController
@RequestMapping("/manage/online-refund")
public class OnlineRefundController extends BaseController {

    @Autowired
    private ITOnlineRefundService itOnlineRefundService;

    @Autowired
    private ITransactionCommonService iTransactionCommonService;

    /**
     * 退款列表
     *
     * @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<>();

        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());

        // 返回结果
        List<OnlineRefundVO> resultList = new ArrayList<>();

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

        if (StringUtils.isNotEmpty(list)) {
            // 获取需要翻译的枚举翻译
            Map<String, String> transactionMap = iTransactionCommonService.exportEnumTransaction(EPayBusinessType.values(), ERefundStatus.values(), EPayChannel.values());

            resultList = BeanUtil.copyToList(list, OnlineRefundVO.class);

            for (OnlineRefundVO onlineRefundVO : resultList) {
                onlineRefundVO.setPayBusinessTypeVal(transactionMap.get(EnumsPrefixConstants.PAY_BUSINESS_TYPE + onlineRefundVO.getPayBusinessType()));
                onlineRefundVO.setRefundStatusVal(transactionMap.get(EnumsPrefixConstants.REFUND_STATUS + onlineRefundVO.getRefundStatus()));
                onlineRefundVO.setRefundChannelVal(transactionMap.get(EnumsPrefixConstants.PAY_CHANNEL + onlineRefundVO.getRefundChannel()));
            }
        }

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

}
