package com.iformall.service.invest.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.iformall.common.ErrorCode;
import com.iformall.common.TableLog;
import com.iformall.domain.po.invest.InvestCustomerEntity;
import com.iformall.mapper.InvestCustomerDao;
import com.iformall.service.WxBrandService;
import com.iformall.service.WxBusinessService;
import com.iformall.service.invest.InvestCustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

import static com.iformall.service.invest.InvestHelper.isTrue;

@Service
public class InvestCustomerServiceImpl extends InvestBaseServiceImpl<InvestCustomerDao, InvestCustomerEntity> implements InvestCustomerService {

	@Autowired
	private WxBrandService brandService;
	@Autowired
	private WxBusinessService businessService ;

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

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

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


	private void checkBeforeSaveOrUpdate(InvestCustomerEntity entity) {
		checkBusiness(businessService, entity.getBusinessId());
		checkBrand(brandService, entity.getBrandId());
		checkType(entity);
	}

	/**
	 * 校验客户类型
	 * @param entity
	 */
	private void checkType(InvestCustomerEntity entity) {
		isTrue(Objects.nonNull(entity.getType()), ErrorCode.SYS_PARAMETER_NOT_NULL, "客户类型");
	}


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

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