package com.yonge.cooleshow.biz.dal.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.yonge.cooleshow.biz.dal.service.UserAccountRecordService;
import com.yonge.toolset.base.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yonge.cooleshow.biz.dal.dao.PlatformCashAccountRecordDao;
import com.yonge.cooleshow.biz.dal.dto.search.PlatformCashAccountRecordSearch;
import com.yonge.cooleshow.biz.dal.entity.PlatformCashAccountRecord;
import com.yonge.cooleshow.biz.dal.enums.AccountBizTypeEnum;
import com.yonge.cooleshow.biz.dal.service.PlatformCashAccountRecordService;
import com.yonge.cooleshow.biz.dal.service.SysConfigService;
import com.yonge.cooleshow.biz.dal.vo.PlatformCashAccountRecordSummaryVo;
import org.springframework.util.CollectionUtils;

/**
 * 平台账户流水表(PlatformCashAccountRecord)表服务实现类
 *
 * @author zx
 * @since 2022-07-21 10:32:07
 */
@Service("platformCashAccountRecordService")
public class PlatformCashAccountRecordServiceImpl extends ServiceImpl<PlatformCashAccountRecordDao, PlatformCashAccountRecord> implements PlatformCashAccountRecordService {

    private final static Logger log = LoggerFactory.getLogger(PlatformCashAccountRecordServiceImpl.class);

    @Autowired
    private SysConfigService sysConfigService;
    @Autowired
    private UserAccountRecordService userAccountRecordService;

    @Override
    public PlatformCashAccountRecordDao getDao() {
        return this.baseMapper;
    }

    @Override
    public IPage<PlatformCashAccountRecord> queryPage(IPage<PlatformCashAccountRecord> page, PlatformCashAccountRecordSearch query) {
        return getDao().queryPage(page, query);
    }

    @Override
    public List<PlatformCashAccountRecord> queryAll(PlatformCashAccountRecordSearch query) {
        return getDao().queryAll(query);
    }

    @Override
    public PlatformCashAccountRecordSummaryVo querySummary(PlatformCashAccountRecordSearch query) {
        return getDao().querySummary(query);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateWaitRecord() {
        //平台账户修改
        List<String> paramNames = new ArrayList<>();
        paramNames.add(AccountBizTypeEnum.PRACTICE.getCode());
        paramNames.add(AccountBizTypeEnum.LIVE.getCode());
        paramNames.add(AccountBizTypeEnum.LIVE_SHARE.getCode());
        paramNames.add(AccountBizTypeEnum.VIDEO.getCode());
        paramNames.add(AccountBizTypeEnum.VIDEO_SHARE.getCode());
        paramNames.add(AccountBizTypeEnum.MUSIC.getCode());
        paramNames.add(AccountBizTypeEnum.MUSIC_SHARE.getCode());
        paramNames.add(AccountBizTypeEnum.ALBUM.getCode());
        paramNames.add(AccountBizTypeEnum.ALBUM_SHARE.getCode());
        paramNames.add(AccountBizTypeEnum.VIP.getCode());
        paramNames.add(AccountBizTypeEnum.VIP_SHARE.getCode());
        paramNames.add(AccountBizTypeEnum.ACTI_REGIST.getCode());
        paramNames.add(AccountBizTypeEnum.ACTI_REGIST_SHARE.getCode());

        List<PlatformCashAccountRecord> list = getDao().queryCanAccountByBizType(paramNames);
        List<Long> recordIds = list.stream().map(PlatformCashAccountRecord::getId).collect(Collectors.toList());
        if(!CollectionUtils.isEmpty(recordIds)){
            getDao().batchRecorded(recordIds);
        }
        //老师账户修改
        userAccountRecordService.updateWaitRecord();
        return true;
    }

    @Override
    public void cancelRecord(String orderNo, String bizType, Long bizId) {
        if(StringUtil.isEmpty(orderNo) || StringUtil.isEmpty(bizType) || null == bizId){
            return;
        }
        baseMapper.cancelRecord(orderNo, bizType, bizId);
        baseMapper.cancelRecord(orderNo, bizType + "_SHARE", bizId);
    }

    @Override
    public void mallRecordedRecord(Long productSkuId, String orderSn, String goodType) {

        baseMapper.mallRecordedRecord(orderSn, goodType, productSkuId);
    }
}

