package com.bonus.canteen.core.allocation.alloc.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.bonus.canteen.core.allocation.alloc.mapper.MerchantLoginAppMapper;
import com.bonus.canteen.core.allocation.alloc.model.MerchantLoginApp;
import com.bonus.canteen.core.allocation.alloc.service.MerchantLoginAppService;
import com.bonus.canteen.core.common.utils.TenantContextHolder;
import com.bonus.canteen.core.data.dataset.Executors;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.houqin.i18n.I18n;
import com.bonus.common.houqin.utils.id.Id;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.List;

@Service
public class MerchantLoginAppServiceImpl implements MerchantLoginAppService {
   private static final Logger log = LoggerFactory.getLogger(MerchantLoginAppServiceImpl.class);
   @Autowired
   @Lazy
   private MerchantLoginAppMapper merchantLoginAppMapper;

   public Long getMerchantIdByAppId(String appid, String loginKey) {
      MerchantLoginApp loginApp = Executors.readInSystem(() -> {
         return this.merchantLoginAppMapper.selectOne(Wrappers.lambdaQuery(MerchantLoginApp.class)
                 .select(MerchantLoginApp::getTenantId).eq(MerchantLoginApp::getLoginKey, loginKey)
                 .eq(MerchantLoginApp::getLoginAppId, appid));
      });
      if (ObjectUtil.isEmpty(loginApp)) {
         log.info("[小程序初始化]请在后台配置小程序appId等配置信息！");
         throw new ServiceException(I18n.getMessage("alloc_merchant_login_app_error", new Object[0]));
      } else {
         return loginApp.getTenantId();
      }
   }

   public void saveList(List<MerchantLoginApp> dataList) {
      if (!CollUtil.isEmpty(dataList)) {
         Iterator var2 = dataList.iterator();

         while(var2.hasNext()) {
            MerchantLoginApp loginApp = (MerchantLoginApp)var2.next();
            Executors.doInSystem((status) -> {
               MerchantLoginApp oldLoginApp = (MerchantLoginApp)this.merchantLoginAppMapper
                       .selectOne((Wrappers.lambdaQuery(MerchantLoginApp.class)
                               .eq(MerchantLoginApp::getLoginKey, loginApp.getLoginKey()))
                               .eq(MerchantLoginApp::getTenantId, TenantContextHolder.getTenantId()));
               if (ObjectUtil.isNull(oldLoginApp)) {
                  loginApp.setId(Id.next());
                  this.merchantLoginAppMapper.insert(loginApp);
               } else {
                  loginApp.setId(oldLoginApp.getId());
                  this.merchantLoginAppMapper.updateById(loginApp);
               }

            });
         }

      }
   }

   public void batchSave(List<MerchantLoginApp> loginAppList) {
      Iterator var2 = loginAppList.iterator();

      while(var2.hasNext()) {
         MerchantLoginApp loginApp = (MerchantLoginApp)var2.next();
         Executors.doInSystem((status) -> {
            this.merchantLoginAppMapper.insert(loginApp);
         });
      }

   }

   public void removeByKey(String loginKey) {
      Executors.doInSystem((status) -> {
         this.merchantLoginAppMapper.delete(Wrappers.lambdaQuery(MerchantLoginApp.class)
                 .eq(MerchantLoginApp::getLoginKey, loginKey)
                 .eq(MerchantLoginApp::getTenantId, TenantContextHolder.getTenantId()));
      });
   }
}
