package co.yixiang.yshop.module.member.service.userlevelequityref;

import cn.hutool.core.collection.CollectionUtil;
import co.yixiang.yshop.module.member.controller.app.userlevel.vo.SettleLevelInfoVO;
import co.yixiang.yshop.module.member.dal.dataobject.userlevelequity.UserLevelEquityDO;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;

import java.util.*;
import java.util.stream.Collectors;

import co.yixiang.yshop.module.member.controller.admin.userlevelequityref.vo.*;
import co.yixiang.yshop.module.member.dal.dataobject.userlevelequityref.UserLevelEquityRefDO;
import co.yixiang.yshop.framework.common.pojo.PageResult;

import co.yixiang.yshop.module.member.convert.userlevelequityref.UserLevelEquityRefConvert;
import co.yixiang.yshop.module.member.dal.mysql.userlevelequityref.UserLevelEquityRefMapper;

import static co.yixiang.yshop.framework.common.exception.util.ServiceExceptionUtil.exception;
import static co.yixiang.yshop.module.member.enums.ErrorCodeConstants.*;

/**
 * 会员等级权益中间 Service 实现类
 *
 * @author admin
 */
@Service
@Validated
public class UserLevelEquityRefServiceImpl implements UserLevelEquityRefService {

    @Resource
    private UserLevelEquityRefMapper userLevelEquityRefMapper;

    @Override
    public Long createUserLevelEquityRef(UserLevelEquityRefCreateReqVO createReqVO) {
        // 插入
        UserLevelEquityRefDO userLevelEquityRef = UserLevelEquityRefConvert.INSTANCE.convert(createReqVO);
        userLevelEquityRefMapper.insert(userLevelEquityRef);
        // 返回
        return userLevelEquityRef.getId();
    }

    @Override
    public void updateUserLevelEquityRef(UserLevelEquityRefUpdateReqVO updateReqVO) {
        // 校验存在
        validateUserLevelEquityRefExists(updateReqVO.getId());
        // 更新
        UserLevelEquityRefDO updateObj = UserLevelEquityRefConvert.INSTANCE.convert(updateReqVO);
        userLevelEquityRefMapper.updateById(updateObj);
    }

    @Override
    public void deleteUserLevelEquityRef(Long id) {
        // 校验存在
        validateUserLevelEquityRefExists(id);
        // 删除
        userLevelEquityRefMapper.deleteById(id);
    }

    private void validateUserLevelEquityRefExists(Long id) {
        if (userLevelEquityRefMapper.selectById(id) == null) {
            throw exception(USER_LEVEL_EQUITY_REF_NOT_EXISTS);
        }
    }

    @Override
    public UserLevelEquityRefDO getUserLevelEquityRef(Long id) {
        return userLevelEquityRefMapper.selectById(id);
    }

    @Override
    public List<UserLevelEquityRefDO> getUserLevelEquityRefList(Collection<Long> ids) {
        return userLevelEquityRefMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<UserLevelEquityRefDO> getUserLevelEquityRefPage(UserLevelEquityRefPageReqVO pageReqVO) {
        return userLevelEquityRefMapper.selectPage(pageReqVO);
    }

    @Override
    public List<UserLevelEquityRefDO> getUserLevelEquityRefList(UserLevelEquityRefExportReqVO exportReqVO) {
        return userLevelEquityRefMapper.selectList(exportReqVO);
    }

    @Override
    public void saveData(Long id, List<UserLevelEquityRefCreateReqVO> levelEquityList, boolean flag) {
        if (!flag) {
            this.deleteByLevelId(id);
        }
        if (CollectionUtil.isNotEmpty(levelEquityList)) {
            List<UserLevelEquityRefDO> list = levelEquityList.stream().map(item -> {
                UserLevelEquityRefDO userLevelEquityRefDO = new UserLevelEquityRefDO();
                userLevelEquityRefDO.setEquityId(item.getEquityId());
                userLevelEquityRefDO.setEquityValue(item.getEquityValue());
                userLevelEquityRefDO.setLevelId(id);
                return userLevelEquityRefDO;
            }).collect(Collectors.toList());
            userLevelEquityRefMapper.insertBatch(list);
        }
    }

    @Override
    public List<UserLevelEquityDO> getUserLevelEquityList(Long levelId) {
        return userLevelEquityRefMapper.getUserLevelEquityList(levelId);
    }

    /**
     * 根据等级id删除权益
     *
     * @param levelId /
     */
    private void deleteByLevelId(Long levelId) {
        userLevelEquityRefMapper.delete(Wrappers.<UserLevelEquityRefDO>lambdaQuery().eq(UserLevelEquityRefDO::getLevelId, levelId));
    }

}
