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

import cn.estsh.i3plus.ext.mes.pcn.api.base.IPartExtService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesPartExt;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.bu.SxPartCategoryModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.MesPartExtRepository;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
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.MesPart;
import cn.estsh.i3plus.pojo.mes.bean.MesPartCategory;
import cn.estsh.i3plus.pojo.mes.repository.MesPartCategoryRepository;
import cn.estsh.i3plus.pojo.mes.repository.MesPartRepository;
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.ArrayList;
import java.util.List;
import java.util.Optional;

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

    @Autowired
    private MesPartExtRepository partExtRepository;

    @Autowired
    private MesPartRepository partRepository;

    @Autowired
    private MesPartCategoryRepository partCategoryRepository;

    @Override
    public MesPartExt getPartExt(String organizeCode, String partNo) {
        if (StringUtils.isEmpty(organizeCode) || StringUtils.isEmpty(partNo) ) {
            return null;
        }
        MesPartExt partExt = partExtRepository.getByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID, MesPcnExtConstWords.PART_NO},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), partNo});
        if (null != partExt) {
            return partExt;
        }
        if (partNo.contains("-") || partNo.contains(")") || partNo.contains("(")) {
            DdlPackBean packBean = DdlPackBean.getDdlPackBean(organizeCode);
            DdlPreparedPack.getStringLikerPack(partNo, MesPcnExtConstWords.PART_NO, packBean);
            List<MesPartExt> partExtList = partExtRepository.findByHqlWhere(packBean);
            if (!CollectionUtils.isEmpty(partExtList)) {
                if (partExtList.size() == 1) {
                    return partExtList.get(0);
                } else {
                    Optional<MesPartExt> partExtOp = partExtList.stream().filter(o -> (null != o && o.getPartNo().length() == partNo.length())).findFirst();
                    if (null != partExtOp && partExtOp.isPresent()) {
                        return partExtOp.get();
                    }
                }
            }
        }
        return null;
    }

    @Override
    public MesPart getPart(String organizeCode, String partNo) {
        if (StringUtils.isEmpty(organizeCode) || StringUtils.isEmpty(partNo) ) {
            return null;
        }
        MesPart part = partRepository.getByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID, MesPcnExtConstWords.PART_NO},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), partNo});
        if (null != part) {
            return part;
        }
        if (partNo.contains("-") || partNo.contains(")") || partNo.contains("(")) {
            DdlPackBean packBean = DdlPackBean.getDdlPackBean(organizeCode);
            DdlPreparedPack.getStringLikerPack(partNo, MesPcnExtConstWords.PART_NO, packBean);
            List<MesPart> partList = partRepository.findByHqlWhere(packBean);
            if (!CollectionUtils.isEmpty(partList)) {
                if (partList.size() == 1) {
                    return partList.get(0);
                } else {
                    Optional<MesPart> partOp = partList.stream().filter(o -> (null != o && o.getPartNo().length() == partNo.length())).findFirst();
                    if (null != partOp && partOp.isPresent()) {
                        return partOp.get();
                    }
                }
            }
        }
        return null;
    }

    @Override
    public List<MesPartExt> getPartExtListByPartNoList(String organizeCode, List<String> partNoList) {
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.getInPackList(partNoList, MesPcnExtConstWords.PART_NO, packBean);
        return partExtRepository.findByHqlWhere(packBean);
    }

    @Override
    public List<SxPartCategoryModel> queryPartCategoryList(String organizeCode) {
        List<SxPartCategoryModel> resultList = null;
        List<MesPartCategory> partCategoryList = partCategoryRepository.findByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue()});
        if (!CollectionUtils.isEmpty(partCategoryList)) {
            for (MesPartCategory partCategory : partCategoryList) {
                if (null == partCategory) {
                    continue;
                }
                SxPartCategoryModel result = new SxPartCategoryModel();
                BeanUtils.copyProperties(partCategory, result);

                if (CollectionUtils.isEmpty(resultList)) {
                    resultList = new ArrayList<>();
                }

                resultList.add(result);
            }
        }
        return resultList;
    }
}
