package com.mortals.xhx.module.device.service.impl;
import com.mortals.xhx.module.device.model.DeviceEntity;
import com.mortals.xhx.module.device.service.DeviceService;
import com.mortals.xhx.module.matter.model.MatterDatumEntity;
import com.mortals.xhx.module.matter.model.MatterEntity;
import com.mortals.xhx.module.matter.service.MatterDatumService;
import com.mortals.xhx.module.matter.service.MatterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mortals.framework.service.impl.AbstractCRUDServiceImpl;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.xhx.module.device.dao.DeviceMatterDatumDao;
import com.mortals.xhx.module.device.model.DeviceMatterDatumEntity;
import com.mortals.xhx.module.device.service.DeviceMatterDatumService;
import org.springframework.util.ObjectUtils;

/**
* DeviceMatterDatumService
* 设备事项申请材料业务 service实现
*
* @author zxfei
* @date 2023-02-25
*/
@Service("deviceMatterDatumService")
public class DeviceMatterDatumServiceImpl extends AbstractCRUDServiceImpl<DeviceMatterDatumDao, DeviceMatterDatumEntity, Long> implements DeviceMatterDatumService {

    @Autowired
    private MatterService matterService;
    @Autowired
    private MatterDatumService matterDatumService;
    @Autowired
    private DeviceService deviceService;

    @Override
    protected void saveBefore(DeviceMatterDatumEntity entity, Context context) throws AppException {
        super.saveBefore(entity, context);
        //entity.getMatterId()
        if(!ObjectUtils.isEmpty(entity.getMatterId())){
            MatterEntity matterEntity = matterService.get(entity.getMatterId());
            if(!ObjectUtils.isEmpty(matterEntity)){
                entity.setMatterCode(matterEntity.getMatterNo());
                entity.setMatterName(matterEntity.getMatterName());
                entity.setDeptId(matterEntity.getDeptId());
                entity.setDeptCode(matterEntity.getDeptCode());
                entity.setDeptName(matterEntity.getDeptName());
            }
        }

        if(!ObjectUtils.isEmpty(entity.getMaterialId())){
            MatterDatumEntity matterDatumEntity = matterDatumService.get(entity.getMaterialId());
            if(!ObjectUtils.isEmpty(matterDatumEntity)){
                entity.setMaterialName(matterDatumEntity.getMaterialName());
                entity.setIsRecommend(matterDatumEntity.getIsRecommend());
                entity.setSampleName(matterDatumEntity.getSampleName());
                entity.setSamplePath(matterDatumEntity.getSamplePath());
                entity.setPreViewPath(matterDatumEntity.getPreViewPath());
                entity.setSource(matterDatumEntity.getSource());
            }
        }
        if(!ObjectUtils.isEmpty(entity.getDeviceId())){
            DeviceEntity deviceEntity = deviceService.get(entity.getDeviceId());
            if(!ObjectUtils.isEmpty(deviceEntity)){
                entity.setDeviceCode(deviceEntity.getDeviceCode());
                entity.setDeviceName(deviceEntity.getDeviceName());
            }
        }

    }


    @Override
    protected void updateBefore(DeviceMatterDatumEntity entity, Context context) throws AppException {
        super.updateBefore(entity, context);
    }
}