package com.liquidnet.service.kylin.service.impl;

import com.liquidnet.common.cache.redis.util.RedisUtil;
import com.liquidnet.service.kylin.constant.KylinRedisConst;
import com.liquidnet.service.kylin.dto.vo.OrderRefundReasonVo;
import com.liquidnet.service.kylin.entity.KylinOrderRefundReasons;
import com.liquidnet.service.kylin.mapper.KylinOrderRefundReasonsMapper;
import com.liquidnet.service.kylin.service.IKylinOrderRefundReasonService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 退款原因（App/H5）
 */
@Slf4j
@Service
public class KylinOrderRefundReasonServiceImpl implements IKylinOrderRefundReasonService {

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private KylinOrderRefundReasonsMapper reasonsMapper;

    @Override
    @SuppressWarnings("unchecked")
    public List<OrderRefundReasonVo> listApplied() {
        Object cached = redisUtil.get(KylinRedisConst.KYLIN_ORDER_REFUND_REASONS_APPLIED);
        if (cached != null) {
            return (List<OrderRefundReasonVo>) cached;
        }
        List<KylinOrderRefundReasons> list = reasonsMapper.listApplied();
        List<OrderRefundReasonVo> voList = new ArrayList<>();
        if (list != null) {
            for (KylinOrderRefundReasons item : list) {
                OrderRefundReasonVo vo = new OrderRefundReasonVo();
                BeanUtils.copyProperties(item, vo);
                voList.add(vo);
            }
        }
        redisUtil.set(KylinRedisConst.KYLIN_ORDER_REFUND_REASONS_APPLIED, voList);
        return voList;
    }

    @Override
    public OrderRefundReasonVo getAppliedByReasonId(String reasonId) {
        if (StringUtils.isBlank(reasonId)) {
            return null;
        }
        List<OrderRefundReasonVo> list = listApplied();
        if (list == null) {
            return null;
        }
        for (OrderRefundReasonVo item : list) {
            if (reasonId.equals(item.getReasonId())) {
                return item;
            }
        }
        return null;
    }
}
