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.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.core.domain.entity.StaffManage;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.mapper.StaffManageMapper;
import com.ruoyi.system.mapper.SysRoleMapper;
import com.ruoyi.system.service.IStaffManageService;
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-18
 */
@Service
public class StaffManageServiceImpl implements IStaffManageService 
{
    @Autowired
    private StaffManageMapper staffManageMapper;
    @Autowired
    private SysRoleMapper roleMapper;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private ISysDictDataService dictDataService;

    /**
     * 查询员工管理
     * 
     * @param staffId 员工管理主键
     * @return 员工管理
     */
    @Override
    public StaffManage selectStaffManageByStaffId(Long staffId)
    {
        return staffManageMapper.selectStaffManageByStaffId(staffId);
    }

    /**
     * 查询员工管理列表
     * 
     * @param staffManage 员工管理
     * @return 员工管理
     */
    @Override
    public List<StaffManage> selectStaffManageList(StaffManage staffManage)
    {
        return staffManageMapper.selectStaffManageList(staffManage);
    }

    /**
     * 新增员工管理
     * 
     * @param staffManage 员工管理
     * @return 结果
     */
    @Override
    public AjaxResult insertStaffManage(StaffManage staffManage)
    {
        //新增账号信息
        SysUser user = new SysUser();
        user.setUserName(staffManage.getPhoneNumber());
        if (!userService.checkUserNameUnique(user))
        {
            return AjaxResult.error(user.getUserName() + "'失败，手机号已存在");
        }
        user.setPassword(SecurityUtils.encryptPassword(UserConstants.PASSWORD));
        user.setPhonenumber(staffManage.getPhoneNumber());
        user.setNickName(staffManage.getStaffName());
        //todo 部门  岗位
        user.setSex(staffManage.getGender());
        user.setStatus(UserConstants.DEPT_NORMAL);
        //查询角色id
        String roleKey = dictDataService.selectDictLabel(UserConstants.SYS_EMP_ROLE_TYPE, staffManage.getStaffCategory());
        SysRole info = roleMapper.checkRoleKeyUnique(roleKey);
        Long[] roleIds = {info.getRoleId()};
        user.setRoleIds(roleIds);
        userService.insertUser(user);

        staffManage.setUserId(user.getUserId());
        staffManage.setCreateTime(DateUtils.getNowDate());
        int i = staffManageMapper.insertStaffManage(staffManage);
        return i > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 修改员工管理
     * 
     * @param staffManage 员工管理
     * @return 结果
     */
    @Override
    public int updateStaffManage(StaffManage staffManage)
    {
        staffManage.setUpdateTime(DateUtils.getNowDate());
        return staffManageMapper.updateStaffManage(staffManage);
    }

    /**
     * 批量删除员工管理
     * 
     * @param staffIds 需要删除的员工管理主键
     * @return 结果
     */
    @Override
    public int deleteStaffManageByStaffIds(Long[] staffIds)
    {

        return staffManageMapper.deleteStaffManageByStaffIds(staffIds);
    }

    /**
     * 删除员工管理信息
     * 
     * @param staffId 员工管理主键
     * @return 结果
     */
    @Override
    public int deleteStaffManageByStaffId(Long staffId)
    {
        StaffManage staffManage = staffManageMapper.selectStaffManageByStaffId(staffId);
        //删除员工账号
        userService.deleteUserByUserName(staffManage.getPhoneNumber());
        return staffManageMapper.deleteStaffManageByStaffId(staffId);
    }

    /**
     * 根据userId查询员工信息
     * @param userId
     * @return
     */
    @Override
    public StaffManage selectStaffManageByUserId(Long userId) {
        return staffManageMapper.selectStaffManageByUserId(userId);
    }
}
