package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.TServiceManage;
import com.ruoyi.system.domain.dto.TUserProfileDto;
import com.ruoyi.system.domain.dto.UserAgeDto;
import com.ruoyi.system.mapper.TEmergentPeopleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.TEmergentPeople;
import com.ruoyi.system.mapper.TUserProfileMapper;
import com.ruoyi.system.domain.TUserProfile;
import com.ruoyi.system.service.ITUserProfileService;

/**
 * 用户档案Service业务层处理
 * 
 * @author zhengjie
 * @date 2023-08-25
 */
@Service
public class TUserProfileServiceImpl implements ITUserProfileService 
{
    @Autowired
    private TUserProfileMapper tUserProfileMapper;
    @Autowired
    private TEmergentPeopleMapper tEmergentPeopleMapper;

    /**
     * 查询用户档案
     * 
     * @param id 用户档案主键
     * @return 用户档案
     */
    @Override
    public TUserProfile selectTUserProfileById(Integer id)
    {
        return tUserProfileMapper.selectTUserProfileById(id);
    }

    /**
     * 查询用户档案列表
     * 
     * @param tUserProfile 用户档案
     * @return 用户档案
     */
    @Override
    public List<TUserProfile> selectTUserProfileList(TUserProfile tUserProfile)
    {
        List<TUserProfile> tUserProfileList = tUserProfileMapper.selectTUserProfileList(tUserProfile);
        for(TUserProfile list : tUserProfileList){
            list.setTEmergentPeopleList(tEmergentPeopleMapper.selectTEmergentPeopleByProfileId(list.getId()));
        }
        return tUserProfileList;
    }

    /**
     * 查询用户档案列表
     *
     * @param tUserProfileDto 用户档案
     * @return 用户档案集合
     */
    @Override
    public List<TUserProfileDto> selectTUserProfileListByDevice(TUserProfileDto tUserProfileDto) {
        return tUserProfileMapper.selectTUserProfileListByDevice(tUserProfileDto);
    }

    /**
     * 新增用户档案
     * 
     * @param tUserProfile 用户档案
     * @return 结果
     */
    @Transactional
    @Override
    public int insertTUserProfile(TUserProfile tUserProfile)
    {
        tUserProfile.setCreateTime(DateUtils.getNowDate());
        int rows = tUserProfileMapper.insertTUserProfile(tUserProfile);
        insertTEmergentPeople(tUserProfile);
        return rows;
    }

    /**
     * 修改用户档案
     * 
     * @param tUserProfile 用户档案
     * @return 结果
     */
    @Transactional
    @Override
    public int updateTUserProfile(TUserProfile tUserProfile)
    {
        tUserProfileMapper.deleteTEmergentPeopleByProfileId(tUserProfile.getId());
        insertTEmergentPeople(tUserProfile);
        return tUserProfileMapper.updateTUserProfile(tUserProfile);
    }

    /**
     * 批量删除用户档案
     * 
     * @param ids 需要删除的用户档案主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTUserProfileByIds(Integer[] ids)
    {
        tUserProfileMapper.deleteTEmergentPeopleByProfileIds(ids);
        return tUserProfileMapper.deleteTUserProfileByIds(ids);
    }

    /**
     * 删除用户档案信息
     * 
     * @param id 用户档案主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTUserProfileById(Integer id)
    {
        tUserProfileMapper.deleteTEmergentPeopleByProfileId(id);
        return tUserProfileMapper.deleteTUserProfileById(id);
    }

    @Override
    public String importTUserProfile(List<TUserProfile> tUserProfileList) {

        if (StringUtils.isNull(tUserProfileList) || tUserProfileList.size() == 0)
        {
            throw new ServiceException("导入用户档案信息数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (TUserProfile tUserProfile : tUserProfileList)
        {
            try
            {
                if (checkManageData(tUserProfile))
                {
                    this.insertTUserProfile(tUserProfile);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "用户档案信息" + " 导入成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "用户档案信息" + " 信息不完整");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "用户档案信息" + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public List<UserAgeDto> selectuserAgeDto() {
        return tUserProfileMapper.selectuserAgeDto();
    }

    @Override
    public Integer selectuserAgeCount() {
        return tUserProfileMapper.selectuserAgeCount();
    }

    public boolean checkManageData(TUserProfile tUserProfile){
        return StringUtils.isNotNull(tUserProfile.getName()) && StringUtils.isNotNull(tUserProfile.getGender()) &&
                StringUtils.isNotNull(tUserProfile.getSfzid()) && StringUtils.isNotNull(tUserProfile.getTelOne()) &&
                StringUtils.isNotNull(tUserProfile.getBirthday());
    }

    /**
     * 新增紧急联系人信息
     * 
     * @param tUserProfile 用户档案对象
     */
    public void insertTEmergentPeople(TUserProfile tUserProfile)
    {
        List<TEmergentPeople> tEmergentPeopleList = tUserProfile.getTEmergentPeopleList();
        Integer id = tUserProfile.getId();
        if (StringUtils.isNotNull(tEmergentPeopleList))
        {
            List<TEmergentPeople> list = new ArrayList<TEmergentPeople>();
            for (TEmergentPeople tEmergentPeople : tEmergentPeopleList)
            {
                tEmergentPeople.setProfileId(id);
                list.add(tEmergentPeople);
            }
            if (list.size() > 0)
            {
                tUserProfileMapper.batchTEmergentPeople(list);
            }
        }
    }
}
