package com.xisc.pm.app.service.impl;

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

import org.hzero.core.base.AopProxy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.xisc.pm.app.service.PmRuleCodeService;
import com.xisc.pm.domain.entity.PmRuleCodeSeq;
import com.xisc.pm.domain.repository.PmRuleCodeSeqRepository;

/**
 * 编码规则
 * @author jun.zeng
 *
 */
@Service
public class PmRuleCodeServiceImpl implements PmRuleCodeService , AopProxy<PmRuleCodeService> {
    
    @Autowired
    private PmRuleCodeSeqRepository pmRuleCodeSeqRepository;

    @Override
    public String generateCode(Long tenantId, String ruleCode, String resetKey , int digit) {
        return this.self().generateCode(tenantId, ruleCode, resetKey ,digit , 1L);
    }
    
    @Override
    public List<String> generateCode(int qty, Long tenantId, String ruleCode, String resetKey, int digit) {
        return this.self().generateCode(qty, tenantId, ruleCode, resetKey ,digit , 1L);
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class , propagation = Propagation.REQUIRES_NEW)
    public List<String> generateCode(int qty, Long tenantId, String ruleCode, String resetKey, int digit,
            Long initValue) {
        PmRuleCodeSeq record = new PmRuleCodeSeq() ;
        record.setTenantId(tenantId);
        record.setRuleCode(ruleCode);
        record.setResetKey(resetKey);
        PmRuleCodeSeq pmRuleCodeSeq = pmRuleCodeSeqRepository.selectOne(record);
        if(pmRuleCodeSeq == null) {
            record.setInitValue(initValue);
            record.setCurrentValue(initValue + (qty - 1));
            record.setRuleName(ruleCode);
            pmRuleCodeSeqRepository.insertSelective(record);
            List<String> code = new ArrayList<String>(qty);
            for(int i = 0 ; i < qty ; i ++) {
                code.add(String.format("%s%0"+ digit + "d" , resetKey , initValue.intValue() + i));
            }
            return code;
        } else {
            List<String> code = new ArrayList<String>(qty);
            for(int i = 0 ; i < qty ; i ++) {
                pmRuleCodeSeq.setCurrentValue(pmRuleCodeSeq.getCurrentValue() + 1);
                code.add(String.format("%s%0"+ digit + "d" , resetKey , pmRuleCodeSeq.getCurrentValue()));
            }
            this.pmRuleCodeSeqRepository.updateByPrimaryKeySelective(pmRuleCodeSeq);
            return code;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class , propagation = Propagation.REQUIRES_NEW)
    public String generateCode(Long tenantId, String ruleCode, String resetKey,int digit, Long initValue) {
        PmRuleCodeSeq record = new PmRuleCodeSeq() ;
        record.setTenantId(tenantId);
        record.setRuleCode(ruleCode);
        record.setResetKey(resetKey);
        PmRuleCodeSeq pmRuleCodeSeq = pmRuleCodeSeqRepository.selectOne(record);
        if(pmRuleCodeSeq == null) {
            record.setInitValue(initValue);
            record.setCurrentValue(initValue);
            record.setRuleName(ruleCode);
            pmRuleCodeSeqRepository.insertSelective(record);
            return String.format("%s%0"+ digit + "d" , resetKey , initValue.intValue());
        } else {
            pmRuleCodeSeq.setCurrentValue(pmRuleCodeSeq.getCurrentValue() + 1);
            this.pmRuleCodeSeqRepository.updateByPrimaryKeySelective(pmRuleCodeSeq);
            return String.format("%s%0"+ digit + "d" , resetKey , pmRuleCodeSeq.getCurrentValue());
        }
    }
    
    
}
