package com.iformall.service.invest.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.iformall.common.ErrorCode;
import com.iformall.domain.po.invest.InvestFollowRecordEntity;
import com.iformall.domain.vo.invest.InvestPageQuery;
import com.iformall.domain.vo.invest.InvestPageResult;
import com.iformall.domain.vo.invest.InvestUserContext;
import com.iformall.enums.EnumFollowType;
import com.iformall.mapper.InvestFollowRecordDao;
import com.iformall.service.invest.InvestDemandService;
import com.iformall.service.invest.InvestFollowRecordService;
import com.iformall.service.invest.InvestHelper;
import com.iformall.service.invest.InvestTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class InvestFollowRecordServiceImpl extends InvestBaseServiceImpl<InvestFollowRecordDao, InvestFollowRecordEntity> implements InvestFollowRecordService {

    @Autowired
    private InvestDemandService demandService ;
    @Autowired
    private InvestTaskService taskService ;

    @Override
    public InvestPageResult<InvestFollowRecordEntity> queryPage(InvestPageQuery<InvestFollowRecordEntity> params) {
        InvestFollowRecordEntity queryData = params.getQueryData();
        InvestHelper.notNull(queryData);
        //InvestHelper.notNull(queryData.getFollowId(), "followId");
        InvestHelper.notNull(queryData.getType(), "type");
        return super.queryPage(params);
    }

    @Override
    public boolean save(InvestFollowRecordEntity entity) {
        entity.setOperator(InvestUserContext.getUserId());
        return super.saveAction(entity, this::checkBeforeSaveOrUpdate);
    }

    private void checkBeforeSaveOrUpdate(InvestFollowRecordEntity entity) {
        InvestHelper.notNull(entity);
        InvestHelper.notNull(entity.getFollowId(), "followId");
        InvestHelper.notNull(entity.getFollowDate(), "followDate");
        Object object = null;
        if(Objects.equals(entity.getType(), EnumFollowType.DEMAND)) {
            object = demandService.getById(entity.getFollowId());
        } else if(Objects.equals(entity.getType(), EnumFollowType.TASK)) {
            object = taskService.getById(entity.getFollowId());
        } else {
            InvestHelper.throwException(ErrorCode.SYS_PARAMETER_NOT_NULL, "followType");
        }
        InvestHelper.notNull(object, "");

        if(Objects.nonNull(entity.getContent())) {
            entity.setContent(stringToJson(entity.getContent()));
        }
    }

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

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

    @Deprecated
    @Override
    public boolean updateById(InvestFollowRecordEntity entity) {
        notSupport();
        return false;
    }
}
