package thyyxxk.webserver.service.examinations;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import thyyxxk.webserver.config.exception.ExceptionEnum;
import thyyxxk.webserver.dao.his.examinations.MixLabelPrintDao;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.examinations.mixlabel.MixLabel;
import thyyxxk.webserver.entity.examinations.mixlabel.MixLabelRange;
import thyyxxk.webserver.utils.ResultVoUtil;
import thyyxxk.webserver.utils.TokenUtil;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class MixLabelPrintService {
    private final MixLabelPrintDao dao;

    @Autowired
    public MixLabelPrintService(MixLabelPrintDao dao) {
        this.dao = dao;
    }

    public ResultVo<String> createMixLabel(MixLabelRange labelRange) {
        dao.createTable(labelRange.getTableName());
        List<String> labels = new ArrayList<>();
        for (int i = labelRange.getBeginNumber(); i <= labelRange.getEndNumber(); i++) {
            String label = labelRange.getPrefix() + i;
            labels.add(label);
            if (labels.size() == 200) {
                dao.insertNewLabels(labelRange.getTableName(), labels);
                labels.clear();
            }
        }
        if (labels.size() > 0) {
            dao.insertNewLabels(labelRange.getTableName(), labels);
        }
        dao.insertNewLabelRange(labelRange);
        return ResultVoUtil.success("成功。");
    }

    public ResultVo<List<MixLabelRange>> selectLabelRanges() {
        return ResultVoUtil.success(dao.selectLabelRanges());
    }

    public ResultVo<String> selectBeginLabel(String tableName) {
        return ResultVoUtil.success(dao.selectMinUnprintedLabel(tableName));
    }

    public ResultVo<MixLabel> selectLabelPrinter(String tableName, String label) {
        MixLabel printer = dao.selectSingleLabel(tableName, label);
        log.info("select label: {}, {}, {}", tableName, label, printer);
        if (null == printer) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "请输入正确的起始标签！");
        }
        return ResultVoUtil.success(printer);
    }

    public ResultVo<MixLabel> printLabel(String tableName, String label) {
        MixLabel printer = new MixLabel();
        printer.setTableName(tableName);
        printer.setLabel(label);
        printer.setStatus(1);
        printer.setPrintStaff(TokenUtil.getInstance().getTokenUserId());
        printer.setPrintDatetime(new Date());
        dao.updatePrinted(printer);
        return ResultVoUtil.success(printer);
    }

    public ResultVo<MixLabel> reprintLabel(MixLabel printer) {
        printer.setReprint(1);
        printer.setReprintStaff(TokenUtil.getInstance().getTokenUserId());
        printer.setReprintDatetime(new Date());
        dao.updateReprinted(printer);
        return ResultVoUtil.success(printer);
    }

    public ResultVo<String> allPrinted(String tableName) {
        Integer notPrintedCount = dao.selectNotPrintedCount(tableName);
        if (null != notPrintedCount && notPrintedCount == 0) {
            dao.updateAllPrinted(tableName);
            return ResultVoUtil.success("打印完成。");
        }
        return ResultVoUtil.success("当前号段还有未打印的条码。");
    }
}
