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

import cn.estsh.i3plus.ext.mes.pcn.api.base.jx.IJxBadnessTypeService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesBadnessType;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.MesBadnessTypeRepository;
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.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.impp.framework.boot.exception.ImppExceptionBuilder;
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.stream.Collectors;

/**
 * @author yumingxing
 * @version 1.0
 * @date 2021/2/5 15:42
 **/
@Service
public class JxBadnessTypeService implements IJxBadnessTypeService {

    @Autowired
    private MesBadnessTypeRepository badnessTypeRepository;

    @Override
    public ListPager<MesBadnessType> queryBadnessTypePager(MesBadnessType badnessType, Pager pager) {
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(badnessType.getOrganizeCode());
        DdlPreparedPack.getStringEqualPack(badnessType.getBadnessTypeName(), MesPcnExtConstWords.BADNESS_TYPE_NAME, packBean);
        DdlPreparedPack.getStringEqualPack(badnessType.getBadnessTypeCode(), MesPcnExtConstWords.BADNESS_TYPE_CODE, packBean);
        pager = PagerHelper.getPager(pager, badnessTypeRepository.findByHqlWhereCount(packBean));
        packBean.setOrderByStr(" order by modifyDatetime desc ");
        List<MesBadnessType> qmsBadnessTypes = badnessTypeRepository.findByHqlWherePage(packBean, pager);
        return new ListPager(qmsBadnessTypes , pager);
    }

    @Override
    public void insertBadnessType(MesBadnessType sxQmsBadnessType, String userName) {
        MesBadnessType sxQmsBadnessTypeDb = badnessTypeRepository.getByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.BADNESS_TYPE_CODE},
                new Object[]{sxQmsBadnessType.getOrganizeCode(), CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), sxQmsBadnessType.getBadnessTypeCode()});

        if (null != sxQmsBadnessTypeDb) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.QMS.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode())
                    .setErrorDetail("根据[不良类型编码]查询已经存在此条数据,请勿重复新增")
                    .build();
        }

        ConvertBean.serviceModelInitialize(sxQmsBadnessType, userName);
        badnessTypeRepository.insert(sxQmsBadnessType);
    }

    @Override
    public void updateBadnessType(MesBadnessType sxQmsBadnessType, String userName) {
        MesBadnessType badnessTypeDb = badnessTypeRepository.getById(sxQmsBadnessType.getId());
        if (null == badnessTypeDb || CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue() == badnessTypeDb.getIsDeleted()) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.QMS.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                    .setErrorDetail("ID[%s]数据不存在", sxQmsBadnessType.getId())
                    .build();
        }

        MesBadnessType badnessTypeUpdateDb = badnessTypeRepository.getByProperty(
                    new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.BADNESS_TYPE_CODE, MesPcnExtConstWords.BADNESS_TYPE_NAME, MesPcnExtConstWords.COLOR},
                    new Object[]{sxQmsBadnessType.getOrganizeCode(), CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), sxQmsBadnessType.getBadnessTypeCode(),sxQmsBadnessType.getBadnessTypeName(),sxQmsBadnessType.getColor()});

        if (null != badnessTypeUpdateDb && badnessTypeUpdateDb.getId().compareTo(sxQmsBadnessType.getId()) != 0) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.QMS.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode())
                    .setErrorDetail("根据[不良类型代码,不良类型名称,颜色]查询已经存在此条数据,修改失败")
                    .build();
        }

        badnessTypeDb.setBadnessTypeCode(sxQmsBadnessType.getBadnessTypeCode());
        badnessTypeDb.setBadnessTypeName(sxQmsBadnessType.getBadnessTypeName());
        badnessTypeDb.setColor(sxQmsBadnessType.getColor());
        badnessTypeDb.setOrganizeCode(sxQmsBadnessType.getOrganizeCode());
        ConvertBean.serviceModelUpdate(badnessTypeDb, userName);
        badnessTypeRepository.save(badnessTypeDb);
    }

    @Override
    public void deleteBadnessType(Long ids[], String userName) {
        badnessTypeRepository.deleteWeaklyByIds(ids, userName);
    }

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

    @Override
    public String getBadnessTypeName(String organizeCode, String badnessTypeCode) {
        MesBadnessType buBadnessType = getBadnessType(organizeCode, badnessTypeCode);
        return null != buBadnessType ? buBadnessType.getBadnessTypeName() : null;
    }

    @Override
    public List<MesBadnessType> getBadnessTypeList(String organizeCode) {
        if (StringUtils.isEmpty(organizeCode)) {
            return null;
        }
        return badnessTypeRepository.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<MesBadnessType>> getBadnessTypeMap(String organizeCode) {
        List<MesBadnessType> badnessTypeList = getBadnessTypeList(organizeCode);
        return CollectionUtils.isEmpty(badnessTypeList) ? null :
                badnessTypeList.stream().filter(o -> null != o).collect(Collectors.groupingBy(MesBadnessType::getBadnessTypeCode));
    }
}
