package cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.busi;

import cn.estsh.i3plus.ext.mes.pcn.api.busi.IMesPrintedSnLogService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.MesProduceSnPrintDataModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.platform.common.convert.ConvertBean;
import cn.estsh.i3plus.pojo.mes.bean.MesPrintedSnLog;
import cn.estsh.i3plus.pojo.mes.repository.IMesPrintedSnLogRepository;
import cn.estsh.i3plus.pojo.mes.util.MesExtEnumUtil;
import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @Description : 条码打印记录表
 * @Reference :
 * @Author : junsheng.li
 * @CreateDate 2024/10/16 13:11
 * @Modify:
 **/
@Slf4j
@Service
public class MesPrintedSnLogServiceImpl implements IMesPrintedSnLogService {

    @Autowired
    private IMesPrintedSnLogRepository mesPrintedSnLogRepository;

    @Override
    public MesPrintedSnLog getMesPrintedSnLog(String userName, String organizeCode, MesProduceSnPrintDataModel printDataModel) {
        MesPrintedSnLog snLog = new MesPrintedSnLog();
        BeanUtil.copyProperties(printDataModel, snLog);
        snLog.setOrganizeCode(organizeCode);
        snLog.setPrintData(JSONObject.toJSONString(printDataModel));
        snLog.setPrintType(MesExtEnumUtil.PRINT_LOG_TYPE.PRINT_LOG_TYPE_10.getValue());
        ConvertBean.serviceModelInitialize(snLog, userName);
        return snLog;
    }

    @Override
    public MesPrintedSnLog getMesCustomPrintedSnLog(String userName, String organizeCode, MesProduceSnPrintDataModel printDataModel, Map<String, Object> printContext) {
        MesPrintedSnLog snLog = new MesPrintedSnLog();
        BeanUtil.copyProperties(printDataModel, snLog);
        snLog.setOrganizeCode(organizeCode);
        snLog.setPrintContext(printContext);
        snLog.setPrintData(JSONObject.toJSONString(printContext));
        snLog.setPrintType(MesExtEnumUtil.PRINT_LOG_TYPE.PRINT_LOG_TYPE_10.getValue());
        ConvertBean.serviceModelInitialize(snLog, userName);
        return snLog;
    }

    @Override
    public void insertMesPrintedSnLog(MesPrintedSnLog mesPrintedSnLog, String userName) {
        MesPrintedSnLog snLog = new MesPrintedSnLog();
        BeanUtil.copyProperties(mesPrintedSnLog, snLog, MesPcnExtConstWords.BASE_BEAN_FIELDS);
        snLog.setPrintType(MesExtEnumUtil.PRINT_LOG_TYPE.PRINT_LOG_TYPE_20.getValue());
        ConvertBean.serviceModelInitialize(snLog, userName);
        mesPrintedSnLogRepository.insert(snLog);
    }
}
