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.CultivatingPrizeInfoMapper;
import cn.wisenergy.model.app.CultivatingPrizeInfo;
import cn.wisenergy.model.dto.CultivatingPrizeInfoDto;
import cn.wisenergy.model.vo.CultivatingPrizeInfoVo;
import cn.wisenergy.service.app.CultivatingPrizeInfoService;
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;

/**
 * Created by m1991 on 2021/3/26 10:22
 */
@Service
@Slf4j
public class CultivatingPrizeInfoServiceImpl  implements CultivatingPrizeInfoService {

    @Autowired
    public CultivatingPrizeInfoMapper cultivatingPrizeInfoMapper;

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

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


        map.put("startNum", cultivatingPrizeInfoDto.getStartNum());
        map.put("endNum", cultivatingPrizeInfoDto.getEndNum());

        int total = cultivatingPrizeInfoMapper.count(map);

        List<CultivatingPrizeInfoVo> list = cultivatingPrizeInfoMapper.getCultivatingPrizeInfoList(map);
        PageInfo<CultivatingPrizeInfoVo> info = new PageInfo<>();
        info.setPageSize(cultivatingPrizeInfoDto.getPageSize());
        info.setPageNum(cultivatingPrizeInfoDto.getPageNo());
        info.setList(list);
        info.setTotal(total);

        return R.ok(info);
    }

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