package cn.exploring.engine.server.db.service;

import cn.exploring.engine.server.db.dao.ClassInfoMapper;
import cn.exploring.engine.server.db.domain.ClassInfo;
import cn.exploring.engine.server.db.domain.ClassInfoExample;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class ClassService {

    @Resource
    ClassInfoMapper classInfoMapper;

    public Map<Integer, String> getClassNameMap() {
        ClassInfoExample example = new ClassInfoExample();
        ClassInfoExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);
        List<ClassInfo> dataList = classInfoMapper.selectByExample(example);
        return dataList.stream().map(x -> {
            Map oneData = new HashMap();
            oneData.put(x.getId(), x.getClassName());
            return oneData;
        }).reduce(new HashMap(), (x, y) -> {
            x.putAll(y);
            return x;
        });
    }

    public List<ClassInfo> selectClass(String className, List<Integer> idList) {
        ClassInfoExample example = new ClassInfoExample();
        ClassInfoExample.Criteria criteria = example.createCriteria();
        if (idList != null && idList.size() > 0) {
            criteria.andIdIn(idList);
        }
        if (!StringUtils.isEmpty(className)) {
            criteria.andClassNameEqualTo(className);
        }
        criteria.andDeletedEqualTo(false);
        return classInfoMapper.selectByExample(example);
    }

    public Map webSelectClass(String className,  Integer page, Integer limit, String sort, String order) {
        ClassInfoExample example = new ClassInfoExample();
        ClassInfoExample.Criteria criteria = example.createCriteria();

        criteria.andDeletedEqualTo(false);
        if (!StringUtils.isEmpty(className)) {
            criteria.andClassNameLike("%" + className + "%");
        }
        PageHelper.startPage(page, limit);
        example.setOrderByClause(sort + " "  + order);
        List<ClassInfo> dataList =  classInfoMapper.selectByExample(example);
        Long total = PageInfo.of(dataList).getTotal();
        return new HashMap(){{
            put("total", total);
            put("items", dataList);
        }};
    }

    /**
     * 添加班级信息
     * @param className
     * @return
     */
    public String addClassInfo(String className) {
        List<ClassInfo> classInfoList = selectClass(className, null);
        if (classInfoList != null && classInfoList.size() != 0) {
            return "fail 当前已存在该班级";
        }
        ClassInfo classInfo = new ClassInfo();
        LocalDateTime now = LocalDateTime.now();
        classInfo.setAddTime(now);
        classInfo.setUpdateTime(now);
        classInfo.setDeleted(false);
        classInfo.setClassName(className);
        classInfoMapper.insertSelective(classInfo);
        return "OK 添加班级成功";
    }

    public void updateClassInfo(ClassInfo classInfo) {
        LocalDateTime now = LocalDateTime.now();
        classInfo.setUpdateTime(now);
        classInfoMapper.updateByPrimaryKey(classInfo);
    }

    public Object batchDeletedData(List<Integer> idList) {
        List<ClassInfo> classInfoList = selectClass(null, idList);
        classInfoList.forEach(x -> {
            x.setDeleted(null);
            updateClassInfo(x);
        });
        return "batch deleted  OK";
    }

    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public List<ClassInfo> listAll() {
        return classInfoMapper.selectByExample(new ClassInfoExample().createCriteria()
                        .andDeletedEqualTo(ClassInfo.NOT_DELETED)
                .example());
    }

    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public ClassInfo selectByPrimaryKey(Integer id) {
        return Objects.isNull(id) ? null
                : classInfoMapper.selectByExample(new ClassInfoExample().createCriteria()
                                .andDeletedEqualTo(ClassInfo.NOT_DELETED)
                                .andIdEqualTo(id)
                        .example()).stream().findFirst().orElse(null);
    }

}
