package com.hzs.member.base.provider;

import com.hzs.common.core.domain.R;
import com.hzs.common.core.enums.ELoginType;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.domain.member.base.CuMember;
import com.hzs.common.domain.member.base.CuMemberRegister;
import com.hzs.common.domain.member.ext.CuMemberAccountExt;
import com.hzs.common.domain.member.ext.CuMemberExt;
import com.hzs.common.domain.member.swing.ext.CuMemberSwingNetExt;
import com.hzs.common.domain.sale.ext.SaOrderExt;
import com.hzs.common.domain.sale.order.SaOrder;
import com.hzs.member.account.dto.BusinessCommissionDTO;
import com.hzs.member.base.dto.LoginMember;
import com.hzs.member.base.dto.ShowWaresDTO;
import com.hzs.member.base.dto.ZeroElementRevokeDTO;
import com.hzs.member.base.service.*;
import com.hzs.member.base.IMemberServiceApi;
import com.hzs.member.empty.service.ICuMemberEmptyCodeService;
import com.hzs.member.handlebusiness.dto.AnalysisBusinessAppealParam;
import com.hzs.member.swing.param.CuMemberSwingNetParam;
import com.hzs.member.swing.service.ICuMemberSwingNetService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 会员涉及dubbo服务
 */
@Slf4j
@DubboService
public class MemberServiceProvider implements IMemberServiceApi {

    @Autowired
    private ICuMemberService iCuMemberService;
    @Autowired
    private ICuMemberBusinessService iCuMemberBusinessService;
    @Autowired
    private ICuMemberRegisterService iCuMemberRegisterService;
    @Autowired
    private ICuMemberEmptyCodeService iCuMemberEmptyCodeService;

    @Override
    public R<LoginMember> getMemberInfo(String memberCode) {
        CuMemberExt cuMember = iCuMemberService.getMemberByCode(memberCode);
        if (null == cuMember) {
            return R.fail("用户名或密码错误");
        }
        LoginMember loginMember = new LoginMember();
        loginMember.setPkId(cuMember.getPkId());
        loginMember.setLoginType(ELoginType.MEMBER.getValue());
        loginMember.setCuMember(cuMember);
        loginMember.setSystemType(cuMember.getSystemType());
        return R.ok(loginMember);
    }

    @Override
    public R<CuMemberExt> getMemberById(Long memberId) {
        return R.ok(iCuMemberService.getMemberById(memberId));
    }

    @Override
    public R<CuMemberExt> getMemberByCode(String memberCode) {
        return R.ok(iCuMemberService.getMemberByCode(memberCode));
    }

    @Override
    public R<List<CuMemberExt>> getMemberInfo(CuMember cuMember) {
        return R.ok(iCuMemberService.getMemberInfoByCondition(cuMember));
    }

    @Override
    public R<Boolean> singleRevokeMemberByRegisterOrder(Long pkApprove, SaOrderExt saOrder, BusinessCommissionDTO businessCommissionDTO) {
        try {
            if (iCuMemberBusinessService.singleRevokeMemberByRegisterOrder(pkApprove, saOrder, businessCommissionDTO)) {
                return R.ok();
            }
            return R.fail("会员信息处理失败");
        } catch (Exception e) {
            log.error("singleRevokeMemberByRegisterOrder error msg: {}", e.getMessage(), e);
            return R.fail(e.getMessage());
        }
    }

    @Override
    public R<Boolean> singleRevokeMemberBySelfRevokeOrder(Long pkApprove, SaOrderExt saOrder, BusinessCommissionDTO businessCommissionDTO) {
        try {
            return R.ok(iCuMemberBusinessService.singleRevokeMemberBySelfRevokeOrder(pkApprove, saOrder, businessCommissionDTO));
        } catch (Exception e) {
            log.error("singleRevokeMemberBySelfRevokeOrder error msg: {}", e.getMessage(), e);
            return R.ok(false);
        }
    }

    @Override
    public R<Boolean> mergeRevokeMemberByRegisterOrder(Long pkApprove, List<Long> memberList, Map<Long, SaOrderExt> saOrderMap, BusinessCommissionDTO businessCommissionDTO) {
        try {
            if (iCuMemberBusinessService.mergeRevokeMemberByRegisterOrder(pkApprove, memberList, saOrderMap, businessCommissionDTO)) {
                return R.ok();
            }
            return R.fail("会员信息处理失败");
        } catch (Exception e) {
            log.error("mergeRevokeMemberByRegisterOrder error msg:{}", e.getMessage(), e);
            return R.fail(e.getMessage());
        }
    }

    @Override
    public R<Boolean> normalRevokeOrder(Long pkApprove, SaOrderExt saOrder, CuMember cuMember, BusinessCommissionDTO businessCommissionDTO) {
        try {
            return R.ok(iCuMemberBusinessService.normalRevokeOrder(pkApprove, saOrder, cuMember, businessCommissionDTO));
        } catch (Exception e) {
            log.error("normalRevokeOrder error msg: {}", e.getMessage(), e);
            return R.ok(false);
        }
    }

