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.MonthAwardMapper;
import cn.wisenergy.mapper.MonthManureMapper;
import cn.wisenergy.mapper.TradeRecordMapper;
import cn.wisenergy.model.app.MonthAward;
import cn.wisenergy.model.app.MonthManure;
import cn.wisenergy.model.app.TradeRecord;
import cn.wisenergy.model.dto.TradeRecordQuery;
import cn.wisenergy.model.enums.UserLevelEnum;
import cn.wisenergy.service.app.TradeRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
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;

/**
 * @author 86187
 */
@Service
@Slf4j
public class TradeRecordServiceImpl extends ServiceImpl<TradeRecordMapper, TradeRecord> implements TradeRecordService {
    @Autowired
    private MonthManureMapper monthManureMapper;

    @Autowired
    private MonthAwardMapper monthAwardMapper;

    @Autowired
    private TradeRecordMapper tradeRecordMapper;

    @Override
    public R<PageInfo<TradeRecord>> queryList(TradeRecordQuery query) {
        log.info("shop-mall[]TradeRecordServiceImpl[]queryList[]input.param.query:" + query);
        if (null == query) {
            return R.error("入参不能为空!");
        }

        pageHandle(query);

        Map<String, Object> map = new HashMap<>(8);
        map.put("userId", query.getUserId());
        int total = tradeRecordMapper.count(map);

        map.put("startNum", query.getStartNum());
        map.put("endNum", query.getEndNum());
        List<TradeRecord> list = tradeRecordMapper.getList(map);
        PageInfo<TradeRecord> 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(TradeRecordQuery 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);
    }
}
