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.UserClassMapper;
import com.qxgmat.data.dao.entity.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

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

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

    @Resource
    private UserClassMapper userClassMapper;

    @Resource
    private UserPayService userPayService;

    // 开通服务
    public boolean used(UserPay pay){
        // todo 分析时长
        add(UserClass.builder().userId(pay.getUserId()).courseId(Integer.valueOf(pay.getModuleExtend())).build());
        return true;
    }

    /**
     * 合并用户信息，将old转移至new
     * @param oldUserId
     * @param newUserId
     */
    public void mergeUser(Number oldUserId, Integer newUserId){
        Example example = new Example(UserClass.class);
        example.and(
                example.createCriteria().andEqualTo("userId", oldUserId)
        );
        update(userClassMapper, example, UserClass.builder().userId(newUserId).build());
    }

    /**
     * 获取用户课程列表
     * @param userId
     * @return
     */
    public List<UserClass> getByUser(Number userId){
        Example example = new Example(UserService.class);
        example.and(
                example.createCriteria()
                        .andEqualTo("userId", userId)
                        .andGreaterThanOrEqualTo("startTime", new Date())
                        .andLessThan("expireTime", new Date())
        );
        return select(userClassMapper, example);
    }

    public Map<Object, Collection<UserClass>> mapByUser(Collection userIds, Boolean time){
        Map<Object, Collection<UserClass>> relationMap = new HashMap<>();
        if(userIds.size() == 0) return relationMap;
        Example example = new Example(UserService.class);
        example.and(
                example.createCriteria()
                        .andIn("userId", userIds)
        );
        if (time){
            example.and(
                    example.createCriteria()
                            .andGreaterThanOrEqualTo("startTime", new Date())
                            .andLessThan("expireTime", new Date())
            );
        }
        List<UserClass> userClassList = select(userClassMapper, example);
        if(userClassList.size() == 0) return relationMap;
        for(UserClass row: userClassList){
            List<UserClass> l;
            Number userId = row.getUserId();
            if(!relationMap.containsKey(userId)){
                l = new ArrayList<>();
                relationMap.put(userId, l);
            }else{
                l = (List<UserClass>)relationMap.get(userId);
            }
            l.add(row);
        }
        return relationMap;
    }

    public UserClass add(UserClass cls){
        int result = insert(userClassMapper, cls);
        cls = one(userClassMapper, cls.getId());
        if(cls == null){
            throw new SystemException("课程记录添加失败");
        }
        return cls;
    }

    public UserClass edit(UserClass cls){
        UserClass in = one(userClassMapper, cls.getId());
        if(in == null){
            throw new ParameterException("课程记录不存在");
        }
        int result = update(userClassMapper, cls);
        return cls;
    }

    public boolean delete(Number id){
        UserClass in = one(userClassMapper, id);
        if(in == null){
            throw new ParameterException("课程记录不存在");
        }
        int result = delete(userClassMapper, id);
        return result > 0;
    }

    public UserClass get(Number id){
        UserClass in = one(userClassMapper, id);

        if(in == null){
            throw new ParameterException("课程记录不存在");
        }
        return in;
    }

    public Page<UserClass> select(int page, int pageSize){
        return select(userClassMapper, page, pageSize);
    }

    public List<UserClass> select(Collection ids){
        return select(userClassMapper, ids);
    }

}
