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

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import javax.annotation.Resource;
import com.bonus.canteen.core.auth.user.entity.MgrUser;
import com.bonus.canteen.core.auth.user.entity.MgrUserInSystem;
import com.bonus.canteen.core.auth.user.mapper.MgrUserInSystemMapper;
import com.bonus.canteen.core.auth.user.mapper.MgrUserMapper;
import com.bonus.canteen.core.auth.user.mapper.MgrUserRoleMapper;
//import com.bonus.canteen.core.auth.user.service.MgrUserService;
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.lang.invoke.SerializedLambda;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class MgrUserApi {
   private static final Logger log = LoggerFactory.getLogger(MgrUserApi.class);
   @Autowired
   MgrUserInSystemMapper mgrUserInSystemMapper;
//   @Autowired
//   @Lazy
//   private MgrUserRoleMapper mgrUserRoleMapper;
//   @Resource
//   private MgrUserMapper mgrUserMapper;
//   @Autowired
//   @Lazy
//   private CustInfoApi custInfoApi;
//   @Autowired
//   @Lazy
//   private MgrUserService mgrUserService;

//   public void addMgrUserInfo(MgrUserAdminAddDTO mgrUserAdminAddDTO) {
//      this.mgrUserService.addMgrUserInfo(mgrUserAdminAddDTO);
//   }
//
//   public void blockMgrUser(long merchantId) {
//      this.mgrUserInSystemMapper.update((Object)null, (Wrapper)((LambdaUpdateWrapper)(new UpdateWrapper()).lambda().eq(MgrUserInSystem::getTenantId, merchantId)).set(MgrUserInSystem::getLockFlag, 1));
//   }
//
//   public MgrUserIdNameVO getUserIdAndRealName(String username) {
//      return this.mgrUserMapper.selectIdAndRealNameByWrapper((Wrapper)((LambdaQueryWrapper)Wrappers.lambdaQuery(MgrUser.class).eq(MgrUser::getRealName, SM4EncryptUtils.sm4Encryptbyconfig(username))).eq(MgrUser::getDelFlag, DelFlagEnum.DEL_FALSE.key()));
//   }

//   public String getPhoneByUserId(Long userId) {
//      MgrUser mgrUser = (MgrUser)this.mgrUserService.getOne((Wrapper)Wrappers.lambdaQuery(MgrUser.class).select(MgrUser::getPhone).eq(MgrUser::getUserId, userId));
//      return ObjectUtil.isNotNull(mgrUser) ? mgrUser.getPhone() : "";
//   }

//   public AuthEleScaleVO getEleScaleUserInfo(Long userId) {
//      MgrUser mgrUser = (MgrUser)this.mgrUserService.getOne((Wrapper)Wrappers.lambdaQuery(MgrUser.class).eq(MgrUser::getUserId, userId));
//      return ObjectUtil.isNotNull(mgrUser) ? new AuthEleScaleVO(mgrUser) : new AuthEleScaleVO();
//   }
//
//   public EffPermissionVO filterCanteenDataPermission(Long canteenIdDTO, Long shopstallIdDTO, Long effId) {
//      EffPermissionVO result = new EffPermissionVO();
//      result.setCanteenId(canteenIdDTO);
//      result.setShopstallId(shopstallIdDTO);
//      result.setCheckSql(true);
//      if (effId == -1L) {
//         return result;
//      } else {
//         Integer effExist = this.mgrUserMapper.selectExistFromMktEffectiveByWrapper((Wrapper)((QueryWrapper)((QueryWrapper)((QueryWrapper)Wrappers.query().eq("eff_id", effId)).eq("del_flag", DelFlagEnum.DEL_FALSE.key())).eq("eff_type", 2)).last("limit 1"));
//         List<EffCanteenVO> canteenList = null;
//         if (ObjectUtil.isNotNull(effExist)) {
//            canteenList = this.mgrUserMapper.selectCanteenByWrapper((Wrapper)Wrappers.query().eq("eff_id", effId));
//         }
//
//         if (!CollUtil.isEmpty(canteenList)) {
//            List<EffCanteenVO> resultCanteenList = CollUtil.newArrayList(new EffCanteenVO[0]);
//            resultCanteenList.addAll(canteenList);
//            Iterator var8 = canteenList.iterator();
//
//            while(var8.hasNext()) {
//               EffCanteenVO canteenVO = (EffCanteenVO)var8.next();
//               if (canteenVO.getShopstallId() == -1L) {
//                  List<EffCanteenVO> shopstallList = this.mgrUserMapper.selectShopstallByWrapper((Wrapper)((QueryWrapper)Wrappers.query().eq("dining_id", canteenVO.getCanteenId())).eq("del_flag", DelFlagEnum.DEL_FALSE.key()));
//                  if (CollUtil.isNotEmpty(shopstallList)) {
//                     resultCanteenList.addAll(shopstallList);
//                  }
//               }
//            }
//
//            if (!ObjectUtil.isNull(canteenIdDTO) && canteenIdDTO != -1L) {
//               if (!ObjectUtil.isNull(shopstallIdDTO) && shopstallIdDTO != -1L) {
//                  EffCanteenVO canteenDTO = new EffCanteenVO();
//                  canteenDTO.setCanteenId(canteenIdDTO);
//                  canteenDTO.setShopstallId(shopstallIdDTO);
//                  if (!canteenList.contains(canteenDTO)) {
//                     result.setCheckSql(false);
//                     return result;
//                  } else {
//                     result.setCanteenId(canteenIdDTO);
//                     result.setShopstallId(shopstallIdDTO);
//                     result.setCanteenList((List)null);
//                     return result;
//                  }
//               } else {
//                  List<EffCanteenVO> shopstallList = this.mgrUserMapper.selectShopstallByWrapper((Wrapper)((QueryWrapper)Wrappers.query().eq("dining_id", canteenIdDTO)).eq("del_flag", DelFlagEnum.DEL_FALSE.key()));
//                  if (CollUtil.isEmpty(shopstallList)) {
//                     if (resultCanteenList.stream().map(EffCanteenVO::getCanteenId).toList().contains(canteenIdDTO)) {
//                        result.setCanteenId(canteenIdDTO);
//                     } else {
//                        result.setCheckSql(false);
//                     }
//
//                     return result;
//                  } else {
//                     List<EffCanteenVO> resultCanteenList = (List)shopstallList.stream().filter((s) -> {
//                        List var10000 = resultCanteenList.stream().map((r) -> {
//                           Long var10000 = r.getCanteenId();
//                           return "" + var10000 + "&" + r.getShopstallId();
//                        }).toList();
//                        Long var10001 = s.getCanteenId();
//                        return var10000.contains("" + var10001 + "&" + s.getShopstallId());
//                     }).collect(Collectors.toList());
//                     result.setCanteenId((Long)null);
//                     result.setShopstallId((Long)null);
//                     result.setCanteenList(resultCanteenList);
//                     return result;
//                  }
//               }
//            } else {
//               result.setCanteenId((Long)null);
//               result.setShopstallId((Long)null);
//               result.setCanteenList(resultCanteenList);
//               return result;
//            }
//         } else {
//            if ((!ObjectUtil.isNotNull(canteenIdDTO) || canteenIdDTO == -1L) && (!ObjectUtil.isNotNull(shopstallIdDTO) || shopstallIdDTO == -1L)) {
//               result.setCanteenId(-1L);
//               result.setShopstallId(-1L);
//            } else {
//               result.setCheckSql(false);
//            }
//
//            return result;
//         }
//      }
//   }
//
//   public List<MgrUserIdNameVO> listUserByIdAndRole(Long userId, String roleName) {
//      return this.mgrUserMapper.listUserByIdAndRole(userId, roleName, DelFlagEnum.DEL_FALSE.key());
//   }
//
//   public MgrUser selectMgrUserByUserId(Long userId) {
//      return (MgrUser)this.mgrUserMapper.selectOne((Wrapper)((LambdaQueryWrapper)Wrappers.lambdaQuery(MgrUser.class).eq(MgrUser::getUserId, userId)).eq(MgrUser::getDelFlag, DelFlagEnum.DEL_FALSE.key()));
//   }
//
//   public List<MgrUser> selectMgrUserByUserIds(List<Long> userIds) {
//      return this.mgrUserMapper.selectList((Wrapper)((LambdaQueryWrapper)Wrappers.lambdaQuery(MgrUser.class).in(MgrUser::getUserId, userIds)).eq(MgrUser::getDelFlag, DelFlagEnum.DEL_FALSE.key()));
//   }
//
//   public void removeByCustId(Long custId) {
//      this.mgrUserService.removeByCustId(custId);
//   }
//
//   public Set<String> listRoles(Long userId) {
//      return this.mgrUserRoleMapper.listRoles(userId);
//   }
//
//   public void checkUserByMobile(String mobile, Long merchantId) {
//      MgrUserInSystem mgrUser;
//      if (ObjectUtil.isNull(merchantId)) {
//         mgrUser = (MgrUserInSystem)Executors.readInSystem(() -> {
//            return (MgrUserInSystem)this.mgrUserInSystemMapper.selectOne((Wrapper)((LambdaQueryWrapper)Wrappers.lambdaQuery(MgrUserInSystem.class).and((item) -> {
//               ((LambdaQueryWrapper)((LambdaQueryWrapper)item.eq(MgrUserInSystem::getPhone, mobile)).or()).eq(MgrUserInSystem::getPhone, SM4EncryptUtils.sm4Encryptbyconfig(mobile));
//            })).eq(MgrUserInSystem::getDelFlag, DelFlagEnum.DEL_FALSE.key()));
//         });
//         if (ObjectUtil.isNull(mgrUser)) {
//            throw new LeException(I18n.getMessage("notice_verify_sms_code_exception", new Object[0]));
//         } else {
//            TenantContextHolder.setTenantId(mgrUser.getTenantId());
//         }
//      } else {
//         mgrUser = (MgrUserInSystem)Executors.readInSystem(() -> {
//            return (MgrUserInSystem)this.mgrUserInSystemMapper.selectOne((Wrapper)((LambdaQueryWrapper)Wrappers.lambdaQuery(MgrUserInSystem.class).and((item) -> {
//               ((LambdaQueryWrapper)((LambdaQueryWrapper)item.eq(MgrUserInSystem::getPhone, mobile)).or()).eq(MgrUserInSystem::getPhone, SM4EncryptUtils.sm4Encryptbyconfig(mobile));
//            })).eq(MgrUserInSystem::getDelFlag, DelFlagEnum.DEL_FALSE.key()));
//         });
//         if (ObjectUtil.isNull(mgrUser)) {
//            TenantContextHolder.setTenantId(merchantId);
//            CustInfo custInfo = this.custInfoApi.getCustInfoByMobileOrCustNum(mobile, (String)null);
//            if (ObjectUtil.isNull(custInfo)) {
//               throw new LeException(I18n.getMessage("notice_verify_sms_code_exception", new Object[0]));
//            }
//         }
//
//      }
//   }
//
//   public boolean checkUserNameOrMobile(String userName) {
//      return (Boolean)Executors.readInSystem(() -> {
//         return this.mgrUserInSystemMapper.exists((Wrapper)((LambdaQueryWrapper)Wrappers.lambdaQuery(MgrUserInSystem.class).eq(MgrUserInSystem::getDelFlag, DelFlagEnum.DEL_FALSE.key())).and((wrapper) -> {
//            ((LambdaQueryWrapper)((LambdaQueryWrapper)wrapper.eq(MgrUserInSystem::getUsername, userName)).or()).and((item) -> {
//               ((LambdaQueryWrapper)((LambdaQueryWrapper)item.eq(MgrUserInSystem::getPhone, userName)).or()).eq(MgrUserInSystem::getPhone, SM4EncryptUtils.sm4Encryptbyconfig(userName));
//            });
//         }));
//      });
//   }
//
//   public void editUserNameAndMobile(String userName, String oldUserName, Long tenantId) {
//      Executors.doInSystem(() -> {
//         return this.mgrUserInSystemMapper.update((Object)null, (Wrapper)((LambdaUpdateWrapper)((LambdaUpdateWrapper)((LambdaUpdateWrapper)Wrappers.lambdaUpdate(MgrUserInSystem.class).set(MgrUserInSystem::getUsername, userName)).set(MgrUserInSystem::getPhone, SM4EncryptUtils.sm4Encryptbyconfig(userName))).eq(MgrUserInSystem::getUsername, oldUserName)).eq(MgrUserInSystem::getTenantId, tenantId));
//      });
//      Executors.doInTenant(tenantId, () -> {
//         return this.mgrUserMapper.update((Object)null, (Wrapper)((LambdaUpdateWrapper)((LambdaUpdateWrapper)Wrappers.lambdaUpdate(MgrUser.class).set(MgrUser::getUsername, userName)).set(MgrUser::getPhone, SM4EncryptUtils.sm4Encryptbyconfig(userName))).eq(MgrUser::getUsername, oldUserName));
//      });
//   }
//
//   public void checkExistsMobile(String mobile) {
//      Boolean flag = (Boolean)Executors.readInSystem(() -> {
//         return this.mgrUserInSystemMapper.exists((Wrapper)Wrappers.lambdaQuery(MgrUserInSystem.class).and((item) -> {
//            ((LambdaQueryWrapper)((LambdaQueryWrapper)item.eq(MgrUserInSystem::getPhone, SM4EncryptUtils.sm4Encryptbyconfig(mobile))).or()).eq(MgrUserInSystem::getPhone, mobile);
//         }));
//      });
//      if (Boolean.TRUE.equals(flag)) {
//         throw new LeException(I18n.getMessage("auth_same_phone", new Object[0]));
//      }
//   }
//
//   public List<MgrRoleUserVO> getAllUserByRoleCode(MgrRoleUserListDTO content) {
//      return this.mgrUserService.getAllUserByRoleCode(content);
//   }
//
//   public List<String> getUserNameOrRoleName(List<String> userInfo) {
//      return this.mgrUserService.getUserNameOrRoleName(userInfo);
//   }
//
//   public String getUserNameByMobile(String mobile) {
//      MgrUserInSystem mgrUser = this.mgrUserService.getUserAndCheckMobile(mobile, false);
//      return mgrUser.getUsername();
//   }
   
}
