//package cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.busi.bu;
//
//import cn.estsh.i3plus.ext.mes.pcn.api.busi.IWorkOrderProcessBomService;
//import cn.estsh.i3plus.ext.mes.pcn.api.busi.bu.IBuWorkOrderProcessBomService;
//import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesProcessBom;
//import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesWorkOrderProcessBom;
//import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.MesProcessBomRepository;
//import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.MesWorkOrderProcessBomRepository;
//import cn.estsh.i3plus.ext.mes.pcn.pojo.sqlpack.MesPcnExtHqlPack;
//import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
//import cn.estsh.i3plus.platform.common.convert.ConvertBean;
//import cn.estsh.i3plus.platform.common.exception.ImppExceptionEnum;
//import cn.estsh.i3plus.platform.common.tool.TimeTool;
//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 cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder;
//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.StringJoiner;
//import java.util.stream.Collectors;
//
///**
// * @author wangjie
// * @version 1.0
// * @date 2021/1/13 9:28
// **/
//@Slf4j
//@Service
//public class BuWorkOrderProcessBomService implements IBuWorkOrderProcessBomService {
//
//    @Autowired
//    private MesWorkOrderProcessBomRepository workOrderProcessBomRepository;
//
//    @Autowired
//    private MesProcessBomRepository processBomRepository;
//
//    @Override
//    public ListPager queryWorkOrderProcessBomByPager(MesWorkOrderProcessBom workOrderProcessBom, Pager pager) {
//        DdlPackBean hqlPack = MesPcnExtHqlPack.getWorkOrderProcessBom(workOrderProcessBom);
//        pager = PagerHelper.getPager(pager, workOrderProcessBomRepository.findByHqlWhereCount(hqlPack));
//        hqlPack.setOrderByStr(workOrderProcessBom.orderBy());
//        return new ListPager(workOrderProcessBomRepository.findByHqlWherePage(hqlPack, pager), pager);
//    }
//
//    @Override
//    public MesWorkOrderProcessBom insertWorkOrderProcessBom(MesWorkOrderProcessBom workOrderProcessBom) {
//        MesWorkOrderProcessBom workOrderProcessBomDb = workOrderProcessBomRepository.getByProperty(
//                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED,
//                        MesPcnExtConstWords.WORK_ORDER_NO, MesPcnExtConstWords.PROCESS_CODE, MesPcnExtConstWords.ITEM_PART_NO},
//                new Object[]{workOrderProcessBom.getOrganizeCode(), CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(),
//                        workOrderProcessBom.getWorkOrderNo(), workOrderProcessBom.getProcessCode(), workOrderProcessBom.getItemPartNo()});
//        if (null != workOrderProcessBomDb) {
//            throw ImppExceptionBuilder.newInstance()
//                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
//                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode())
//                    .setErrorDetail("生产工单[%s]工序代码[%s]原材料物料编码[%s]记录已存在",
//                            workOrderProcessBom.getWorkOrderNo(), workOrderProcessBom.getProcessCode(), workOrderProcessBom.getItemPartNo())
//                    .build();
//        }
//
//        ConvertBean.serviceModelInitialize(workOrderProcessBom, workOrderProcessBom.getModifyUser());
//        return workOrderProcessBomRepository.insert(workOrderProcessBom);
//    }
//
//    @Override
//    public MesWorkOrderProcessBom updateWorkOrderProcessBom(MesWorkOrderProcessBom workOrderProcessBom) {
//        MesWorkOrderProcessBom workOrderProcessBomDb = workOrderProcessBomRepository.getById(workOrderProcessBom.getId());
//        if (null == workOrderProcessBomDb) {
//            throw ImppExceptionBuilder.newInstance()
//                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
//                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
//                    .setErrorDetail("ID[%s]记录不存在", workOrderProcessBom.getId())
//                    .build();
//        }
//        if (CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue() == workOrderProcessBomDb.getIsDeleted() ||
//                CommonEnumUtil.IS_VAILD.INVAILD.getValue() == workOrderProcessBomDb.getIsValid()) {
//            throw ImppExceptionBuilder.newInstance()
//                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
//                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
//                    .setErrorDetail("ID[%s]记录无效", workOrderProcessBom.getId())
//                    .build();
//        }
//        workOrderProcessBomDb.setQty(workOrderProcessBom.getQty());
//        ConvertBean.serviceModelUpdate(workOrderProcessBomDb, workOrderProcessBom.getModifyUser());
//        return workOrderProcessBomRepository.save(workOrderProcessBomDb);
//    }
//
//    @Override
//    public int updateWorkOrderProcessBomValidById(Long id, Integer status, String modifyUser) {
//        if (status != CommonEnumUtil.IS_VAILD.INVAILD.getValue() && status != CommonEnumUtil.IS_VAILD.VAILD.getValue()) {
//            throw ImppExceptionBuilder.newInstance()
//                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
//                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
//                    .setErrorDetail("启用，禁用枚举状态值【%s】未被定义", status)
//                    .build();
//        }
//        return workOrderProcessBomRepository.updateByProperties(new String[]{MesPcnExtConstWords.ID}, new Object[]{id},
//                new String[]{MesPcnExtConstWords.IS_VALID, MesPcnExtConstWords.MODIFY_USER, MesPcnExtConstWords.MODIFY_DATE_TIME},
//                new Object[]{status, modifyUser, TimeTool.getNowTime(true)});
//    }
//
//    @Override
//    public List<MesWorkOrderProcessBom> getWorkOrderProcessBomList(String organizeCode, String workOrderNo, String processCode, String partNo) {
//        if (StringUtils.isEmpty(organizeCode) || StringUtils.isEmpty(workOrderNo) || StringUtils.isEmpty(processCode) || StringUtils.isEmpty(partNo)) {
//            return null;
//        }
//        return workOrderProcessBomRepository.findByProperty(
//                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID,
//                        MesPcnExtConstWords.WORK_ORDER_NO, MesPcnExtConstWords.PROCESS_CODE, MesPcnExtConstWords.PART_NO},
//                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(),
//                        workOrderNo, processCode, partNo});
//    }
//
//    @Override
//    public ListPager queryProcessBomByPager(MesProcessBom processBom, Pager pager, String workOrderNo, String processCode, String partNo) {
//        DdlPackBean hqlPack = MesPcnExtHqlPack.getProcessBom(processBom);
//        pager = PagerHelper.getPager(pager, processBomRepository.findByHqlWhereCount(hqlPack));
//        List<MesWorkOrderProcessBom> workOrderProcessBomList = getWorkOrderProcessBomList(processBom.getOrganizeCode(), workOrderNo, processCode, partNo);
//        CollectionUtils.isEmpty(workOrderProcessBomList) ? null : workOrderProcessBomList.stream()
//                .collect(Collectors.groupingBy(workOrderProcessBom ->
//                        new StringJoiner(MesPcnExtConstWords.AND).add(workOrderProcessBom.getProcessCode()).add(workOrderProcessBom.get) ));
//        processBomRepository.findByHqlWhere(hqlPack);
//        hqlPack.setOrderByStr(" order by seq desc ");
//        return new ListPager(processBomRepository.findByHqlWherePage(hqlPack, pager), pager);
//    }
//}
