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.SentenceQuestionMapper;
import com.qxgmat.data.dao.UserSentenceProcessMapper;
import com.qxgmat.data.dao.entity.SentenceQuestion;
import com.qxgmat.data.dao.entity.User;
import com.qxgmat.data.dao.entity.UserSentenceProcess;
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.Collection;
import java.util.List;

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

    @Resource
    private UserSentenceProcessMapper userSentenceProcessMapper;

    /**
     * 更新长难句文章进度
     * @param userId
     * @param chapter
     * @param part
     * @param process
     * @return
     */
    @Transactional
    public UserSentenceProcess updateProcess(Integer userId, Integer chapter, Integer part, Integer process, Integer maxPart){
        Example example = new Example(UserSentenceProcess.class);
        example.and(
                example.createCriteria()
                        .andEqualTo("userId", userId)
                        .andEqualTo("chapter", chapter)
                        .andEqualTo("part", part)
        );
        UserSentenceProcess entity;
        UserSentenceProcess in = one(userSentenceProcessMapper, example);
        if (in != null){
            if (in.getProcess() <= process){
                in.setProcess(process);
                entity = edit(in);
                return entity;
            }else{
                entity = in;
            }
        }else{
            entity = add(UserSentenceProcess.builder()
            .userId(userId)
            .chapter(chapter)
            .part(part)
            .process(process).build());
        }
        // 计算本章节的总体进度
        List<UserSentenceProcess> list = listByChapter(userId, chapter);
        Integer total = 0;
        for (UserSentenceProcess p: list) {
            total += p.getProcess();
        }
        updateTotalProcess(userId, chapter, (total * 100) / (maxPart * 100));
        return entity;
    }

    /**
     * 更新单个章节总体进度
     * @param userId
     * @param chapter
     * @param process
     */
    private void updateTotalProcess(Integer userId, Integer chapter, Integer process){
        Example example = new Example(UserSentenceProcess.class);
        example.and(
                example.createCriteria()
                        .andEqualTo("userId", userId)
                        .andEqualTo("chapter", chapter)
                        .andEqualTo("part", 0)
        );
        UserSentenceProcess in = one(userSentenceProcessMapper, example);
        if (in != null){
            if (in.getProcess() <= process){
                in.setProcess(process);
                edit(in);
            }
        }else{
            add(UserSentenceProcess.builder()
                    .userId(userId)
                    .chapter(chapter)
                    .part(0)
                    .process(process).build());
        }

    }

    /**
     * 获取用户单个章节的所有进度
     * @param userId
     * @param chapter
     * @return
     */
    public List<UserSentenceProcess> listByChapter(Integer userId, Integer chapter){
        Example example = new Example(UserSentenceProcess.class);
        example.and(
                example.createCriteria()
                        .andEqualTo("userId", userId)
                        .andEqualTo("chapter", chapter)
                        .andNotEqualTo("part", 0)
        );
        return select(userSentenceProcessMapper, example);
    }

    /**
     * 获取用户所有章节进度：试用情况下
     * @param userId
     * @return
     */
    public List<UserSentenceProcess> listAllByTrail(Integer userId){
        Example example = new Example(UserSentenceProcess.class);
        example.and(
                example.createCriteria()
                        .andEqualTo("userId", userId)
                        .andNotEqualTo("part", 0)
        );
        return select(userSentenceProcessMapper, example);
    }

    /**
     * 获取用户所有章节总体进度
     * @param userId
     * @return
     */
    public List<UserSentenceProcess> listTotal(Integer userId){
        Example example = new Example(UserSentenceProcess.class);
        example.and(
                example.createCriteria()
                        .andEqualTo("userId", userId)
                        .andEqualTo("part", 0)
        );
        return select(userSentenceProcessMapper, example);
    }

    public UserSentenceProcess add(UserSentenceProcess process){
        int result = insert(userSentenceProcessMapper, process);
        process = one(userSentenceProcessMapper, process.getId());
        if(process == null){
            throw new SystemException("进度添加失败");
        }
        return process;
    }

    public UserSentenceProcess edit(UserSentenceProcess process){
        UserSentenceProcess in = one(userSentenceProcessMapper, process.getId());
        if(in == null){
            throw new ParameterException("进度不存在");
        }
        int result = update(userSentenceProcessMapper, process);
        return process;
    }

    public boolean delete(Number id){
        UserSentenceProcess in = one(userSentenceProcessMapper, id);
        if(in == null){
            throw new ParameterException("进度不存在");
        }
        int result = delete(userSentenceProcessMapper, id);
        return result > 0;
    }

    public UserSentenceProcess get(Number id){
        UserSentenceProcess in = one(userSentenceProcessMapper, id);

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

    public Page<UserSentenceProcess> select(int page, int pageSize){
        return select(userSentenceProcessMapper, page, pageSize);
    }

    public List<UserSentenceProcess> select(Collection ids){
        return select(userSentenceProcessMapper, ids);
    }

}
