package com.yanzhu.manage.service.impl;

import com.yanzhu.common.core.constant.SecurityConstants;
import com.yanzhu.common.core.context.SecurityContextHolder;
import com.yanzhu.common.core.utils.DateUtils;
import com.yanzhu.manage.domain.ProProjectInfoSubdeptsUsers;
import com.yanzhu.manage.mapper.ProProjectInfoSubdeptsUsersMapper;
import com.yanzhu.manage.service.IProProjectInfoSubdeptsUsersService;
import com.yanzhu.manage.service.IUniService;
import com.yanzhu.system.api.RemoteUserService;
import com.yanzhu.system.api.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 分包单位工人Service业务层处理
 * 
 * @author JiangYuQi
 * @date 2024-08-25
 */
@Service
public class ProProjectInfoSubdeptsUsersServiceImpl implements IProProjectInfoSubdeptsUsersService 
{
    @Autowired
    private ProProjectInfoSubdeptsUsersMapper proProjectInfoSubdeptsUsersMapper;

    @Autowired
    private RemoteUserService remoteUserService;

    @Autowired
    private IUniService uniService;

    /**
     * 查询分包单位工人
     * 
     * @param id 分包单位工人主键
     * @return 分包单位工人
     */
    @Override
    public ProProjectInfoSubdeptsUsers selectProProjectInfoSubdeptsUsersById(Long id)
    {
        return proProjectInfoSubdeptsUsersMapper.selectProProjectInfoSubdeptsUsersById(id);
    }

    /**
     * 查询分包单位工人列表
     * 
     * @param proProjectInfoSubdeptsUsers 分包单位工人
     * @return 分包单位工人
     */
    @Override
    public List<ProProjectInfoSubdeptsUsers> selectProProjectInfoSubdeptsUsersList(ProProjectInfoSubdeptsUsers proProjectInfoSubdeptsUsers)
    {
        return proProjectInfoSubdeptsUsersMapper.selectProProjectInfoSubdeptsUsersList(proProjectInfoSubdeptsUsers);
    }

    /**
     * 新增分包单位工人
     * 
     * @param proProjectInfoSubdeptsUsers 分包单位工人
     * @return 结果
     */
    @Override
    public int insertProProjectInfoSubdeptsUsers(ProProjectInfoSubdeptsUsers proProjectInfoSubdeptsUsers)
    {
        proProjectInfoSubdeptsUsers.setCreateTime(DateUtils.getNowDate());
        proProjectInfoSubdeptsUsers.setCreateBy(SecurityContextHolder.getUserName());
        SysUser user=proProjectInfoSubdeptsUsers.getUser();
        if(user.getUserId()==null||user.getUserId().intValue()==0){
         Long userId= remoteUserService.registerUserInfo(user, SecurityConstants.INNER).getData();
         user.setUserId(userId);
         proProjectInfoSubdeptsUsers.setUserId(userId);
        }else{
            remoteUserService.updateUserInfo(user,SecurityConstants.INNER).getData();
        }
        uniService.syncUniUser(user.getUserId(),proProjectInfoSubdeptsUsers.getProjectId());
        return proProjectInfoSubdeptsUsersMapper.insertProProjectInfoSubdeptsUsers(proProjectInfoSubdeptsUsers);
    }

    /**
     * 修改分包单位工人
     * 
     * @param proProjectInfoSubdeptsUsers 分包单位工人
     * @return 结果
     */
    @Override
    public int updateProProjectInfoSubdeptsUsers(ProProjectInfoSubdeptsUsers proProjectInfoSubdeptsUsers)
    {
        proProjectInfoSubdeptsUsers.setUpdateTime(DateUtils.getNowDate());
        proProjectInfoSubdeptsUsers.setUpdateBy(SecurityContextHolder.getUserName());
        SysUser user=proProjectInfoSubdeptsUsers.getUser();
        if(user.getUserId()==null||user.getUserId().intValue()==0){
            Long userId= remoteUserService.registerUserInfo(user, SecurityConstants.INNER).getData();
            user.setUserId(userId);
            proProjectInfoSubdeptsUsers.setUserId(userId);
        }else{
            remoteUserService.updateUserInfo(user,SecurityConstants.INNER).getData();
        }
        uniService.syncUniUser(user.getUserId(),proProjectInfoSubdeptsUsers.getProjectId());
        return proProjectInfoSubdeptsUsersMapper.updateProProjectInfoSubdeptsUsers(proProjectInfoSubdeptsUsers);
    }



    /**
     * 批量删除分包单位工人
     * 
     * @param ids 需要删除的分包单位工人主键
     * @return 结果
     */
    @Override
    public int deleteProProjectInfoSubdeptsUsersByIds(Long[] ids)
    {
        for(Long id:ids){
            uniService.syncUserRevoke(id);
        }
        return proProjectInfoSubdeptsUsersMapper.deleteProProjectInfoSubdeptsUsersByIds(ids);
    }

    /**
     * 删除分包单位工人信息
     *
     * @param id 分包单位工人主键
     * @return 结果
     */
    @Override
    public int deleteProProjectInfoSubdeptsUsersById(Long id)
    {
        uniService.syncUserRevoke(id);
        return proProjectInfoSubdeptsUsersMapper.deleteProProjectInfoSubdeptsUsersById(id);
    }
}
