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.TradeRecordMapper;
import cn.wisenergy.model.app.MonthAward;
import cn.wisenergy.model.app.TradeRecord;
import cn.wisenergy.model.dto.MonthAwardQuery;
import cn.wisenergy.model.dto.TradeRecordQuery;
import cn.wisenergy.model.vo.MonthAwardVo;
import cn.wisenergy.service.app.MonthAwardService;
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.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
 */
@Slf4j
@Service
public class MonthAwardServiceImpl extends ServiceImpl<MonthAwardMapper, MonthAward> implements MonthAwardService {
    private static final String PATTERN = "yyyy-MM";

    @Autowired
    private TradeRecordMapper tradeRecordMapper;

    @Override
    public R<MonthAwardVo> queryMonthAward(String userId) {
        log.info("shop-mall[]MonthAwardServiceImpl[]queryMonthAward[]input.param.userId:" + userId);
        if (StringUtils.isBlank(userId)) {
            return R.error("入参为空!");
        }

        //获取本月最新的一条数据
        String yearMonth = DateUtil.convertDateToStr(new Date(), "yyyy-MM");
        MonthAwardVo monthAwardVo = new MonthAwardVo();
        MonthAward monthAward = baseMapper.getByTime(yearMonth);
        if (null == monthAward) {
            return R.ok(monthAwardVo);
        }

        monthAwardVo.setAwardTime(monthAward.getCreateTime());
        monthAwardVo.setAwardTotal(monthAward.getAwardTotal());
        monthAwardVo.setFarmerAward(monthAward.getFarmerAward());
        monthAwardVo.setForestStartAward(monthAward.getForestStartAward());
        monthAwardVo.setGoldAward(monthAward.getGoldAward());
        monthAwardVo.setGrowthAward(monthAward.getGrowthAward());
        monthAwardVo.setMonthAwardTotal(monthAward.getMonthAwardTotal());
        monthAwardVo.setMonthIncreased(monthAward.getMonthIncreased());
        monthAwardVo.setPartnerAward(monthAward.getPartnerAward());

        //获取用户本月奖金
        Double sum = tradeRecordMapper.queryMonthAward(userId, new Date());
        if (null == sum) {
            monthAwardVo.setUserMonthAward(0.00);
        } else {
            monthAwardVo.setUserMonthAward(sum);
        }
        return R.ok(monthAwardVo);
    }

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

        pageHandle(query);

        Map<String, Object> map = new HashMap<>(8);
        if (!StringUtils.isBlank(query.getQueryTime())) {
            map.put("queryTime", query.getQueryTime());
        }
        int total = baseMapper.count(map);

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