package com.yanzhu.manage.service.impl;

import com.yanzhu.common.core.context.SecurityContextHolder;
import com.yanzhu.common.core.utils.DateUtils;
import com.yanzhu.common.security.utils.SecurityUtils;
import com.yanzhu.manage.domain.ProProjectInfoUsers;
import com.yanzhu.manage.mapper.ProProjectInfoUsersMapper;
import com.yanzhu.manage.service.IProProjectInfoUsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 项目人员Service业务层处理
 * 
 * @author JiangYuQi
 * @date 2024-08-25
 */
@Service
public class ProProjectInfoUsersServiceImpl implements IProProjectInfoUsersService 
{
    @Autowired
    private ProProjectInfoUsersMapper proProjectInfoUsersMapper;

    /**
     * 查询项目人员
     * 
     * @param id 项目人员主键
     * @return 项目人员
     */
    @Override
    public ProProjectInfoUsers selectProProjectInfoUsersById(Long id)
    {
        return proProjectInfoUsersMapper.selectProProjectInfoUsersById(id);
    }

    /**
     * 查询项目人员列表
     * 
     * @param proProjectInfoUsers 项目人员
     * @return 项目人员
     */
    @Override
    public List<ProProjectInfoUsers> selectProProjectInfoUsersList(ProProjectInfoUsers proProjectInfoUsers)
    {
        return proProjectInfoUsersMapper.selectProProjectInfoUsersList(proProjectInfoUsers);
    }

    /**
     * 新增项目人员
     * 
     * @param proProjectInfoUsers 项目人员
     * @return 结果
     */
    @Override
    public int insertProProjectInfoUsers(ProProjectInfoUsers proProjectInfoUsers)
    {
        proProjectInfoUsers.setCreateTime(DateUtils.getNowDate());
        proProjectInfoUsers.setCreateBy(SecurityContextHolder.getUserName());
        return proProjectInfoUsersMapper.insertProProjectInfoUsers(proProjectInfoUsers);
    }

    /**
     * 修改项目人员
     * 
     * @param proProjectInfoUsers 项目人员
     * @return 结果
     */
    @Override
    public int updateProProjectInfoUsers(ProProjectInfoUsers proProjectInfoUsers)
    {
        proProjectInfoUsers.setUpdateTime(DateUtils.getNowDate());
        proProjectInfoUsers.setUpdateBy(SecurityContextHolder.getUserName());
        return proProjectInfoUsersMapper.updateProProjectInfoUsers(proProjectInfoUsers);
    }

    /**
     * 批量删除项目人员
     * 
     * @param ids 需要删除的项目人员主键
     * @return 结果
     */
    @Override
    public int deleteProProjectInfoUsersByIds(Long[] ids)
    {
        return proProjectInfoUsersMapper.deleteProProjectInfoUsersByIds(ids);
    }

    /**
     * 删除项目人员信息
     * 
     * @param id 项目人员主键
     * @return 结果
     */
    @Override
    public int deleteProProjectInfoUsersById(Long id)
    {
        return proProjectInfoUsersMapper.deleteProProjectInfoUsersById(id);
    }

    /**
     * 修改项目人员的默认项目
     *
     * @param id 项目人员主键
     * @return 结果
     */
    @Override
    @Transactional
    public int editDefaultProjectById(Long id){
        int res = proProjectInfoUsersMapper.cancelDefaultSortByUserId(SecurityUtils.getUserId());
        if(res>0){
            ProProjectInfoUsers entity = new ProProjectInfoUsers();
            entity.setId(id);
            entity.setSortBy(0L);
            proProjectInfoUsersMapper.updateProProjectInfoUsers(entity);
        }
        return res;
    }

    /**
     * 根据用户编号查询入场项目信息
     *
     * @param userId 用户ID
     * @return 结果
     */
    @Override
    public List<Map<String, Object>> selectProjectsByUserId(Long userId){
        return proProjectInfoUsersMapper.selectProjectsByUserId(userId);
    }
}
