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.enums.EnumFollowType;
import com.iformall.enums.EnumInvestMessageType;
import com.iformall.mapper.InvestDemandDao;
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.Arrays;
import java.util.List;
import java.util.Objects;


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

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

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

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

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

    private void saveMessage(InvestDemandEntity entity) {
        Long owner = entity.getOwner();
        if (Objects.nonNull(owner)) {
            List<Long> ownerArray = Arrays.asList(owner);
            this.saveBatchMessage(ownerArray, EnumInvestMessageType.CUSTOMER_CREATE, EnumFollowType.DEMAND, entity.getId());
        }
    }

    private void checkBeforeSaveOrUpdate(InvestDemandEntity entity) {
        checkCustomerExit(customerService, entity.getCustomerId());
        checkUserExit(userInfoService, entity.getOwner());
        checkShop(shopService, entity);
        if(Objects.nonNull(entity.getIntent())) {
            entity.setIntent(stringToJson(entity.getIntent()));
        }
    }

    @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)) ;
    }
}
