ColumnServiceImpl.java 2.64 KB
package com.sincere.student.service.impl;

import com.sincere.student.dto.ColumnDto;
import com.sincere.student.mapper.ColumnMapper;
import com.sincere.student.model.ColumnType;
import com.sincere.student.service.ColumnService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
public class ColumnServiceImpl implements ColumnService {

    @Autowired
    ColumnMapper columnMapper;

    @Override
    public List<ColumnType> getList(ColumnDto columnDto) {
        if (StringUtils.isNotBlank(columnDto.getName())) {
            columnDto.setName("%" + columnDto.getName() + "%");
        }
        return columnMapper.getList(columnDto);
    }

    @Override
    public ColumnType selectDetail(int id) {
        return columnMapper.selectDetail(id);
    }

    @Override
    public int create(ColumnType columnType) {
        //栏目类型
        Integer type = columnType.getType();
        //栏目排序
        Integer sort = columnType.getSort();
        List<ColumnType> countList = columnMapper.countSort(sort.intValue(), type.intValue(), 1, null);
        if (!CollectionUtils.isEmpty(countList)) {
            int i = 1;
            for (ColumnType oldType : countList) {
                int addSort = i + sort;
                if (addSort > oldType.getSort()) {
                    //若已存在自增1
                    oldType.setSort(oldType.getSort() + 1);
                    columnMapper.update(oldType);
                }
                i++;
            }
        }
        return columnMapper.create(columnType);
    }

    @Override
    public int update(ColumnType columnType) {
        ColumnType colum = columnMapper.selectDetail(columnType.getId());
        //栏目类型
        Integer type = columnType.getType();
        //栏目排序
        Integer sort = columnType.getSort();
        List<ColumnType> countList = columnMapper.countSort(sort.intValue(), type.intValue(), 2, columnType.getId());
        if (!CollectionUtils.isEmpty(countList)) {
            int i = 1;
            for (ColumnType oldType : countList) {
                int addSort = i + sort;
                if (addSort > oldType.getSort()) {
                    //若已存在自增1
                    oldType.setSort(oldType.getSort() + 1);
                    columnMapper.update(oldType);
                }
                i++;
            }
        }
        return columnMapper.update(columnType);
    }

    @Override
    public int delete(int id) {
        return columnMapper.delete(id);
    }
}