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

import cn.newfeifan.mall.module.distri.controller.app.integralchangelog.vo.IntegralChangeLogPageReqVO;
import cn.newfeifan.mall.module.distri.controller.app.integralchangelog.vo.IntegralChangeLogRespVO;
import cn.newfeifan.mall.module.distri.controller.app.integralchangelog.vo.IntegralChangeLogSaveReqVO;
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 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.consumptionchangelog.ConsumptionChangeLogMapper;

import java.util.List;

import static cn.newfeifan.mall.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

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

    @Resource
    private ConsumptionChangeLogMapper consumptionChangeLogMapper;

    @Resource
    private MemberUserService memberUserService;

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

    @Override
    public void createIntegralChangeLogs(List<IntegralChangeLogSaveReqVO> list){
        consumptionChangeLogMapper.insertBatch(BeanUtils.toBean(list, IntegralChangeLogDO.class));
    }


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

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

}