package com.zcloud.basic.info.persistence.repository.impl;

import com.alibaba.cola.dto.PageResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jjb.saas.framework.repository.common.PageHelper;
import com.jjb.saas.framework.repository.repo.impl.BaseRepositoryImpl;
import com.zcloud.basic.info.domain.enums.UserEmploymentFlagEnum;
import com.zcloud.basic.info.domain.model.UserE;
import com.zcloud.basic.info.persistence.dataobject.UserCorpDO;
import com.zcloud.basic.info.persistence.mapper.UserCorpMapper;
import com.zcloud.basic.info.persistence.repository.UserCorpRepository;
import com.zcloud.gbscommon.utils.PageQueryHelper;
import com.zcloud.gbscommon.utils.Query;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * web-infrastructure
 *
 * @Author zhaokai
 * @Date 2025-12-02 11:13:33
 */
@Service
@RequiredArgsConstructor
public class UserCorpRepositoryImpl extends BaseRepositoryImpl<UserCorpMapper, UserCorpDO> implements UserCorpRepository {
    private final UserCorpMapper userCorpMapper;

    @Override
    public PageResponse<UserCorpDO> listPage(Map<String, Object> params) {
        IPage<UserCorpDO> iPage = new Query<UserCorpDO>().getPage(params);
        QueryWrapper<UserCorpDO> queryWrapper = new QueryWrapper<>();
        queryWrapper = PageQueryHelper.createPageQueryWrapper(queryWrapper, params);
        queryWrapper.orderByDesc("create_time");
        IPage<UserCorpDO> result = userCorpMapper.selectPage(iPage, queryWrapper);
        return PageHelper.pageToResponse(result, result.getRecords());
    }

    @Override
    public UserCorpDO getInfoByUserId(Long userId, Long corpinfoId) {
        return userCorpMapper.getInfoByUserId(userId, corpinfoId);
    }

    @Override
    public UserCorpDO getUserCorpInfoByUserId(Long userId) {
        QueryWrapper<UserCorpDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        return userCorpMapper.selectOne(queryWrapper);
    }

    @Override
    public List<UserCorpDO> listByUserIdAndCorpIdAndNoStatus(Long userId, Long corpinfoId, List<Integer> statusList) {
        QueryWrapper<UserCorpDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("corpinfo_id", corpinfoId);
        queryWrapper.notIn("employment_flag", statusList);
        return list(queryWrapper);
    }

    @Override
    public List<UserCorpDO> listByUserIdAndCorpIdAndStatus(Long userId, Long corpinfoId, List<Integer> statusList) {
        QueryWrapper<UserCorpDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("corpinfo_id", corpinfoId);
        queryWrapper.in("employment_flag", statusList);
        return list(queryWrapper);
    }

    @Override
    public List<UserCorpDO> listByUserIdAndNoStatus(Long userId, Integer status) {
        QueryWrapper<UserCorpDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.ne("employment_flag", status);
        return list(queryWrapper);
    }

    @Override
    public UserE executeResignation(UserCorpDO userCorpDO, Long corpinfoId, Integer employmentFlag) {
        userCorpDO.setEmploymentFlag(employmentFlag);
        userCorpMapper.updateById(userCorpDO);
        UserE userE = new UserE();
        BeanUtils.copyProperties(userCorpDO, userE);
        return userE;
    }

    @Override
    public UserE executeReviewStatus(Long id, Long corpinfoId, Integer employmentFlag) {
        UserCorpDO userCorpDO = getInfoByUserId(id, corpinfoId);
        if (userCorpDO == null) {
            return null;
        }
        userCorpDO.setEmploymentFlag(employmentFlag);
        userCorpMapper.updateById(userCorpDO);
        UserE userE = new UserE();
        BeanUtils.copyProperties(userCorpDO, userE);
        userE.setId(userCorpDO.getUserId());
        return userE;
    }

    @Override
    public void removeByUserId(Long userId) {
        QueryWrapper<UserCorpDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        remove(queryWrapper);
    }


}

