package com.hzs.third.job;

import cn.hutool.core.collection.CollectionUtil;
import com.hzs.common.core.domain.R;
import com.hzs.common.domain.member.ext.CuMemberExt;
import com.hzs.member.account.IMemberJobServiceApi;
import com.hzs.member.base.IMemberServiceApi;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 会员模块定时任务
 */
@ConditionalOnProperty(name = "xxl-job.start", havingValue = "true")
@Component
@Slf4j
public class CuMemberJob {

    @DubboReference
    IMemberJobServiceApi iMemberJobServiceApi;
    @DubboReference
    IMemberServiceApi iMemberServiceApi;

    /**
     * 定时插入账户详情数据
     */
    @XxlJob("insertAccountDetail")
    public void insertAccountDetail() {
        iMemberJobServiceApi.insertAccountDetail();
    }

    /**
     * 每月月初定时删除上上个月的数据
     */
    @XxlJob("deleteAccountDetailByMonth")
    public void deleteAccountDetailByMonth() {
        iMemberJobServiceApi.deleteAccountDetailByMonth();
    }

    /**
     * 自动提现
     */
    @XxlJob("automaticWithdrawal")
    public void automaticWithdrawal() {
        iMemberJobServiceApi.automaticWithdrawal();
    }

    @XxlJob("sendPlaceErrorMsg")
    public void sendPlaceErrorMsg() {
        List<CuMemberExt> cuMemberList = iMemberServiceApi.queryPlaceErrorCode().getData();
        if (CollectionUtil.isNotEmpty(cuMemberList)) {
            StringBuilder sb = new StringBuilder();
            for (CuMemberExt cuMemberExt : cuMemberList) {
                Long pkPlaceParent = cuMemberExt.getPkPlaceParent();
                Integer placeDept = cuMemberExt.getPlaceDept();
                sb.append("会员ID: ").append(pkPlaceParent).append(" ,部门: ").append(placeDept).append(" ,");
            }
            sb.append("结算错误，安置有问题");
        }
    }

    /**
     * 提前四个月自动服务协议有效期续期一年
     */
    @XxlJob("updateMemberWhiteExpireDate")
    public void updateMemberExpireDate() {
        R<?> result = iMemberServiceApi.updateMemberExpireDate();
        if (!result.isSuccess()) {
            log.error("提前四个月自动服务协议有效期续期一年，{}", result.getMsg());
        }
    }

}
