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.ProgressPrizeMapper;
import cn.wisenergy.model.app.ProgressPrize;
import cn.wisenergy.model.app.TeamPerformance;
import cn.wisenergy.model.dto.ProgressPrizeQuery;
import cn.wisenergy.model.dto.TeamQueryDto;
import cn.wisenergy.model.vo.PrizeShowVo;
import cn.wisenergy.model.vo.ProgressPrizeVo;
import cn.wisenergy.service.app.ProgressPrizeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

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

/**
 * @author 86187
 */
@Service
@Slf4j
public class ProgressPrizeServiceImpl extends ServiceImpl<ProgressPrizeMapper, ProgressPrize> implements ProgressPrizeService {

    @Override
    public R<PrizeShowVo> getProgressPrizes() {
        log.info("shop-mall[]ProgressPrizeServiceImpl[]getProgressPrizes[]input.method");
        //获取本月进步奖 列表
        Date date = new Date();
        String yearMonth = DateUtil.convertDateToStr(date, "yyyy-MM");
        List<ProgressPrizeVo> list = baseMapper.getByTime(yearMonth);
        if (CollectionUtils.isEmpty(list)) {
            return R.ok(new PrizeShowVo());
        }

        double sum = 0.0;
        for (ProgressPrizeVo prizeVo : list) {
            sum = sum + prizeVo.getAwardMoney();
        }
        PrizeShowVo prizeShowVo = new PrizeShowVo();
        prizeShowVo.setGrowthTotal(sum);
        prizeShowVo.setPrizeVos(list);
        return R.ok(prizeShowVo);
    }

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

        pageHandle(query);

        Map<String, Object> map = new HashMap<>(8);
        map.put("queryTime", query.getQueryTime());
        if (!StringUtils.isBlank(query.getQueryTime())) {
            Date date = DateUtil.convertStrToDate(query.getQueryTime(), "yyyy-MM-dd");
            map.put("queryTime", date);
        }
        int total = baseMapper.count(map);

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