    @Override
    public R<Boolean> cancelMemberAccount(Long pkMember, SaOrder saOrder) {
        try {
            return R.ok(iCuMemberBusinessService.cancelMemberAccount(pkMember, saOrder));
        } catch (Exception e) {
            return R.fail();
        }
    }

    @Override
    public R<List<CuMemberExt>> querySponsorOnUmbrella(Long memberId, String memberCode, String phone) {
        return R.ok(iCuMemberService.queryPlaceSponsorOnUmbrella(memberId, memberCode, phone));
    }

    @Override
    public R<List<CuMemberExt>> querySponsorOnUmbrella(Long memberId, List<String> memberCodeList) {
        return R.ok(iCuMemberService.querySponsorOnUmbrella(memberId, memberCodeList));
    }

    @Override
    public R<List<CuMemberExt>> selectPlaceSponsorUnderUmbrellaByCondition(Long memberId, List<CuMemberExt> cuMemberExtList) {
        return R.ok(iCuMemberService.selectPlaceSponsorUnderUmbrellaByCondition(memberId, cuMemberExtList));
    }

    @Override
    public R<List<CuMemberExt>> queryPlaceSponsorUnderUmbrella(Long memberId, String... placeParent) {
        return R.ok(iCuMemberService.queryPlaceSponsorUnderUmbrella(memberId, placeParent));
    }

    @Override
    public R<CuMemberExt> queryPlaceSponsorLastLeft(Long memberId, Integer placeDept) {
        return R.ok(iCuMemberService.queryPlaceSponsorLastLeft(memberId, placeDept));
    }

    @Override
    public R<CuMember> getMember(String memberCode) {
        return R.ok(iCuMemberService.getMember(memberCode));
    }

    @Override
    public R<CuMember> getMember(Long pkMember) {
        return R.ok(iCuMemberService.getMember(pkMember));
    }

    @Override
    public R<CuMember> queryMember(String memberCode) {
        return R.ok(iCuMemberService.queryMember(memberCode));
    }

    @Override
    public R<CuMember> queryMember(Long pkMember) {
        return R.ok(iCuMemberService.queryMember(pkMember));
    }

    @Override
    public R<Boolean> saveMember(CuMember cuMember) {
        try {
            return R.ok(iCuMemberService.saveMember(cuMember));
        } catch (Exception e) {
            log.error("调用会员处理一失败", e);
            return R.fail(e.getMessage());
        }
    }

    @Override
    public R<Integer> queryCuMemberByPkMember(Long pkReference) {
        return R.ok(iCuMemberService.queryCuMemberByPkMember(pkReference));
    }

    @Override
    public R<Boolean> saveMember(SaOrderExt saOrder, CuMemberAccountExt cuMemberAccountExt) {
        try {
            return R.ok(iCuMemberBusinessService.saveMember(null, saOrder, cuMemberAccountExt));
        } catch (Exception e) {
            log.error("调用会员处理二失败", e);
            return R.ok(false);
        }
    }

    @Override
    public R<Boolean> saveMember(CuMember cuMember, SaOrderExt saOrder, CuMemberAccountExt cuMemberAccountExt) {
        try {
            return R.ok(iCuMemberBusinessService.saveMember(cuMember, saOrder, cuMemberAccountExt));
        } catch (Exception e) {
            log.error("saveMember 3 error!!!", e);
            return R.fail(e.getMessage());
        }
    }

    public R<Boolean> saveMember(CuMember cuMember, SaOrderExt saOrder, Boolean isPay, CuMemberAccountExt cuMemberAccountExt) {
        try {
            return R.ok(iCuMemberBusinessService.saveMember(cuMember, saOrder, isPay, cuMemberAccountExt));
        } catch (Exception e) {
            log.error("saveMember 4 error", e);
            return R.fail(e.getMessage());
        }
    }

    @Override
    public R<CuMember> createMemberIdAndCode(CuMember cuMember) {
        return R.ok(iCuMemberService.createMemberIdAndCode(cuMember));
    }

    @Override
    public R<CuMember> createRegisterMemberCode(CuMember cuMember) {
        return R.ok(iCuMemberService.createRegisterMemberCode(cuMember));
    }

    @Override
    public R<CuMember> createEmptyOrderMember(CuMemberExt member) {
        return R.ok(iCuMemberService.createEmptyOrderMember(member));
    }

    @Override
    public R<Boolean> checkLowerMember(Long pkMember) {
        return R.ok(iCuMemberService.checkLowerMember(pkMember));
    }

    @Override
    public R<Boolean> cancelMember(Long pkMember) {
        return R.ok(iCuMemberService.cancelMember(pkMember));
    }

    @Override
    public R<List<CuMemberExt>> findMemberByMemberId(List<Long> pkMemberList, Integer pkCountry) {
        return R.ok(iCuMemberService.findMemberByMemberId(pkMemberList, pkCountry));
    }

    @Override
    public R<CuMemberRegister> queryCuMemberRegister(Long pkMember) {
        return R.ok(iCuMemberRegisterService.queryCuMemberRegister(pkMember));
    }

