package com.neusoft.smart.pos.printer.repository;


import com.neusoft.smart.pos.printer.persistent.domain.*;
import com.neusoft.smart.pos.printer.persistent.mapper.PrinterRuleMapper;
import com.neusoft.smart.pos.printer.persistent.mapper.RuleAttributeMapper;
import com.neusoft.smart.pos.printer.persistent.mapper.RuleAttributeValueMapper;
import lombok.val;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author zcf
 * @date 2018-10-18 10：43
 * @description 打印机规则管理使用Repository
 */
@Component
public class PrinterRuleRepository {
    
    @Resource
    private PrinterRuleMapper printerRuleMapper;
    
    @Resource
    private RuleAttributeMapper ruleAttributeMapper;
    
    @Resource
    private RuleAttributeValueMapper ruleAttributeValueMapper;
    
    /**
     * 保存规则
     *
     * @param printerRule
     */
    public void create(PrinterRule printerRule) {
        printerRuleMapper.insertSelective(printerRule);
    }
    
    /**
     * 根据规则ID查询规则Bean信息方法
     *
     * @return
     */
    public PrinterRule queryPrinterRulesByRuleId(Long ruleId) {
        return printerRuleMapper.selectByPrimaryKey(ruleId);
    }
    
    /**
     *
     * @param ruleIds
     * @return
     */
    public List<PrinterRule> queryPrinterRulesByRuleIds(List<Long> ruleIds) {
        PrinterRuleExample printerRuleExample = new PrinterRuleExample();
        printerRuleExample.createCriteria().andIdIn(ruleIds);
        return printerRuleMapper.selectByExample(printerRuleExample);
    }
    
    /**
     * 规则字典列表查询方法
     * @return
     */
    public List<PrinterRule> queryPrinterRuleList(String isEnable){
        val example = new PrinterRuleExample();
        example.createCriteria();
        if(!StringUtils.isEmpty(isEnable)){
            example.getOredCriteria().get(0).andIsEnableEqualTo(isEnable);
        }
        example.setOrderByClause("created_at asc");
        return printerRuleMapper.selectByExample(example);
    }
    
    /**
     * 规则字典属性列表查询方法
     * @return
     */
    public List<RuleAttribute> queryPrinterRuleAttributeList(Long ruleId){
        val example = new RuleAttributeExample();
        example.createCriteria().andRuleIdEqualTo(ruleId);
        example.setOrderByClause("created_at asc");
        return ruleAttributeMapper.selectByExample(example);
    }
    
    /**
     * 规则字典属性列表查询方法
     * @return
     */
    public RuleAttribute queryRuleAttributeById(Long attributeId){
        return ruleAttributeMapper.selectByPrimaryKey(attributeId);
    }
    
    public RuleAttributeValue queryRuleAttributeValue(Long attributeId,String attributeValue){
        val example = new RuleAttributeValueExample();
        example.createCriteria().andAttributeIdEqualTo(attributeId)
                    .andAttributeValueEqualTo(attributeValue);
        List<RuleAttributeValue> attributeValueList = ruleAttributeValueMapper.selectByExample(example);
        return attributeValueList.size()>0?attributeValueList.get(0):null;
    }
    
    /**
     * 规则字典属性值列表查询方法
     * @return
     */
    public List<RuleAttributeValue> queryPrinterRuleAttributeValueList(Long attributeId){
        val example = new RuleAttributeValueExample();
        example.createCriteria().andAttributeIdEqualTo(attributeId);
        example.setOrderByClause("created_at asc");
        return ruleAttributeValueMapper.selectByExample(example);
    }
}
