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

import cn.estsh.i3plus.ext.mes.pcn.api.base.jx.IJxBadnessClassifyService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesBadnessClassify;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.jx.JxBadnessClassifyModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.MesBadnessClassifyRepository;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.MesBadnessTypeRepository;
import cn.estsh.i3plus.ext.mes.pcn.pojo.sqlpack.MesPcnExtHqlPack;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.pojo.base.bean.DdlPackBean;
import cn.estsh.i3plus.pojo.base.bean.ListPager;
import cn.estsh.i3plus.pojo.base.common.Pager;
import cn.estsh.i3plus.pojo.base.common.PagerHelper;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
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.Map;
import java.util.StringJoiner;
import java.util.stream.Collectors;

/**
 * @PROJECT_NAME: i3plus-qms-panasonic
 * @DESCRIPTION:
 * @USER: xinwang.yi
 * @DATE: 2022-06-07 10:45
 */
@Service
public class JxBadnessClassifyService implements IJxBadnessClassifyService {

    @Autowired
    private MesBadnessClassifyRepository badnessClassifyRepository;

    @Override
    public ListPager<JxBadnessClassifyModel> queryBuBadnessClassifyPager(MesBadnessClassify badnessClassify, Pager pager) {
        DdlPackBean packBean = MesPcnExtHqlPack.getSxBadnessClassify(badnessClassify);
        pager = PagerHelper.getPager(pager, badnessClassifyRepository.findByHqlWhereCount(packBean));
        packBean.setOrderByStr(" order by modifyDatetime desc ");
        List<MesBadnessClassify> badnessClassifies = badnessClassifyRepository.findByHqlWherePage(packBean, pager);
        return new ListPager(badnessClassifies, pager);
    }

    @Override
    public MesBadnessClassify getBadnessClassify(String organizeCode, String badnessTypeCode, String badnessClassifyCode) {
        if (StringUtils.isEmpty(organizeCode) || StringUtils.isEmpty(badnessTypeCode) || StringUtils.isEmpty(badnessClassifyCode)) {
            return null;
        }
        return badnessClassifyRepository.getByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID, MesPcnExtConstWords.BADNESS_TYPE_CODE, MesPcnExtConstWords.BADNESS_CLASSIFY_CODE},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), badnessTypeCode, badnessClassifyCode});
    }

    @Override
    public String getBadnessClassifyName(String organizeCode, String badnessTypeCode, String badnessClassifyCode) {
        MesBadnessClassify buBadnessClassify = getBadnessClassify(organizeCode, badnessTypeCode, badnessClassifyCode);
        return null != buBadnessClassify ? buBadnessClassify.getBadnessClassifyName() : null;
    }

    @Override
    public List<MesBadnessClassify> getBadnessClassifyList(String organizeCode) {
        if (StringUtils.isEmpty(organizeCode)) {
            return null;
        }
        return badnessClassifyRepository.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()});
    }

    @Override
    public Map<String, List<MesBadnessClassify>> getBadnessClassifyMap(String organizeCode) {
        List<MesBadnessClassify> badnessClassifyList = getBadnessClassifyList(organizeCode);
        return CollectionUtils.isEmpty(badnessClassifyList) ? null :
                badnessClassifyList.stream().filter(o -> null != o).collect(Collectors.groupingBy(o -> new StringJoiner(MesPcnExtConstWords.AND).add(o.getBadnessTypeCode()).add(o.getBadnessClassifyCode()).toString()));
    }

}
