package com.hzs.member.base.controller.manage;

import com.hzs.common.core.constant.TableNameConstants;
import com.hzs.common.core.domain.R;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.domain.member.achieve.CuMemberSettlePeriod;
import com.hzs.common.domain.member.base.CuMember;
import com.hzs.common.domain.member.ext.CuMemberSettleExt;
import com.hzs.common.domain.system.base.BdCountry;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.member.achieve.service.ICuMemberSettlePeriodService;
import com.hzs.member.achieve.service.ICuMemberTreeService;
import com.hzs.member.base.service.ICuMemberService;
import com.hzs.system.base.ICountryServiceApi;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @BelongsProject: hzs_cloud
 * @BelongsPackage: com.hzs.member.base.controller.manager
 * @Author: yh
 * @CreateTime: 2023-06-14  16:47
 * @Description:
 * @Version: 1.0
 */
@RestController
@RequestMapping("/manage/member-statistics")
public class MemeberStatisticsController extends BaseController {

    @Autowired
    private ICuMemberTreeService memberTreeService;
    @Autowired
    private ICuMemberService memberService;
    @Autowired
    private ICuMemberSettlePeriodService memberSettlePeriodService;

    @DubboReference
    ICountryServiceApi countryServiceApi;

    /**
     * 用户伞下安置新增业绩
     */
    @GetMapping("brolly-below-resettle")
    public AjaxResult brollyBelowResettle(String time, String memberCode) {
        Date date = new Date();
        if (time == null) {
            time = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, date);
        }
        Long pkMember = null;
        if (StringUtils.isBlank(memberCode)) {
            memberCode = SecurityUtils.getMemberCode();
        }
        if (StringUtils.isNotBlank(memberCode)) {
            CuMember member = memberService.getMember(memberCode);
            if (member == null) {
                return AjaxResult.error("会员编号不存在");
            }
            pkMember = member.getPkId();
        } else {
            R<Map<Integer, BdCountry>> allCountryMap = countryServiceApi.getAllCountryMap();
            for (Map.Entry<Integer, BdCountry> be : allCountryMap.getData().entrySet()) {
                if (be.getKey().equals(SecurityUtils.getPkCountry())) {
                    memberCode = be.getValue().getCode() + "30720201";
                    CuMember member = memberService.getMember(memberCode);
                    pkMember = member.getPkId();
                }
            }
        }
        Integer period = null;
        CuMemberSettlePeriod csPeriod = memberSettlePeriodService.getCuMemberSettlePeriodByDate(time);
        if (csPeriod != null) {
            period = csPeriod.getPkId();
        }
        // 查询 历史
        String settleTableName = TableNameConstants.CU_MEMBER_SETTLE + period;
        Integer tableNumber = memberTreeService.queryExistOracleTable(settleTableName);
        List<CuMemberSettleExt> memberSettleExtList = new ArrayList<>();
        if (tableNumber != null && tableNumber == 1) {
            // 伞下安置新增业绩
            memberSettleExtList = memberTreeService.brollyBelowResettle(settleTableName, pkMember);
        }
        return AjaxResult.success(memberSettleExtList);
    }
}
