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.AccountTrade;
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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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;

/**
 * 账户交易记录Service业务层处理
 * 
 * @author xsheng
 * @date 2025-04-05
 */
@Service
public class AccTradeServiceImpl implements IAccTradeService {
    private static final Logger log = LoggerFactory.getLogger(AccTradeServiceImpl.class);

    @Autowired
    private AccTradeMapper accTradeMapper;

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

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

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

    /**
     * 新增账户交易记录
     * 
     * @param accountTrade 账户交易记录
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
    public int insertAccTrade(AccountTrade accountTrade) {
        try {
            accountTrade.setCreateTime(DateUtils.getNowDate());
            accountTrade.setUpdateTime(DateUtils.getNowDate());
            return accTradeMapper.insertAccTrade(accountTrade);
        } catch (Exception e) {
            log.error("insertAccTrade 异常：", e);
            throw new ServiceException("系统异常");
        }
    }

    @Override
    public int insertAccTradeVo(AccTradeVo accTradeVo) {
        accTradeVo.setCreateTime(DateUtils.getNowDate());
        try {
            AccountTrade accountTrade = new AccountTrade();
            BeanUtils.copyProperties(accTradeVo, accountTrade);
            return accTradeMapper.insertAccTrade(accountTrade);
        } catch (Exception e) {
            log.error("insertAccTradeVo 异常：", e);
            throw new ServiceException("系统异常");
        }
    }

    /**
     * 修改账户交易记录
     * 
     * @param accountTrade 账户交易记录
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
    public int updateAccTrade(AccountTrade accountTrade) {
        accountTrade.setUpdateTime(DateUtils.getNowDate());
        try {
            return accTradeMapper.updateAccTrade(accountTrade);
        } catch (Exception e) {
            log.error("updateAccTrade 异常：", e);
            throw new ServiceException("系统异常");
        }
    }

    @Override
    public int updateAccTradeVoByOrderNo(AccTradeVo accTradeVo) {
        accTradeVo.setUpdateTime(DateUtils.getNowDate());
        try {
            return accTradeMapper.updateAccTradeVoByOrderNo(accTradeVo);
        } catch (Exception e) {
            log.error("updateAccTradeVoByOrderNo 异常：", e);
            throw new ServiceException("系统异常");
        }
    }

    /**
     * 批量删除账户交易记录
     * 
     * @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);
    }
}
