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

import cn.newfeifan.mall.module.distri.enums.ConsumptionEnum;
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.consumptionchangelog.vo.*;
import cn.newfeifan.mall.module.distri.dal.dataobject.consumptionchangelog.ConsumptionChangeLogDO;
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.consumptionchangelog.ConsumptionChangeLogMapper;

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 ConsumptionChangeLogServiceImpl implements ConsumptionChangeLogService {

    @Resource
    private ConsumptionChangeLogMapper consumptionChangeLogMapper;

    @Resource
    private MemberUserService memberUserService;

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

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

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

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

    @Override
    public ConsumptionChangeLogDO getConsumptionChangeLog(Long id) {
        return consumptionChangeLogMapper.selectById(id);
    }

    @Override
    public PageResult<ConsumptionChangeLogRespVO> getConsumptionChangeLogPage(ConsumptionChangeLogPageReqVO pageReqVO) {
        PageResult<ConsumptionChangeLogRespVO> bean = BeanUtils.toBean(consumptionChangeLogMapper.selectPage(pageReqVO), ConsumptionChangeLogRespVO.class);

        for (ConsumptionChangeLogRespVO consumptionChangeLogRespVO : bean.getList()) {
            consumptionChangeLogRespVO.setConsumptionStatusName(ConsumptionEnum.getName(consumptionChangeLogRespVO.getConsumptionStatus()));
            if(consumptionChangeLogRespVO.getGenerateUserId() != null){
                MemberUserDO user = memberUserService.getUser(consumptionChangeLogRespVO.getGenerateUserId());
                consumptionChangeLogRespVO.setGenerateUserName(user.getUsername());
            }
        }
        return bean;
    }

    @Override
    public List<ConsumptionChangeLogDO> selectListByStatus(Integer type) {
        return consumptionChangeLogMapper.selectList(ConsumptionChangeLogDO::getConsumptionStatus,type);
    }

}