package cn.wisenergy.service.app.impl;

import cn.wisenergy.common.constant.CommonAttributes;
import cn.wisenergy.common.utils.R;
import cn.wisenergy.mapper.MemberPercentMapper;
import cn.wisenergy.model.app.MemberPercent;
import cn.wisenergy.model.app.TradeRecord;
import cn.wisenergy.model.dto.MemberPercentQuery;
import cn.wisenergy.model.dto.TradeRecordQuery;
import cn.wisenergy.service.app.MemberPercentService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 86187
 */
@Service
@Slf4j
public class MemberPercentServiceImpl implements MemberPercentService {
    @Autowired
    private MemberPercentMapper memberPercentMapper;

    @Override
    public R<PageInfo<MemberPercent>> getList(MemberPercentQuery query) {
        log.info("sho[-mall[]MemberPercentServiceImpl[]getList[]input.method");
        pageHandle(query);

        Map<String,Object> map=new HashMap<>();
        int total = memberPercentMapper.count();

        map.put("startNum", query.getStartNum());
        map.put("endNum", query.getEndNum());
        List<MemberPercent> list = memberPercentMapper.getList(map);
        PageInfo<MemberPercent> info = new PageInfo<>();
        info.setPageSize(query.getPageSize());
        info.setPageNum(query.getPageNo());
        info.setTotal(total);
        info.setList(list);
        return R.ok(info);
    }

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