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

import com.neusoft.smart.pos.printer.persistent.domain.PrinterTaskInfo;
import com.neusoft.smart.pos.printer.persistent.domain.PrinterTaskInfoExample;
import com.neusoft.smart.pos.printer.persistent.ext.domain.PrinterTaskQuery;
import com.neusoft.smart.pos.printer.persistent.mapper.PrinterTaskInfoMapper;
import com.neusoft.smart.pos.utils.RequestHeaderUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

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


@Component
public class PrinterTaskInfoRepository {
    
    @Autowired
    public PrinterTaskInfoMapper printerTaskInfoMapper;
    
    
    /**
     * @param printerTaskInfo
     */
    public void create(PrinterTaskInfo printerTaskInfo) {
        printerTaskInfoMapper.insertSelective(printerTaskInfo);
    }
    
    /**
     * @param printerTaskInfo
     */
    public void update(PrinterTaskInfo printerTaskInfo) {
        printerTaskInfoMapper.updateByPrimaryKey(printerTaskInfo);
    }
    
    /**
     * @param taskId
     * @param taskStatus
     */
    public void updateTaskStatus(Integer taskId, String taskStatus) {
        PrinterTaskInfo printerTaskInfo = new PrinterTaskInfo();
        printerTaskInfo.setId(taskId);
        printerTaskInfo.setTaskStatus(taskStatus);
        printerTaskInfoMapper.updateByPrimaryKeySelective(printerTaskInfo);
    }
    
    /**
     * @param id
     */
    public void delete(Integer id) {
        printerTaskInfoMapper.deleteByPrimaryKey(id);
    }
    
    /**
     * @param id
     * @return
     */
    public PrinterTaskInfo get(Integer id) {
        return printerTaskInfoMapper.selectByPrimaryKey(id);
    }
    
    /**
     * @return
     */
    public List<PrinterTaskInfo> getTaskByOrderNumber(String orderNumber) {
        PrinterTaskInfoExample printerTaskInfoExample = new PrinterTaskInfoExample();
        printerTaskInfoExample.createCriteria().andOrderNumberEqualTo(orderNumber);
        return printerTaskInfoMapper.selectByExample(printerTaskInfoExample);
    }
    
    /**
     * 查询列表
     *
     * @param printerTaskQuery
     * @return
     */
    public List<PrinterTaskInfo> getList(PrinterTaskQuery printerTaskQuery) {
        PrinterTaskInfoExample printerTaskInfoExample = this.printerTaskQueryExampleProvider(printerTaskQuery);
        if (printerTaskQuery.getIsPage()) {
            printerTaskInfoExample.setLimit(printerTaskQuery.getLimit());
            printerTaskInfoExample.setOffset(printerTaskQuery.getOffset());
        }
        printerTaskInfoExample.setOrderByClause("create_time desc");
        return printerTaskInfoMapper.selectByExample(printerTaskInfoExample);
    }
    
    /**
     * @param printerTaskQuery
     * @return
     */
    public Integer getCount(PrinterTaskQuery printerTaskQuery) {
        PrinterTaskInfoExample printerTaskInfoExample = this.printerTaskQueryExampleProvider(printerTaskQuery);
        return printerTaskInfoMapper.countByExample(printerTaskInfoExample);
    }
    
    private PrinterTaskInfoExample printerTaskQueryExampleProvider(@NotNull PrinterTaskQuery printerTaskQuery) {
        PrinterTaskInfoExample printerTaskInfoExample = new PrinterTaskInfoExample();
        PrinterTaskInfoExample.Criteria criteria = printerTaskInfoExample.createCriteria();
        
        if (!StringUtils.isEmpty(printerTaskQuery.getOrderType())) {
            criteria.andOrderTypeEqualTo(printerTaskQuery.getOrderType());
        }
        if (!StringUtils.isEmpty(printerTaskQuery.getPrinterType())) {
            criteria.andPrinterTypeEqualTo(printerTaskQuery.getPrinterType());
        }
        if (!StringUtils.isEmpty(printerTaskQuery.getTaskStatus())) {
            criteria.andTaskStatusEqualTo(printerTaskQuery.getTaskStatus());
        }
        switch (RequestHeaderUtil.getRole()) {
            case "admin":
                criteria.andOrgIdEqualTo(RequestHeaderUtil.getOrgId());
                break;
            case "user":
                criteria.andShopIdIn(RequestHeaderUtil.getStoreIds());
                criteria.andOrgIdEqualTo(RequestHeaderUtil.getOrgId());
                break;
            default:
        }
        return printerTaskInfoExample;
    }
}
