package com.bonus.canteen.core.merchant.api;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.bonus.canteen.core.common.utils.TenantContextHolder;
import com.bonus.canteen.core.data.dataset.Executors;
import com.bonus.canteen.core.merchant.entity.MercMerchant;
import com.bonus.canteen.core.merchant.entity.MercMerchantInSystem;
import com.bonus.canteen.core.merchant.mapper.MercMerchantInSystemMapper;
import com.bonus.canteen.core.merchant.mapper.MercMerchantMapper;
import com.bonus.common.houqin.constant.LeConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class MercMerchantApi {
   @Autowired
   @Lazy
   MercMerchantInSystemMapper mercMerchantInSystemMapper;
   @Resource
   @Lazy
   private MercMerchantMapper mercMerchantMapper;
//   @Autowired
//   @Lazy
//   private MercMerchantService mercMerchantService;

//   public List<MercMerchantInSystem> getAllMerchantIdNameList() {
//      return (List)Executors.readInSystem(() -> {
//         return this.mercMerchantInSystemMapper.selectMerchantIdAndNameListByWrapper((Wrapper)Wrappers.lambdaQuery(MercMerchantInSystem.class).eq(MercMerchantInSystem::getStatus, MerchantStatusEnum.NORMAL.key()));
//      });
//   }

   public MercMerchant getMerchant() {
      return (MercMerchant)this.mercMerchantMapper.selectOne(Wrappers.lambdaQuery(MercMerchant.class));
   }

   public String getMerchantName() {
      return (String) Executors.readInTenant(TenantContextHolder.getTenantId(), () -> {
         return this.mercMerchantMapper.selectOne(Wrappers.lambdaQuery(MercMerchant.class)
                 .select(MercMerchant::getMerchantName)).getMerchantName();
      });
   }
//
//   public Long registerMerchant(MercMerchantAddDTO mercMerchant) {
//      return this.mercMerchantService.register(mercMerchant);
//   }
//
//   public void auditMerchant(MercMerchantAuditDTO mercMerchantAuditDTO) {
//      this.mercMerchantService.auditMerchant(mercMerchantAuditDTO);
//   }
//
//   public List<MercMerchantInSystem> listByIds(List<Long> ids) {
//      return (List)Executors.readInSystem(() -> {
//         return this.mercMerchantInSystemMapper.selectBatchIds(ids);
//      });
//   }
//
//   public Page<MercMerchantInSystem> getMercMerchantPage(Page<MercMerchantInSystem> page, String name) {
//      LambdaQueryWrapper<MercMerchantInSystem> queryWrapper = Wrappers.lambdaQuery(MercMerchantInSystem.class);
//      if (StringUtils.isNotEmpty(name)) {
//         queryWrapper.like(MercMerchantInSystem::getMerchantName, name);
//      }
//
//      return (Page)Executors.readInSystem(() -> {
//         return (Page)this.mercMerchantInSystemMapper.selectPage(page, queryWrapper);
//      });
//   }
//
   public boolean checkCurrentMerchantLimitFlag() {
      Long tenantId = TenantContextHolder.getTenantId();
      MercMerchantInSystem merchant = Executors.readInSystem(() -> {
         return this.mercMerchantInSystemMapper.selectOne(Wrappers.lambdaQuery(MercMerchantInSystem.class)
                 .select(MercMerchantInSystem::getLimitFlag)
                 .eq(MercMerchantInSystem::getTenantId, tenantId));
      });
      return ObjectUtil.equal(LeConstants.COMMON_YES, merchant.getLimitFlag());
   }
//
//   public boolean showServerInfo() {
//      Long tenantId = TenantContextHolder.getTenantId();
//      MercMerchantInSystem merchant = (MercMerchantInSystem)Executors.readInSystem(() -> {
//         return (MercMerchantInSystem)this.mercMerchantInSystemMapper.selectOne((Wrapper)Wrappers.lambdaQuery(MercMerchantInSystem.class).select(new SFunction[]{MercMerchantInSystem::getServerFlag}).eq(MercMerchantInSystem::getTenantId, tenantId));
//      });
//      return ObjectUtil.equal(LeConstants.COMMON_YES, merchant.getServerFlag());
//   }
}
