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

import cn.estsh.i3plus.ext.mes.pcn.api.busi.jx.IJxProdBindRecordExtService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesProdBindRecordExt;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.MesProdBindRecordExtRepository;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.ConvertBeanExt;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtEnumUtil;
import cn.estsh.i3plus.platform.common.convert.ConvertBean;
import cn.estsh.i3plus.pojo.base.bean.DdlPackBean;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
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 lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @Author: wangjie
 * @CreateDate: 2021/01/18 11:41 上午
 * @Description:
 **/
@Slf4j
@Service
public class JxProdBindRecordExtService implements IJxProdBindRecordExtService {

    @Autowired
    private MesProdBindRecordExtRepository prodBindRecordExtRepository;

    @Autowired
    private MesProdBindRecordRepository prodBindRecordRepository;

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

    @Override
    public List<MesProdBindRecordExt> getProdBindRecordExtListByPsOrMc(String organizeCode, String workOrderNo, String workCellCode, String productSn, String thirdPartyPid) {
        if (StringUtils.isEmpty(organizeCode) || StringUtils.isEmpty(workOrderNo) || StringUtils.isEmpty(workCellCode)) return null;
        if (StringUtils.isEmpty(productSn) && StringUtils.isEmpty(thirdPartyPid)) return null;
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.getStringEqualPack(workOrderNo, MesPcnExtConstWords.WORK_ORDER_NO, packBean);
        DdlPreparedPack.getStringEqualPack(workCellCode, MesPcnExtConstWords.WORK_CELL_CODE, packBean);
        DdlPreparedPack.getStringEqualPack(productSn, MesPcnExtConstWords.PRODUCT_SN, packBean);
        DdlPreparedPack.getStringEqualPack(thirdPartyPid, MesPcnExtConstWords.THIRD_PARTY_PID, packBean);
        List<MesProdBindRecordExt> prodBindRecordExtList = prodBindRecordExtRepository.findByHqlWhere(packBean);
        prodBindRecordExtList = CollectionUtils.isEmpty(prodBindRecordExtList) ? null :
                prodBindRecordExtList.stream().filter(o -> (null != o && MesPcnExtEnumUtil.MES_ACTION_TYPE.BIND.getValue() == o.getActionType())).collect(Collectors.toList());
        return prodBindRecordExtList;
    }

    @Override
    public List<MesProdBindRecord> getProdBindRecordListByKpSn(String organizeCode, String kpSn) {
        if (StringUtils.isEmpty(organizeCode) || StringUtils.isEmpty(kpSn)) return null;
        return prodBindRecordRepository.findByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID, MesPcnExtConstWords.KP_SN},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), kpSn});
    }

    @Override
    public Boolean getProdBindRecordIsExist(String organizeCode, String workOrderNo, String serialNumber, String workCenterCode, String workCellCode, String kpSn) {
        if (StringUtils.isEmpty(organizeCode) || StringUtils.isEmpty(workOrderNo) || StringUtils.isEmpty(serialNumber) ||
                StringUtils.isEmpty(workCenterCode) || StringUtils.isEmpty(workCellCode) || StringUtils.isEmpty(kpSn)) return false;
        List<MesProdBindRecord> prodBindRecordList = prodBindRecordRepository.findByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID,
                        MesPcnExtConstWords.WORK_ORDER_NO, MesPcnExtConstWords.SERIAL_NUMBER, MesPcnExtConstWords.WORK_CENTER_CODE, MesPcnExtConstWords.WORK_CELL_CODE, MesPcnExtConstWords.KP_SN},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), workOrderNo, serialNumber, workCenterCode, workCellCode, kpSn});
        Optional<MesProdBindRecord> option = CollectionUtils.isEmpty(prodBindRecordList) ? null : prodBindRecordList.stream().filter(o -> (null != o && MesPcnExtEnumUtil.MES_ACTION_TYPE.BIND.getValue() == o.getActionType())).findFirst();
        return (null == option || !option.isPresent()) ? false : true;
    }

    @Override
    public Integer doUnBindKeyPartList(String organizeCode, String userInfo, String workOrderNo, String serialNumber, String workCenterCode, String workCellCode) {
        if (StringUtils.isEmpty(organizeCode) || StringUtils.isEmpty(workOrderNo) || StringUtils.isEmpty(serialNumber) || StringUtils.isEmpty(workCenterCode) || StringUtils.isEmpty(workCellCode)) return MesPcnExtConstWords.ZERO;
        List<MesProdBindRecordExt> prodBindRecordExtList = prodBindRecordExtRepository.findByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID,
                        MesPcnExtConstWords.WORK_ORDER_NO, MesPcnExtConstWords.SERIAL_NUMBER, MesPcnExtConstWords.WORK_CENTER_CODE, MesPcnExtConstWords.WORK_CELL_CODE},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), workOrderNo, serialNumber, workCenterCode, workCellCode});
        prodBindRecordExtList = CollectionUtils.isEmpty(prodBindRecordExtList) ? null :
                prodBindRecordExtList.stream().filter(o -> (null != o && MesPcnExtEnumUtil.MES_ACTION_TYPE.BIND.getValue() == o.getActionType())).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(prodBindRecordExtList)) return MesPcnExtConstWords.ZERO;
        for (MesProdBindRecordExt prodBindRecordExt : prodBindRecordExtList) {
            if (null == prodBindRecordExt) continue;
            prodBindRecordExt.setActionType(MesPcnExtEnumUtil.MES_ACTION_TYPE.UNTYING.getValue());
            ConvertBean.serviceModelUpdate(prodBindRecordExt, userInfo);
            new ConvertBeanExt(prodBindRecordExt).convertBean(prodBindRecordExt);
            prodBindRecordExtRepository.save(prodBindRecordExt);
        }
        return prodBindRecordExtList.size();
    }
}