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

import cn.newfeifan.mall.module.distri.enums.IntegralEnum;
import cn.newfeifan.mall.module.member.dal.dataobject.user.MemberUserDO;
import cn.newfeifan.mall.module.member.service.user.MemberUserService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.newfeifan.mall.module.distri.controller.admin.integralchangelog.vo.*;
import cn.newfeifan.mall.module.distri.dal.dataobject.integralchangelog.IntegralChangeLogDO;
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.integralchangelog.IntegralChangeLogMapper;

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

/**
 * 平台消费分变动记录 Service 实现类
 *
 * @author 非繁人
 */
@Service
@Validated
public class IntegralChangeLogServiceImpl implements IntegralChangeLogService {

    @Resource
    private IntegralChangeLogMapper integralChangeLogMapper;

    @Resource
    private MemberUserService memberUserService;

    @Override
    public Long createConsumptionChangeLog(IntegralChangeLogSaveReqVO createReqVO) {
        // 插入
        IntegralChangeLogDO consumptionChangeLog = BeanUtils.toBean(createReqVO, IntegralChangeLogDO.class);
        integralChangeLogMapper.insert(consumptionChangeLog);
        // 返回
        return consumptionChangeLog.getId();
    }

    @Override
    public void updateConsumptionChangeLog(IntegralChangeLogSaveReqVO updateReqVO) {
        // 校验存在
        validateConsumptionChangeLogExists(updateReqVO.getId());
        // 更新
        IntegralChangeLogDO updateObj = BeanUtils.toBean(updateReqVO, IntegralChangeLogDO.class);
        integralChangeLogMapper.updateById(updateObj);
    }

    @Override
    public void deleteConsumptionChangeLog(Long id) {
        // 校验存在
        validateConsumptionChangeLogExists(id);
        // 删除
        integralChangeLogMapper.deleteById(id);
    }

    private void validateConsumptionChangeLogExists(Long id) {
        if (integralChangeLogMapper.selectById(id) == null) {
            throw exception(CONSUMPTION_CHANGE_LOG_NOT_EXISTS);
        }
    }

    @Override
    public IntegralChangeLogDO getConsumptionChangeLog(Long id) {
        return integralChangeLogMapper.selectById(id);
    }

    @Override
    public PageResult<IntegralChangeLogRespVO> getConsumptionChangeLogPage(IntegralChangeLogPageReqVO pageReqVO) {
        PageResult<IntegralChangeLogRespVO> bean = BeanUtils.toBean(integralChangeLogMapper.selectPage(pageReqVO), IntegralChangeLogRespVO.class);

        for (IntegralChangeLogRespVO integralChangeLogRespVO : bean.getList()) {
            integralChangeLogRespVO.setConsumptionStatusName(IntegralEnum.getName(integralChangeLogRespVO.getIntegralStatus()));
            if(integralChangeLogRespVO.getGenerateUserId() != null){
                MemberUserDO user = memberUserService.getUser(integralChangeLogRespVO.getGenerateUserId());
                integralChangeLogRespVO.setGenerateUserName(user.getUsername());
            }
        }
        return bean;
    }

    @Override
    public List<IntegralChangeLogDO> selectListByStatus(Integer type) {
        return integralChangeLogMapper.selectList(IntegralChangeLogDO::getIntegralStatus,type);
    }

    @Override
    public void saveBatch(List<IntegralChangeLogSaveReqVO> IntegralChangeLogSaveReqVOList) {
        List<IntegralChangeLogDO> integralChangeLogDOList = BeanUtils.toBean(IntegralChangeLogSaveReqVOList, IntegralChangeLogDO.class);
        integralChangeLogMapper.insertBatch(integralChangeLogDOList);
    }

}