package com.ruoyi.system.service.impl;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.ResidentInfo;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.mapper.ResidentInfoMapper;
import com.ruoyi.system.mapper.SysRoleMapper;
import com.ruoyi.system.service.IResidentInfoService;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 居住人员信息，存储居住人员的详细信息Service业务层处理
 * 
 * @author boman
 * @date 2025-02-20
 */
@Service
public class ResidentInfoServiceImpl implements IResidentInfoService 
{
    @Autowired
    private ResidentInfoMapper residentInfoMapper;
    @Autowired
    private SysRoleMapper roleMapper;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private ISysDictDataService dictDataService;

    /**
     * 查询居住人员信息，存储居住人员的详细信息
     * 
     * @param residentId 居住人员信息，存储居住人员的详细信息主键
     * @return 居住人员信息，存储居住人员的详细信息
     */
    @Override
    public ResidentInfo selectResidentInfoByResidentId(Long residentId)
    {
        return residentInfoMapper.selectResidentInfoByResidentId(residentId);
    }

    /**
     * 查询居住人员信息，存储居住人员的详细信息列表
     * 
     * @param residentInfo 居住人员信息，存储居住人员的详细信息
     * @return 居住人员信息，存储居住人员的详细信息
     */
    @Override
    public List<ResidentInfo> selectResidentInfoList(ResidentInfo residentInfo)
    {
        return residentInfoMapper.selectResidentInfoList(residentInfo);
    }

    @Override
    public AjaxResult selectCount(ResidentInfo residentInfo) {
        return AjaxResult.success(residentInfoMapper.selectCount(residentInfo));
    }

    /**
     * 新增居住人员信息，存储居住人员的详细信息
     * 
     * @param residentInfo 居住人员信息，存储居住人员的详细信息
     * @return 结果
     */
    @Override
    public AjaxResult insertResidentInfo(ResidentInfo residentInfo)
    {
        //新增账号信息
        SysUser user = new SysUser();
        user.setUserName(residentInfo.getResidentPhone());
        if (!userService.checkUserNameUnique(user))
        {
            return AjaxResult.error(user.getUserName() + "'失败，手机号已存在");
        }
        user.setPassword(SecurityUtils.encryptPassword(UserConstants.PASSWORD));
        user.setPhonenumber(residentInfo.getResidentPhone());
        user.setNickName(residentInfo.getResidentName());
        //todo 部门  岗位
        user.setSex(String.valueOf(residentInfo.getResidentGender()-1));
        user.setStatus(UserConstants.DEPT_NORMAL);
        //查询角色id
        Long[] roleIds = {6L};
        user.setRoleIds(roleIds);
        userService.insertUser(user);

        residentInfo.setCreateTime(DateUtils.getNowDate());
        residentInfo.setUserId(user.getUserId());
        int i = residentInfoMapper.insertResidentInfo(residentInfo);
        return i > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 修改居住人员信息，存储居住人员的详细信息
     * 
     * @param residentInfo 居住人员信息，存储居住人员的详细信息
     * @return 结果
     */
    @Override
    public int updateResidentInfo(ResidentInfo residentInfo)
    {
        residentInfo.setUpdateTime(DateUtils.getNowDate());
        return residentInfoMapper.updateResidentInfo(residentInfo);
    }

    /**
     * 批量删除居住人员信息，存储居住人员的详细信息
     * 
     * @param residentIds 需要删除的居住人员信息，存储居住人员的详细信息主键
     * @return 结果
     */
    @Override
    public int deleteResidentInfoByResidentIds(Long[] residentIds)
    {
        return residentInfoMapper.deleteResidentInfoByResidentIds(residentIds);
    }

    /**
     * 删除居住人员信息，存储居住人员的详细信息信息
     * 
     * @param residentId 居住人员信息，存储居住人员的详细信息主键
     * @return 结果
     */
    @Override
    public int deleteResidentInfoByResidentId(Long residentId)
    {
        return residentInfoMapper.deleteResidentInfoByResidentId(residentId);
    }

    /**
     * 根据userId查询居住人员信息
     * @param userId
     * @return
     */
    @Override
    public ResidentInfo selectResidentInfoByUserId(Long userId) {
        return residentInfoMapper.selectResidentInfoByUserId(userId);
    }
}
