package beilv.cardpurchaserecord.service.impl;

import beilv.cardpurchaserecord.domain.CardPurchaseRecord;
import beilv.cardpurchaserecord.mapper.CardPurchaseRecordMapper;
import beilv.cardpurchaserecord.service.ICardPurchaseRecordService;
import beilv.common.core.text.Convert;
import beilv.common.utils.DateUtils;
import beilv.common.utils.ShiroUtils;
import beilv.common.utils.StringUtils;
import beilv.system.service.ISysDictDataService;
import beilv.usermembershipcard.domain.UserMembershipCard;
import beilv.usermembershipcard.mapper.UserMembershipCardMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

/**
 * 购卡记录Service业务层处理
 *
 * @author ruoyi
 * @date 2025-01-02
 */
@Service
public class CardPurchaseRecordServiceImpl implements ICardPurchaseRecordService {
    @Autowired
    private CardPurchaseRecordMapper cardPurchaseRecordMapper;

    @Autowired
    private ISysDictDataService dictDataService;

    @Autowired
    private UserMembershipCardMapper userMembershipCardMapper;
    /**
     * 查询购卡记录
     *
     * @param id 购卡记录主键
     * @return 购卡记录
     */
    @Override
    public CardPurchaseRecord selectCardPurchaseRecordById(Long id) {
        return cardPurchaseRecordMapper.selectCardPurchaseRecordById(id);
    }

    /**
     * 查询购卡记录列表
     *
     * @param cardPurchaseRecord 购卡记录
     * @return 购卡记录
     */
    @Override
    public List<CardPurchaseRecord> selectCardPurchaseRecordList(CardPurchaseRecord cardPurchaseRecord) {
        List<CardPurchaseRecord> cardPurchaseRecords = cardPurchaseRecordMapper.selectCardPurchaseRecordList(cardPurchaseRecord);
        for (CardPurchaseRecord cardPurchaseRecord2 : cardPurchaseRecords) {
            cardPurchaseRecord2.setTypeLabel(dictDataService.selectDictLabel("payment_status",cardPurchaseRecord2.getType()));
            cardPurchaseRecord2.setCardTypeLabel(dictDataService.selectDictLabel("site_type",cardPurchaseRecord2.getCardType()));
        }
        return cardPurchaseRecords;
    }

    /*
    * 购卡生成订单
    *
    * @author 韩福成
    * @date 2025/1/16 下午2:20
    */
    @Override
    public String insertCardPurchaseRecord(CardPurchaseRecord cardPurchaseRecord) {
        //新增购卡记录
        if (StringUtils.isEmpty(cardPurchaseRecord.getUserId())) {
            cardPurchaseRecord.setCreateBy(ShiroUtils.getUserId().toString());
        }else {
            cardPurchaseRecord.setCreateBy(cardPurchaseRecord.getUserId());
        }
        cardPurchaseRecord.setCreateTime(DateUtils.getNowDate());
        cardPurchaseRecord.setType("payment_status_to_be_paid");
        cardPurchaseRecord.setTime(DateUtils.getNowDate());
        cardPurchaseRecord.setVersion("0");
        cardPurchaseRecord.setOrderId(UUID.randomUUID().toString());

        if("会员卡".equals(cardPurchaseRecord.getPaymentWay())){
            //扣除会员卡金额

            //写入支付记录
        }

        cardPurchaseRecordMapper.insertCardPurchaseRecord(cardPurchaseRecord);
        return cardPurchaseRecord.getOrderId();
    }

    /**
     * 购卡回调
     *
     * @param cardPurchaseRecord 购卡记录
     * @return 结果
     */
    @Override
    @Transactional
    public int addCardCallback(CardPurchaseRecord cardPurchaseRecord) {
        CardPurchaseRecord record = new CardPurchaseRecord();
        record.setOrderId(cardPurchaseRecord.getOrderId());
        CardPurchaseRecord cardPurchaseRecords = cardPurchaseRecordMapper.selectCardPurchaseRecordList(record).get(0);
        //已支付状态新增会员管理
        if (cardPurchaseRecord.getType().equals("payment_status_have_paid")){
            UserMembershipCard userMembershipCard = new UserMembershipCard();
            userMembershipCard.setUserId(cardPurchaseRecords.getUserId());
            userMembershipCard.setRecordId(cardPurchaseRecords.getId().toString());
            userMembershipCard.setRemainingNumber(cardPurchaseRecords.getTotalNumber());
            if (StringUtils.isEmpty(cardPurchaseRecord.getUserId())) {
                userMembershipCard.setCreateBy(ShiroUtils.getUserId().toString());
            }else {
                userMembershipCard.setCreateBy(cardPurchaseRecords.getCreateBy());
            }
            userMembershipCard.setCreateTime(DateUtils.getNowDate());
            userMembershipCard.setState("3");
            userMembershipCard.setVersion("0");
            userMembershipCardMapper.insertUserMembershipCard(userMembershipCard);
        }
        //修改购卡记录状态
        if (StringUtils.isEmpty(cardPurchaseRecord.getUserId())) {
            cardPurchaseRecord.setUpdateBy(ShiroUtils.getUserId().toString());
        }else {
            cardPurchaseRecord.setUpdateBy(cardPurchaseRecord.getUserId());
        }
        cardPurchaseRecord.setTime(DateUtils.getNowDate());
        cardPurchaseRecord.setUpdateTime(DateUtils.getNowDate());
        return cardPurchaseRecordMapper.updateCardPurchaseRecord(cardPurchaseRecord);
    }

    /**
     * 修改购卡记录
     *
     * @param cardPurchaseRecord 购卡记录
     * @return 结果
     */
    @Override
    public int updateCardPurchaseRecord(CardPurchaseRecord cardPurchaseRecord) {
        cardPurchaseRecord.setUpdateTime(DateUtils.getNowDate());
        return cardPurchaseRecordMapper.updateCardPurchaseRecord(cardPurchaseRecord);
    }

    /**
     * 批量删除购卡记录
     *
     * @param ids 需要删除的购卡记录主键
     * @return 结果
     */
    @Override
    public int deleteCardPurchaseRecordByIds(String ids) {
        return cardPurchaseRecordMapper.deleteCardPurchaseRecordByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除购卡记录信息
     *
     * @param id 购卡记录主键
     * @return 结果
     */
    @Override
    public int deleteCardPurchaseRecordById(Long id) {
        return cardPurchaseRecordMapper.deleteCardPurchaseRecordById(id);
    }
}
