package cn.newfeifan.mall.module.distri.service.orderpercentage;

import cn.newfeifan.mall.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

import cn.newfeifan.mall.module.distri.controller.admin.orderpercentage.vo.*;
import cn.newfeifan.mall.module.distri.dal.dataobject.orderpercentage.OrderPercentageDO;
import cn.newfeifan.mall.framework.common.pojo.PageResult;
import cn.newfeifan.mall.framework.common.pojo.PageParam;
import cn.newfeifan.mall.framework.common.util.object.BeanUtils;

import cn.newfeifan.mall.module.distri.dal.mysql.orderpercentage.OrderPercentageMapper;

import static cn.newfeifan.mall.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.newfeifan.mall.module.distri.constant.DistriConstants.ONE_HUNDRED;
import static cn.newfeifan.mall.module.distri.constant.DistriConstants.PERCENT;
import static cn.newfeifan.mall.module.distri.enums.ErrorCodeConstants.*;

/**
 * 积分相关计算浮动百分比设置 Service 实现类
 *
 * @author 非繁人
 */
@Service
@Validated
public class OrderPercentageServiceImpl implements OrderPercentageService {

    @Resource
    private OrderPercentageMapper orderPercentageMapper;

    @Override
    public Long createOrderPercentage(OrderPercentageSaveReqVO createReqVO) {
       /* OrderPercentageSaveReqVO orderPercentageSaveReqVO = new OrderPercentageSaveReqVO();
        orderPercentageSaveReqVO.setDivideIntoPerc((int) (createReqVO.getDivideIntoPerc() * PERCENT));
        orderPercentageSaveReqVO.setGrossProfitPerc((int) (createReqVO.getGrossProfitPerc() * PERCENT));
        orderPercentageSaveReqVO.setGrossProfitAncestorQuotaPerc((int) (createReqVO.getGrossProfitAncestorQuotaPerc() * PERCENT));
        orderPercentageSaveReqVO.setGrossProfitBonusQuotaPerc((int) (createReqVO.getGrossProfitBonusQuotaPerc() * PERCENT));
        orderPercentageSaveReqVO.setGrossProfitPlatformQuotaPerc((int) (createReqVO.getGrossProfitPlatformQuotaPerc() * PERCENT));
        orderPercentageSaveReqVO.setGrossProfitUserQuotaPerc((int) (createReqVO.getGrossProfitUserQuotaPerc() * PERCENT));
        if (checkPercent(createReqVO)){
            throw  exception(PERCENT_LARGE_ONE_HUNDRED);
        }*/
        // 插入
        OrderPercentageDO orderPercentage = BeanUtils.toBean(createReqVO, OrderPercentageDO.class);
        orderPercentageMapper.insert(orderPercentage);
        // 返回
        return orderPercentage.getId();
    }

  /*  public boolean checkPercent(OrderPercentageDoubleSaveReqVO createReqVO){
        // 判断是否等于1
        Double total =  createReqVO.getGrossProfitUserQuotaPerc()+createReqVO.getGrossProfitAncestorQuotaPerc()+createReqVO.getGrossProfitPlatformQuotaPerc()
                +createReqVO.getGrossProfitBonusQuotaPerc();
        return total > ONE_HUNDRED;
    }*/


    @Override
    public void updateOrderPercentage(OrderPercentageSaveReqVO updateReqVO) {
        // 校验存在
        validateOrderPercentageExists(updateReqVO.getId());
        // 更新
        OrderPercentageDO updateObj = BeanUtils.toBean(updateReqVO, OrderPercentageDO.class);
        orderPercentageMapper.updateById(updateObj);
    }

    @Override
    public void deleteOrderPercentage(Long id) {
        // 校验存在
        validateOrderPercentageExists(id);
        // 删除
        orderPercentageMapper.deleteById(id);
    }

    private void validateOrderPercentageExists(Long id) {
        if (orderPercentageMapper.selectById(id) == null) {
            throw exception(ORDER_PERCENTAGE_NOT_EXISTS);
        }
    }

    @Override
    public OrderPercentageDO getOrderPercentage(Long id) {
        return orderPercentageMapper.selectById(id);
    }

    @Override
    public PageResult<OrderPercentageDO> getOrderPercentagePage(OrderPercentagePageReqVO pageReqVO) {
        return orderPercentageMapper.selectPage(pageReqVO);
    }

    @Override
    public OrderPercentageDO queryStatus() {
        List<OrderPercentageDO> orderPercentageDOS = orderPercentageMapper.selectList(new LambdaQueryWrapperX<OrderPercentageDO>()
                .eqIfPresent(OrderPercentageDO::getStatus, 1));

        if (orderPercentageDOS.isEmpty()) {
            return null;
        }
        return orderPercentageDOS.get(0);
    }

}