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

import cn.estsh.i3plus.ext.mes.pcn.api.busi.recyclablepackage.IMesRecyclablePackageBindingLogService;
import cn.estsh.i3plus.ext.mes.pcn.apiservice.dao.IMesInsertBatchDao;
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.recyclablepackage.MesRecyclablePackageBinding;
import cn.estsh.i3plus.pojo.mes.bean.recyclablepackage.MesRecyclablePackageBindingDetail;
import cn.estsh.i3plus.pojo.mes.bean.recyclablepackage.MesRecyclablePackageBindingLog;
import cn.estsh.i3plus.pojo.mes.repository.recyclablepackage.MesRecyclablePackageBindingLogRepository;
import cn.estsh.i3plus.pojo.mes.util.MesExtEnumUtil;
import groovy.util.logging.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * @Description : 回收包装绑定LOG
 * @Reference :
 * @Author : junsheng.li
 * @CreateDate 2024/9/13 9:18
 * @Modify:
 **/
@Service
@Slf4j
public class MesRecyclablePackageBindingLogServiceImpl implements IMesRecyclablePackageBindingLogService {

    @Autowired
    private MesRecyclablePackageBindingLogRepository mesRecyclablePackageBindingLogRepository;

    @Autowired
    private IMesInsertBatchDao mesInsertBatchDao;

    @Override
    public MesRecyclablePackageBindingLog insertCreate(MesRecyclablePackageBinding mesRecyclablePackageBinding) {
        return insert(mesRecyclablePackageBinding, MesExtEnumUtil.RECYCLABLE_PACKAGE_PACKAGE_BINDING_MODIFY_TYPE.CREATE);
    }

    @Override
    public MesRecyclablePackageBindingLog insertClose(MesRecyclablePackageBinding mesRecyclablePackageBinding) {
        return insert(mesRecyclablePackageBinding, MesExtEnumUtil.RECYCLABLE_PACKAGE_PACKAGE_BINDING_MODIFY_TYPE.CLOSE);
    }

    @Override
    public MesRecyclablePackageBindingLog insertBinding(MesRecyclablePackageBinding mesRecyclablePackageBinding, MesRecyclablePackageBindingDetail mesRecyclablePackageBindingDetail) {
        return insert(mesRecyclablePackageBinding, mesRecyclablePackageBindingDetail, MesExtEnumUtil.RECYCLABLE_PACKAGE_PACKAGE_BINDING_MODIFY_TYPE.BINDING);
    }

    @Override
    public MesRecyclablePackageBindingLog insertUnBinding(MesRecyclablePackageBinding mesRecyclablePackageBinding, MesRecyclablePackageBindingDetail mesRecyclablePackageBindingDetail) {
        return insert(mesRecyclablePackageBinding, mesRecyclablePackageBindingDetail, MesExtEnumUtil.RECYCLABLE_PACKAGE_PACKAGE_BINDING_MODIFY_TYPE.UN_BINDING);
    }

