package cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.busi.ep;

import cn.estsh.i3plus.ext.mes.pcn.api.busi.ep.ISxWorkCenterMovtrnCfgService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesWorkCenterMovtrnCfg;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.ep.WorkCenterMovtrnCfgModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.MesWorkCenterMovtrnCfgRepository;
import cn.estsh.i3plus.ext.mes.pcn.pojo.sqlpack.MesPcnExtHqlPack;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
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.impp.framework.boot.exception.ImppExceptionBuilder;
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;

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

    @Autowired
    private MesWorkCenterMovtrnCfgRepository workCenterMovtrnCfgRepository;

    @Override
    public MesWorkCenterMovtrnCfg getWorkCenterMovtrnCfgByCenter(String organizeCode, String workCenterCode) {
        if (StringUtils.isEmpty(organizeCode) || StringUtils.isEmpty(workCenterCode)) {
            return null;
        }
        return workCenterMovtrnCfgRepository.getByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID, MesPcnExtConstWords.WORK_CENTER_CODE},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), workCenterCode});
    }

    @Override
    public ListPager<WorkCenterMovtrnCfgModel> queryWorkCenterMovtrnCfgByPager(MesWorkCenterMovtrnCfg workCenterMovtrnCfg, Pager pager) {
        DdlPackBean hqlPack = MesPcnExtHqlPack.getWorkCenterMovtrnCfg(workCenterMovtrnCfg);
        pager = PagerHelper.getPager(pager, workCenterMovtrnCfgRepository.findByHqlWhereCount(hqlPack));
        hqlPack.setOrderByStr(workCenterMovtrnCfg.orderBy());
        List<MesWorkCenterMovtrnCfg> workCenterMovtrnCfgList = workCenterMovtrnCfgRepository.findByHqlWherePage(hqlPack, pager);
        List<WorkCenterMovtrnCfgModel> resultList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(workCenterMovtrnCfgList)) {
            for (MesWorkCenterMovtrnCfg movtrnCfg : workCenterMovtrnCfgList) {
                WorkCenterMovtrnCfgModel result = new WorkCenterMovtrnCfgModel();
                BeanUtils.copyProperties(movtrnCfg, result);
                result.setIdStr(movtrnCfg.getId().toString());
                result.setStatusName(MesPcnExtEnumUtil.WORK_CENTER_MOVTRN_STATUS.valueOfDescription(movtrnCfg.getStatus()));
                resultList.add(result);
            }
        }
        return new ListPager(resultList, pager);
    }

    @Override
    public synchronized void saveWorkCenterMovtrnCfg(List<WorkCenterMovtrnCfgModel> workCenterMovtrnCfgList, Integer flag) {
        if (CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue() == flag) saveWorkCenterMovtrnCfgSingle(workCenterMovtrnCfgList.get(0));
        if (CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue() == flag) saveWorkCenterMovtrnCfgBatch(workCenterMovtrnCfgList);
    }

    private void saveWorkCenterMovtrnCfgSingle(WorkCenterMovtrnCfgModel movtrnCfg) {
        if (StringUtils.isEmpty(movtrnCfg.getIdStr())) insertWorkCenterMovtrnCfg(movtrnCfg);
        if (!StringUtils.isEmpty(movtrnCfg.getIdStr())) updateWorkCenterMovtrnCfg(movtrnCfg);
    }

    private void insertWorkCenterMovtrnCfg(WorkCenterMovtrnCfgModel movtrnCfg) {
        MesWorkCenterMovtrnCfg movtrnCfgDb = getWorkCenterMovtrnCfgByCenter(movtrnCfg.getOrganizeCode(), movtrnCfg.getWorkCenterCode());
        if (null != movtrnCfgDb) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode())
                    .setErrorDetail("生产线[%s]已经存在立库配置,不能重复新增", movtrnCfg.getWorkCenterCode())
                    .build();
        }

        insertWorkCenterMovtrnCfgSingle(movtrnCfg);

    }

    private void insertWorkCenterMovtrnCfgSingle(WorkCenterMovtrnCfgModel movtrnCfg) {
        MesWorkCenterMovtrnCfg movtrnCfgDb = new MesWorkCenterMovtrnCfg();
        movtrnCfgDb.setWorkCenterCode(movtrnCfg.getWorkCenterCode());
        movtrnCfgDb.setStatus(movtrnCfg.getStatus());
        movtrnCfgDb.setOrganizeCode(movtrnCfg.getOrganizeCode());
        ConvertBean.serviceModelInitialize(movtrnCfgDb, movtrnCfg.getModifyUser());
        workCenterMovtrnCfgRepository.insert(movtrnCfgDb);
    }

    private void updateWorkCenterMovtrnCfg(WorkCenterMovtrnCfgModel movtrnCfg) {
        MesWorkCenterMovtrnCfg movtrnCfgDb = workCenterMovtrnCfgRepository.getById(Long.parseLong(movtrnCfg.getIdStr()));
        if (null == movtrnCfgDb || CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue() == movtrnCfgDb.getIsDeleted()) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                    .setErrorDetail("生产线[%s]不存在立库配置,操作失败", movtrnCfg.getWorkCenterCode())
                    .build();
        }

        updateWorkCenterMovtrnCfgSingle(movtrnCfgDb, movtrnCfg);
    }

    private void updateWorkCenterMovtrnCfgSingle(MesWorkCenterMovtrnCfg movtrnCfgDb, WorkCenterMovtrnCfgModel movtrnCfg) {
        if (!StringUtils.isEmpty(movtrnCfg.getIsDeleted()) && CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue() == movtrnCfg.getIsDeleted()) {
            movtrnCfgDb.setIsDeleted(CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue());
        } else {
            movtrnCfgDb.setStatus(movtrnCfg.getStatus());
        }
        ConvertBean.serviceModelUpdate(movtrnCfgDb, movtrnCfg.getModifyUser());
        workCenterMovtrnCfgRepository.save(movtrnCfgDb);
    }

    private void saveWorkCenterMovtrnCfgBatch(List<WorkCenterMovtrnCfgModel> workCenterMovtrnCfgList) {
        for (WorkCenterMovtrnCfgModel movtrnCfg : workCenterMovtrnCfgList) {
            if (null == movtrnCfg) continue;

            MesWorkCenterMovtrnCfg movtrnCfgDb = getWorkCenterMovtrnCfgByCenter(movtrnCfg.getOrganizeCode(), movtrnCfg.getWorkCenterCode());
            if (null == movtrnCfgDb) insertWorkCenterMovtrnCfgSingle(movtrnCfg);
            if (null != movtrnCfgDb) updateWorkCenterMovtrnCfgSingle(movtrnCfgDb, movtrnCfg);
        }
    }

    @Override
    public WorkCenterMovtrnCfgModel queryWorkCenterMovtrnCfgByWorkCenterCode(String organizeCode, String workCenterCode) {
        WorkCenterMovtrnCfgModel result = null;
        MesWorkCenterMovtrnCfg workCenterMovtrnCfgDb = getWorkCenterMovtrnCfgByCenter(organizeCode, workCenterCode);
        if (null != workCenterMovtrnCfgDb) {
            result = new WorkCenterMovtrnCfgModel();
            BeanUtils.copyProperties(workCenterMovtrnCfgDb, result);
            result.setIdStr(workCenterMovtrnCfgDb.getId().toString());
            result.setStatusName(MesPcnExtEnumUtil.WORK_CENTER_MOVTRN_STATUS.valueOfDescription(workCenterMovtrnCfgDb.getStatus()));
        }
        return result;
    }

}
