package com.community.huiju.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.community.commom.fushi.FuShiRequestAPI;
import com.community.commom.mode.ResponseBean;
import com.community.commom.uuid.IdGen;
import com.community.huiju.common.perproties.FuShiProperties;
import com.community.huiju.log.LogAnnotation;
import com.community.huiju.log.LogEnums;
import com.community.huiju.service.IFuShiService;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Map;

/**
 * 富士业务实现
 * @author weiximei
 */
@Slf4j
@Service
public class FuShiServiceImpl implements IFuShiService {

    @Autowired
    private FuShiRequestAPI fuShiRequestAPI;

    @Autowired
    private FuShiProperties fuShiProperties;

    private IdGen idGen = IdGen.get();

    /**
     * 获取 富士 json 值
     * @param jsonString
     * @return
     */
    private Map<String,Object> getFuShiInfo(String jsonString) {
        JSONObject jsonObject = JSONObject.parseObject(jsonString);
        Map<String, Object> map = Maps.newHashMap();
        Map<String, Object> jsonParam = jsonObject.getJSONObject("JsonParam").getInnerMap();
        // 移除优惠券
        jsonParam.remove("CouponList");
        map.put("JsonParam", jsonParam);
        map.put("Redirect", jsonObject.getString("Redirect"));
        map.put("DataType", jsonObject.getInteger("DataType"));
        return map;
    }

    @LogAnnotation(value = LogEnums.DA_SU, alias = "获取停车信息（通过车牌号)")
    @Override
    public ResponseBean getParkInfoByCarNo(String carNo) {
        ResponseBean responseBean = new ResponseBean();
        String parkInfo = fuShiRequestAPI.getParkInfoByCarNo(fuShiProperties.getParkingCode(), carNo);
        Map<String, Object> fuShiInfo = getFuShiInfo(parkInfo);
        responseBean.addSuccess(fuShiInfo);
        return responseBean;
    }

    @LogAnnotation(value = LogEnums.DA_SU, alias = "第三方月卡上发续费")
    @Override
    public ResponseBean apiThirdPartyMonthCardPay(String Phone, String RenewDayType, String RenewDay, String PayStyle, String CarNo, String CardNo,String RechargeAmt) {
        ResponseBean responseBean = new ResponseBean();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
        // 交易编码
        String dealNo = idGen.nextId() + "";
        // 第三方交易日期
        String dateTime = dateTimeFormatter.format(LocalDateTime.now());
        // 富士云请求
        String partyMonthCardPay = fuShiRequestAPI.apiThirdPartyMonthCardPay(RenewDayType, RenewDay, dealNo, PayStyle, CarNo, fuShiProperties.getParkingCode(), CardNo, RechargeAmt, dateTime);
        String tradeNo = JSONObject.parseObject(partyMonthCardPay).getString("TradeNo");
        Map<String, Object> map = Maps.newHashMap();
        map.put("TradeNo", tradeNo);
        responseBean.addSuccess(map);
        return responseBean;
    }

    /**
     * 第三方临时卡上发缴费
     * @param Amount 应缴金额（单位元）
     * @param ActualAmount 实缴金额（单位元）
     * @param DeductionAmount 抵扣金额（单位元）
     * @param Reason 抵扣原因
     * @param PayStyle 交易方式(汉字描述)，例如：微信、支付宝、银联
     * @param CarNo 车牌号
     * @param CardNo 停车卡号
     * @param CouponList 使用的优惠券列表(JSON字符串)
     *               MerchantName: 商户名称
     *               CouponType: 优惠券优惠类型1全免2减免金额3减免时长4减免百分比
     *               CouponValue： 对应优惠券类型的值 金额/时间/折扣
     *               CouponCode： 优惠券编码
     *               FavorableType： 优惠券类型1 纸质券 2 电子券 3 活动券
     *               UseStatus：标识优惠券是线上或线下使用的状态 1只在线上使用 2只在线下使用 3线上线下都可以使用
     *               IsAuto： 是否自动使用优惠券1是2否
     *               EndTime： 截至时间
     *               CouponDescribe： 优惠券描述
     *               CouponName： 优惠券名称
     *
     * @return
     */
    @LogAnnotation(value = LogEnums.DA_SU, alias = "第三方临时卡上发缴费")
    @Override
    public ResponseBean apiThirdPartyTemporaryCardPay(String Amount, String ActualAmount, String DeductionAmount, String Reason, String PayStyle, String CarNo, String CardNo, String CouponList) {
        ResponseBean responseBean = new ResponseBean();
        // 交易编号
        String dealNo = idGen.nextId() + "";
        String apiThirdPartyTemporaryCardPay = fuShiRequestAPI.apiThirdPartyTemporaryCardPay(Amount, ActualAmount, DeductionAmount, Reason, dealNo, PayStyle, CarNo, fuShiProperties.getParkingCode(), CardNo, "", CouponList);
        String tradeNo = JSONObject.parseObject(apiThirdPartyTemporaryCardPay).getString("TradeNo");
        Map<String, Object> map = Maps.newHashMap();
        map.put("TradeNo", tradeNo);
        responseBean.addSuccess(map);
        return responseBean;
    }

    @LogAnnotation(value = LogEnums.DA_SU, alias = "获取线下算费结果")
    @Override
    public ResponseBean apiGetOfflineFee(String CarNoOrCardNo) {
        ResponseBean responseBean = new ResponseBean();
        String apiGetOfflineFee = fuShiRequestAPI.apiGetOfflineFee(fuShiProperties.getParkingCode(), CarNoOrCardNo);
        String result = JSONObject.parseObject(apiGetOfflineFee).getString("Result");
        Map<String, Object> map = Maps.newHashMap();
        map.put("Result", result);
        responseBean.addSuccess(map);
        return responseBean;
    }

    /**
     * 获取月租车信息（通过车牌号）
     * @param CarNo
     * @return
     */
    @LogAnnotation(value = LogEnums.DA_SU, alias = "获取月租车信息（通过车牌号）")
    @Override
    public ResponseBean parkGetMonthCardInfoByCarNo(String CarNo) {
        ResponseBean responseBean = new ResponseBean();
        String parkGetMonthCardInfoByCarNo = fuShiRequestAPI.parkGetMonthCardInfoByCarNo(fuShiProperties.getParkingCode(), CarNo);
        JSONObject jsonObject = JSONObject.parseObject(parkGetMonthCardInfoByCarNo);
        Map<String, Object> map = Maps.newHashMap();
        Map<String, Object> result = jsonObject.getJSONObject("Result").getInnerMap();
        result.remove("CouponList");
        map.put("Result", result);
        responseBean.addSuccess(map);
        return responseBean;
    }

    /**
     * 获取临时车信息（通过车牌号）
     * @param CarNo
     * @return
     */
    @LogAnnotation(value = LogEnums.DA_SU, alias = "获取临时车信息（通过车牌号）")
    @Override
    public ResponseBean parkGetTempCarPaymentInfoByCarNo(String CarNo) {
        ResponseBean responseBean = new ResponseBean();
        String parkGetMonthCardInfoByCarNo = fuShiRequestAPI.parkGetTempCarPaymentInfoByCarNo(fuShiProperties.getParkingCode(), CarNo);
        JSONObject jsonObject = JSONObject.parseObject(parkGetMonthCardInfoByCarNo);
        Map<String, Object> map = Maps.newHashMap();
        Map<String, Object> result = jsonObject.getJSONObject("Result").getInnerMap();
        result.remove("CouponList");
        map.put("Result", result);
        responseBean.addSuccess(map);
        return responseBean;
    }
}
