package com.ruoyi.cai.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.cai.domain.AccountChangeLog;
import com.ruoyi.cai.domain.Gift;
import com.ruoyi.cai.domain.User;
import com.ruoyi.cai.enums.account.AccountCateEnum;
import com.ruoyi.cai.enums.account.AccountChangeCodeEnum;
import com.ruoyi.cai.mapper.AccountChangeLogMapper;
import com.ruoyi.cai.service.AccountChangeLogService;
import com.ruoyi.cai.service.UserService;
import com.ruoyi.common.core.domain.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 账户明细Service业务层处理
 *
 * @author 77
 * @date 2024-01-05
 */
@Service
public class AccountChangeLogServiceImpl extends ServiceImpl<AccountChangeLogMapper,AccountChangeLog> implements AccountChangeLogService {

    @Override
    public AccountChangeLog getLogNoAdmin(Long userId, String usercode, AccountChangeCodeEnum change, Long price, Long traceId){
        return this.getAccountChangeLog(userId, usercode, change, price, traceId, 0);
    }

    @Override
    public AccountChangeLog saveLogNoAdmin(Long userId, String usercode, AccountChangeCodeEnum change, Long price, Long traceId){
        AccountChangeLog changeLog = this.getAccountChangeLog(userId, usercode, change, price, traceId, 0);
        this.save(changeLog);
        return changeLog;
    }

    @Override
    public AccountChangeLog saveLogNoAdmin(Long userId, String usercode, AccountChangeCodeEnum change, Long price, Long traceId
            ,Long tarUserId){
        AccountChangeLog changeLog = this.getAccountChangeLog(userId, usercode, change, price, traceId, 0);
        changeLog.setTarUserId(tarUserId);
        this.save(changeLog);
        return changeLog;
    }

    @Override
    public AccountChangeLog saveLogNoAdmin(Long userId, String usercode, AccountChangeCodeEnum change, Long price, Long traceId
            , Long tarUserId, Gift gift){
        AccountChangeLog changeLog = this.getAccountChangeLog(userId, usercode, change, price, traceId, 0);
        changeLog.setTarUserId(tarUserId);
        if(gift != null){
            changeLog.setTarParam(gift.getId()+"");
            changeLog.setTarImg(gift.getImg());
        }
        this.save(changeLog);
        return changeLog;
    }

    private AccountChangeLog getAccountChangeLog(Long userId, String usercode, AccountChangeCodeEnum change, Long price, Long traceId, Integer admin){
        AccountChangeLog log = new AccountChangeLog();
        log.init(change);
        log.setUserId(userId);
        log.setUsercode(usercode);
        log.setTraceId(traceId);
        log.setChangeValue(price);
        log.setIsAdmin(admin);
        return log;
    }

    @Override
    public AccountChangeLog saveLogAdmin(Long userId, String usercode, AccountChangeCodeEnum change, Long price,Long traceId) {
        AccountChangeLog changeLog = this.getAccountChangeLog(userId, usercode, change, price, traceId, 1);
        this.save(changeLog);
        return changeLog;
    }

    @Autowired
    private UserService userService;

    @Override
    public Page<AccountChangeLog> pageAdmin(PageQuery pageQuery, AccountChangeLog bo) {
        Page<AccountChangeLog> page = this.page(pageQuery.build(), Wrappers.lambdaQuery(bo).orderByDesc(AccountChangeLog::getCreateTime));
        List<AccountChangeLog> records = page.getRecords();
        Set<Long> userIds = records.stream().map(AccountChangeLog::getTarUserId).collect(Collectors.toSet());
        if(records.isEmpty()){
            return page;
        }
        List<User> users = userService.listByIds(userIds);
        Map<Long, User> userMap = users.stream().collect(Collectors.toMap(User::getId, Function.identity()));
        for (AccountChangeLog record : records) {
            User user = userMap.get(record.getTarUserId());
            if(user != null){
                AccountChangeCodeEnum changeEnum = AccountChangeCodeEnum.getByCode(record.getChangeType());
                if(changeEnum != null){
                    Boolean in = changeEnum.getIn();
                    if(in == null){
                        record.setShowMessage(record.getCateAdminName());
                    }else{
                        String format = String.format("%s%s%s",
                                record.getCateAdminName(),
                                in?"来自":"给",
                                user.getNickname());
                        record.setShowMessage(format);
                    }
                }else{
                    record.setShowMessage(record.getCateAdminName());
                }
            }else{
                record.setShowMessage(record.getCateAdminName());
            }
            if(AccountCateEnum.IM.getCode().equals(record.getCateId())
                || AccountCateEnum.VIDEO.getCode().equals(record.getCateId())
                || AccountCateEnum.GUARD.getCode().equals(record.getCateId())
                || AccountCateEnum.SYSTEM.getCode().equals(record.getCateId())
                || AccountCateEnum.UNION.getCode().equals(record.getCateId())){
                if(record.getTarUserId() != null){
                    if(user != null){
                        record.setTarImg(user.getAvatar());
                    }
                }
            }
        }
        return page;
    }


}
