package com.dgtly.order.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import com.dgtly.common.utils.DateUtils;
import com.dgtly.order.domain.OrderSalesGroup;
import com.dgtly.order.mapper.OrderSalesGroupMapper;
import com.dgtly.order.service.IOrderSalesGroupService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dgtly.common.core.text.Convert;

import javax.annotation.Resource;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author dgtly
 * @date 2020-12-01
 */
@Service
public class OrderSalesGroupServiceImpl implements IOrderSalesGroupService
{
    @Resource
    private OrderSalesGroupMapper orderSalesGroupMapper;

    /**
     * 查询【请填写功能名称】
     * 
     * @param id 【请填写功能名称】ID
     * @return 【请填写功能名称】
     */
    @Override
    public OrderSalesGroup selectOrderSalesGroupById(Long id)
    {
        return orderSalesGroupMapper.selectOrderSalesGroupById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param orderSalesGroup 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<OrderSalesGroup> selectOrderSalesGroupList(OrderSalesGroup orderSalesGroup)
    {

        orderSalesGroup.setCalMonth(DateUtils.getYearMonthStr());
        List<OrderSalesGroup> orderSalesGroupList = orderSalesGroupMapper.selectOrderSalesGroupList(orderSalesGroup);
        if(orderSalesGroupList != null && orderSalesGroupList.size() > 0){
            orderSalesGroupList.forEach(salesGroup ->{
                if(salesGroup != null) salesGroup = shrinkMilVezesOrderSalesCustomer(salesGroup);
            });
        }
        return orderSalesGroupList;
    }

    /**
     * 目标与达成缩小一千倍
     * @return
     */
    public static OrderSalesGroup shrinkMilVezesOrderSalesCustomer(OrderSalesGroup salesGroup) {
        //预算
        String nowYearBud = salesGroup.getNowYearBud();
        if(StringUtils.isNotBlank(nowYearBud))
            salesGroup.setNowYearBud(new BigDecimal(nowYearBud).setScale(3,BigDecimal.ROUND_HALF_UP)+"");
        //已复核
        String nowYearAct = salesGroup.getNowYearAct();
        if(StringUtils.isNotBlank(nowYearAct))
            salesGroup.setNowYearAct(divide(new BigDecimal(nowYearAct),new BigDecimal("1000"),3)+"");
        //未复核
        String reviewNo= salesGroup.getReviewNo();
        if(StringUtils.isNotBlank(reviewNo))
            salesGroup.setReviewNo(divide(new BigDecimal(reviewNo),new BigDecimal("1000"),3)+"");
        //未清
        String uncleared = salesGroup.getUncleared();
        if(StringUtils.isNotBlank(uncleared))
            salesGroup.setUncleared(divide(new BigDecimal(uncleared),new BigDecimal("1000"),3)+"");
        //冻结
        String freeze = salesGroup.getFreeze();
        if(StringUtils.isNotBlank(freeze))
            salesGroup.setFreeze(divide(new BigDecimal(freeze),new BigDecimal("1000"),3)+"");
        return salesGroup;
    }

    /**
     * @param dividend 被除数
     * @param divisor 除数
     * @param number 保留几位小数
     * @return
     */
    public static double divide(BigDecimal dividend , BigDecimal divisor, int number){
        return dividend
                .divide(divisor, number,
                        RoundingMode.HALF_UP).doubleValue();
    }
}
