package com.ruoyi.base.service.impl;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mchange.v2.beans.BeansUtils;
import com.ruoyi.base.domain.BaseCard;
import com.ruoyi.base.domain.BaseCardRights;
import com.ruoyi.base.mapper.BaseCardMapper;
import com.ruoyi.base.service.IBaseCardRightsService;
import com.ruoyi.base.service.IBaseCardService;
import com.ruoyi.base.vo.BaseCardVO;
import com.ruoyi.base.vo.CardRightsVO;
import com.ruoyi.common.core.domain.DlBaseEntity;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

/**
 * 会员卡Service业务层处理
 * 
 * @author vinjor-m
 * @date 2025-03-17
 */
@Service
public class BaseCardServiceImpl extends ServiceImpl<BaseCardMapper,BaseCard>  implements IBaseCardService
{
    @Autowired
    private BaseCardMapper baseCardMapper;
    @Resource
    private IBaseCardRightsService cardRightsService;

    @Override
    public IPage<BaseCard> queryListPage(BaseCard pageReqVO, Page<BaseCard> page) {
        return baseCardMapper.queryListPage(pageReqVO, page);
    }

    /**
     * 通过会员卡id查询会员及权益
     *
     * @param cardId 卡片id
     * @return com.ruoyi.base.vo.BaseCardVO
     * @author PQZ
     * @date 16:25 2025/3/26
     **/
    @Override
    public BaseCardVO queryByCardId(String cardId) {
        BaseCardVO cardVO = new BaseCardVO();
        BaseCard card = getById(cardId);
        BeanUtils.copyProperties(card,cardVO);
        List<CardRightsVO> rights = cardRightsService.queryByCardId(cardId);
        cardVO.setRights(rights);
        return cardVO;
    }

    /**
     * 会员卡数组
     *
     * @param userType 用户类型
     * @return java.util.List<com.ruoyi.base.domain.BaseCard>
     * @author PQZ
     * @date 10:17 2025/4/7
     **/
    @Override
    public JSONArray baseCardList(String userType) {
        LambdaQueryWrapper<BaseCard> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DlBaseEntity::getDelFlag,0)
                .eq(BaseCard::getUserType,userType)
                .eq(BaseCard::getIsSystem,0)
                .orderByAsc(BaseCard::getSort);
        List<BaseCard> list = list(lambdaQueryWrapper);
        JSONArray resultArray = new JSONArray();
        for (BaseCard card : list) {
            JSONObject object = new JSONObject();

            object.put("id",card.getId());
            object.put("cardName",card.getCardName());
            object.put("image",card.getImage());

            JSONArray array = new JSONArray();
            JSONObject yearObject = new JSONObject();
            yearObject.put("title","年付会员");
            yearObject.put("price",card.getYearPrice());
            yearObject.put("dayPrice",card.getYearPrice().divide(new BigDecimal(365), RoundingMode.HALF_UP));
            yearObject.put("isHot",true);
            array.add(yearObject);

            JSONObject querterObject = new JSONObject();
            querterObject.put("title","季付会员");
            querterObject.put("price",card.getQuarterPrice());
            querterObject.put("dayPrice",card.getQuarterPrice().divide(new BigDecimal(121), RoundingMode.HALF_UP));
            querterObject.put("isHot",false);
            array.add(querterObject);

            JSONObject monthPrice = new JSONObject();
            monthPrice.put("title","月付会员");
            monthPrice.put("price",card.getMonthPrice());
            monthPrice.put("dayPrice",card.getMonthPrice().divide(new BigDecimal(30), RoundingMode.HALF_UP));
            monthPrice.put("isHot",false);
            array.add(monthPrice);

            object.put("priceList",array);
            resultArray.add(object);
        }

        return resultArray;
    }

    /**
     * 通过userType查询会员卡
     *
     * @param userType 用户类型
     * @return java.util.List<com.ruoyi.base.domain.BaseCard>
     * @author PQZ
     * @date 15:12 2025/4/7
     **/
    @Override
    public List<BaseCard> listByUserType(String userType) {
        LambdaQueryWrapper<BaseCard> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DlBaseEntity::getDelFlag,0)
                .eq(BaseCard::getUserType,userType)
                .orderByAsc(BaseCard::getSort);
        return list(lambdaQueryWrapper);
    }
}
