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

import cn.estsh.i3plus.ext.mes.api.base.IPartAutoLandedService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesPartAutoLanded;
import cn.estsh.i3plus.ext.mes.pojo.model.ExcelPartAutoLanded;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesPartAutoLandedRepository;
import cn.estsh.i3plus.platform.common.convert.ConvertBean;
import cn.estsh.i3plus.platform.common.exception.BaseImppException;
import cn.estsh.i3plus.platform.common.exception.ImppExceptionEnum;
import cn.estsh.i3plus.platform.common.tool.TimeTool;
import cn.estsh.i3plus.platform.common.util.MesConstWords;
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.MesPart;
import cn.estsh.i3plus.pojo.mes.repository.MesPartRepository;
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 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.ObjectUtils;

import java.util.List;

/**
 * @author yumingxing
 * @version 1.0
 * @date 2021/1/27 17:00
 **/
@Service
@Slf4j
public class PartAutoLandedService implements IPartAutoLandedService {


    @Autowired
    private MesPartAutoLandedRepository partAutoLandedRepository;
    @Autowired
    private MesPartRepository partRepository;
    @Override
    public MesPartAutoLanded insert(MesPartAutoLanded mesPartAutoLanded) {
        ConvertBean.modelInitialize(mesPartAutoLanded, AuthUtil.getSessionUser());
        mesPartAutoLanded.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
        return partAutoLandedRepository.insert(mesPartAutoLanded);
    }
    @Override
    public MesPartAutoLanded update(MesPartAutoLanded mesPartAutoLanded) {
        //1、判断单据是否存在
        Long id = mesPartAutoLanded.getId();
        MesPartAutoLanded partAutoLanded = partAutoLandedRepository.getById(id);
        if(ObjectUtils.isEmpty(partAutoLanded)){
            throwImppException(CommonEnumUtil.SOFT_TYPE.MES.getCode(), ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode(), "数据不存在");
        }
        BeanUtils.copyProperties(mesPartAutoLanded,partAutoLanded);
        ConvertBean.serviceModelUpdate(partAutoLanded, AuthUtil.getSessionUser().getUserName());
        return partAutoLandedRepository.save(partAutoLanded);
    }

    @Override
    public ListPager<MesPartAutoLanded> queryByPager(MesPartAutoLanded mesPartAutoLanded, Pager pager) {
        DdlPackBean packBean = MesHqlPack.getMesAllData(AuthUtil.getOrganizeCode());
        DdlPreparedPack.getStringEqualPack(mesPartAutoLanded.getPartNo(),"partNo",packBean);
        DdlPreparedPack.getNumEqualPack(mesPartAutoLanded.getIsValid(),"isValid",packBean);
        DdlPreparedPack.getStringEqualPack(mesPartAutoLanded.getWorkCenterCode(),"workCenterCode",packBean);
        pager = PagerHelper.getPager(pager, partAutoLandedRepository.findByHqlWhereCount(packBean));
        return new ListPager(partAutoLandedRepository.findByHqlWherePage(packBean, pager), pager);
    }

    @Override
    public int updateIsValidById(long id, Integer isValid) {
        //1、判断单据是否存在
        MesPartAutoLanded mesPartAutoLanded = partAutoLandedRepository.getById(id);
        if(ObjectUtils.isEmpty(mesPartAutoLanded)){
            throwImppException(CommonEnumUtil.SOFT_TYPE.MES.getCode(), ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode(), "数据不存在");
        }

        return partAutoLandedRepository.updateByProperties(new String[]{"id"}, new Object[]{id},
                new String[]{MesConstWords.IS_VALID, "modifyUser", "modifyDatetime"},
                new Object[]{isValid, AuthUtil.getSessionUser().getUserName(), TimeTool.getNowTime(true)});
    }

    /**
     * 导入数据
     *
     * @param excelPartAutoLandeds excel部分自动降落
     */
    @Override
    public void importData(List<ExcelPartAutoLanded> excelPartAutoLandeds) {
        if (CollectionUtils.isEmpty(excelPartAutoLandeds)) {
            throw new BaseImppException("导入数据为空");
        }
        for (ExcelPartAutoLanded excelPartAutoLanded : excelPartAutoLandeds) {
            String partNo = excelPartAutoLanded.getPartNo();
            String workCenterCode = excelPartAutoLanded.getWorkCenterCode();
            String organizeCode = AuthUtil.getOrganize().getOrganizeCode();
            MesPart mesPart = partRepository.getByProperty(
                    new String[]{"partNo", "organizeCode", "isValid", "isDeleted"},
                    new Object[]{partNo, organizeCode,
                            CommonEnumUtil.IS_VAILD.VAILD.getValue(),
                            CommonEnumUtil.IS_DEAL.NO.getValue()});
            if (ObjectUtils.isEmpty(mesPart)) {
                throw new BaseImppException("物料不存在");
            }
            MesPartAutoLanded mesPartAutoLanded = new MesPartAutoLanded();
            BeanUtils.copyProperties(excelPartAutoLanded,mesPartAutoLanded);
            mesPartAutoLanded.setPartNameRdd(mesPart.getPartName());

            MesPartAutoLanded mesPartAutoLanded1 = partAutoLandedRepository.getByProperty(
                    new String[]{"partNo", "workCenterCode", "organizeCode", "isValid", "isDeleted"},
                    new Object[]{partNo, workCenterCode, organizeCode,
                            CommonEnumUtil.IS_VAILD.VAILD.getValue(),
                            CommonEnumUtil.IS_DEAL.NO.getValue()});

            if(ObjectUtils.isEmpty(mesPartAutoLanded1)){
                ConvertBean.modelInitialize(mesPartAutoLanded, AuthUtil.getSessionUser());
                partAutoLandedRepository.save(mesPartAutoLanded);
            }else{
                ConvertBean.modelUpdate(mesPartAutoLanded,AuthUtil.getSessionUser());
                partAutoLandedRepository.save(mesPartAutoLanded);
            }
        }
    }


    /**
     * 异常
     * @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();
    }
}
