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


import com.neusoft.smart.pos.constants.DictionaryConstant;
import com.neusoft.smart.pos.printer.persistent.domain.PrinterInfo;
import com.neusoft.smart.pos.printer.persistent.domain.PrinterInfoExample;
import com.neusoft.smart.pos.printer.persistent.ext.domain.PrinterDataQuery;
import com.neusoft.smart.pos.printer.persistent.mapper.PrinterInfoMapper;
import com.neusoft.smart.pos.utils.RequestHeaderUtil;
import lombok.val;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author zcf
 * @date 2018-10-18 10：43
 * @description 打印机管理使用Repository
 */
@Component
public class PrinterInfoRepository {
    
    @Resource
    private PrinterInfoMapper printerInfoMapper;
    
    /**
     * 保存打印机
     *
     * @param printerInfo
     */
    public void create(PrinterInfo printerInfo) {
        printerInfoMapper.insertSelective(printerInfo);
    }
    
    public void update(PrinterInfo printerInfo) {
        printerInfoMapper.updateByPrimaryKey(printerInfo);
    }
    
    /**
     * @param printerInfo
     */
    public void updateSelective(PrinterInfo printerInfo) {
        printerInfoMapper.updateByPrimaryKeySelective(printerInfo);
    }
    
    /**
     * 子打印机信息查询方法
     *
     * @param orgId：集团编号
     * @param shopId：门店编号
     * @return
     */
    public List<PrinterInfo> queryPrinterChilds(Integer orgId, Integer shopId, String isEnable) {
        val example = new PrinterInfoExample();
        example.createCriteria()
                .andOrgIdEqualTo(orgId)
                .andShopIdEqualTo(shopId)
                .andDeviceTypeEqualTo(DictionaryConstant.PRINTER_DEVICE_TYPE_2.getCode());
        if (!StringUtils.isEmpty(isEnable)) {
            example.getOredCriteria().get(0).andPrinterStatusEqualTo(isEnable);
        }
        example.setOrderByClause("created_at asc");
        return printerInfoMapper.selectByExample(example);
    }
    
    public List<PrinterInfo> queryPrinterList(PrinterDataQuery printerDataQuery) {
        PrinterInfoExample printerInfoExample = this.printerExampleProvider(printerDataQuery);
        if (printerDataQuery.getIsPage()) {
            printerInfoExample.page(printerDataQuery.getOffset(), printerDataQuery.getLimit());
        }
        printerInfoExample.setOrderByClause("created_at desc");
        return printerInfoMapper.selectByExample(printerInfoExample);
    }
    
    public Integer queryPinterCount(PrinterDataQuery printerDataQuery) {
        PrinterInfoExample printerInfoExample = this.printerExampleProvider(printerDataQuery);
        return printerInfoMapper.countByExample(printerInfoExample);
    }
    
    
    private PrinterInfoExample printerExampleProvider(@NotNull PrinterDataQuery printerDataQuery) {
        PrinterInfoExample printerInfoExample = new PrinterInfoExample();
        PrinterInfoExample.Criteria criteria = printerInfoExample.createCriteria();
        if (!StringUtils.isEmpty(printerDataQuery.getSn())) {
            criteria.andPrinterSnLike("%" + printerDataQuery.getSn() + "%");
        }
        if (!StringUtils.isEmpty(printerDataQuery.getName())) {
            criteria.andPrinterNameLike("%" + printerDataQuery.getName() + "%");
        }
        if (!StringUtils.isEmpty(printerDataQuery.getDeviceType())) {
            criteria.andDeviceTypeEqualTo(printerDataQuery.getDeviceType());
        }
        if (null != printerDataQuery.getStatus()) {
            criteria.andPrinterStatusEqualTo(printerDataQuery.getStatus());
        }
        if (!"sa".equals(RequestHeaderUtil.getRole())) {
            criteria.andOrgIdEqualTo(RequestHeaderUtil.getOrgId());
        }
        if (null != printerDataQuery.getShopId()) {
            criteria.andShopIdEqualTo(printerDataQuery.getShopId());
        }
        if (printerDataQuery.getIsTree()) {
            criteria.andParentIdEqualTo(0l);
        }
        if (!printerDataQuery.getIsTree() && printerDataQuery.getIsSinglePrinter()) {
            criteria.andParentIdEqualTo(0l).andDeviceTypeEqualTo(DictionaryConstant.PRINTER_DEVICE_TYPE_2.getCode());
        }
        if (printerDataQuery.getEnable()) {
            criteria.andPrinterStatusEqualTo("1");
        }
        return printerInfoExample;
    }
    
    /**
     * 打印机信息查询方法
     *
     * @param printerId：打印机编号
     * @return
     */
    public PrinterInfo queryPrinterByPrinterId(Long printerId) {
        return printerInfoMapper.selectByPrimaryKey(printerId);
    }
    
    /**
     * @param parentId
     * @return
     */
    public List<PrinterInfo> queryPrinterByParentId(Long parentId) {
        val example = new PrinterInfoExample();
        example.createCriteria().andParentIdEqualTo(parentId)
                .andDeviceTypeEqualTo(DictionaryConstant.PRINTER_DEVICE_TYPE_2.getCode());
        return printerInfoMapper.selectByExample(example);
    }
    
    /**
     * 查询全部
     *
     * @return
     */
    public List<PrinterInfo> queryAll(Integer orgId, Integer shopId) {
        return queryAll(orgId,shopId,null);
    }
    
    /**
     * 查询全部
     *
     * @return
     */
    public List<PrinterInfo> queryAll(Integer orgId, Integer shopId,String printerStatus) {
        val example = new PrinterInfoExample();
        example.createCriteria().andOrgIdEqualTo(orgId).andShopIdEqualTo(shopId);
        if(!StringUtils.isEmpty(printerStatus)){
            example.getOredCriteria().get(0).andPrinterStatusEqualTo(printerStatus);
        }
        return printerInfoMapper.selectByExample(example);
    }
}
