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

import cn.estsh.i3plus.ext.mes.pcn.api.busi.IWorkOrderLogExtService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesWorkOrderExt;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesWorkOrderLogExt;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.MesWorkOrderLogExtRepository;
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.MesWorkOrderLog;
import cn.estsh.i3plus.pojo.mes.repository.MesWorkOrderLogRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
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.stream.Collectors;

/**
 * @author wangjie
 * @version 1.0
 * @date 2021/1/13 9:28
 **/
@Slf4j
@Service
public class WorkOrderLogExtService implements IWorkOrderLogExtService {

    @Autowired
    private MesWorkOrderLogExtRepository workOrderLogExtRepository;

    @Autowired
    private MesWorkOrderLogRepository workOrderLogRepository;

    @Override
    public MesWorkOrderLogExt insertMesWorkOrderLogExt(MesWorkOrderExt workOrderExt) {
        MesWorkOrderLogExt workOrderLogExt = new MesWorkOrderLogExt();
        BeanUtils.copyProperties(workOrderExt, workOrderLogExt, MesPcnExtConstWords.ID);
        workOrderLogExt.setSystemSyncStatusExt(MesPcnExtEnumUtil.IF_SYNC_STATUS.NO_SYNC.getValue());
        ConvertBean.serviceModelInitialize(workOrderLogExt, workOrderExt.getModifyUser());
        new ConvertBeanExt(workOrderLogExt).convertBean(workOrderLogExt);
        return workOrderLogExtRepository.insert(workOrderLogExt);
    }

    @Override
    public List<MesWorkOrderLogExt> getWorkOrderLogExtList(String organizeCode, String orderNo) {
        if (StringUtils.isEmpty(organizeCode) || StringUtils.isEmpty(orderNo) ) {
            return null;
        }
        return workOrderLogExtRepository.findByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID, MesPcnExtConstWords.ORDER_NO},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), orderNo});
    }

    @Override
    public List<MesWorkOrderLog> getWorkOrderLogList(String organizeCode, String orderNo) {
        if (StringUtils.isEmpty(organizeCode) || StringUtils.isEmpty(orderNo) ) {
            return null;
        }
        return workOrderLogRepository.findByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID, MesPcnExtConstWords.ORDER_NO},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), orderNo});
    }

    @Override
    public void deleteWorkOrderLogExt(String organizeCode, List<Integer> workOrderStatusList, String modifyDatetime) {
        for (Integer workOrderStatus : workOrderStatusList) {
            DdlPackBean packBean = DdlPackBean.getDdlPackBean(organizeCode);
            DdlPreparedPack.getNumEqualPack( workOrderStatus, MesPcnExtConstWords.WORK_ORDER_STATUS, packBean);
            DdlPreparedPack.getStringSmallerPack(modifyDatetime, MesPcnExtConstWords.MODIFY_DATE_TIME, packBean);
            List<MesWorkOrderLogExt> mesWorkOrderLogExts = workOrderLogExtRepository.findByHqlWhere(packBean);

            List<String> workOrderNoList = CollectionUtils.isEmpty(mesWorkOrderLogExts) ? null : mesWorkOrderLogExts.stream().map(MesWorkOrderLogExt::getOrderNo).distinct().collect(Collectors.toList());

            if(!CollectionUtils.isEmpty(workOrderNoList)){
                for (String workOrderNo : workOrderNoList) {
                    workOrderLogExtRepository.deleteByProperties(new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.ORDER_NO}, new Object[]{ organizeCode, workOrderNo });
                }
            }
            //
            //if(!CollectionUtils.isEmpty(mesWorkOrderLogExts)) workOrderLogExtRepository.deleteAll(mesWorkOrderLogExts);
        }
    }
}
