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

import cn.estsh.i3plus.ext.mes.api.base.IPlantInfoService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesPlantInfo;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesPlantInfoRepository;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
import cn.estsh.i3plus.platform.common.convert.ConvertBean;
import cn.estsh.i3plus.pojo.base.bean.DdlPackBean;
import cn.estsh.impp.framework.boot.auth.AuthUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: xiangming.liao
 * @CreateDate: 2021/02/07 15:46
 * @Description:
 **/
@Service
@Slf4j
public class PlantInfoService implements IPlantInfoService {
    @Autowired
    private MesPlantInfoRepository plantInfoRDao;

    @Override
    public void updatePlantInfo(MesPlantInfo plantInfo) {

        if(plantInfo.getId() == null || plantInfo.getId() == 0){
            ConvertBean.serviceModelInitialize(plantInfo, plantInfo.getOrganizeCode());
            log.info("新增工厂信息plantInfoRDaoById:{}", plantInfo);
            plantInfoRDao.insert(plantInfo);
        }else{
            MesPlantInfo plantInfoRDaoById = plantInfoRDao.getById(plantInfo.getId());
            log.info("更新工厂信息plantInfoRDaoById:{}", plantInfoRDaoById);

            if (null == plantInfoRDaoById) {
                ConvertBean.serviceModelInitialize(plantInfo, plantInfo.getOrganizeCode());
                log.info("新增工厂信息plantInfoRDaoById:{}", plantInfo);
                plantInfoRDao.insert(plantInfo);
            }else{
                BeanUtils.copyProperties(plantInfo, plantInfoRDaoById, MesExtConstWords.ID);
                ConvertBean.saveOrUpdate(plantInfo, AuthUtil.getSessionUser().getUserName());
                log.info("修改工厂信息plantInfo:{}", plantInfoRDaoById);
                plantInfoRDao.save(plantInfo);
            }
        }

    }


    @Override
    public List<MesPlantInfo> queryPlantInfo(String organizeCode) {
        DdlPackBean packBean=DdlPackBean.getDdlPackBean(organizeCode);
        return plantInfoRDao.findByHqlWhere(packBean);
    }
}
