package com.srs.dormitory.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.comprehensive.domain.SrsStudent;
import com.srs.dormitory.domain.Dto.DmsSearch;
import com.srs.dormitory.domain.Dto.WenMingImport;
import com.srs.dormitory.domain.Vo.DomInfo;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.srs.dormitory.mapper.DmsNewWenmingMapper;
import com.srs.dormitory.domain.DmsNewWenming;
import com.srs.dormitory.service.IDmsNewWenmingService;

/**
 * 文明宿舍Service业务层处理
 *
 * @author srs
 * @date 2024-03-28
 */
@Service
public class DmsNewWenmingServiceImpl extends ServiceImpl<DmsNewWenmingMapper,DmsNewWenming> implements IDmsNewWenmingService {
    @Autowired
    private DmsNewWenmingMapper dmsNewWenmingMapper;

    @Override
    public List<DomInfo> listView(DmsSearch param){
        return dmsNewWenmingMapper.listView(param);
    }

    @Override
   public String importWenMing(List<DmsNewWenming> dmsNewWenmingList, String operName){
        if (StringUtils.isNull(dmsNewWenmingList) || dmsNewWenmingList.size() == 0)
        {
            throw new ServiceException("导入文明宿舍信息数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (DmsNewWenming wenMing :dmsNewWenmingList )
        {
            try
            {
                // 验证是否导入这条记录
                DmsNewWenming wen = dmsNewWenmingMapper.selectDmsNewWenmingById(wenMing.getId());
                if (wen==null)
                {
                    int res=dmsNewWenmingMapper.insert(wenMing);
                    if(res == 1){
                        successNum++;
                        successMsg.append("<br/> 宿舍序号 " + wenMing.getId() + " 导入成功");
                    }else{
                        failureNum++;
                        failureMsg.append("<br/> 宿舍序号 " + wenMing.getId() + " 导入失败");
                    }

                }
                else{
                    int res = dmsNewWenmingMapper.updateById(wenMing);
                    if(res == 1){
                        successNum++;
                        successMsg.append("<br/> 宿舍序号 " + wenMing.getId() + " 更新成功");
                    }else{
                        failureNum++;
                        failureMsg.append("<br/> 宿舍序号 " + wenMing.getId() + " 更新失败");
                    }
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/> 宿舍序号 " + wenMing.getId()  + " 导入失败：";
                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();
    }

    /**
     * 查询文明宿舍
     *
     * @param id 文明宿舍主键
     * @return 文明宿舍
     */
    @Override
    public DmsNewWenming selectDmsNewWenmingById(Long id) {
        return dmsNewWenmingMapper.selectDmsNewWenmingById(id);
    }

    /**
     * 查询文明宿舍列表
     *
     * @param dmsNewWenming 文明宿舍
     * @return 文明宿舍
     */
    @Override
    public List<DmsNewWenming> selectDmsNewWenmingList(DmsNewWenming dmsNewWenming) {
        return dmsNewWenmingMapper.selectDmsNewWenmingList(dmsNewWenming);
    }

    /**
     * 新增文明宿舍
     *
     * @param dmsNewWenming 文明宿舍
     * @return 结果
     */
    @Override
    public int insertDmsNewWenming(DmsNewWenming dmsNewWenming) {
                dmsNewWenming.setCreateTime(DateUtils.getNowDate());
            return dmsNewWenmingMapper.insertDmsNewWenming(dmsNewWenming);
    }

    /**
     * 修改文明宿舍
     *
     * @param dmsNewWenming 文明宿舍
     * @return 结果
     */
    @Override
    public int updateDmsNewWenming(DmsNewWenming dmsNewWenming) {
                dmsNewWenming.setUpdateTime(DateUtils.getNowDate());
        return dmsNewWenmingMapper.updateDmsNewWenming(dmsNewWenming);
    }

    /**
     * 批量删除文明宿舍
     *
     * @param ids 需要删除的文明宿舍主键
     * @return 结果
     */
    @Override
    public int deleteDmsNewWenmingByIds(Long[] ids) {
        return dmsNewWenmingMapper.deleteDmsNewWenmingByIds(ids);
    }

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