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

import cn.estsh.i3plus.ext.mes.api.base.bu.IPartDataExtService;
import cn.estsh.i3plus.ext.mes.pojo.model.bu.PartCategoryModel;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
import cn.estsh.i3plus.mes.api.iservice.base.IPartCategoryService;
import cn.estsh.i3plus.mes.api.iservice.base.IPartDataService;
import cn.estsh.i3plus.mes.apiservice.serviceimpl.base.PartDataService;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.mes.bean.MesPart;
import cn.estsh.i3plus.pojo.mes.bean.MesPartCategory;
import cn.estsh.i3plus.pojo.mes.bean.MesProduceCategory;
import cn.estsh.i3plus.pojo.mes.repository.MesPartCategoryRepository;
import cn.estsh.i3plus.pojo.mes.repository.MesPartRepository;
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;

/**
 * @PROJECT_NAME: i3plus-mes-panasonic-bu
 * @DESCRIPTION:
 * @USER: xinwang.yi
 * @DATE: 2022-06-06 13:29
 */
@Service
public class PartDataExtService implements IPartDataExtService{

    @Autowired
    private IPartDataService partDataService;

    @Autowired
    private MesPartRepository partRepository;

    @Autowired
    private MesPartCategoryRepository partCategoryRepository;

    public List<PartCategoryModel> queryMesPartCategoryByProperties(MesPartCategory partCategory) {
        List<MesPartCategory> mesPartCategories = partDataService.queryMesPartCategoryByProperties(partCategory);
        List<PartCategoryModel> partCategoryModels = new ArrayList<>();
        mesPartCategories.stream().forEach(mesPartCategory -> {
            PartCategoryModel partCategoryModel = new PartCategoryModel();
            BeanUtils.copyProperties( mesPartCategory, partCategoryModel);
            partCategoryModels.add(partCategoryModel);
        });
        return partCategoryModels;
    }

    @Override
    public List<PartCategoryModel> queryMesProduceCategoryAll(String organizeCode) {
        List<MesPartCategory> partCategoryList = partCategoryRepository.findByProperty(new String[]{"organizeCode", "isDeleted", "isValid"}, new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue()});
        List<PartCategoryModel> partCategoryModels = new ArrayList<>();
        partCategoryList.stream().forEach(mesPartCategory -> {
            PartCategoryModel partCategoryModel = new PartCategoryModel();
            BeanUtils.copyProperties( mesPartCategory, partCategoryModel);
            partCategoryModels.add(partCategoryModel);
        });
        return partCategoryModels;
    }

    @Override
    public MesPart getMesPart(String organizeCode, String partNo) {
        return partRepository.getByProperty(
                new String[]{MesExtConstWords.ORGANIZE_CODE, MesExtConstWords.IS_DELETED, MesExtConstWords.IS_VALID, MesExtConstWords.PART_NO},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), partNo});
    }
}
