package com.iformall.service.invest.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.additional.query.impl.LambdaQueryChainWrapper;
import com.iformall.domain.po.invest.InvestRemindEntity;
import com.iformall.domain.vo.invest.InvestUserContext;
import com.iformall.mapper.InvestRemindDao;
import com.iformall.service.invest.InvestRemindService;
import org.springframework.stereotype.Service;

import java.util.Objects;


@Service
public class InvestRemindServiceImpl extends InvestBaseServiceImpl<InvestRemindDao, InvestRemindEntity> implements InvestRemindService {

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

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

    @Override
    public boolean saveOrUpdate(InvestRemindEntity entity) {
        return super.saveOrUpdateAction(entity, this::checkBeforeSaveOrUpdate);
    }

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

    @Override
    public boolean removeAll() {
        LambdaUpdateWrapper<InvestRemindEntity> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(InvestRemindEntity::getTenantId, InvestUserContext.getUser().getTenantId());
        wrapper.eq(InvestRemindEntity::getOwner, InvestUserContext.getUserId());
        return super.remove(wrapper);
    }

    private void checkBeforeSaveOrUpdate(InvestRemindEntity input) {
        input.setOwner(InvestUserContext.getUserId());
        if (Objects.nonNull(input.getContent())) {
            input.setContent(stringToJson(input.getContent()));
        }
    }
}
