package com.prolog.cs.biz.outbound.service.impl;

import com.prolog.cs.biz.outbound.dao.PrintLogMapper;
import com.prolog.cs.biz.outbound.model.entity.PrintLog;
import com.prolog.cs.biz.outbound.service.PrintLogService;
import com.prolog.framework.authority.core.dto.UserDTO;
import com.prolog.framework.authority.user.UserTemplate;
import com.prolog.framework.core.restriction.Criteria;
import com.prolog.framework.core.restriction.Restriction;
import com.prolog.framework.core.restriction.Restrictions;
import com.prolog.framework.utils.StringUtils;
import com.prolog.framework.utils.UUIDUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class PrintLogServiceImpl implements PrintLogService {

    @Autowired
    private PrintLogMapper printLogMapper;
    @Autowired
    private UserTemplate userTemplate;

    @Override
    public void save(String billNo, String printCode,boolean success) {

        UserDTO userDTO = userTemplate.getUserDTO();
        PrintLog printLog = new PrintLog();
        printLog.setId(UUIDUtils.generateWithoutSeperator());
        printLog.setBillno(billNo);
        printLog.setPrintCode(printCode);
        if(success){
            printLog.setStatus("200");
        }else {
            printLog.setStatus("0");
        }
        if(userDTO == null){
            printLog.setCreator("EIS");
        }
        printLog.setCreator(userDTO.getNickname());
        printLog.setGmtCreate(new Date());

        printLogMapper.save(printLog);
    }

    @Override
    public void save(String billNo,String barcode, String printCode,boolean success) {

        UserDTO userDTO = userTemplate.getUserDTO();
        PrintLog printLog = new PrintLog();
        printLog.setId(UUIDUtils.generateWithoutSeperator());
        printLog.setBillno(billNo);
        printLog.setPrintCode(printCode);
        if(success){
            printLog.setStatus("200");
        }else {
            printLog.setStatus("0");
        }
        if(userDTO == null){
            printLog.setCreator("EIS");
        }
        printLog.setAttrStr3(barcode);
        printLog.setCreator(userDTO.getNickname());
        printLog.setGmtCreate(new Date());

        printLogMapper.save(printLog);
    }

    @Override
    public void save(String billNo, String printCode, String itemId, String batchId, boolean success) {
        UserDTO userDTO = userTemplate.getUserDTO();
        PrintLog printLog = new PrintLog();
        printLog.setId(UUIDUtils.generateWithoutSeperator());
        printLog.setBillno(billNo);
        printLog.setPrintCode(printCode);
        printLog.setAttrStr1(itemId);
        printLog.setAttrStr2(batchId);
        if(success){
            printLog.setStatus("200");
        }else {
            printLog.setStatus("0");
        }
        if(userDTO == null){
            printLog.setCreator("EIS");
        }
        printLog.setCreator(userDTO.getNickname());
        printLog.setGmtCreate(new Date());

        printLogMapper.save(printLog);
    }

    @Override
    public void saveBatch(List<PrintLog> printLogList) {
        for (PrintLog printLog : printLogList) {
            printLog.setId(UUIDUtils.generateWithoutSeperator());
            UserDTO userDTO = userTemplate.getUserDTO();
            if(userDTO == null){
                printLog.setCreator("EIS");
            }
            printLog.setCreator(userDTO.getNickname());
            printLog.setGmtCreate(new Date());
        }
        printLogMapper.saveBatch(printLogList);
    }

    @Override
    public List<PrintLog> findbyItemIds(List<String> itemIds,String printCode) {
        if(itemIds == null || itemIds.size() <= 0){
            return null;
        }
        Criteria crt = Criteria.forClass(PrintLog.class);
        Restriction r1 = Restrictions.in("attrStr1", itemIds.toArray());
        Restriction r2 = null;
        if(!StringUtils.isBlank(printCode)){
            r2 = Restrictions.eq("printCode",printCode);
        }
        crt.setRestriction(Restrictions.and(r1,r2));
        return printLogMapper.findByCriteria(crt);
    }
}
