package com.nantian.cloud.dbcapp.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.nantian.cloud.dbcapp.entity.ExceptionTypeModel;
import com.nantian.cloud.dbcapp.mapper.record.ExceptionTypeMapper;
import com.nantian.cloud.dbcapp.service.ExceptionTypeService;
import com.nantian.sc.cloud.common.core.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;


/**
 * <p>
 * 异常分类 服务实现类
 * </p>
 *
 * @author zfw
 * @since 2020-08-05
 */
@Service("exceptionTypeService")
public class ExceptionTypeServiceImpl implements ExceptionTypeService {

    @Autowired
    private ExceptionTypeMapper exceptionTypeMapper;

    /***
    * @Description: 查询列表
    * @Param: []
    * @return: com.nantian.cloud.dbcapp.entity.ExceptionTypeModel
    * @Author: zfw
    * @Date: 2021/2/22
    * @Date: 15:40
    */
    @Override
    public List<ExceptionTypeModel> getList(){
        List<ExceptionTypeModel> list=exceptionTypeMapper.getList();
        return  list;
    }

    /**
    * @Description: 新增或修改
    * @Param: [exceptionTypeModelDto]
    * @return: com.nantian.cloud.dbcapp.entity.ExceptionTypeModel
    * @Author: zfw
    * @Date: 2021/2/22
    * @Date: 15:46
    */
    @Override
    public R addOrUpdate(ExceptionTypeModel exceptionTypeModel) {
        int flag=0;
        if (exceptionTypeModel.getId() !=null ){ //修改
            exceptionTypeModel.setUpdateTime(LocalDateTime.now());
             flag=exceptionTypeMapper.updateById(exceptionTypeModel);
        }else{ //新增
            //查询异常是否已经存在
            ExceptionTypeModel exceptionTypeModel1=exceptionTypeMapper.selectOne(new QueryWrapper<ExceptionTypeModel>().eq(ExceptionTypeModel.TYPE, exceptionTypeModel.getType()).eq(ExceptionTypeModel.DELETEFLAG,0));
            if(exceptionTypeModel1!=null ){
                return R.failed(0,"该类型已存在");
            }else{
                exceptionTypeModel.setCreateTime(LocalDateTime.now());
                flag= exceptionTypeMapper.insert(exceptionTypeModel);
            }
        }
        if (flag>0){
            return R.ok(1,"操作成功");
        }else{
            return R.failed(0,"操作失败");
        }

    }

    /**
    * @Description: 通过id删除
    * @Param: [id]
    * @return: com.nantian.sc.cloud.common.core.util.R
    * @Author: zfw
    * @Date: 2021/2/22
    * @Date: 16:20
    */
    @Override
    public R deleteById(Integer id) {
        ExceptionTypeModel noticeModel=exceptionTypeMapper.selectById(id);
        noticeModel.setDeleteFlag(1);
        noticeModel.setDeleteTime(LocalDateTime.now());
        int row = this.exceptionTypeMapper.updateById(noticeModel);

        if (row > 0) {
            return R.ok(1,"操作成功");
        } else {
            return R.failed(0,"操作失败");
        }

    }


}