    @Override
    public void insertUnBinding(MesRecyclablePackageBinding mesRecyclablePackageBinding, List<MesRecyclablePackageBindingDetail> mesRecyclablePackageBindingDetailList) {
        List<MesRecyclablePackageBindingLog> logList = new ArrayList<>();
        MesRecyclablePackageBindingLog mesRecyclablePackageBindingLog;
        for (MesRecyclablePackageBindingDetail mesRecyclablePackageBindingDetail : mesRecyclablePackageBindingDetailList) {
            mesRecyclablePackageBindingLog = new MesRecyclablePackageBindingLog();
            BeanUtils.copyProperties(mesRecyclablePackageBinding, mesRecyclablePackageBindingLog, MesPcnExtConstWords.ID, MesPcnExtConstWords.SYSTEM_SYNC_STATUS, MesPcnExtConstWords.SYSTEM_SYNC_DATE_TIME);
            //明细数据填充
            mesRecyclablePackageBindingLog.setPackageSn(mesRecyclablePackageBindingDetail.getProductSn());
            mesRecyclablePackageBindingLog.setPartNo(mesRecyclablePackageBindingDetail.getPartNo());
            mesRecyclablePackageBindingLog.setPartName(mesRecyclablePackageBindingDetail.getPartName());
            mesRecyclablePackageBindingLog.setQty(mesRecyclablePackageBindingDetail.getQty());
            mesRecyclablePackageBindingLog.setOrderCode(mesRecyclablePackageBindingDetail.getOrderCode());
            mesRecyclablePackageBindingLog.setLotNumber(mesRecyclablePackageBindingDetail.getLotNumber());
            mesRecyclablePackageBindingLog.setModifyType(MesExtEnumUtil.RECYCLABLE_PACKAGE_PACKAGE_BINDING_MODIFY_TYPE.UN_BINDING.getValue());
            ConvertBean.serviceModelInitialize(mesRecyclablePackageBindingLog, mesRecyclablePackageBindingDetail.getModifyUser());
            logList.add(mesRecyclablePackageBindingLog);
        }
        mesInsertBatchDao.insertMesRecyclablePackageBindingLog(logList,mesRecyclablePackageBinding.getOrganizeCode());
    }

    public MesRecyclablePackageBindingLog insert(MesRecyclablePackageBinding mesRecyclablePackageBinding, MesExtEnumUtil.RECYCLABLE_PACKAGE_PACKAGE_BINDING_MODIFY_TYPE modifyType) {
        MesRecyclablePackageBindingLog mesRecyclablePackageBindingLog = new MesRecyclablePackageBindingLog();
        BeanUtils.copyProperties(mesRecyclablePackageBinding, mesRecyclablePackageBindingLog, MesPcnExtConstWords.ID, MesPcnExtConstWords.SYSTEM_SYNC_STATUS, MesPcnExtConstWords.SYSTEM_SYNC_DATE_TIME);
        mesRecyclablePackageBindingLog.setModifyType(modifyType.getValue());
        ConvertBean.serviceModelInitialize(mesRecyclablePackageBindingLog, mesRecyclablePackageBinding.getModifyUser());
        return mesRecyclablePackageBindingLogRepository.insert(mesRecyclablePackageBindingLog);
    }


    public MesRecyclablePackageBindingLog insert(MesRecyclablePackageBinding mesRecyclablePackageBinding, MesRecyclablePackageBindingDetail mesRecyclablePackageBindingDetail, MesExtEnumUtil.RECYCLABLE_PACKAGE_PACKAGE_BINDING_MODIFY_TYPE modifyType) {
        MesRecyclablePackageBindingLog mesRecyclablePackageBindingLog = new MesRecyclablePackageBindingLog();
        BeanUtils.copyProperties(mesRecyclablePackageBinding, mesRecyclablePackageBindingLog, MesPcnExtConstWords.ID, MesPcnExtConstWords.SYSTEM_SYNC_STATUS, MesPcnExtConstWords.SYSTEM_SYNC_DATE_TIME);
        //明细数据填充
        mesRecyclablePackageBindingLog.setPackageSn(mesRecyclablePackageBindingDetail.getProductSn());
        mesRecyclablePackageBindingLog.setPartNo(mesRecyclablePackageBindingDetail.getPartNo());
        mesRecyclablePackageBindingLog.setPartName(mesRecyclablePackageBindingDetail.getPartName());
        mesRecyclablePackageBindingLog.setQty(mesRecyclablePackageBindingDetail.getQty());
        mesRecyclablePackageBindingLog.setOrderCode(mesRecyclablePackageBindingDetail.getOrderCode());
        mesRecyclablePackageBindingLog.setLotNumber(mesRecyclablePackageBindingDetail.getLotNumber());
        mesRecyclablePackageBindingLog.setModifyType(modifyType.getValue());
        ConvertBean.serviceModelInitialize(mesRecyclablePackageBindingLog, mesRecyclablePackageBindingDetail.getModifyUser());
        return mesRecyclablePackageBindingLogRepository.insert(mesRecyclablePackageBindingLog);
    }
}
