package com.iformall.service.invest.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.iformall.domain.po.invest.InvestMessageEntity;
import com.iformall.domain.vo.invest.InvestUserContext;
import com.iformall.mapper.InvestMessageDao;
import com.iformall.service.invest.InvestHelper;
import com.iformall.service.invest.InvestMessageService;
import org.springframework.stereotype.Service;


@Service
public class InvestMessageServiceImpl extends InvestBaseServiceImpl<InvestMessageDao, InvestMessageEntity> implements InvestMessageService {

	@Override
	public boolean save(InvestMessageEntity entity) {
		return super.saveAction(entity, this::checkBeforeSaveOrUpdate);
	}

	@Override
	public boolean update(InvestMessageEntity entity, Wrapper<InvestMessageEntity> updateWrapper) {
		return super.updateActon(entity, updateWrapper, this::checkBeforeSaveOrUpdate);
	}

	@Override
	public boolean updateById(InvestMessageEntity entity) {
		return super.updateByIdAction(entity, this::checkBeforeSaveOrUpdate);
	}

	private void checkBeforeSaveOrUpdate(InvestMessageEntity entity) {
		InvestHelper.notNull(entity, entity.getMessage(), entity.getOwner());
	}

	@Deprecated
	@Override
	public boolean saveOrUpdate(InvestMessageEntity entity) {
		notSupport();
		return false;
	}

	@Deprecated
	@Override
	public boolean update(Wrapper<InvestMessageEntity> updateWrapper) {
		notSupport();
		return false;
	}

    @Override
    public boolean removeAll() {
	    InvestMessageEntity entity = new InvestMessageEntity();
	    entity.setOwner(InvestUserContext.getUserId());
	    LambdaUpdateWrapper<InvestMessageEntity> wrapper = new LambdaUpdateWrapper<>();
	    wrapper.eq(InvestMessageEntity::getTenantId, InvestUserContext.getUser().getTenantId());
	    wrapper.eq(InvestMessageEntity::getOwner, InvestUserContext.getUserId());
	    wrapper.set(InvestMessageEntity::getDeleted, 1);
	    return super.update(null, wrapper);
    }
}
