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.TeamUserInfoMapper;
import cn.wisenergy.model.app.TeamUserInfo;
import cn.wisenergy.model.dto.TeamUserInfoDto;
import cn.wisenergy.model.vo.TeamUserInfoVo;
import cn.wisenergy.service.app.TeamUserInfoService;
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.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class TeamUserInfoServiceImpl implements TeamUserInfoService {

    @Autowired
    TeamUserInfoMapper teamUserInfoMapper;

    /**
     * 根据用户id获取用户的团队信息
     * @param userId
     * @return
     */
    @Override
    public TeamUserInfo getOneById(String userId) {
        TeamUserInfo byUserId = teamUserInfoMapper.getByUserId(userId);
        return byUserId;
    }

    /**
     * 获取团队总人数
     * @param userId 用户Id
     * @return 用户团队总人数
     */
    @Override
    public int getTeamUserCount(String userId){

        int teamUserCount = teamUserInfoMapper.getTeamUserCount(userId);
        return teamUserCount;
    }

    @Override
    public R<PageInfo<TeamUserInfoVo>> getTeamUserInfoList(TeamUserInfoDto teamUserInfoDto) {
        log.info("shop-mall[]TeamUserInfoServiceImpl[]getTeamUserInfoList[]input.param.query:" + teamUserInfoDto);
        if (null == teamUserInfoDto) {
            return R.error("入参不能为空!");
        }
        pageHandle(teamUserInfoDto);
        Map<String, Object> map = new HashMap<>(8);

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


        int total = teamUserInfoMapper.count(map);

        map.put("startNum", teamUserInfoDto.getStartNum());
        map.put("endNum", teamUserInfoDto.getEndNum());
        List<TeamUserInfoVo> list = teamUserInfoMapper.getTeamUserInfoList(map);
        PageInfo<TeamUserInfoVo> info = new PageInfo<>();
        info.setPageSize(teamUserInfoDto.getPageSize());
        info.setPageNum(teamUserInfoDto.getPageNo());
        info.setList(list);
        info.setTotal(total);

        return R.ok(info);
    }

    /**
     * 分页处理方法
     *
     * @param schemeVo 参数
     */
    private void pageHandle(TeamUserInfoDto 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);
    }

}
