package com.iformall.service.invest.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.iformall.common.TableLog;
import com.iformall.domain.po.invest.InvestDemandEntity;
import com.iformall.mapper.InvestDemandMapper;
import com.iformall.service.MallUserInfoService;
import com.iformall.service.WxShopService;
import com.iformall.service.invest.InvestCustomerService;
import com.iformall.service.invest.InvestDemandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;


@Service
public class InvestDemandServiceImpl extends InvestBaseServiceImpl<InvestDemandMapper, InvestDemandEntity> implements InvestDemandService {

    @Autowired
    private WxShopService shopService;
    @Autowired
    private MallUserInfoService userInfoService;
    @Autowired
    private InvestCustomerService customerService;

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

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

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

    private void checkBeforeSaveOrUpdate(InvestDemandEntity entity) {
        checkCustomerExit(customerService, entity.getCustomerId());
        checkShop(shopService, entity);
        if (Objects.nonNull(entity.getIntent())) {
            entity.setIntent(stringToJson(entity.getIntent()));
        }
        if (Objects.isNull(entity.getOwner()) || Objects.equals(entity.getOwner(), 0L)) {
            entity.setOwner(0L);
        } else {
            checkUserExit(userInfoService, entity.getOwner());
        }
    }

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

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

    @Override
    public InvestDemandEntity getByCustomerId(Long customerId) {
        if(Objects.isNull(customerId)) {
            return null ;
        }
        return getOne(new LambdaUpdateWrapper<InvestDemandEntity>().eq(InvestDemandEntity::getCustomerId,customerId)) ;
    }

}
