package com.srs.comprehensive.service.impl;

import java.util.List;

import com.srs.common.exception.ServiceException;
import com.srs.common.utils.DateUtils;
import com.srs.common.utils.StringUtils;
import com.srs.common.utils.bean.BeanValidators;
import com.srs.comprehensive.domain.SrsStudent;
import com.srs.comprehensive.mapper.SrsStudentMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.srs.comprehensive.mapper.SrsDormitoryMapper;
import com.srs.comprehensive.domain.SrsDormitory;
import com.srs.comprehensive.service.ISrsDormitoryService;

import javax.validation.Validator;

/**
 * 宿舍信息Service业务层处理
 * 
 * @author srs
 * @date 2023-06-27
 */
@Service
public class SrsDormitoryServiceImpl implements ISrsDormitoryService 
{
    private static final Logger log = LoggerFactory.getLogger(SrsStudentServiceImpl.class);
    @Autowired
    private SrsDormitoryMapper srsDormitoryMapper;

    @Autowired
    protected Validator validator;

    /**
     * 查询宿舍信息
     * 
     * @param id 宿舍信息主键
     * @return 宿舍信息
     */
    @Override
    public SrsDormitory selectSrsDormitoryById(Long id)
    {
        return srsDormitoryMapper.selectSrsDormitoryById(id);
    }

    /**
     * 查询宿舍信息列表
     * 
     * @param srsDormitory 宿舍信息
     * @return 宿舍信息
     */
    @Override
    public List<SrsDormitory> selectSrsDormitoryList(SrsDormitory srsDormitory)
    {
        return srsDormitoryMapper.selectSrsDormitoryList(srsDormitory);
    }

    /**
     * 新增宿舍信息
     * 
     * @param srsDormitory 宿舍信息
     * @return 结果
     */
    @Override
    public int insertSrsDormitory(SrsDormitory srsDormitory)
    {
        srsDormitory.setCreateTime(DateUtils.getNowDate());
        return srsDormitoryMapper.insertSrsDormitory(srsDormitory);
    }

    /**
     * 修改宿舍信息
     * 
     * @param srsDormitory 宿舍信息
     * @return 结果
     */
    @Override
    public int updateSrsDormitory(SrsDormitory srsDormitory)
    {
        srsDormitory.setUpdateTime(DateUtils.getNowDate());
        return srsDormitoryMapper.updateSrsDormitory(srsDormitory);
    }

    /**
     * 批量删除宿舍信息
     * 
     * @param ids 需要删除的宿舍信息主键
     * @return 结果
     */
    @Override
    public int deleteSrsDormitoryByIds(Long[] ids)
    {
        return srsDormitoryMapper.deleteSrsDormitoryByIds(ids);
    }

    /**
     * 删除宿舍信息信息
     * 
     * @param id 宿舍信息主键
     * @return 结果
     */
    @Override
    public int deleteSrsDormitoryById(Long id)
    {
        return srsDormitoryMapper.deleteSrsDormitoryById(id);
    }

    /**
     * 宿舍信息导入
     * @param dormitoryList 宿舍信息列表
     * @param isUpdateSupport 是否更新
     * @param operName 操作人
     * @return
     */
    @Override
    public String importDormitory(List<SrsDormitory> dormitoryList, Boolean isUpdateSupport, String operName)
    {
        if (StringUtils.isNull(dormitoryList) || dormitoryList.size() == 0)
        {
            throw new ServiceException("导入宿舍信息数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
//        String password = configService.selectConfigByKey("sys.user.initPassword");
        for (SrsDormitory dormitory : dormitoryList )
        {
            try
            {
                // 验证是否存在这个宿舍

                SrsDormitory dormitory1 = srsDormitoryMapper.selectSrsDormitoryBedNum(dormitory);
                if (StringUtils.isNull(dormitory1))
                {
                    BeanValidators.validateWithException(validator, dormitory);//验证数据
                    dormitory.setCreateBy(operName);
                    this.insertSrsDormitory(dormitory);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、单元号 " + dormitory.getUnitId() +"-"+ dormitory.getRoomNo() +" 导入成功");
                }
                else if (isUpdateSupport)
                {
                    BeanValidators.validateWithException(validator, dormitory);

//                    checkUserDataScope(user.getUserId());
                    dormitory.setUpdateBy(operName);
                    this.updateSrsDormitory(dormitory);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、单元号 " + dormitory.getUnitId() +"-"+ dormitory.getRoomNo()+ " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、单元号 " + dormitory.getUnitId() +"-"+ dormitory.getRoomNo()+" 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、单元号 " + dormitory.getUnitId() +"-"+ dormitory.getRoomNo()+" 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