    @Override
    public R<Boolean> updateCuMemberAccountByDraw(Long pkMember, Integer pkCountry, Integer accountValue, BigDecimal rewardMoney) {
        try {
            return R.ok(iCuMemberBusinessService.updateCuMemberAccountByDraw(pkMember, pkCountry, accountValue, rewardMoney));
        } catch (Exception e) {
            return R.fail();
        }
    }

    @Override
    public R<Boolean> updateCuMemberAccountByDraw(CuMemberAccountExt cuMemberAccountExt, BigDecimal payMoney) {
        try {
            return R.ok(iCuMemberBusinessService.updateCuMemberAccountByDraw(cuMemberAccountExt, payMoney));
        } catch (Exception e) {
            log.error("updateCuMemberAccountByDraw Exception: ", e);
            return R.fail();
        }
    }

    @Override
    public R<Boolean> updateMemberLevel(CuMember cuMember, SaOrder saOrder, Integer pkOldLevel) {
        return R.ok(iCuMemberBusinessService.updateMemberLevel(cuMember, saOrder, pkOldLevel));
    }

    @Override
    public R<Boolean> checkParent(String topCode, Long userId) {
        return R.ok(iCuMemberService.checkParent(topCode, userId) > 0);
    }

    @Override
    public Integer getAwardsTranslateValue(Integer pkCountry, Integer pkId) {
        return iCuMemberService.getAwardsTranslateValue(pkCountry, pkId);
    }

    @Override
    public R<List<CuMemberExt>> findAll() {
        return R.ok(iCuMemberService.findAll());
    }

    @Override
    public R<Map<Integer, Boolean>> checkIsShowWares(List<ShowWaresDTO> showWaresDTOS) {
        return R.ok(iCuMemberService.checkIsShowWares(showWaresDTOS));
    }

    @Override
    public R<Integer> clearMemberEmptyCode() {
        return R.ok(iCuMemberEmptyCodeService.clearMemberEmptyCode());
    }

    @Override
    public R<List<ZeroElementRevokeDTO>> zeroByBusinessList(AnalysisBusinessAppealParam analysisBusinessAppealParam) {
        return R.ok(iCuMemberService.zeroByBusinessList(analysisBusinessAppealParam));
    }

    @Override
    public R<CuMemberExt> getMenuMember(Long pkMember) {
        return R.ok(iCuMemberService.getMenuMember(pkMember));
    }

    @Override
    public R<CuMember> getParentMember(String memberCode) {
        return R.ok(iCuMemberService.getParentMember(memberCode));
    }

    @Override
    public R<Integer> countGradeMemberPoint(String memberName, String phone, Integer pkGrade, Integer pkCountry) {
        return R.ok(iCuMemberService.countGradeMemberPoint(memberName, phone, pkGrade, pkCountry));
    }

    @Override
    public R<CuMember> getRetailMember(Long pkMember, String memberCode) {
        if (null != pkMember) {
            return R.ok(iCuMemberService.getRetailMemberById(pkMember));
        } else if (StringUtils.isNotEmpty(memberCode)) {
            return R.ok(iCuMemberService.getRetailMemberByCode(memberCode));
        }
        log.error("查询新零售会员没有传入参数");
        return R.fail();
    }

    @Override
    public R<CuMember> getRetailTopMember(Long pkMember, String memberCode, Long topPkMember, String topMemberCode) {
        return R.ok(iCuMemberService.getRetailTopMember(pkMember, memberCode, topPkMember, topMemberCode));
    }

    @Override
    public R<CuMember> createRetailMemberIdAndCode(CuMember cuMember) {
        return R.ok(iCuMemberService.createRetailMemberIdAndCode(cuMember));
    }

    @Override
    public R<?> saveRetailMember(CuMember cuMember, SaOrderExt saOrder, CuMemberAccountExt cuMemberAccountExt) {
        try {
            iCuMemberBusinessService.saveRetailMember(cuMember, saOrder, cuMemberAccountExt);
            return R.ok();
        } catch (Exception e) {
            log.error("保存新零售注册会员异常! error:", e);
            return R.fail(e.getMessage());
        }
    }

    @Override
    public R<?> updateRetailMember(CuMember cuMember, SaOrderExt saOrder, CuMemberAccountExt cuMemberAccountExt) {
        try {
            iCuMemberBusinessService.updateRetailMember(cuMember, saOrder, cuMemberAccountExt);
            return R.ok();
        } catch (Exception e) {
            log.error("保存新零售升级会员异常! error:", e);
            return R.fail(e.getMessage());
        }
    }

    @Override
    public R<?> otherRetailMember(CuMember cuMember, SaOrderExt saOrder, CuMemberAccountExt cuMemberAccountExt, Boolean activateFlag) {
        try {
            iCuMemberBusinessService.otherRetailMember(cuMember, saOrder, cuMemberAccountExt, activateFlag);
            return R.ok();
        } catch (Exception e) {
            log.error("保存新零售会员其它订单异常! error:", e);
            return R.fail(e.getMessage());
        }
    }

    @Override
    public R<String> checkRegisterRelation(String phone, String parentCode) {
        return R.ok(iCuMemberService.checkRegisterRelation(phone, parentCode));
    }

}
