package com.hzs.member.account.provider;

import com.hzs.common.core.domain.R;
import com.hzs.member.account.IMemberJobServiceApi;
import com.hzs.member.account.service.ICuMemberAccountDetailService;
import com.hzs.member.account.service.ICuMemberAccountService;
import com.hzs.member.base.service.ICuMemberRegisterService;
import com.hzs.member.base.service.ICuMemberService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * 会员模块定时任务服务提供
 */
@Slf4j
@DubboService
public class IMemberJobServiceProvider implements IMemberJobServiceApi {

    @Autowired
    private ICuMemberAccountDetailService iCuMemberAccountDetailService;
    @Autowired
    private ICuMemberService iCuMemberService;
    @Autowired
    private ICuMemberAccountService iCuMemberAccountService;
    @Autowired
    private ICuMemberRegisterService iCuMemberRegisterService;

    @Override
    public R<?> insertAccountDetail() {
        try {
            iCuMemberAccountDetailService.insertAccountDetail();
        } catch (Exception e) {
            log.info("定时插入账户详情数据失败：", e);
            return R.fail(e.getMessage());
        }
        return R.ok();
    }

    @Override
    public R<?> deleteAccountDetailByMonth() {
        try {
            iCuMemberAccountDetailService.deleteAccountDetailByMonth();
        } catch (Exception e) {
            log.info("定时删除账户详情数据失败：", e);
            return R.fail(e.getMessage());
        }
        return R.ok();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public R<?> clearZeroMember() {
        try {
            // 删除会员注册信息
            iCuMemberRegisterService.clearZeroMember();
            // 删除会员账户
            iCuMemberAccountService.clearZeroMember();
            // 账户会员信息
            iCuMemberService.clearZeroMember();
        } catch (Exception e) {
            log.info("清除7天前0元会员失败：", e);
            return R.fail(e.getMessage());
        }
        return R.ok();
    }

}
