package com.hzs.member.base.provider;

import cn.hutool.core.collection.CollectionUtil;
import com.hzs.common.core.domain.R;
import com.hzs.common.core.enums.EPlaceDept;
import com.hzs.common.core.enums.EUpgradeType;
import com.hzs.common.domain.member.base.CuMember;
import com.hzs.common.domain.member.base.CuMemberRegister;
import com.hzs.common.domain.member.base.CuMemberUpgrade;
import com.hzs.common.domain.member.ext.CuMemberAccountExt;
import com.hzs.common.domain.member.ext.CuMemberExt;
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.NotActivePointDTO;
import com.hzs.member.base.dto.ShowWaresDTO;
import com.hzs.member.base.service.*;
import com.hzs.member.base.IMemberServiceApi;
import com.hzs.member.handlebusiness.dto.AnalysisBusinessAppealParam;
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.ArrayList;
import java.util.Date;
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 ICuMemberUpgradeService iCuMemberUpgradeService;

    @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.setCuMember(cuMember);
        return R.ok(loginMember);
    }

    @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> 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, List<Long> pkOrderItemList, BusinessCommissionDTO businessCommissionDTO) {
        try {
            return R.ok(iCuMemberBusinessService.normalRevokeOrder(pkApprove, saOrder, cuMember, pkOrderItemList, businessCommissionDTO));
        } catch (Exception e) {
            log.error("normalRevokeOrder error msg: {}", e.getMessage(), e);
            return R.ok(false);
        }
    }

    @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>> 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.getMemberByCode(memberCode));
    }

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

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

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

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

    @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, SaOrder saOrder) {
        try {
            return R.ok(iCuMemberBusinessService.saveMember(cuMember, saOrder));
        } 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("支付订单更新会员失败", e);
            return R.ok(false);
        }
    }

    @Override
    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("注册保存会员失败", e);
            return R.ok(false);
        }
    }

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

    @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) {
            return R.fail();
        }
    }

    @Override
    public R<Boolean> getMemberPlaceParent(Integer pkPlaceParent, String memberCode) {
        return R.ok(iCuMemberService.getMemberPlaceParent(pkPlaceParent, memberCode));
    }

    @Override
    public R<CuMemberUpgrade> queryCuMemberUpgradeByCondition(Integer pkCountry, Long pkMember) {
        return R.ok(iCuMemberUpgradeService.queryCuMemberUpgradeByCondition(pkCountry, pkMember));
    }

    @Override
    public R<Boolean> updateMemberLevel(CuMember cuMember, Integer pkOldLevel, EUpgradeType eUpgradeType, String remark) {
        return R.ok(iCuMemberBusinessService.updateMemberLevel(cuMember, pkOldLevel, eUpgradeType, remark));
    }

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

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

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

    @Override
    public R<List<Integer>> queryMemberPlace(Long pkMember) {
        // 查询会员安置部门
        List<Integer> placeDeptList = iCuMemberService.queryMemberPlaceDept(pkMember);
        if (CollectionUtil.isEmpty(placeDeptList)) {
            // 如果为空，则没有安置过人，默认只能为1部门
            placeDeptList.add(EPlaceDept.LEFT_DEPT.getValue());
        } else {
            // 如果不为空，则需要进行补以及追加1个部门
            // 最后一个部门
            int lastDept = placeDeptList.get(placeDeptList.size() - 1) + 1;
            placeDeptList = new ArrayList<>();
            for (int i = 1; i <= lastDept; i++) {
                placeDeptList.add(i);
            }
        }
        return R.ok(placeDeptList);
    }

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

    @Override
    public R<List<CuMemberExt>> getMemberAll(List<Integer> gradeId, Integer pkCountry, Date startTime, Date endTime) {
        return R.ok(iCuMemberService.getMemberAll(gradeId, pkCountry, startTime, endTime));
    }

}
