package cn.iocoder.yudao.module.member.service.activityvoucheruser;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.member.controller.admin.activityvoucheruser.vo.ActivityVoucherUserPageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.activityvoucheruser.vo.ActivityVoucherUserSaveReqVO;
import cn.iocoder.yudao.module.member.controller.app.activity.vo.ActivityVoucherVO;
import cn.iocoder.yudao.module.member.controller.app.activity.vo.AppActivityVoucherUserRespVO;
import cn.iocoder.yudao.module.member.dal.dataobject.activityvoucher.ActivityVoucherDO;
import cn.iocoder.yudao.module.member.dal.dataobject.activityvoucheruser.ActivityVoucherUserDO;
import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO;
import cn.iocoder.yudao.module.member.dal.mysql.activityvoucheruser.ActivityVoucherUserMapper;
import cn.iocoder.yudao.module.member.service.user.MemberUserService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.ACTIVITY_VOUCHER_USER_NOT_EXISTS;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.USER_MOBILE_NOT_EXISTS;

/**
 * 活动用户拥有代金券 Service 实现类
 *
 * @author 我是秦俊旗
 */
@Service
@Validated
public class ActivityVoucherUserServiceImpl implements ActivityVoucherUserService {

    @Resource
    private ActivityVoucherUserMapper activityVoucherUserMapper;

    @Resource
    @Lazy
    private MemberUserService memberUserService;

    @Override
    public Long createActivityVoucherUser(ActivityVoucherUserSaveReqVO createReqVO) {
        // 插入
        ActivityVoucherUserDO activityVoucherUser = BeanUtils.toBean(createReqVO, ActivityVoucherUserDO.class);
        MemberUserDO userByMobile = memberUserService.getUserByMobile(createReqVO.getMobile());
        if(userByMobile == null){
            throw exception(USER_MOBILE_NOT_EXISTS);
        }
        activityVoucherUser.setUserId(userByMobile.getId());
        activityVoucherUserMapper.insert(activityVoucherUser);
        // 返回
        return activityVoucherUser.getId();
    }

    @Override
    public void updateActivityVoucherUser(ActivityVoucherUserSaveReqVO updateReqVO) {
        // 校验存在
        validateActivityVoucherUserExists(updateReqVO.getId());
        // 更新
        ActivityVoucherUserDO updateObj = BeanUtils.toBean(updateReqVO, ActivityVoucherUserDO.class);
        activityVoucherUserMapper.updateById(updateObj);
    }

    @Override
    public void deleteActivityVoucherUser(Long id) {
        // 校验存在
        validateActivityVoucherUserExists(id);
        // 删除
        activityVoucherUserMapper.deleteById(id);
    }

    private void validateActivityVoucherUserExists(Long id) {
        if (activityVoucherUserMapper.selectById(id) == null) {
            throw exception(ACTIVITY_VOUCHER_USER_NOT_EXISTS);
        }
    }

    @Override
    public ActivityVoucherUserDO getActivityVoucherUser(Long id) {
        return activityVoucherUserMapper.selectById(id);
    }

    @Override
    public PageResult<ActivityVoucherUserDO> getActivityVoucherUserPage(ActivityVoucherUserPageReqVO pageReqVO) {
        return activityVoucherUserMapper.selectPage(pageReqVO);
    }

    @Override
    public PageResult<AppActivityVoucherUserRespVO> getActivityVoucherPage(ActivityVoucherUserPageReqVO pageReqVO) {

        Page<AppActivityVoucherUserRespVO> activityAwardsUserRespVOPage = activityVoucherUserMapper.selectJoinPage(new Page<>(pageReqVO.getPageNo(), pageReqVO.getPageSize()), AppActivityVoucherUserRespVO.class,
                new MPJLambdaWrapper<ActivityVoucherUserDO>()
                        .selectAll(ActivityVoucherUserDO.class)
                        .select(ActivityVoucherDO::getName,  ActivityVoucherDO::getType, ActivityVoucherDO::getNum)
                        .leftJoin(ActivityVoucherDO.class, ActivityVoucherDO::getId, ActivityVoucherUserDO::getVoucherId)
                        .eq(ActivityVoucherUserDO::getUserId, pageReqVO.getUserId())
                        .orderByDesc(ActivityVoucherUserDO::getCreateTime)
        );
        return new PageResult<>(activityAwardsUserRespVOPage.getRecords(), activityAwardsUserRespVOPage.getTotal());
    }


    public List<AppActivityVoucherUserRespVO> getActivityVoucherList(ActivityVoucherUserPageReqVO pageReqVO) {

        return activityVoucherUserMapper.selectJoinList(AppActivityVoucherUserRespVO.class,
                new MPJLambdaWrapper<ActivityVoucherUserDO>()
                        .selectAll(ActivityVoucherUserDO.class)
                        .select(ActivityVoucherDO::getName,  ActivityVoucherDO::getType, ActivityVoucherDO::getNum)
                        .leftJoin(ActivityVoucherDO.class, ActivityVoucherDO::getId, ActivityVoucherUserDO::getVoucherId)
                        .eq(ActivityVoucherUserDO::getUserId, pageReqVO.getUserId())
                        .eq(ActivityVoucherUserDO::getStatus, pageReqVO.getStatus())
                        .orderByDesc(ActivityVoucherUserDO::getCreateTime)
        );
    }

    @Override
    public Boolean checkVoucher(ActivityVoucherVO vo) {
        List<ActivityVoucherUserDO> activityVoucherUserDOS = activityVoucherUserMapper.selectList(Wrappers.lambdaQuery(ActivityVoucherUserDO.class)
                .eq(ActivityVoucherUserDO::getUserId, vo.getUserId())
                .eq(ActivityVoucherUserDO::getStatus, '0')
        );
        if (activityVoucherUserDOS.isEmpty()) {
            return false;
        }
//        long count = activityVoucherUserDOS.stream().filter(voucher -> "1".equals(voucher.getStatus())).count();
//        if (count >= 2) {
//            return false;
//        }
        return true;
    }

    @Override
    public void updateByVoucherIdAndUserId(Long id) {
         activityVoucherUserMapper.update(null,Wrappers.lambdaUpdate(ActivityVoucherUserDO.class)
                .eq(ActivityVoucherUserDO::getId,id)
                .set(ActivityVoucherUserDO::getStatus,"1")
         );
    }

    @Override
    public void updateByIds(List<String> ids) {
        activityVoucherUserMapper.update(null,Wrappers.lambdaUpdate(ActivityVoucherUserDO.class)
                .in(ActivityVoucherUserDO::getId,ids)
                .set(ActivityVoucherUserDO::getStatus,"1")
        );
    }

    @Override
    public List<ActivityVoucherUserDO> getListByIds(List<String> ids) {
        return activityVoucherUserMapper.selectList(Wrappers.lambdaQuery(ActivityVoucherUserDO.class)
                .in(ActivityVoucherUserDO::getId,ids)
        );
    }
}