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

import cn.estsh.i3plus.ext.mes.pcn.api.base.IProdBindRecordExtService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesKeyPartData;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesProdBindRecordExt;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.MesKeyPartDataRepository;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.MesProdBindRecordExtRepository;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.platform.common.convert.ConvertBean;
import cn.estsh.i3plus.platform.common.tool.TimeTool;
import cn.estsh.i3plus.pojo.base.bean.DdlPackBean;
import cn.estsh.i3plus.pojo.base.tool.DdlPreparedPack;
import cn.estsh.i3plus.pojo.mes.bean.MesProdBindRecord;
import cn.estsh.i3plus.pojo.mes.repository.MesProdBindRecordRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @Description :
 * @Reference :
 * @Author : wangjie
 * @CreateDate : 2019-04-23
 * @Modify:
 **/
@Service
public class ProdBindRecordExtService implements IProdBindRecordExtService {
    public static final Logger LOGGER = LoggerFactory.getLogger(ProdBindRecordExtService.class);

    @Autowired
    private MesProdBindRecordRepository prodBindRecordRepository;

    @Autowired
    private MesProdBindRecordExtRepository prodBindRecordExtRepository;

    @Autowired
    private MesKeyPartDataRepository keyPartDataRepository;

    private static final int INSERT = 1;
    private static final int UPDATE = 2;
    private static final int UPDATE_STATUS = 3;

    @Override
    public void insertProdBindRecordExtNoRollback(MesProdBindRecordExt prodBindRecordExt) {
        prodBindRecordExtRepository.insert(prodBindRecordExt);
    }

    @Override
    public List<MesProdBindRecordExt> getProdBindRecordExtList(String organizeCode, String workOrderNo, String serialNumber) {
        if (StringUtils.isEmpty(organizeCode) || (StringUtils.isEmpty(workOrderNo) && StringUtils.isEmpty(serialNumber))) {
            return null;
        }
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.getStringEqualPack(workOrderNo, MesPcnExtConstWords.WORK_ORDER_NO, packBean);
        DdlPreparedPack.getStringEqualPack(serialNumber, MesPcnExtConstWords.SERIAL_NUMBER, packBean);
        return prodBindRecordExtRepository.findByHqlWhere(packBean);
    }

    @Override
    public List<MesProdBindRecord> getProdBindRecordList(String organizeCode, String workOrderNo, String serialNumber, String workCellCode) {
        if (StringUtils.isEmpty(organizeCode) || (StringUtils.isEmpty(workOrderNo) && StringUtils.isEmpty(serialNumber)) || StringUtils.isEmpty(workCellCode)) {
            return null;
        }
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.getStringEqualPack(workOrderNo, MesPcnExtConstWords.WORK_ORDER_NO, packBean);
        DdlPreparedPack.getStringEqualPack(serialNumber, MesPcnExtConstWords.SERIAL_NUMBER, packBean);
        DdlPreparedPack.getStringEqualPack(workCellCode, MesPcnExtConstWords.WORK_CELL_CODE, packBean);
        return prodBindRecordRepository.findByHqlWhere(packBean);
    }

    @Override
    public MesKeyPartData insertKeyPartData(MesKeyPartData keyPartAndRfidRecord, String userName, MesProdBindRecordExt prodBindRecordExt, MesKeyPartData partAndRfidRecordDb, Integer flag, Integer operationType) {
        if(operationType == INSERT){
            StringBuffer kpSnStr = new StringBuffer();
            kpSnStr.append(prodBindRecordExt.getKpSn()).append(MesPcnExtConstWords.COMMA);
            kpSnStr.deleteCharAt(kpSnStr.length() -1);
            keyPartAndRfidRecord.setKeyData(kpSnStr.toString());
            ConvertBean.serviceModelInitialize(keyPartAndRfidRecord, userName);
            return keyPartDataRepository.insert(keyPartAndRfidRecord);
        }else if(operationType == UPDATE){
            StringBuffer kpSnStr = new StringBuffer(partAndRfidRecordDb.getKeyData());
            kpSnStr.append(MesPcnExtConstWords.COMMA).append(prodBindRecordExt.getKpSn()).append(MesPcnExtConstWords.COMMA);
            kpSnStr.deleteCharAt(kpSnStr.length() -1);
            partAndRfidRecordDb.setKeyData(kpSnStr.toString());
            partAndRfidRecordDb.setModifyDatetime(TimeTool.getNowTime(true));
            partAndRfidRecordDb.setFlag(flag);
//            ConvertBean.serviceModelUpdate(keyPartAndRfidRecord, userName);
            return keyPartDataRepository.save(partAndRfidRecordDb);
        }else{
            partAndRfidRecordDb.setFlag(flag);
            partAndRfidRecordDb.setModifyDatetime(TimeTool.getNowTime(true));
            return keyPartDataRepository.save(partAndRfidRecordDb);
        }
    }
}
