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

import cn.newfeifan.mall.framework.common.pojo.PageResult;
import cn.newfeifan.mall.framework.common.util.object.BeanUtils;
import cn.newfeifan.mall.module.distri.controller.admin.socialstatus.bo.SocialStatusBO;
import cn.newfeifan.mall.module.distri.controller.admin.socialstatus.vo.SocialStatusPageReqVO;
import cn.newfeifan.mall.module.distri.controller.admin.socialstatus.vo.SocialStatusSaveReqVO;
import cn.newfeifan.mall.module.distri.dal.dataobject.socialstatus.SocialStatusDO;
import cn.newfeifan.mall.module.distri.dal.mysql.socialstatus.SocialStatusMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static cn.newfeifan.mall.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.newfeifan.mall.module.distri.enums.ErrorCodeConstants.SOCIAL_STATUS_NOT_EXISTS;

/**
 * 分销用户身价 Service 实现类
 *
 * @author 非繁人
 */
@Service
@Validated
public class SocialStatusServiceImpl implements SocialStatusService {

    @Resource
    private SocialStatusMapper socialStatusMapper;

    @Override
    public Long createSocialStatus(SocialStatusSaveReqVO createReqVO) {
        // 插入
        SocialStatusDO socialStatus = BeanUtils.toBean(createReqVO, SocialStatusDO.class);
        socialStatusMapper.insert(socialStatus);
        // 返回
        return socialStatus.getId();
    }

    @Override
    public void updateSocialStatus(SocialStatusSaveReqVO updateReqVO) {
        // 校验存在
        validateSocialStatusExists(updateReqVO.getId());
        // 更新
        SocialStatusDO updateObj = BeanUtils.toBean(updateReqVO, SocialStatusDO.class);
        socialStatusMapper.updateById(updateObj);
    }

    @Override
    public void deleteSocialStatus(Long id) {
        // 校验存在
        validateSocialStatusExists(id);
        // 删除
        socialStatusMapper.deleteById(id);
    }

    private void validateSocialStatusExists(Long id) {
        if (socialStatusMapper.selectById(id) == null) {
            throw exception(SOCIAL_STATUS_NOT_EXISTS);
        }
    }

    @Override
    public SocialStatusDO getSocialStatus(Long id) {
        return socialStatusMapper.selectById(id);
    }

    @Override
    public PageResult<SocialStatusDO> getSocialStatusPage(SocialStatusPageReqVO pageReqVO) {
        return socialStatusMapper.selectPage(pageReqVO);
    }

    @Override
    public SocialStatusBO calculateSocial(Long point) {
        List<SocialStatusDO> socialStatusDOS = socialStatusMapper.selectList(new LambdaQueryWrapper<SocialStatusDO>()
                .orderByAsc(SocialStatusDO::getPoint)
        );

        SocialStatusBO socialStatusBO = SocialStatusBO.builder().build();
        for (int i = 0; i < socialStatusDOS.size(); i++) {
            if (point < socialStatusDOS.get(i).getPoint()) {
                socialStatusBO.setId(socialStatusDOS.get(i - 1).getId());
                socialStatusBO.setSocialUpNeed(socialStatusDOS.get(i).getPoint() - point);
                break;
            }
        }
        if(socialStatusBO.getId() == null){
            socialStatusBO.setId(socialStatusDOS.get(socialStatusDOS.size() - 1).getId());
            socialStatusBO.setSocialUpNeed(0L);
        }
        return socialStatusBO;
    }


}