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

import cn.estsh.i3plus.ext.mes.pcn.api.base.IFourMDetailService;
import cn.estsh.i3plus.ext.mes.pcn.apiservice.dao.ISxCommonDao;
import cn.estsh.i3plus.ext.mes.pcn.apiservice.daoimpl.SxCommonDao;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.*;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.*;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtEnumUtil;
import cn.estsh.i3plus.platform.common.convert.ConvertBean;
import cn.estsh.i3plus.platform.common.exception.ImppExceptionEnum;
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.i3plus.pojo.base.tool.DdlPreparedPack;
import cn.estsh.i3plus.pojo.mes.bean.MesWorkCell;
import cn.estsh.i3plus.pojo.mes.repository.MesWorkCellRepository;
import cn.estsh.i3plus.pojo.mes.sqlpack.MesHqlPack;
import cn.estsh.impp.framework.boot.auth.AuthUtil;
import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author yumingxing
 * @version 1.0
 * @date 2021/2/2 15:19
 **/
@Service
public class FourMDetailService implements IFourMDetailService {

    @Autowired
    private MesFourMDetailRepository fourMDetailRepository;

    @Autowired
    private MesWorkCenterExtRepository workCenterRepository;

    @Autowired
    private MesWorkOrderExtRepository workOrderExtRepository;

    @Autowired
    private MesFourMManageRepository fourMManageRepository;

    @Autowired
    private MesFourMConfirmRepository fourMConfirmRepository;

    @Autowired
    private MesFourMChangeRepository fourMChangeRepository;

    @Autowired
    private MesWorkCellRepository workCellRepository;


    /**
     * 新增
     *
     * @param speParam
     * @return
     */
    @Override
    public MesFourMDetail insert(MesFourMDetail speParam) {
        ConvertBean.modelInitialize(speParam, AuthUtil.getSessionUser());
        return fourMDetailRepository.insert(speParam);
    }

    /**
     * 修改
     *
     * @param speParam
     * @return
     */
    @Override
    public void update(MesFourMDetail speParam) {
        //1、判断单据是否存在
        Long id = speParam.getId();
        MesFourMDetail speParam2 = fourMDetailRepository.getById(id);
        if (ObjectUtils.isEmpty(speParam2)) {
            throwImppException(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode(), ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode(), "数据不存在");
        }

        BeanUtils.copyProperties(speParam, speParam2);
        ConvertBean.serviceModelUpdate(speParam2, AuthUtil.getSessionUser().getUserName());
        fourMDetailRepository.update(speParam2);

    }

    /**
     * 查询
     *
     * @param speParam
     * @param pager
     * @return
     */
    @Override
    public ListPager<MesFourMDetail> queryByPager(MesFourMDetail speParam, Pager pager) {

        String workCenterCode = speParam.getWorkCenterCode();
        String organizeCode = speParam.getOrganizeCode();
        String orderNo = speParam.getOrderNo();
        DdlPackBean packBean = MesHqlPack.getMesAllData(organizeCode);
        DdlPreparedPack.getNumEqualPack(speParam.getIsValid(), "isValid", packBean);
        DdlPreparedPack.getStringEqualPack(workCenterCode, "workCenterCode", packBean);
        DdlPreparedPack.getStringEqualPack(orderNo, "orderNo", packBean);
        pager = PagerHelper.getPager(pager, fourMDetailRepository.findByHqlWhereCount(packBean));
        List<MesFourMDetail> list = fourMDetailRepository.findByHqlWherePage(packBean, pager);

        DdlPackBean workOrderCondition = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.getStringEqualPack(orderNo, "orderNo", workOrderCondition);
        MesWorkOrderExt workOrderExt = workOrderExtRepository.getByProperty(workOrderCondition);
        if (!ObjectUtils.isEmpty(workOrderExt)) {
            for (MesFourMDetail mesFourMDetail : list) {
                mesFourMDetail.setPartNo(workOrderExt.getPartNo());
                mesFourMDetail.setPartNameRdd(workOrderExt.getPartNameRdd());
            }
        }

        return new ListPager(list, pager);
    }

    private MesWorkCenterExt getWorkCenter(String workCenterCode, String organizeCode) {
        return workCenterRepository.getByProperty(
                new String[]{"workCenterCode", "organizeCode", "isValid", "isDeleted"},
                new Object[]{workCenterCode, organizeCode,
                        CommonEnumUtil.IS_VAILD.VAILD.getValue(),
                        CommonEnumUtil.IS_DEAL.NO.getValue()});
    }


