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


import com.neusoft.smart.pos.constants.DictionaryConstant;
import com.neusoft.smart.pos.printer.persistent.domain.PrinterGoodsRefInfo;
import com.neusoft.smart.pos.printer.persistent.domain.PrinterGoodsRefInfoExample;
import com.neusoft.smart.pos.printer.persistent.mapper.PrinterGoodsRefInfoMapper;
import lombok.val;
import org.springframework.stereotype.Component;

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

/**
 * @author zcf
 * @date 2018-10-18 10：43
 * @description 商品打印信息关联使用Repository
 */
@Component
public class PrinterGoodsRefInfoRepository {
    
    @Resource
    private PrinterGoodsRefInfoMapper printerGoodsRefInfoMapper;
    
    
    /**
     * 批量保存打印机可打印菜或类别
     *
     * @param infos
     */
    public void createBatch(List<PrinterGoodsRefInfo> infos) {
        printerGoodsRefInfoMapper.batchInsert(infos);
    }
    
    /**
     * @param printerId
     */
    public void deleteByPrinterId(Long printerId) {
        val example = new PrinterGoodsRefInfoExample();
        example.createCriteria().andPrinterIdEqualTo(printerId);
        printerGoodsRefInfoMapper.deleteByExample(example);
    }
    
    /**
     * 子打印机信息查询方法
     *
     * @param orgId：集团编号
     * @param shopId：门店编号
     * @return
     */
    public List<PrinterGoodsRefInfo> queryPrinterGoodsRefInfoByGoodsIds(Integer orgId, Integer shopId, List<Integer> goodsIds) {
        val example = new PrinterGoodsRefInfoExample();
        example.createCriteria()
                .andOrgIdEqualTo(orgId)
                .andShopIdEqualTo(shopId)
                .andRelationshipIdIn(goodsIds)
                .andRelationshipTypeEqualTo(DictionaryConstant.PRINTER_RELATIONSHIP_TYPE_1.getCode());
        example.setOrderByClause("printer_id asc");
        return printerGoodsRefInfoMapper.selectByExample(example);
    }
    
    /**
     * 子打印机信息查询方法
     *
     * @param orgId：集团编号
     * @param shopId：门店编号
     * @return
     */
    public List<PrinterGoodsRefInfo> queryPrinterGoodsRefInfoByRelationshipIds(Integer orgId, Integer shopId, List<Integer> goodsIds, List<Integer> categoryIds,List<Long> printerIds) {
        val example = new PrinterGoodsRefInfoExample();
        example.createCriteria();
        
        PrinterGoodsRefInfoExample.Criteria criteria1 = example.createCriteria();
        criteria1.andOrgIdEqualTo(orgId)
                .andShopIdEqualTo(shopId)
                .andRelationshipIdIn(goodsIds)
                .andPrinterIdIn(printerIds)
                .andRelationshipTypeEqualTo(DictionaryConstant.PRINTER_RELATIONSHIP_TYPE_1.getCode());
        example.or(criteria1);
        
        PrinterGoodsRefInfoExample.Criteria criteria2 = example.createCriteria();
        criteria2.andOrgIdEqualTo(orgId)
                .andShopIdEqualTo(shopId)
                .andRelationshipIdIn(categoryIds)
                .andPrinterIdIn(printerIds)
                .andRelationshipTypeEqualTo(DictionaryConstant.PRINTER_RELATIONSHIP_TYPE_2.getCode());
        example.or(criteria2);
        
        example.setOrderByClause("printer_id asc");
        return printerGoodsRefInfoMapper.selectByExample(example);
    }
    
    /**
     * @param printerId
     * @return
     */
    public List<PrinterGoodsRefInfo> queryPrinterGoodsRefInfoByPrinterId(Long printerId) {
        val example = new PrinterGoodsRefInfoExample();
        example.createCriteria()
                .andPrinterIdEqualTo(printerId);
        return printerGoodsRefInfoMapper.selectByExample(example);
    }
    
}
