package com.sqx.modules.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqx.common.utils.Result;
import com.sqx.modules.app.dao.VipDetailsDao;
import com.sqx.modules.app.entity.VipDetails;
import com.sqx.modules.app.service.VipDetailsService;
import org.springframework.stereotype.Service;

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

@Service
public class VipDetailsServiceImpl extends ServiceImpl<VipDetailsDao, VipDetails> implements VipDetailsService {

    @Override
    public Result selectVipDetails() {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("vip_type", 0);
        queryWrapper.orderByAsc("id");
        List<VipDetails> list = baseMapper.selectList(queryWrapper);
        for(int i=0;i<list.size();i++){
            VipDetails vd = list.get(i);
            String rateStr = numberToChineseNum(vd.getRate());
            vd.setRateStr(rateStr);
        }
        return Result.success().put("data", list);
    }

    @Override
    public Result getVipDetailByUser(Long userId){
        VipDetails vipDetails = baseMapper.getVipDetailByUser(userId);
        if(vipDetails != null){
            String rateStr = numberToChineseNum(vipDetails.getRate());
            vipDetails.setRateStr(rateStr);
        }else{
            vipDetails = new VipDetails();
            vipDetails.setVipName("非会员");
            vipDetails.setRate(new BigDecimal(100));
            vipDetails.setRateStr("十");
            vipDetails.setIsVip(2);
            vipDetails.setEndTime("3000-01-01 00:00:00");

        }
        return Result.success().put("data", vipDetails);
    }

    @Override
    public Result insertVipDetails(VipDetails vipDetails) {
        int cpunt = baseMapper.insert(vipDetails);
        if (cpunt > 0) {
            return Result.success("添加成功！");
        } else {
            return Result.error("添加失败");
        }
    }

    String numberToChineseNum(BigDecimal bd){
        StringBuffer sb = new StringBuffer();
        Integer number = bd.intValue();
        char[] CN_NUMBERS = {'零', '一', '二', '三', '四', '五', '六', '七', '八', '九'};
        while (number > 0) {
            int part = number % 10;
            if (part == 0) {

            } else {
                sb.append(CN_NUMBERS[part]);
            }
            number /= 10;
        }
        return sb.reverse().toString();
    }

}
