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

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.jjb.saas.framework.repository.common.PageHelper;
import com.zcloud.basic.info.domain.enums.UserJobStatusEnum;
import com.zcloud.basic.info.persistence.dataobject.UserJobHandoverDO;
import com.zcloud.basic.info.persistence.mapper.UserJobHandoverMapper;
import com.zcloud.basic.info.persistence.repository.UserJobHandoverRepository;
import com.alibaba.cola.dto.SingleResponse;
import com.alibaba.cola.dto.PageResponse;
import com.zcloud.gbscommon.utils.PageQueryHelper;
import com.zcloud.gbscommon.utils.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jjb.saas.framework.repository.repo.impl.BaseRepositoryImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

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

/**
 * web-infrastructure
 *
 * @Author SondonYong
 * @Date 2025-12-02 14:08:57
 */
@Service
@RequiredArgsConstructor
public class UserJobHandoverRepositoryImpl extends BaseRepositoryImpl<UserJobHandoverMapper, UserJobHandoverDO> implements UserJobHandoverRepository {
    private final UserJobHandoverMapper userJobHandoverMapper;

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

    @Override
    public List<UserJobHandoverDO> list(Map<String, Object> params) {
        QueryWrapper<UserJobHandoverDO> queryWrapper = new QueryWrapper<>();
        queryWrapper = PageQueryHelper.createPageQueryWrapper(queryWrapper, params);
        queryWrapper.orderByDesc("create_time");
        List<UserJobHandoverDO> result = userJobHandoverMapper.selectList(queryWrapper);
        return result;
    }

    @Override
    public SingleResponse<UserJobHandoverDO> getInfoById(Long id) {
        return SingleResponse.of(userJobHandoverMapper.selectById(id));
    }

    @Override
    public List<UserJobHandoverDO> getListByChangerRecordId(String userChangeRecordId) {

        QueryWrapper<UserJobHandoverDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_change_record_id", userChangeRecordId);
        queryWrapper.orderByDesc("create_time");
        List<UserJobHandoverDO> result = userJobHandoverMapper.selectList(queryWrapper);
        return result;
    }


    @Override
    public void processUserJobHandover(String userChangeRecordId) {
        //根据user_change_record_id 修改状态
        LambdaUpdateWrapper<UserJobHandoverDO> updateWrapper = new LambdaUpdateWrapper<>();
        // 设置更新条件
        updateWrapper.eq(UserJobHandoverDO::getUserChangeRecordId, userChangeRecordId);
        updateWrapper.eq(UserJobHandoverDO::getHandoverStatus, UserJobStatusEnum.UNTREATED.getCode());

        // 设置要更新的字段
        updateWrapper.set(UserJobHandoverDO::getHandoverStatus, UserJobStatusEnum.CLOSE_TASK.getCode());

        // 执行更新
        userJobHandoverMapper.update(null, updateWrapper);
    }

    @Override
    public List<UserJobHandoverDO> getListByUserId(Long userId, Long corpinfoId) {
        QueryWrapper<UserJobHandoverDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("corpinfo_id", corpinfoId);
        queryWrapper.eq("handover_status", UserJobStatusEnum.UNTREATED.getCode());
        queryWrapper.orderByDesc("create_time");
        List<UserJobHandoverDO> result = userJobHandoverMapper.selectList(queryWrapper);
        return result;
    }

    @Override
    public void deleteUserJob(Long id, Long corpinfoId) {
        //根据用户id和企业id删除数据
        userJobHandoverMapper.delete(new QueryWrapper<UserJobHandoverDO>().eq("user_id", id).eq("corpinfo_id", corpinfoId).eq("delete_enum", "FALSE"));
    }
}

