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.uuid.IdUtils;
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 java.util.List;

/**
 * 购卡记录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.setStateLabel(dictDataService.selectDictLabel("card_status",cardPurchaseRecord2.getState()));
        }
        return cardPurchaseRecords;
    }

    /**
     * 新增购卡记录
     *
     * @param cardPurchaseRecord 购卡记录
     * @return 结果
     */
    @Override
    public int insertCardPurchaseRecord(CardPurchaseRecord cardPurchaseRecord) {
        cardPurchaseRecord.setCreateBy(ShiroUtils.getUserId().toString());
        cardPurchaseRecord.setCreateTime(DateUtils.getNowDate());
        cardPurchaseRecord.setState("1");
        cardPurchaseRecord.setPaymentTime(DateUtils.getNowDate());
        cardPurchaseRecord.setVersion("0");
        //新增会员管理
        UserMembershipCard userMembershipCard = new UserMembershipCard();
        userMembershipCard.setUserId(cardPurchaseRecord.getUserId());
        userMembershipCard.setCardId(cardPurchaseRecord.getCardId());
        userMembershipCard.setTotalNumber(cardPurchaseRecord.getTotalNumber());
        userMembershipCard.setRemainingNumber(cardPurchaseRecord.getTotalNumber());
        userMembershipCard.setCreateBy(ShiroUtils.getUserId().toString());
        userMembershipCard.setCreateTime(DateUtils.getNowDate());
        userMembershipCardMapper.insertUserMembershipCard(userMembershipCard);
        return cardPurchaseRecordMapper.insertCardPurchaseRecord(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);
    }
}