    @Override
    public List<MesWorkCell> queryCell(String workCenterCode, String organizeCode) {
        DdlPackBean packBean = MesHqlPack.getMesAllData(organizeCode);
        DdlPreparedPack.getStringEqualPack(workCenterCode, "workCenterCode", packBean);
        return workCellRepository.findByHqlWhere(packBean);
    }


    @Override
    public List<MesWorkOrderExt> queryWorkOrder(String workCenterCode, String organizeCode) {
        DdlPackBean ddlPackBean = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.getNumEqualPack(MesPcnExtEnumUtil.WORK_ORDER_STATUS.OPEN.getValue(), "workOrderStatus", ddlPackBean);
        DdlPreparedPack.getStringEqualPack(workCenterCode, "workCenterCode", ddlPackBean);
        ddlPackBean.setOrderByStr(" order by seq desc ");
        List<MesWorkOrderExt> mesWorkOrderExts = workOrderExtRepository.findByHqlWhere(ddlPackBean);

        MesWorkCenterExt workCenter = getWorkCenter(workCenterCode, organizeCode);
        for (MesWorkOrderExt mesWorkOrderExt : mesWorkOrderExts) {
            mesWorkOrderExt.setShiftCode(workCenter.getShiftCode());
        }
        return mesWorkOrderExts;

    }


    /**
     * 异常
     *
     * @param softTypeCode          softTypeCode
     * @param errorDetailString     错误消息字符串
     * @param imppExceptionEnumCode 异常代码
     * @param args                  参数
     */
    private void throwImppException(String softTypeCode, String errorDetailString, String imppExceptionEnumCode, Object... args) {
        throw ImppExceptionBuilder.newInstance()
                .setSystemID(softTypeCode)
                .setErrorCode(imppExceptionEnumCode)
                .setErrorDetail(errorDetailString, args)
                .build();
    }


    /**
     * 查询
     *
     * @param fourMChange
     * @param pager
     * @return
     */
    @Override
    public ListPager<MesFourMChange> queryByPager(MesFourMChange fourMChange, Pager pager) {
        DdlPackBean packBean = MesHqlPack.getMesAllData(AuthUtil.getOrganizeCode());
        DdlPreparedPack.getStringEqualPack(fourMChange.getWorkCellCode(), "workCellCode", packBean);
        DdlPreparedPack.getNumEqualPack(fourMChange.getIsValid(), "isValid", packBean);
        DdlPreparedPack.getStringEqualPack(fourMChange.getWorkCenterCode(), "workCenterCode", packBean);
        pager = PagerHelper.getPager(pager, fourMChangeRepository.findByHqlWhereCount(packBean));
        return new ListPager(fourMChangeRepository.findByHqlWherePage(packBean, pager), pager);
    }


    /**
     * 查询
     *
     * @param fourMConfirm
     * @param pager
     * @return
     */
    @Override
    public ListPager<MesFourMConfirm> queryByPager(MesFourMConfirm fourMConfirm, Pager pager) {
        DdlPackBean packBean = MesHqlPack.getMesAllData(AuthUtil.getOrganizeCode());
        DdlPreparedPack.getStringEqualPack(fourMConfirm.getConfirmCode(), "confirmCode", packBean);
        DdlPreparedPack.getNumEqualPack(fourMConfirm.getIsValid(), "isValid", packBean);
        DdlPreparedPack.getNumEqualPack(fourMConfirm.getManageType(), "manageType", packBean);
        pager = PagerHelper.getPager(pager, fourMConfirmRepository.findByHqlWhereCount(packBean));
        return new ListPager(fourMConfirmRepository.findByHqlWherePage(packBean, pager), pager);
    }


    /**
     * 查询
     *
     * @param equipmentParam
     * @param pager
     * @return
     */
    @Override
    public ListPager<MesFourMManage> queryByPager(MesFourMManage equipmentParam, Pager pager) {
        DdlPackBean packBean = MesHqlPack.getMesAllData(AuthUtil.getOrganizeCode());
        DdlPreparedPack.getStringEqualPack(equipmentParam.getManageCode(), "manageCode", packBean);
        DdlPreparedPack.getNumEqualPack(equipmentParam.getManageType(), "manageType", packBean);
        DdlPreparedPack.getNumEqualPack(equipmentParam.getIsValid(), "isValid", packBean);
        pager = PagerHelper.getPager(pager, fourMManageRepository.findByHqlWhereCount(packBean));
        return new ListPager(fourMManageRepository.findByHqlWherePage(packBean, pager), pager);
    }
}
