package com.iamberry.wechat.service.giftCard;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.iamberry.wechat.core.entity.giftCard.GiftCardPayCode;
import com.iamberry.wechat.face.giftCard.GiftCardPayCodeService;
import com.iamberry.wechat.service.mapper.GiftCardPayCodeMapper;

@Service
public class GiftCardPayCodeServiceImpl implements GiftCardPayCodeService {
    @Autowired
    private GiftCardPayCodeMapper giftCardPayCodeMapper;
    @Override
    public boolean insertPayCodeList(String openid, int num) {
        // TODO Auto-generated method stub
        boolean flag = false;
        List<GiftCardPayCode> list = new ArrayList<GiftCardPayCode>();
        for (int i = 0; i < num; i++) {
            String password = getPassword(11);
            GiftCardPayCode payCode = new GiftCardPayCode();
            payCode.setCodeReferrerOpenid(openid);
            payCode.setCodePassword(password);
            payCode.setCodeStatus(1);
            list.add(payCode);
        }
        giftCardPayCodeMapper.insertPayCodeList(list);
        return flag;
    }
    @Override
    public GiftCardPayCode getPayCodeByIdOrPassword(GiftCardPayCode payCode) {
        // TODO Auto-generated method stub
        return giftCardPayCodeMapper.getPayCodeByIdOrPassword(payCode);
    }
    
    /**
     * 生成 i 位的随机数字
     * 2017年3月3日
     * @author muzx
     * @param i
     * @return
     */
    private String getPassword(int i){
        String chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        int s = 0;
        StringBuilder sb = new StringBuilder();
        while (s < 1) {
            //生成一个随机的大写字母
            String letter = String.valueOf(chars.charAt((int)(Math.random() * 26)));
            //该大写字母不能是O,I,L等字母，如果是O,I,L则继续生成
            if (!"O".equals(letter) &&  !"I".equals(letter) && !"L".equals(letter)) {
                sb.append(letter);
                s++;
            }
        }
        
        long j = 1L;
        double num = Math.random();
        for (int n = 0; n < i;n++) {
            j = j * 10;
        }
        j = (long) (num * j);
        sb.append(j);
        String str = sb.toString();
        
        //如果位数小于(i+1)位 则重新生成
        if(str.length() < (i + 1)){     
            return getPassword(i);
        }
        
        //查询数据库中有没有重复的抵扣密码 有则重新生成
        List<GiftCardPayCode> list = giftCardPayCodeMapper.listPayCode(new GiftCardPayCode());
        for (GiftCardPayCode code : list) {
            if(Objects.equals(str, code.getCodePassword())){
                str = getPassword(i);
                break;
            }
        }
        return str;
    }
    @Override
    public List<GiftCardPayCode> listPayCode(GiftCardPayCode payCode) {
        // TODO Auto-generated method stub
        payCode.getPage().initRecordBegin();
        return giftCardPayCodeMapper.listPayCode(payCode);
    }
    @Override
    public int countPayCode() {
        // TODO Auto-generated method stub
        return giftCardPayCodeMapper.countPayCode();
    }
}
