package com.ruoyi.care.service.impl;

import java.util.List;

import com.ruoyi.care.domain.CareCompanies;
import com.ruoyi.care.domain.CareNurses;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.mapper.SysUserPostMapper;
import com.ruoyi.system.mapper.SysUserRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.care.mapper.CarePersonsMapper;
import com.ruoyi.care.domain.CarePersons;
import com.ruoyi.care.service.ICarePersonsService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 特困人员Service业务层处理
 * 
 * @author qy
 * @date 2024-11-25
 */
@Service
public class CarePersonsServiceImpl implements ICarePersonsService 
{
    @Autowired
    private CarePersonsMapper carePersonsMapper;
    @Autowired
    private SysUserMapper userMapper;
    @Autowired
    private SysUserRoleMapper userRoleMapper;

    @Autowired
    private SysUserPostMapper userPostMapper;
    /**
     * 查询特困人员
     * 
     * @param personId 特困人员主键
     * @return 特困人员
     */
    @Override
    public CarePersons selectCarePersonsByPersonId(Long personId)
    {
        return carePersonsMapper.selectCarePersonsByPersonId(personId);
    }

    /**
     * 查询特困人员列表
     * 
     * @param carePersons 特困人员
     * @return 特困人员
     */
    @Override
    public List<CarePersons> selectCarePersonsList(CarePersons carePersons)
    {
        return carePersonsMapper.selectCarePersonsList(carePersons);
    }

    /**
     * 新增特困人员
     * 
     * @param carePersons 特困人员
     * @return 结果
     */
    @Override
    public int insertCarePersons(CarePersons carePersons)
    {
        carePersons.setCreateTime(DateUtils.getNowDate());
        return carePersonsMapper.insertCarePersons(carePersons);
    }

    /**
     * 修改特困人员
     * 
     * @param carePersons 特困人员
     * @return 结果
     */
    @Override
    public int updateCarePersons(CarePersons carePersons)
    {
        carePersons.setUpdateTime(DateUtils.getNowDate());
        return carePersonsMapper.updateCarePersons(carePersons);
    }

    /**
     * 批量删除特困人员
     * 
     * @param personIds 需要删除的特困人员主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteCarePersonsByPersonIds(Long[] personIds)
    {
        Long[] userIds=new Long[personIds.length];
        for (int i = 0; i < personIds.length; i++){
            CarePersons carePersons = selectCarePersonsByPersonId(personIds[i]);
            userIds[i]=carePersons.getUserId();
        }
        // 删除用户与角色关联
        userRoleMapper.deleteUserRole(userIds);
        // 删除用户
        userMapper.deleteUserByIds(userIds);

        return carePersonsMapper.deleteCarePersonsByPersonIds(personIds);
    }

    /**
     * 删除特困人员信息
     * 
     * @param personId 特困人员主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteCarePersonsByPersonId(Long personId)
    {
        CarePersons carePersons=selectCarePersonsByPersonId(personId);
        // 删除用户与角色关联
        userRoleMapper.deleteUserRoleByUserId(carePersons.getUserId());
        // 删除用户
        userMapper.deleteUserById(carePersons.getUserId());

        return carePersonsMapper.deleteCarePersonsByPersonId(personId);
    }
}
