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

import cn.newfeifan.mall.module.distri.enums.SocialStatusEnum;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;

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

import cn.newfeifan.mall.module.distri.dal.mysql.socialstatuschangelog.SocialStatusChangeLogMapper;

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

/**
 * 会员身价变化日志 Service 实现类
 *
 * @author 非繁人
 */
@Service
@Validated
public class SocialStatusChangeLogServiceImpl implements SocialStatusChangeLogService {

    @Resource
    private SocialStatusChangeLogMapper socialStatusChangeLogMapper;

    @Override
    public Long createSocialStatusChangeLog(Long userId, Long socialValue, Long objectId, SocialStatusEnum socialStatusEnum) {
        // 插入
        SocialStatusChangeLogDO socialStatusChangeLog = SocialStatusChangeLogDO.builder()
                .socialStatusChangeCategoryId(Long.valueOf(socialStatusEnum.getStatus()))
                .description(socialStatusEnum.getName())
                .variationValue(socialValue)
                .objectId(objectId)
                .effective(IS_EFFECTIVE)
                .userId(userId).build();
        socialStatusChangeLogMapper.insert(socialStatusChangeLog);
        // 返回
        return socialStatusChangeLog.getId();
    }

    @Override
    public Long createSocialStatusChangeLog(SocialStatusChangeLogSaveReqVO createReqVO) {
        // 插入
        SocialStatusChangeLogDO socialStatusChangeLog = BeanUtils.toBean(createReqVO, SocialStatusChangeLogDO.class);
        socialStatusChangeLogMapper.insert(socialStatusChangeLog);
        // 返回
        return socialStatusChangeLog.getId();
    }

    @Override
    public void updateSocialStatusChangeLog(SocialStatusChangeLogSaveReqVO updateReqVO) {
        // 校验存在
        validateSocialStatusChangeLogExists(updateReqVO.getId());
        // 更新
        SocialStatusChangeLogDO updateObj = BeanUtils.toBean(updateReqVO, SocialStatusChangeLogDO.class);
        socialStatusChangeLogMapper.updateById(updateObj);
    }

    @Override
    public void deleteSocialStatusChangeLog(Long id) {
        // 校验存在
        validateSocialStatusChangeLogExists(id);
        // 删除
        socialStatusChangeLogMapper.deleteById(id);
    }

    private void validateSocialStatusChangeLogExists(Long id) {
        if (socialStatusChangeLogMapper.selectById(id) == null) {
            throw exception(SOCIAL_STATUS_CHANGE_LOG_NOT_EXISTS);
        }
    }

    @Override
    public SocialStatusChangeLogDO getSocialStatusChangeLog(Long id) {
        return socialStatusChangeLogMapper.selectById(id);
    }

    @Override
    public PageResult<SocialStatusChangeLogDO> getSocialStatusChangeLogPage(SocialStatusChangeLogPageReqVO pageReqVO) {
        return socialStatusChangeLogMapper.selectPage(pageReqVO);
    }

    @Override
    public PageResult<UserSignSocialLogRespVO> getUserSignLog(SocialStatusChangeLogPageReqVO pageReqVO) {
        PageResult<SocialStatusChangeLogDO> userSignLog = socialStatusChangeLogMapper.getUserSignLog(pageReqVO);

        PageResult<UserSignSocialLogRespVO> bean = BeanUtils.toBean(userSignLog, UserSignSocialLogRespVO.class);
        for (int i = 0; i < bean.getList().size(); i++) {
            bean.getList().get(i).setSort( (i + 1) + (pageReqVO.getPageNo() -1) *pageReqVO.getPageSize());
        }
        return bean;
    }

}
