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.model.vo.SetMemberPercentVo;
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.math.BigDecimal;
import java.math.RoundingMode;
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.param.query:" + query);
        if (null == query) {
            return R.error("入参不能为空!");
        }
        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);
    }

    @Override
    public R<Boolean> setMemberPercent(SetMemberPercentVo percentVo) {
        log.info("sho[-mall[]MemberPercentServiceImpl[]setMemberPercent[]input.param.percentVo:" + percentVo);
        if (null == percentVo || null == percentVo.getPercent() ||
                null == percentVo.getType() || null == percentVo.getUserLevel()) {
            return R.error("入参不能为空!");
        }

        //1、更据等级和类型，获取会员等级优惠  存在更新，不存 在新增
        MemberPercent memberPercent = memberPercentMapper.getByLevelAndType(percentVo.getUserLevel(), percentVo.getType());
        MemberPercent member = new MemberPercent();
        BigDecimal bigDecimal = BigDecimal.valueOf(percentVo.getPercent()).setScale(2, RoundingMode.HALF_UP);
        if (null == memberPercent) {
            member.setType(percentVo.getType());
            member.setPercent(bigDecimal);
            member.setUserLevel(percentVo.getUserLevel());
            int count = memberPercentMapper.add(member);
            if (count == 0) {
                return R.ok(1, false);
            }
            return R.ok(0, true);
        }

        //存在则更新
        memberPercent.setPercent(bigDecimal);
        int count = memberPercentMapper.edit(memberPercent);

        if (count == 0) {
            return R.ok(1, false);
        }
        return R.ok(0, true);
    }

    /**
     * 分页处理方法
     *
     * @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);
    }
}
