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

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yonge.cooleshow.biz.dal.entity.PlatformCashAccountRecord;
import com.yonge.cooleshow.biz.dal.entity.TenantInfo;
import com.yonge.cooleshow.biz.dal.execl.entity.TenantAccountRecordImport;
import com.yonge.cooleshow.biz.dal.service.PlatformCashAccountRecordService;
import com.yonge.cooleshow.biz.dal.service.TenantInfoService;
import com.yonge.cooleshow.common.enums.PostStatusEnum;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.beans.BeanUtils;
import lombok.extern.slf4j.Slf4j;
import com.yonge.cooleshow.biz.dal.entity.TenantAccountRecord;
import com.yonge.cooleshow.biz.dal.wrapper.TenantAccountRecordWrapper;
import com.yonge.cooleshow.biz.dal.mapper.TenantAccountRecordMapper;
import com.yonge.cooleshow.biz.dal.service.TenantAccountRecordService;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 机构流水表
 * 2023-07-31 13:47:30
 */
@Slf4j
@Service
public class TenantAccountRecordServiceImpl extends ServiceImpl<TenantAccountRecordMapper, TenantAccountRecord> implements TenantAccountRecordService {

    @Autowired
    private TenantInfoService tenantInfoService;

    @Autowired
    private PlatformCashAccountRecordService platformCashAccountRecordService;

	/**
     * 查询详情
     * @param id 详情ID
     * @return TenantAccountRecord
     */
	@Override
    public TenantAccountRecord detail(Long id) {
        
        return baseMapper.selectById(id);
    }
    
    /**
     * 分页查询
     * @param page IPage<TenantAccountRecord>
     * @param query TenantAccountRecordWrapper.TenantAccountRecordQuery
     * @return IPage<TenantAccountRecord>
     */
    @Override
    public IPage<TenantAccountRecordWrapper.TenantAccountRecord> selectPage(IPage<TenantAccountRecordWrapper.TenantAccountRecord> page, TenantAccountRecordWrapper.TenantAccountRecordQuery query) {

        List<TenantAccountRecordWrapper.TenantAccountRecord> tenantAccountRecords =
                baseMapper.selectPage(page, query);

        if (CollectionUtils.isEmpty(tenantAccountRecords)) {
            return page;
        }

        // 设置机构名

        // 机构ID集合
        List<Long> tenantIds = tenantAccountRecords.stream().map(TenantAccountRecordWrapper.TenantAccountRecord::getTenantId).collect(Collectors.toList());

        // 机构信息
        Map<Long, TenantInfo> tenantInfoMap = tenantInfoService.getMapByIds(tenantIds);

        tenantAccountRecords.forEach(tenantAccountRecord -> {
            TenantInfo tenantInfo = tenantInfoMap.get(tenantAccountRecord.getTenantId());
            if (tenantInfo != null) {
                tenantAccountRecord.setTenantName(tenantInfo.getName());
            }
        });

        return page.setRecords(tenantAccountRecords);
    }
	
    /**
     * 添加
     * @param tenantAccountRecord TenantAccountRecordWrapper.TenantAccountRecord
     * @return Boolean
     */
    @Override
    public Boolean add(TenantAccountRecordWrapper.TenantAccountRecord tenantAccountRecord) {    	
        
        return this.save(JSON.parseObject(tenantAccountRecord.jsonString(), TenantAccountRecord.class));
    }

    /**
     * 更新
     * @param tenantAccountRecord TenantAccountRecordWrapper.TenantAccountRecord
     * @return Boolean
     */
    @Override
    public Boolean update(TenantAccountRecordWrapper.TenantAccountRecord tenantAccountRecord){

        return this.updateById(JSON.parseObject(tenantAccountRecord.jsonString(), TenantAccountRecord.class));       
    }

    @Override
    public TenantAccountRecordWrapper.TenantAccountRecordStat getStatistics(TenantAccountRecordWrapper.TenantAccountRecordQuery query) {
        return baseMapper.getStatistics(query);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updatePostStatus(List<TenantAccountRecordImport> importList) {
        if (CollectionUtils.isEmpty(importList)) {
            return;
        }
        // 过滤为空的
        List<TenantAccountRecordImport> collect = importList.stream().filter(o -> o.getPostStatus() != null).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(collect)) {
            return;
        }
        // 过滤出结算成功的
        List<String> successIds = collect.stream().filter(o -> PostStatusEnum.RECORDED.equals(o.getPostStatus())).map(TenantAccountRecordImport::getId).collect(Collectors.toList());
        List<String> waitIds = collect.stream().filter(o -> PostStatusEnum.WAIT.equals(o.getPostStatus())).map(TenantAccountRecordImport::getId).collect(Collectors.toList());

        if (CollectionUtils.isNotEmpty(successIds)) {
            List<TenantAccountRecord> list = this.lambdaQuery()
                    .in(TenantAccountRecord::getId, successIds)
                    .list();
            this.lambdaUpdate()
                    .in(TenantAccountRecord::getId, successIds)
                    .set(TenantAccountRecord::getPostStatus,PostStatusEnum.RECORDED)
                    .update();

            // 平台支出ID集合
            List<Long> platformCashAccountRecordIds = list.stream().map(o -> o.getPlatformCashAccountRecordId()).collect(Collectors.toList());

            platformCashAccountRecordService.lambdaUpdate()
                    .in(PlatformCashAccountRecord::getId,platformCashAccountRecordIds)
                    .set(PlatformCashAccountRecord::getPostStatus,PostStatusEnum.RECORDED)
                    .update();
        }
        if (CollectionUtils.isNotEmpty(waitIds)) {
            List<TenantAccountRecord> list = this.lambdaQuery()
                    .in(TenantAccountRecord::getId, waitIds)
                    .list();
            this.lambdaUpdate()
                    .in(TenantAccountRecord::getId, waitIds)
                    .set(TenantAccountRecord::getPostStatus,PostStatusEnum.WAIT)
                    .update();

            // 平台支出ID集合
            List<Long> platformCashAccountRecordIds = list.stream().map(o -> o.getPlatformCashAccountRecordId()).collect(Collectors.toList());

            platformCashAccountRecordService.lambdaUpdate()
                    .in(PlatformCashAccountRecord::getId,platformCashAccountRecordIds)
                    .set(PlatformCashAccountRecord::getPostStatus,PostStatusEnum.WAIT)
                    .update();
        }
    }
}