package com.sqx.modules.bl.commission.jjr.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqx.common.utils.PageUtils;
import com.sqx.common.utils.Result;
import com.sqx.modules.bl.commission.jjr.dao.JjrDataDao;
import com.sqx.modules.bl.commission.jjr.entity.JjrApply;
import com.sqx.modules.bl.commission.jjr.entity.JjrConfig;
import com.sqx.modules.bl.commission.jjr.entity.JjrData;
import com.sqx.modules.bl.commission.jjr.service.JjrConfigService;
import com.sqx.modules.bl.commission.jjr.service.JjrDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Service
public class JjrDataServiceImpl extends ServiceImpl<JjrDataDao, JjrData> implements JjrDataService {

    @Autowired
    private JjrConfigService configService;

    @Override
    public Result findPage(JjrData jjrData){
        Page<JjrData> pages=new Page<>(jjrData.getPage(),jjrData.getLimit());
        return Result.success().put("data",new PageUtils(baseMapper.findPage(pages,jjrData)));
    }

    @Override
    public Result invitationArtificerPage(JjrData jjrData){
        Page<JjrData> pages=new Page<>(jjrData.getPage(),jjrData.getLimit());
        return Result.success().put("data",new PageUtils(baseMapper.invitationArtificerPage(pages,jjrData)));
    }

    @Override
    public Result artificerPage(JjrData jjrData){
        Page<JjrData> pages=new Page<>(jjrData.getPage(),jjrData.getLimit());
        return Result.success().put("data",new PageUtils(baseMapper.artificerPage(pages,jjrData)));
    }

    @Override
    public void cancelInvitation(JjrData jjrData){
        baseMapper.cancelInvitation(jjrData);
    }

    @Override
    public Result findJjrPage(JjrData jjrData){
        Page<JjrData> pages=new Page<>(jjrData.getPage(),jjrData.getLimit());
        return Result.success().put("data",new PageUtils(baseMapper.findJjrPage(pages,jjrData)));
    }

    @Override
    public JjrData getByCode(String invitationCode){
        return baseMapper.getByCode(invitationCode);
    }

    @Override
    public void modifyJjr(JjrData jjrData){
        JjrConfig jjrConfig = configService.getOne(new QueryWrapper<JjrConfig>());
        BigDecimal rate = jjrConfig.getRate();
        rate = rate.divide(new BigDecimal(100)).setScale(2,BigDecimal.ROUND_HALF_UP);
        jjrData.setRate(rate);
        baseMapper.modifyJjr(jjrData);
    }
}