package com.ruoyi.system.service.impl;

import java.util.List;

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.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.KeyPeopleInfoMapper;
import com.ruoyi.system.domain.KeyPeopleInfo;
import com.ruoyi.system.service.IKeyPeopleInfoService;
import org.springframework.transaction.annotation.Transactional;

import javax.management.relation.Role;

/**
 * 重点人群信息Service业务层处理
 * 
 * @author boman
 * @date 2022-09-27
 */
@Service
public class KeyPeopleInfoServiceImpl implements IKeyPeopleInfoService 
{
    @Autowired
    private KeyPeopleInfoMapper keyPeopleInfoMapper;

    /**
     * 查询重点人群信息
     * 
     * @param id 重点人群信息主键
     * @return 重点人群信息
     */
    @Override
    public KeyPeopleInfo selectKeyPeopleInfoById(Long id)
    {
        return keyPeopleInfoMapper.selectKeyPeopleInfoById(id);
    }

    /**
     * 查询重点人群信息列表
     * 
     * @param keyPeopleInfo 重点人群信息
     * @return 重点人群信息
     */
    @Override
    public List<KeyPeopleInfo> selectKeyPeopleInfoList(KeyPeopleInfo keyPeopleInfo)
    {
        //权限控制
        SysUser user = SecurityUtils.getLoginUser().getUser();
        List<SysRole> roles = user.getRoles();
        String deptId = user.getDeptId().toString();
        for (SysRole role : roles) {
            if("超级管理员".equals(role.getRoleName())){
                deptId = "";
                break;
            }
        }
        keyPeopleInfo.setDeptId(deptId);
        return keyPeopleInfoMapper.selectKeyPeopleInfoList(keyPeopleInfo);
    }

    /**
     * 新增重点人群信息
     * 
     * @param keyPeopleInfo 重点人群信息
     * @return 结果
     */
    @Override
    public int insertKeyPeopleInfo(KeyPeopleInfo keyPeopleInfo)
    {
        keyPeopleInfo.setCreateTime(DateUtils.getNowDate());
        return keyPeopleInfoMapper.insertKeyPeopleInfo(keyPeopleInfo);
    }

    /**
     * 修改重点人群信息
     * 
     * @param keyPeopleInfo 重点人群信息
     * @return 结果
     */
    @Override
    public int updateKeyPeopleInfo(KeyPeopleInfo keyPeopleInfo)
    {
        keyPeopleInfo.setUpdateTime(DateUtils.getNowDate());
        return keyPeopleInfoMapper.updateKeyPeopleInfo(keyPeopleInfo);
    }

    /**
     * 批量删除重点人群信息
     * 
     * @param ids 需要删除的重点人群信息主键
     * @return 结果
     */
    @Override
    public int deleteKeyPeopleInfoByIds(Long[] ids)
    {
        return keyPeopleInfoMapper.deleteKeyPeopleInfoByIds(ids);
    }

    /**
     * 删除重点人群信息信息
     * 
     * @param id 重点人群信息主键
     * @return 结果
     */
    @Override
    public int deleteKeyPeopleInfoById(Long id)
    {
        return keyPeopleInfoMapper.deleteKeyPeopleInfoById(id);
    }

    /**
     * 导入人员信息
     * @param keyPeopleList
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @Async
    public void importUser(List<KeyPeopleInfo> keyPeopleList) {
        //插入数据
        for (KeyPeopleInfo keyPeopleInfo : keyPeopleList) {
            //查询一下这个人是否存在
            List<KeyPeopleInfo> keyPeopleInfos = keyPeopleInfoMapper.selectKeyPeopleInfoList(keyPeopleInfo);
            if (keyPeopleInfos != null && keyPeopleInfos.size() > 0){
                keyPeopleInfoMapper.updateKeyPeopleInfo(keyPeopleInfo);
            }else {
                keyPeopleInfoMapper.insertKeyPeopleInfo(keyPeopleInfo);
            }
        }
    }
}
