package com.salpa.subject.service.impl;

import com.github.pagehelper.PageHelper;
import com.salpa.common.constant.UserConstants;
import com.salpa.subject.domain.MonitorSubject;
import com.salpa.subject.mapper.SubjectMapper;
import com.salpa.subject.service.SubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SubjectServiceImpl implements SubjectService {

    @Autowired
    private SubjectMapper subjectMapper;

    /**
     * 获取主题管理列表
     * @author zhuff
     * @param pageNum
     * @param pageSize
     * @param subjectName
     * @return
     */
    @Override
    public List<MonitorSubject> getList(Integer pageNum,Integer pageSize,String subjectName) {
        PageHelper.startPage(pageNum,pageSize);
        List<MonitorSubject> list = subjectMapper.getList(subjectName);
        for (MonitorSubject monitorSubject : list) {
            int count = subjectMapper.selectCount(monitorSubject.getId());
            monitorSubject.setProjectCount(count);
        }
        return list;
    }

    /**
     * 根据项目id获取主题管理
     * @author zhuff
     * @param id
     * @return
     */
    @Override
    public MonitorSubject getById(Integer id) {
        return subjectMapper.getById(id);
    }

    /**
     * 判断主题名称是否存在
     * @param subjectName
     * @return
     */
    @Override
    public String checkSubjectNameUnique(String subjectName) {
        int count = subjectMapper.checkSubjectNameUnique(subjectName);
        if (count > 0)
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 新增主题管理
     * @author zhuff
     * @param monitorSubject
     * @return
     */
    @Override
    public int addSubject(MonitorSubject monitorSubject) {
        return subjectMapper.addSubject(monitorSubject);
    }

    /**
     * 修改主题管理
     * @author zhuff
     * @param monitorSubject
     * @return
     */
    @Override
    public int updateSubject(MonitorSubject monitorSubject) {
        return subjectMapper.updateSubject(monitorSubject);
    }

    /**
     * 删除主题管理
     * @author zhuff
     * @param id
     * @return
     */
    @Override
    public int deleteSubject(Integer id) {
        return subjectMapper.deleteSubject(id);
    }
}
