package com.qxgmat.service.inline;

import com.github.pagehelper.Page;
import com.nuliji.tools.AbstractService;
import com.nuliji.tools.exception.ParameterException;
import com.nuliji.tools.exception.SystemException;
import com.nuliji.tools.mybatis.Example;
import com.qxgmat.data.dao.UserCourseProgressMapper;
import com.qxgmat.data.dao.entity.UserCourse;
import com.qxgmat.data.dao.entity.UserCourseProgress;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

@Service
public class UserCourseProgressService extends AbstractService {
    private static final Logger logger = LoggerFactory.getLogger(UserCourseProgressService.class);

    @Resource
    private UserCourseProgressMapper userCourseProgressMapper;

    /**
     * 更新课时进度
     * @param userId
     * @param courseId
     * @param courseNoId
     * @param progress
     * @return
     */
    @Transactional
    public UserCourseProgress updateProgress(Integer userId, Integer courseId, Integer courseNoId, Integer progress, Integer recordId){
        Example example = new Example(UserCourseProgress.class);
        example.and(
                example.createCriteria()
                        .andEqualTo("userId", userId)
                        .andEqualTo("recordId", recordId)
                        .andEqualTo("courseId", courseId)
                        .andEqualTo("courseNoId", courseNoId)
        );
        UserCourseProgress entity;
        UserCourseProgress in = one(userCourseProgressMapper, example);
        if (in != null){
            if (in.getProgress() <= progress) {
                in.setProgress(progress);
                entity = edit(in);
            }else if(in.getProgress() == 100){
                in.setProgress(progress);
                in.setTimes(in.getTimes() + 1);
                entity = edit(in);
                return entity;
            }else{
                entity = in;
                // 不用更新进度
                return entity;
            }
        }else{
            entity = add(UserCourseProgress.builder()
                    .userId(userId)
                    .recordId(recordId)
                    .courseId(courseId)
                    .courseNoId(courseNoId)
                    .times(0)
                    .progress(progress).build());
        }
        return entity;
    }

    public List<UserCourseProgress> listCourse(Integer recordId, Integer courseId){
        Example example = new Example(UserCourseProgress.class);
        example.and(
                example.createCriteria()
                    .andEqualTo("recordId", recordId)
                    .andEqualTo("courseId", courseId)
        );
        return select(userCourseProgressMapper, example);
    }

    public List<UserCourseProgress> getByRecordId(Integer recordId){
        Example example = new Example(UserCourseProgress.class);
        example.and(
                example.createCriteria()
                        .andEqualTo("recordId", recordId)
        );
        return select(userCourseProgressMapper, example);
    }


    /**
     * 获取课程记录分组列表
     * @param recordIds
     * @return
     */
    public Map<Object, Collection<UserCourseProgress>> groupByRecordId(Collection recordIds){
        Map<Object, Collection<UserCourseProgress>> relationMap = new HashMap<>();
        if (recordIds == null || recordIds.size() == 0) return relationMap;
        Example example = new Example(UserCourseProgress.class);
        example.and(
                example.createCriteria()
                        .andIn("recordId", recordIds)
        );
        List<UserCourseProgress> nos =  select(userCourseProgressMapper, example);
        Collection<UserCourseProgress> list;
        for(UserCourseProgress no : nos){
            if (!relationMap.containsKey(no.getRecordId())){
                list = new ArrayList<>();
                relationMap.put(no.getRecordId(), list);
            }else{
                list = relationMap.get(no.getRecordId());
            }
            list.add(no);
        }
        return relationMap;
    }

    public UserCourseProgress add(UserCourseProgress progress){
        int result = insert(userCourseProgressMapper, progress);
        progress = one(userCourseProgressMapper, progress.getId());
        if(progress == null){
            throw new SystemException("统计添加失败");
        }
        return progress;
    }

    public UserCourseProgress edit(UserCourseProgress progress){
        UserCourseProgress in = one(userCourseProgressMapper, progress.getId());
        if(in == null){
            throw new ParameterException("统计不存在");
        }
        int result = update(userCourseProgressMapper, progress);
        return progress;
    }

    public boolean delete(Number id){
        UserCourseProgress in = one(userCourseProgressMapper, id);
        if(in == null){
            throw new ParameterException("统计不存在");
        }
        int result = delete(userCourseProgressMapper, id);
        return result > 0;
    }

    public UserCourseProgress get(Number id){
        UserCourseProgress in = one(userCourseProgressMapper, id);

        if(in == null){
            throw new ParameterException("统计不存在");
        }
        return in;
    }

    public Page<UserCourseProgress> select(int page, int pageSize){
        return select(userCourseProgressMapper, page, pageSize);
    }

    public Page<UserCourseProgress> select(Integer[] ids){
        return page(()->select(userCourseProgressMapper, ids), 1, ids.length);
    }

    public List<UserCourseProgress> select(Collection ids){
        return select(userCourseProgressMapper, ids);
    }
}