package com.ruoyi.care.service.impl;

import java.util.ArrayList;
import java.util.List;

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.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysUserRole;
import com.ruoyi.system.mapper.SysRoleMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.mapper.SysUserRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.care.mapper.CareCompaniesMapper;
import com.ruoyi.care.domain.CareCompanies;
import com.ruoyi.care.service.ICareCompaniesService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 护理公司Service业务层处理
 * 
 * @author qy
 * @date 2024-11-26
 */
@Service
public class CareCompaniesServiceImpl implements ICareCompaniesService 
{
    @Autowired
    private CareCompaniesMapper careCompaniesMapper;
    @Autowired
    private SysUserMapper userMapper;
    @Autowired
    private SysUserRoleMapper userRoleMapper;
    @Autowired
    private SysRoleMapper roleMapper;

    /**
     * 查询护理公司
     * 
     * @param companyId 护理公司主键
     * @return 护理公司
     */
    @Override
    public CareCompanies selectCareCompaniesByCompanyId(Long companyId)
    {
        return careCompaniesMapper.selectCareCompaniesByCompanyId(companyId);
    }

    /**
     * 查询护理公司列表
     * 
     * @param careCompanies 护理公司
     * @return 护理公司
     */
    @Override
    public List<CareCompanies> selectCareCompaniesList(CareCompanies careCompanies)
    {
        return careCompaniesMapper.selectCareCompaniesList(careCompanies);
    }

    /**
     * 新增护理公司
     * 
     * @param careCompanies 护理公司
     * @return 结果
     */
    @Override
    @Transactional
    public int insertCareCompanies(CareCompanies careCompanies)
    {
        //新增用户信息
        SysUser user = new SysUser();
        user.setUserName(careCompanies.getLoginName());
        user.setNickName(careCompanies.getName());
        if(StringUtils.isNotEmpty(careCompanies.getPassword())){
            user.setPassword(SecurityUtils.encryptPassword(careCompanies.getPassword()));
        }
        user.setUserType("01");//企业用户
        userMapper.insertUser(user);

        // 新增企业信息信息
        careCompanies.setCreateTime(DateUtils.getNowDate());
        careCompanies.setUserId(user.getUserId());
        int rows =careCompaniesMapper.insertCareCompanies(careCompanies);


        //获取企业权限company
        SysRole role=roleMapper.checkRoleKeyUnique("company");
        // 新增用户与角色管理
        insertUserRole(user.getUserId(), role.getRoleId());

        return rows;
    }
    /**
     * 新增用户角色信息
     *
     * @param userId 用户ID
     * @param roleId 角色组
     */
    public void insertUserRole(Long userId,Long roleId)
    {
        if (StringUtils.isNotEmpty(roleId.toString()))
        {
            // 新增用户与角色管理
            List<SysUserRole> list = new ArrayList<SysUserRole>(0);
            SysUserRole ur = new SysUserRole();
            ur.setUserId(userId);
            ur.setRoleId(roleId);
            list.add(ur);
            userRoleMapper.batchUserRole(list);
        }
    }

    /**
     * 修改护理公司
     * 
     * @param careCompanies 护理公司
     * @return 结果
     */
    @Override
    @Transactional
    public int updateCareCompanies(CareCompanies careCompanies)
    {
        Long userId = careCompanies.getUserId();
        // 修改用户信息
        SysUser user=userMapper.selectUserById(userId);
        user.setUserName(careCompanies.getLoginName());
        user.setNickName(careCompanies.getName());
        if(StringUtils.isNotEmpty(careCompanies.getPassword())){
            user.setPassword(SecurityUtils.encryptPassword(careCompanies.getPassword()));
        }
        userMapper.updateUser(user);

        careCompanies.setUpdateTime(DateUtils.getNowDate());
        return careCompaniesMapper.updateCareCompanies(careCompanies);
    }

    /**
     * 批量删除护理公司
     * 
     * @param companyIds 需要删除的护理公司主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteCareCompaniesByCompanyIds(Long[] companyIds)
    {
        Long[] userIds=new Long[companyIds.length];
        for (int i = 0; i < companyIds.length; i++) {
            Long companyId = companyIds[i];
            CareCompanies careCompanies=careCompaniesMapper.selectCareCompaniesByCompanyId(companyId);
            userIds[i]=careCompanies.getUserId();
        }
        // 删除用户与角色关联
        userRoleMapper.deleteUserRole(userIds);
        // 删除用户
        userMapper.deleteUserByIds(userIds);

        return careCompaniesMapper.deleteCareCompaniesByCompanyIds(companyIds);
    }

    /**
     * 删除护理公司信息
     * 
     * @param companyId 护理公司主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteCareCompaniesByCompanyId(Long companyId)
    {
        CareCompanies careCompanies=careCompaniesMapper.selectCareCompaniesByCompanyId(companyId);
        // 删除用户与角色关联
        userRoleMapper.deleteUserRoleByUserId(careCompanies.getUserId());
        // 删除用户
        userMapper.deleteUserById(careCompanies.getUserId());

        return careCompaniesMapper.deleteCareCompaniesByCompanyId(companyId);
    }
}
