package com.zcloud.basic.info.command;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.cola.exception.BizException;
import com.jjb.saas.system.client.user.facade.UserFacade;
import com.zcloud.basic.info.constant.RedisConstant;
import com.zcloud.basic.info.domain.enums.UserEmploymentFlagEnum;
import com.zcloud.basic.info.domain.gateway.UserGateway;
import com.zcloud.basic.info.dto.AppUserLogOutCmd;
import com.zcloud.basic.info.persistence.dataobject.UserCorpDO;
import com.zcloud.basic.info.persistence.dataobject.UserDO;
import com.zcloud.basic.info.persistence.repository.UserCorpRepository;
import com.zcloud.basic.info.persistence.repository.UserRepository;
import com.zcloud.gbscommon.utils.Const;
import com.zcloud.gbscommon.utils.ZcloudRedisUtil;
import lombok.AllArgsConstructor;
import org.apache.dubbo.config.annotation.DubboReference;
import org.junit.platform.commons.function.Try;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
* web-app
* @Author zhangyue
* @Date 2025-11-04 14:07:36
*/
@Component
@AllArgsConstructor
public class UserRemoveExe {
    private final UserGateway userGateway;
    private final UserRepository userRepository;
    private final UserCorpRepository userCorpRepository;
    @DubboReference(check = false)
    private UserFacade userFacade;
//    @Autowired
    private ZcloudRedisUtil zcloudRedisUtil;

    @Transactional(rollbackFor = Exception.class)
    public boolean execute(Long id) {
        try {
            userGateway.deletedUserById(id);
        } catch (Exception e){
            e.printStackTrace();
            throw new BizException("删除失败");
        }
      //  zcloudRedisUtil.del(Const.REDIS_USER_PREFIX+id);
        return true;
    }

    @Transactional(rollbackFor = Exception.class)
    public boolean executeLogOut(AppUserLogOutCmd cmd) {

        UserDO userDO = userRepository.getById(cmd.getId());

        // 获取redis验证码
        Object phoneCodeObj = zcloudRedisUtil.get(RedisConstant.PHONE_CODE_KEY + userDO.getPhone());
        if(phoneCodeObj == null || !phoneCodeObj.toString().equals(cmd.getPhoneCode())){
            throw new BizException("验证码已过期或错误");
        }
        zcloudRedisUtil.del(RedisConstant.PHONE_CODE_KEY + userDO.getPhone());

        try {
            // 删除底座用户信息
            userFacade.delete(cmd.getId());
            // 删除用户表
            Long userId = cmd.getId();
            userGateway.deletedUserById(userId);
            // 删除user_corp表
            userCorpRepository.removeByUserId(userId);
        } catch (Exception e){
            e.printStackTrace();
            throw new BizException("删除失败");
        }
        return true;
    }

    @Transactional(rollbackFor = Exception.class)
    public boolean executeCheckOnboarding(AppUserLogOutCmd cmd) {
        // 校验是否还有入职信息
        UserDO userDO = userRepository.getInfoById(cmd.getId());
        if(!UserEmploymentFlagEnum.RESIGNATION.equals(userDO.getEmploymentFlag().toString())){
            return false;
        }
        List<UserCorpDO> userCorpDOS = userCorpRepository.listByUserIdAndNoStatus(cmd.getId(), 0);
        if(CollUtil.isNotEmpty(userCorpDOS)){
            return false;
        }
        return true;
    }

    @Transactional(rollbackFor = Exception.class)
    public boolean execute(Long[] ids) {
        boolean res = userGateway.deletedUserByIds(ids);
        if(!res){
            throw new BizException("删除失败");
        }
//        for (Long id : ids) {
//            zcloudRedisUtil.del(Const.REDIS_USER_PREFIX+id);
//        }
        return true;
    }
}

