package com.sqx.modules.member.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqx.common.exception.SqxException;
import com.sqx.common.utils.PageUtils;
import com.sqx.modules.app.service.UserService;
import com.sqx.modules.member.dao.VipPromoCodeDao;
import com.sqx.modules.member.dto.VipPromoCodeDTO;
import com.sqx.modules.member.entity.VipPromoCode;
import com.sqx.modules.member.query.VipPromoCodeQuery;
import com.sqx.modules.member.service.VipPromoCodeService;
import com.sqx.modules.member.vo.VipPromoCodeVO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class VipPromoCodeServiceImpl extends ServiceImpl<VipPromoCodeDao, VipPromoCode> implements VipPromoCodeService {

    private final UserService userService;

    @Override
    public PageUtils pages(VipPromoCodeQuery query) {
        IPage<VipPromoCode> page = baseMapper.selectPage(
                new Page<>(query.getPage(), query.getLimit()),
                new LambdaQueryWrapper<VipPromoCode>()
                        .like(StrUtil.isNotBlank(query.getUserName()), VipPromoCode::getUserName, query.getUserName())
                        .like(StrUtil.isNotBlank(query.getPromoCode()), VipPromoCode::getPromoCode, query.getPromoCode())
                        .like(StrUtil.isNotBlank(query.getPhone()), VipPromoCode::getPhone, query.getPhone())
                        .orderByDesc(VipPromoCode::getCreateTime)
        );

        List<VipPromoCodeVO> vos = page.getRecords().stream().map(e -> {
            VipPromoCodeVO vo = new VipPromoCodeVO();
            BeanUtil.copyProperties(e, vo);
            return vo;
        }).collect(Collectors.toList());

        return new PageUtils(vos, (int) page.getTotal(), query.getLimit(), query.getPage());
    }

    @Override
    public void save(VipPromoCodeDTO dto) {
        // 检验参数
        checkParam(dto);

        VipPromoCode vipPromoCode = new VipPromoCode();
        BeanUtil.copyProperties(dto, vipPromoCode);
        vipPromoCode.setCreateTime(new Date());
        baseMapper.insert(vipPromoCode);
    }

    @Override
    public void update(VipPromoCodeDTO dto) {
        // 校验参数
        checkParam(dto);

        VipPromoCode vipPromoCode = new VipPromoCode();
        BeanUtil.copyProperties(dto, vipPromoCode);
        baseMapper.updateById(vipPromoCode);
    }

    @Override
    public VipPromoCode getByCode(String promoCode) {
        LambdaQueryWrapper<VipPromoCode> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(VipPromoCode::getPromoCode, promoCode);
        return baseMapper.selectOne(wrapper);
    }

    @Override
    public void increasePromoCount(Long promoCodeId) {
        baseMapper.incrementPromoCountById(promoCodeId);
    }

    private void checkParam(VipPromoCodeDTO dto) {
        LambdaQueryWrapper<VipPromoCode> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(VipPromoCode::getPromoCode, dto.getPromoCode());
        wrapper.ne(ObjectUtil.isNotNull(dto.getId()), VipPromoCode::getId, dto.getId());
        if (baseMapper.selectCount(wrapper) > 0) {
            throw new SqxException("推广码已存在");
        }

        if (ObjectUtil.isNull(userService.queryByPhone(dto.getPhone()))) {
            throw new SqxException("手机号码不存在，请确认！");
        }
    }
}
