package cn.wisenergy.service.app.impl;

import cn.wisenergy.common.constant.CommonAttributes;
import cn.wisenergy.common.utils.DateUtil;
import cn.wisenergy.common.utils.R;
import cn.wisenergy.mapper.RecommendUserMapper;
import cn.wisenergy.mapper.UsersMapper;
import cn.wisenergy.model.app.RecommendUser;
import cn.wisenergy.model.app.User;
import cn.wisenergy.model.dto.RecommendUserDto;
import cn.wisenergy.model.dto.TeamQueryDto;
import cn.wisenergy.model.vo.RecommendUserVo;
import cn.wisenergy.model.vo.UserAndRecommendVo;
import cn.wisenergy.service.app.RecommendUserService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
@Slf4j
public class RecommendUserServiceImpl implements RecommendUserService {

    @Autowired
    RecommendUserMapper recommendUserMapper;

    @Autowired
    UsersMapper usersMapper;


    /**
     * 获取用户的直推信息表数据
     * @param userId
     * @return
     */
    @Override
    public RecommendUser getMyRecommed(String userId) {
        return recommendUserMapper.getByUserId(userId);
    }

    /**
     * 获取我的直推用户信息表
     * @param userId
     * @param userLevel
     * @return
     */
    @Override
    public List<UserAndRecommendVo> getMyRecommendInfo(String userId, Integer userLevel){

        //当前用户的邀请码
        String userIntiveCode = usersMapper.getByUserId(userId).getInviteCode();
        //通过用户的邀请码获取各个等级的直推用户信息
        List<User> byInviteCode = usersMapper.getByInviteCode(userIntiveCode,userLevel);
        List<UserAndRecommendVo> userAndRecommendVos = new ArrayList<>();
        for (User user : byInviteCode) {
            UserAndRecommendVo userAndRecommendVo = new UserAndRecommendVo();
            userAndRecommendVo.setUserLevel(user.getUserLevel());
            userAndRecommendVo.setHeadImage(user.getHeadImage());
            userAndRecommendVo.setUserId(user.getUserId());
            userAndRecommendVo.setMonthyCount(recommendUserMapper.getByUserId(userId).getMonthyCount());
            userAndRecommendVo.setHistoryCount(recommendUserMapper.getByUserId(userId).getHistoryCount());
            userAndRecommendVos.add(userAndRecommendVo);
        }

        return userAndRecommendVos;
    }

    @Override
    public R<PageInfo<RecommendUserVo>> getRecommendUserList(RecommendUserDto recommendUserDto) {

        log.info("shop-mall[]RecommendUserServiceImpl[]getRecommendUserList[]input.param.query:" + recommendUserDto);
        if (null == recommendUserDto) {
            return R.error("入参不能为空!");
        }
        pageHandle(recommendUserDto);
        Map<String, Object> map = new HashMap<>(8);

        if (!StringUtils.isBlank(recommendUserDto.getUserId())) {
            map.put("userId", recommendUserDto.getUserId());
        }
        if (!StringUtils.isBlank(recommendUserDto.getQueryTime())) {
            Date date = DateUtil.convertStrToDate(recommendUserDto.getQueryTime(), "yyyy-MM-dd");
            String queryTime = recommendUserDto.getQueryTime();
            map.put("queryTime", queryTime);
        }


        int total = recommendUserMapper.count(map);

        map.put("startNum", recommendUserDto.getStartNum());
        map.put("endNum", recommendUserDto.getEndNum());
        List<RecommendUserVo> list = recommendUserMapper.getRecommendUserList(map);
        PageInfo<RecommendUserVo> info = new PageInfo<>();
        info.setPageSize(recommendUserDto.getPageSize());
        info.setPageNum(recommendUserDto.getPageNo());
        info.setList(list);
        info.setTotal(total);

        return R.ok(info);
    }

    /**
     * 分页处理方法
     *
     * @param schemeVo 参数
     */
    private void pageHandle(RecommendUserDto schemeVo) {
        Integer pageNum = schemeVo.getPageNo();
        Integer pageSize = schemeVo.getPageSize();
        if (null == pageSize || pageSize == 0) {
            pageSize = 10;
        }
        if (null == pageNum || pageNum == 0) {
            pageNum = 1;
        }
        Integer endNum = pageSize;
        Integer startNum = (pageNum - CommonAttributes.NUM_ONE) * pageSize;
        schemeVo.setEndNum(endNum);
        schemeVo.setStartNum(startNum);
        schemeVo.setPageNo(pageNum);
        schemeVo.setPageSize(pageSize);
    }

}
