package com.hzs.member.account.provider;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.hzs.common.core.constant.MagicNumberConstants;
import com.hzs.common.core.domain.R;
import com.hzs.common.core.enums.EDelFlag;
import com.hzs.common.core.enums.ETradeStatus;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.domain.member.account.CuMemberTradeRecoil;
import com.hzs.member.account.ICuMemberTradeRecoilServiceApi;
import com.hzs.member.account.service.ICuMemberTradeRecoilService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

/**
 * 会员活动发放复消币明细dubbo服务
 */
@Slf4j
@DubboService
public class CuMemberTradeRecoilServiceProvider implements ICuMemberTradeRecoilServiceApi {

    @Autowired
    private ICuMemberTradeRecoilService iCuMemberTradeRecoilService;

    @Override
    public R<?> insertBatch(List<CuMemberTradeRecoil> recordList) {
        if (iCuMemberTradeRecoilService.saveBatch(recordList)) {
            return R.ok();
        }
        return R.fail();
    }

    @Override
    public R<?> deleteBatch(Long pkMember, String orderCode) {
        try {
            LambdaUpdateWrapper<CuMemberTradeRecoil> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(CuMemberTradeRecoil::getSourcePkMember, pkMember);
            if (StringUtils.isNotEmpty(orderCode)) {
                updateWrapper.eq(CuMemberTradeRecoil::getSourceCode, orderCode);
            }
            updateWrapper.eq(CuMemberTradeRecoil::getDelFlag, EDelFlag.UN_DELETE.getValue());
            updateWrapper.eq(CuMemberTradeRecoil::getTradeStatue, ETradeStatus.CONSUMABLE.getValue());
            updateWrapper.set(CuMemberTradeRecoil::getDelFlag, EDelFlag.DELETE.getValue());
            updateWrapper.set(CuMemberTradeRecoil::getModifiedTime, new Date());
            updateWrapper.set(CuMemberTradeRecoil::getPkModified, MagicNumberConstants.PK_ADMIN);
            iCuMemberTradeRecoilService.update(updateWrapper);
            return R.ok();
        } catch (Exception e) {
            return R.fail();
        }
    }

    @Override
    public R<?> deleteUpdateBatch(Long pkMember, Integer pkGrade) {
        try {
            iCuMemberTradeRecoilService.cancelOrderHandle(pkMember, pkGrade);
            return R.ok();
        } catch (Exception e) {
            return R.fail();
        }
    }

    @Override
    public R<List<CuMemberTradeRecoil>> queryCuMemberTradeRecoil(Date settleDate) {
        return R.ok(iCuMemberTradeRecoilService.queryCuMemberTradeRecoil(settleDate));
    }

    @Override
    public R<Boolean> updateCuMemberTradeRecoil(Date settleDate) {
        try {
            return R.ok(iCuMemberTradeRecoilService.updateCuMemberTradeRecoil(settleDate));
        } catch (Exception e) {
            return R.fail(Boolean.FALSE);
        }
    }
}
