package com.bonus.canteen.core.account.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.bonus.canteen.core.account.constants.AccTradeTypeEnum;
import com.bonus.canteen.core.account.domain.AccTrade;
import com.bonus.canteen.core.account.domain.bo.TradeAndWallerInfo;
import com.bonus.canteen.core.account.domain.param.AccConsumeDetailQueryParam;
import com.bonus.canteen.core.account.domain.param.AccWalletOperationQueryParam;
import com.bonus.canteen.core.account.domain.vo.AccConsumeDetailVO;
import com.bonus.canteen.core.account.domain.vo.AccOperationListVO;
import com.bonus.canteen.core.account.domain.vo.AccTradeVo;
import com.bonus.canteen.core.account.mapper.AccTradeMapper;
import com.bonus.canteen.core.account.service.IAccTradeService;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.core.utils.DateUtils;
import com.bonus.common.houqin.utils.SM4EncryptUtils;
import com.bonus.common.houqin.utils.id.Id;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 账户交易记录Service业务层处理
 * 
 * @author xsheng
 * @date 2025-04-05
 */
@Service
public class AccTradeServiceImpl implements IAccTradeService {
    @Autowired
    private AccTradeMapper accTradeMapper;

    /**
     * 查询账户交易记录
     * 
     * @param tradeId 账户交易记录主键
     * @return 账户交易记录
     */
    @Override
    public AccTrade selectAccTradeByTradeId(Long tradeId) {
        return accTradeMapper.selectAccTradeByTradeId(tradeId);
    }

    @Override
    public List<AccTrade> selectAccTradeByOrderNo(Long orderNo) {
        return accTradeMapper.selectAccTradeByOrderNo(orderNo);
    }

    /**
     * 查询账户交易记录列表
     * 
     * @param accTrade 账户交易记录
     * @return 账户交易记录
     */
    @Override
    public List<AccTrade> selectAccTradeList(AccTrade accTrade) {
        return accTradeMapper.selectAccTradeList(accTrade);
    }

    /**
     * 新增账户交易记录
     * 
     * @param accTrade 账户交易记录
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
    public int insertAccTrade(AccTrade accTrade) {
        accTrade.setCreateTime(DateUtils.getNowDate());
        accTrade.setUpdateTime(DateUtils.getNowDate());
        return accTradeMapper.insertAccTrade(accTrade);
    }

    @Override
    public int insertAccTradeVo(AccTradeVo accTradeVo) {
        accTradeVo.setCreateTime(DateUtils.getNowDate());
        try {
            AccTrade accTrade = new AccTrade();
            BeanUtils.copyProperties(accTradeVo, accTrade);
            accTrade.setTradeId(Id.next());
            return accTradeMapper.insertAccTrade(accTrade);
        } catch (Exception e) {
            throw new ServiceException("错误信息描述" + e.getMessage());
        }
    }

    /**
     * 修改账户交易记录
     * 
     * @param accTrade 账户交易记录
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
    public int updateAccTrade(AccTrade accTrade) {
        accTrade.setUpdateTime(DateUtils.getNowDate());
        try {
            return accTradeMapper.updateAccTrade(accTrade);
        } catch (Exception e) {
            throw new ServiceException("错误信息描述" + e.getMessage());
        }
    }

    @Override
    public int updateAccTradeVoByOrderNo(AccTradeVo accTradeVo) {
        accTradeVo.setUpdateTime(DateUtils.getNowDate());
        try {
            return accTradeMapper.updateAccTradeVoByOrderNo(accTradeVo);
        } catch (Exception e) {
            throw new ServiceException("错误信息描述" + e.getMessage());
        }
    }

    /**
     * 批量删除账户交易记录
     * 
     * @param tradeIds 需要删除的账户交易记录主键
     * @return 结果
     */
    @Override
    public int deleteAccTradeByTradeIds(Long[] tradeIds) {
        return accTradeMapper.deleteAccTradeByTradeIds(tradeIds);
    }

    /**
     * 删除账户交易记录信息
     * 
     * @param tradeId 账户交易记录主键
     * @return 结果
     */
    @Override
    public int deleteAccTradeByTradeId(Long tradeId) {
        return accTradeMapper.deleteAccTradeByTradeId(tradeId);
    }

    @Override
    public List<AccOperationListVO> queryAccOperationList(AccWalletOperationQueryParam param) {
        String encryptedSearchValue = SM4EncryptUtils.sm4Encrypt(param.getSearchValue());
        List<AccOperationListVO> list = accTradeMapper.queryAccOperationList(param, encryptedSearchValue);
        if(CollUtil.isNotEmpty(list)) {
            for(AccOperationListVO vo : list) {
                vo.setPhoneNumber(SM4EncryptUtils.sm4Decrypt(vo.getPhoneNumber()));
            }
        }
        return list;
    }

    @Override
    public List<TradeAndWallerInfo> queryTradeAndWallerInfoByOrderNo(String orderNo, AccTradeTypeEnum accTradeTypeEnum) {
        return accTradeMapper.queryTradeAndWallerInfoByOrderNo(orderNo, accTradeTypeEnum.getKey());
    }

    @Override
    public List<AccConsumeDetailVO> queryAccConsumeDetail(AccConsumeDetailQueryParam param) {
        String encryptedSearchValue = SM4EncryptUtils.sm4Encrypt(param.getSearchValue());
        return accTradeMapper.queryAccConsumeDetail(param, encryptedSearchValue);
    }
}
