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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.hzs.common.core.annotation.AccessPermissions;
import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.constant.EnumsPrefixConstants;
import com.hzs.common.core.constant.msg.ConfigMsgConstants;
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.TOnlinePayment;
import com.hzs.common.domain.third.pay.ext.TOnlinePaymentExt;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.common.service.ITransactionCommonService;
import com.hzs.common.util.TransactionUtils;
import com.hzs.third.pay.param.OnlinePaymentParam;
import com.hzs.third.pay.param.OnlinePaymentRetryParam;
import com.hzs.third.pay.service.IPayService;
import com.hzs.third.pay.service.ITOnlinePaymentService;
import com.hzs.third.pay.vo.OnlinePaymentVO;
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.Date;
import java.util.List;
import java.util.Map;

/**
 * 在线支付明细
 */
@RestController
@RequestMapping("/manage/online-payment")
public class OnlinePaymentController extends BaseController {

    @Autowired
    private ITOnlinePaymentService itOnlinePaymentService;
    @Autowired
    private IPayService iPayService;

    @Autowired
    private ITransactionCommonService iTransactionCommonService;

    /**
     * 列表
     *
     * @param param 查询参数
     * @return
     */
    @AccessPermissions("OnlinePayDetails")
    @Log(module = EOperationModule.ONLINE_PAY_DETAIL, business = EOperationBusiness.ONLINE_PAY_DETAIL, method = EOperationMethod.SELECT)
    @GetMapping("/list")
    public TableDataInfo list(OnlinePaymentParam param) {
        DateUtils.handTimeStartAndEnd(param.getCreationTime());
        DateUtils.handTimeStartAndEnd(param.getPayTime());

        List<OnlinePaymentVO> resultList = new ArrayList<>();

        startPage();
        List<TOnlinePaymentExt> list = itOnlinePaymentService.queryList(param, SecurityUtils.getPkCountry());
        if (CollectionUtil.isNotEmpty(list)) {
            resultList = BeanUtil.copyToList(list, OnlinePaymentVO.class);
        }

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

    /**
     * 导出
     *
     * @param response
     * @param param    查询参数
     */
    @Log(module = EOperationModule.ONLINE_PAY_DETAIL, business = EOperationBusiness.ONLINE_PAY_DETAIL, method = EOperationMethod.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OnlinePaymentParam param) {
        DateUtils.handTimeStartAndEnd(param.getCreationTime());
        DateUtils.handTimeStartAndEnd(param.getPayTime());

        List<OnlinePaymentVO> resultList = new ArrayList<>();

        List<TOnlinePaymentExt> list = itOnlinePaymentService.queryList(param, SecurityUtils.getPkCountry());
        if (CollectionUtil.isNotEmpty(list)) {
            // 获取需要翻译的枚举翻译
            Map<String, String> transactionMap = iTransactionCommonService.exportEnumTransaction(EPayBusinessType.values(), EPayStatus.values(), EPayChannel.values(), EPayType.values());

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

            for (OnlinePaymentVO onlinePaymentVO : resultList) {
                onlinePaymentVO.setBusinessTypeVal(transactionMap.get(EnumsPrefixConstants.PAY_BUSINESS_TYPE + onlinePaymentVO.getBusinessType()));
                onlinePaymentVO.setPayStatusVal(transactionMap.get(EnumsPrefixConstants.PAY_STATUS + onlinePaymentVO.getPayStatus()));
                onlinePaymentVO.setPayChannelVal(transactionMap.get(EnumsPrefixConstants.PAY_CHANNEL + onlinePaymentVO.getPayChannel()));
                onlinePaymentVO.setPayTypeVal(transactionMap.get(EnumsPrefixConstants.PAY_TYPE + onlinePaymentVO.getPayType()));
            }
        }

        ExcelUtil<OnlinePaymentVO> util = new ExcelUtil<>(OnlinePaymentVO.class);
        util.exportExcel(response, resultList, "在线支付明细导出");
    }

    /**
     * 业务重试
     *
     * @param param 业务重试入参
     * @return
     */
    @Log(module = EOperationModule.ONLINE_PAY_DETAIL, business = EOperationBusiness.ONLINE_PAY_DETAIL, method = EOperationMethod.SUBMIT)
    @PostMapping("/retry")
    public AjaxResult retry(@RequestBody OnlinePaymentRetryParam param) {
        if (null == param.getBusinessCode() || StringUtils.isEmpty(param.getBusinessCode())) {
            // 缺少参数
            return AjaxResult.error("缺少参数");
        }
        TOnlinePayment tOnlinePayment = itOnlinePaymentService.queryByBusiness(param.getBusinessType(), param.getBusinessCode(), SecurityUtils.getPkCountry());
        if (null == tOnlinePayment) {
            // 数据不存在
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.HO_OBJECT_NOT_REPEAT));
        }
        if (EPayStatus.PAID.getValue() != tOnlinePayment.getPayStatus()) {
            // 未支付成功，不能进行操作
            return AjaxResult.error("未支付成功，不能进行操作");
        }
        if (ECallbackStatus.SUCCESS.getValue() == tOnlinePayment.getCallbackStatus()) {
            // 业务回调已成功，不能重复处理
            return AjaxResult.error("业务回调已成功，不能重复处理");
        }

        tOnlinePayment.setPkModified(SecurityUtils.getUserId());
        tOnlinePayment.setModifiedTime(new Date());

        return toAjax(iPayService.retryHandle(tOnlinePayment, false));
    }

}
