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

import cn.estsh.i3plus.ext.mes.api.base.bu.IMesProductPrintTypeService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesProductPrintType;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesProductPrintTypeRepository;
import cn.estsh.i3plus.ext.mes.pojo.sqlpack.MesExtHqlPack;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtEnumUtil;
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.auth.AuthUtil;
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.ArrayList;
import java.util.List;

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

    @Autowired
    private MesProductPrintTypeRepository productPrintTypeRepository;

    @Override
    public ListPager<MesProductPrintType> queryProductPrintTypeByPager(MesProductPrintType productPrintType, Pager pager) {
        DdlPackBean hqlPack = MesExtHqlPack.getProductPrintTypeSelectQuery(productPrintType);
        pager = PagerHelper.getPager(pager, productPrintTypeRepository.findByHqlWhereCount(hqlPack));
        hqlPack.setOrderByStr(productPrintType.orderBy());
        List<MesProductPrintType> productPrintTypeList = productPrintTypeRepository.findByHqlWherePage(hqlPack, pager);
        return new ListPager(productPrintTypeList, pager);
    }

    @Override
    public MesProductPrintType insertProductPrintType(MesProductPrintType productPrintType) {

        if (productPrintTypeRepository.isExitByProperty(
                new String[]{MesExtConstWords.ORGANIZE_CODE, MesExtConstWords.PART_NO, MesExtConstWords.PRINT_TYPE, MesExtConstWords.IS_DELETED, MesExtConstWords.IS_VALID},
                new Object[]{AuthUtil.getOrganize().getOrganizeCode(), productPrintType.getPartNo(), productPrintType.getPrintType(), CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue()})) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(cn.estsh.impp.framework.boot.exception.ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode())
                    .setErrorDetail("工厂代码【%s】,物料编码【%s】，打印类型【%s】的配置项已存在",
                            AuthUtil.getOrganize().getOrganizeCode(), productPrintType.getPartNo(), productPrintType.getPrintType())
                    .setErrorSolution("请重新操作！")
                    .build();
        }

        ConvertBean.serviceModelInitialize(productPrintType, productPrintType.getCreateUser());
        return productPrintTypeRepository.insert(productPrintType);
    }

    @Override
    public MesProductPrintType updateProductPrintType(MesProductPrintType productPrintType) {
        MesProductPrintType existed = productPrintTypeRepository.getById(productPrintType.getId());
        if (existed == null) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                    .setErrorDetail("产品打印类型配置记录不存在")
                    .build();
        }

        //唯一性校验
        MesProductPrintType productPrintTypeDb = productPrintTypeRepository.getByProperty(new String[]{MesExtConstWords.ORGANIZE_CODE, MesExtConstWords.PART_NO, MesExtConstWords.PRINT_TYPE, MesExtConstWords.IS_DELETED, MesExtConstWords.IS_VALID},
                new Object[]{AuthUtil.getOrganize().getOrganizeCode(), productPrintType.getPartNo(), productPrintType.getPrintType(), CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue()});

        if (productPrintTypeDb != null && !productPrintTypeDb.getId().equals(existed.getId())) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(cn.estsh.impp.framework.boot.exception.ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode())
                    .setErrorDetail("工厂代码【%s】,物料编码【%s】，打印类型【%s】的配置项已存在",
                            AuthUtil.getOrganize().getOrganizeCode(), productPrintType.getPartNo(), productPrintType.getPrintType())
                    .setErrorSolution("请重新操作！")
                    .build();
        }

        existed.setPrintType(productPrintType.getPrintType());
        existed.setPrintMode(productPrintType.getPrintMode());
        ConvertBean.serviceModelUpdate(existed, productPrintType.getModifyUser());
        return productPrintTypeRepository.save(existed);
    }

    @Override
    public void updateProductPrintTypeValidById(String[] ids, Integer status, String organizeCode, String userName) {

        if (status != CommonEnumUtil.IS_VAILD.INVAILD.getValue() && status != CommonEnumUtil.IS_VAILD.VAILD.getValue()) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                    .setErrorDetail("启用，禁用枚举状态值[%s]未被定义", status)
                    .build();
        }

        List<MesProductPrintType> MesProductPrintTypeList = new ArrayList<>();

        for (String id : ids) {
            if (StringUtils.isEmpty(id)) continue;

            MesProductPrintType productPrintTypeExisted = productPrintTypeRepository.getById(Long.parseLong(id));

            if (productPrintTypeExisted == null || CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue() == productPrintTypeExisted.getIsDeleted()) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                        .setErrorDetail("ID[%s]记录不存在", id)
                        .build();
            }

            //唯一性校验
            MesProductPrintType productPrintTypeDb = productPrintTypeRepository.getByProperty(new String[]{MesExtConstWords.ORGANIZE_CODE, MesExtConstWords.PART_NO, MesExtConstWords.PRINT_TYPE, MesExtConstWords.IS_DELETED, MesExtConstWords.IS_VALID},
                    new Object[]{AuthUtil.getOrganize().getOrganizeCode(), productPrintTypeExisted.getPartNo(), productPrintTypeExisted.getPrintType(), CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue()});

            if (productPrintTypeDb != null && status == MesExtEnumUtil.IS_VAILD.VAILD.getValue()) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                        .setErrorCode(cn.estsh.impp.framework.boot.exception.ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode())
                        .setErrorDetail("工厂代码【%s】,物料编码【%s】，打印类型【%s】的配置项已存在",
                                AuthUtil.getOrganize().getOrganizeCode(), productPrintTypeExisted.getPartNo(), productPrintTypeExisted.getPrintType())
                        .setErrorSolution("请重新操作！")
                        .build();
            }

            productPrintTypeExisted.setIsValid(status);
            ConvertBean.serviceModelUpdate(productPrintTypeExisted, userName);
            MesProductPrintTypeList.add(productPrintTypeExisted);
        }
        if(!CollectionUtils.isEmpty(MesProductPrintTypeList)) {
            productPrintTypeRepository.saveAll(MesProductPrintTypeList);
        }
    }

}
