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.JjrApplyDao;
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.service.JjrApplyService;
import com.sqx.modules.bl.commission.jjr.service.JjrConfigService;
import com.sqx.modules.sys.entity.SysUserEntity;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class JjrApplyServiceImpl extends ServiceImpl<JjrApplyDao, JjrApply> implements JjrApplyService {

    @Autowired
    private JjrConfigService configService;

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

    @Override
    public int add(JjrApply jjrApply){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        jjrApply.setCreateTime(sdf.format(new Date()));
        jjrApply.setStatus(0);
        return baseMapper.add(jjrApply);
    }

    @Override
    public int update(JjrApply jjrApply){
        return baseMapper.update(jjrApply);
    }

    @Override
    public void approve(JjrApply jjrApply){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        jjrApply.setApproveTime(sdf.format(new Date()));
        jjrApply.setApproveUser(((SysUserEntity) SecurityUtils.getSubject().getPrincipal()).getUserId());
        baseMapper.approve(jjrApply);
        setJjrRate(jjrApply);
    }

    @Override
    public Result findUserPage(JjrApply jjrApply){
        Page<JjrApply> pages=new Page<>(jjrApply.getUserPage(),jjrApply.getUserLimit());
        return Result.success().put("data",new PageUtils(baseMapper.findUserPage(pages,jjrApply)));
    }

    @Override
    public void setJjrRate(JjrApply jjrApply){
        JjrConfig jjrConfig = configService.getOne(new QueryWrapper<JjrConfig>());
        if(jjrConfig != null){
            if(jjrApply == null){
                List<JjrApply> list = baseMapper.findList(null);
                for(int i=0;i<list.size();i++){
                    JjrApply entity = list.get(i);
                    BigDecimal rate = jjrConfig.getRate();
                    rate = rate.divide(new BigDecimal(100)).setScale(2,BigDecimal.ROUND_HALF_UP);
                    entity.setRate(rate);
                    baseMapper.setRate(entity);
                }
            }else{
                JjrApply entity = baseMapper.selectById(jjrApply.getId());
                if(jjrApply.getStatus() == 1){
                    BigDecimal rate = jjrConfig.getRate();
                    rate = rate.divide(new BigDecimal(100)).setScale(2,BigDecimal.ROUND_HALF_UP);
                    entity.setRate(rate);
                }else if(jjrApply.getStatus() == 2){
                    entity.setRate(BigDecimal.ZERO);
                }
                baseMapper.setRate(entity);
            }
        }
    }
}