package com.tuoheng.admin.service.user.delete;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.tuoheng.admin.entity.User;
import com.tuoheng.admin.enums.InspectionFileStatusEnum;
import com.tuoheng.admin.enums.code.user.DeleteUserCodeEnum;
import com.tuoheng.admin.enums.code.user.EditUserCodeEnum;
import com.tuoheng.admin.mapper.UserMapper;
import com.tuoheng.admin.service.third.oidc.OidcService;
import com.tuoheng.admin.utils.CurrentUserUtil;
import com.tuoheng.common.core.config.common.CommonConfig;
import com.tuoheng.common.core.utils.CommonUtils;
import com.tuoheng.common.core.utils.DateUtils;
import com.tuoheng.common.core.utils.JsonResult;
import com.tuoheng.common.core.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 删除用户业务层处理
 *
 * @author wanjing
 * @team tuoheng
 * @date 2022-12-16
 */
@Slf4j
@Service
public class DeleteUserService {

    @Autowired
    private UserMapper userMapper;

    /**
     * 修改用户数据
     *
     * @return
     */
    public JsonResult deleteByIdList(List<String> idList) {
        log.info("进入删除用户业务接口, idList={}", idList);
        String tenantId = CurrentUserUtil.getTenantId();
        JsonResult result = this.check(tenantId, idList);
        if (0 != result.getCode()) {
            log.info("删除用户业务接口：校验失败：{}", result.getMsg());
            return result;
        }

        Map<String, Object> map = new HashMap<>();
        map.put("mark", 0);
        map.put("updateUser", CurrentUserUtil.getUserId());
        map.put("updateTime", DateUtils.now());
        map.put("idList", idList);
        Integer rowCount = userMapper.updateByIdList(map);
        if (rowCount <= 0) {
            return JsonResult.error(DeleteUserCodeEnum.DELETE_IS_FAILED.getCode(), DeleteUserCodeEnum.DELETE_IS_FAILED.getMsg());
        }
        return JsonResult.success();
    }
    /**
     * 检查参数
     * @param tenantId
     * @param idList
     * @return
     */
    private JsonResult check(String tenantId, List<String> idList) {
        if (CollectionUtil.isEmpty(idList)) {
            return JsonResult.error(DeleteUserCodeEnum.USER_ID_IS_NULL.getCode(), DeleteUserCodeEnum.USER_ID_IS_NULL.getMsg());
        }
        return JsonResult.success();
    }

}
