package com.hzs.member.detail.provider;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONUtil;
import com.hzs.common.core.domain.R;
import com.hzs.common.core.enums.EApprovalBusiness;
import com.hzs.common.core.utils.ComputeUtil;
import com.hzs.common.domain.member.business.UpPolicyCurrency;
import com.hzs.common.domain.member.detail.CuMemberServiceLog;
import com.hzs.member.detail.ICuMemberServiceLogServiceApi;
import com.hzs.member.detail.service.ICuMemberServiceLogService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@DubboService
public class CuMemberServiceLogProvider implements ICuMemberServiceLogServiceApi {

    @Autowired
    private ICuMemberServiceLogService iCuMemberServiceLogService;

    @Override
    public R<UpPolicyCurrency> queryMaxCuMemberServiceLog(Long pkMember, List<Integer> editTypeList) {
        UpPolicyCurrency upPolicyCurrency = null;

        // 个人政策币配置
        List<CuMemberServiceLog> cuMemberServiceLogs = iCuMemberServiceLogService.listMaxMemberServiceLog(pkMember, editTypeList);
        if (CollectionUtil.isNotEmpty(cuMemberServiceLogs)) {
            Map<Integer, CuMemberServiceLog> cuMemberServiceLogMap = new HashMap<>(ComputeUtil.mapInitCapacity(cuMemberServiceLogs.size()));
            cuMemberServiceLogs.forEach(serviceLog -> cuMemberServiceLogMap.put(serviceLog.getEditType(), serviceLog));
            try {
                upPolicyCurrency = JSONUtil.toBean(cuMemberServiceLogMap.get(EApprovalBusiness.POLICY_CURRENCY_YES_NO.getValue()).getNewValue(), UpPolicyCurrency.class);
            } catch (Exception e) {
                log.error("政策币转换异常1， upPolicyCurrency: {}", upPolicyCurrency, e);
            }
        }

        if (null == upPolicyCurrency) {
            // 血缘上政策币
            CuMemberServiceLog cuMemberServiceLog = iCuMemberServiceLogService.getBloodMemberServiceLog(pkMember, EApprovalBusiness.POLICY_CURRENCY_YES_NO.getValue());
            if (null != cuMemberServiceLog) {
                try {
                    upPolicyCurrency = JSONUtil.toBean(cuMemberServiceLog.getNewValue(), UpPolicyCurrency.class);
                } catch (Exception e) {
                    log.error("政策币转换异常2， upPolicyCurrency: {}", upPolicyCurrency, e);
                }
            }
        }

        return R.ok(upPolicyCurrency);
    }

    @Override
    public Integer selectByMaxApproveNumber(Long pkMember, Integer editType, Integer approveStatus) {
        return iCuMemberServiceLogService.selectByMaxApproveNumber(pkMember, editType, approveStatus);
    }

    @Override
    public R<Boolean> insert(CuMemberServiceLog memberLog) {
        return R.ok(iCuMemberServiceLogService.save(memberLog));
    }

    @Override
    public R<CuMemberServiceLog> selectByBusinessNo(String businessNo) {
        return iCuMemberServiceLogService.selectByBusinessNo(businessNo);
    }

    @Override
    public R<Boolean> updateByBusinessNo(CuMemberServiceLog memberLog) {
        return iCuMemberServiceLogService.updateByBusinessNo(memberLog);
    }

    @Override
    public R<CuMemberServiceLog> selectTopData(Long pkMember, int editType, int approveStatus) {
        return R.ok(iCuMemberServiceLogService.selectTopData(pkMember, editType, approveStatus));
    }

}
