package beilv.usermembershipcard.service.impl;

import beilv.cardpurchaserecord.domain.CardPurchaseRecord;
import beilv.cardpurchaserecord.mapper.CardPurchaseRecordMapper;
import beilv.common.core.text.Convert;
import beilv.common.utils.DateUtils;
import beilv.common.utils.DictUtils;
import beilv.common.utils.ShiroUtils;
import beilv.common.utils.StringUtils;
import beilv.system.service.ISysDictDataService;
import beilv.usermembershipcard.domain.MemberStream;
import beilv.usermembershipcard.domain.UserMembershipCard;
import beilv.usermembershipcard.mapper.UserMembershipCardMapper;
import beilv.usermembershipcard.service.IUserMembershipCardService;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.Map;

/**
 * 用户会员卡Service业务层处理
 *
 * @author ruoyi
 * @date 2025-01-02
 */
@Service
public class UserMembershipCardServiceImpl implements IUserMembershipCardService {
    @Autowired
    private UserMembershipCardMapper userMembershipCardMapper;

    @Autowired
    private CardPurchaseRecordMapper cardPurchaseRecordMapper;

    @Autowired
    private ISysDictDataService dictDataService;

    /**
     * 查询用户会员卡
     *
     * @param id 用户会员卡主键
     * @return 用户会员卡
     */
    @Override
    public UserMembershipCard selectUserMembershipCardById(Long id) {
        return userMembershipCardMapper.selectUserMembershipCardById(id);
    }

    /**
     * 查询用户会员卡列表
     *
     * @param userMembershipCard 用户会员卡
     * @return 用户会员卡
     */
    @Override
    public List<UserMembershipCard> selectUserMembershipCardList(UserMembershipCard userMembershipCard) {
        List<UserMembershipCard> userMembershipCards = userMembershipCardMapper.selectUserMembershipCardList(userMembershipCard);
        Map<String, String> cardStatus = DictUtils.getDictCacheToMap("card_status");
        Map<String, String> siteType = DictUtils.getDictCacheToMap("site_type");

        for (UserMembershipCard userMembershipCard1 : userMembershipCards) {
            userMembershipCard1.setStateLabel(MapUtils.getString(cardStatus, userMembershipCard1.getState()));
            userMembershipCard1.setCardTypeLable(MapUtils.getString(siteType, userMembershipCard1.getCardType()));
        }
        return userMembershipCards;
    }

    /**
     * 新增用户会员卡
     *
     * @param userMembershipCard 用户会员卡
     * @return 结果
     */
    @Override
    public int insertUserMembershipCard(UserMembershipCard userMembershipCard) {
        userMembershipCard.setCreateTime(DateUtils.getNowDate());
        return userMembershipCardMapper.insertUserMembershipCard(userMembershipCard);
    }

    /**
     * 修改用户会员卡
     *
     * @param userMembershipCard 用户会员卡
     * @return 结果
     */
    @Override
    public int updateUserMembershipCard(UserMembershipCard userMembershipCard) {
        userMembershipCard.setUpdateTime(DateUtils.getNowDate());
        return userMembershipCardMapper.updateUserMembershipCard(userMembershipCard);
    }

    /**
     * 批量删除用户会员卡
     *
     * @param ids 需要删除的用户会员卡主键
     * @return 结果
     */
    @Override
    public int deleteUserMembershipCardByIds(String ids) {
        return userMembershipCardMapper.deleteUserMembershipCardByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除用户会员卡信息
     *
     * @param id 用户会员卡主键
     * @return 结果
     */
    @Override
    public int deleteUserMembershipCardById(Long id) {
        return userMembershipCardMapper.deleteUserMembershipCardById(id);
    }

    /*
    * 查询流水列表
    *
    * @author 韩福成
    * @date 2025/1/8 上午9:29
    */
    @Override
    public List<Map<String,Object>> getStream(Long id) {
        return userMembershipCardMapper.getStream(id);
    }

    /*
    * 新增会员卡流水
    *
    * @author 韩福成
    * @date 2025/1/8 上午9:29
    */
    @Override
    @Transactional
    public int addStream(MemberStream memberStream) {
        //
        UserMembershipCard userMembershipCard1 = userMembershipCardMapper.selectUserMembershipCardById(Long.valueOf(memberStream.getUserCardId()));
        if(userMembershipCard1.getRemainingNumber() == 0){
            return -1;
        }
        //增加/扣除会员卡剩余次数（约场-1；退场+1）
        UserMembershipCard userMembershipCard = new UserMembershipCard();
        userMembershipCard.setId(Long.valueOf(memberStream.getUserCardId()));
        userMembershipCard.setRemainingNumber(Integer.valueOf(memberStream.getType()));
        userMembershipCardMapper.updateRemainingNumber(userMembershipCard);
        //增加流水
        memberStream.setCreateTime(DateUtils.getNowDate());
        return userMembershipCardMapper.addStream(memberStream);
    }

    /*
     * 新增退卡记录
     *
     * @author 韩福成
     * @date 2025/1/7 上午10:50
     */
    @Override
    @Transactional
    public int  refundCard(UserMembershipCard userMembershipCard) {
        CardPurchaseRecord cardPurchaseRecord = cardPurchaseRecordMapper.selectCardPurchaseRecordById(Long.parseLong(userMembershipCard.getRecordId()));
        //使用次数
        int useCount = cardPurchaseRecord.getTotalNumber() - userMembershipCard.getRemainingNumber();
        //原单价
        BigDecimal originalPrice = cardPurchaseRecord.getOriginalPrice().divide(BigDecimal.valueOf(cardPurchaseRecord.getTotalNumber()),2, RoundingMode.HALF_UP);
        //使用总价
        BigDecimal usePrice = originalPrice.multiply(new BigDecimal(useCount));
        if (usePrice.compareTo(cardPurchaseRecord.getMemberPrice())<0){
            //退款价格
            BigDecimal refundPrice = cardPurchaseRecord.getMemberPrice().subtract(usePrice);
            /*调用退款接口*/



        }else {
            return -1;
        }
        /*新增退卡记录*/
        CardPurchaseRecord record = new CardPurchaseRecord();
        BeanUtils.copyProperties(cardPurchaseRecord,record);
        record.setId(null);
        if(StringUtils.isEmpty(userMembershipCard.getUserId())){
            record.setCreateBy(ShiroUtils.getUserId().toString());
        }else {
            record.setCreateBy(userMembershipCard.getUserId());
        }
        record.setCreateTime(DateUtils.getNowDate());
        record.setType("payment_status_refunded");
        record.setTime(DateUtils.getNowDate());
        record.setVersion("0");
        cardPurchaseRecordMapper.insertCardPurchaseRecord(record);
        /*修改用户会员卡*/
        userMembershipCard.setState("2");
        userMembershipCard.setRemainingNumber(0);
        return userMembershipCardMapper.updateUserMembershipCard(userMembershipCard);
    }

    @Override
    public UserMembershipCard selectUserMembershipCardRecordId(String recordId) {
        return userMembershipCardMapper.selectUserMembershipCardRecordId(recordId);
    }
}
