package com.starry.admin.modules.shop.service.impl;

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.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.starry.admin.modules.shop.mapper.PlayCouponDetailsMapper;
import com.starry.admin.modules.shop.module.constant.CouponUseState;
import com.starry.admin.modules.shop.module.entity.PlayCouponDetailsEntity;
import com.starry.admin.modules.shop.module.entity.PlayCouponInfoEntity;
import com.starry.admin.modules.shop.module.vo.PlayCouponDetailsQueryVo;
import com.starry.admin.modules.shop.module.vo.PlayCouponDetailsReturnVo;
import com.starry.admin.modules.shop.service.IPlayCouponDetailsService;
import com.starry.common.utils.IdUtils;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

/**
 * 优惠券详情Service业务层处理
 *
 * @author admin
 * @since 2024-07-04
 */
@Service
public class PlayCouponDetailsServiceImpl extends ServiceImpl<PlayCouponDetailsMapper, PlayCouponDetailsEntity>
        implements
            IPlayCouponDetailsService {
    @Resource
    private PlayCouponDetailsMapper playCouponDetailsMapper;

    /**
     * 查询优惠券详情
     *
     * @param id
     *            优惠券详情主键
     * @return 优惠券详情
     */
    @Override
    public PlayCouponDetailsReturnVo selectPlayCouponDetailsById(String id) {
        MPJLambdaWrapper<PlayCouponDetailsEntity> lambdaWrapper = new MPJLambdaWrapper<>();
        lambdaWrapper.selectAll(PlayCouponDetailsEntity.class);
        lambdaWrapper.leftJoin(PlayCouponInfoEntity.class, PlayCouponInfoEntity::getId,
                PlayCouponDetailsEntity::getCouponId);
        lambdaWrapper.eq(PlayCouponDetailsEntity::getId, id);
        return this.baseMapper.selectJoinOne(PlayCouponDetailsReturnVo.class, lambdaWrapper);
    }

    @Override
    public IPage<PlayCouponDetailsReturnVo> selectByPage(PlayCouponDetailsQueryVo vo) {
        MPJLambdaWrapper<PlayCouponDetailsEntity> lambdaWrapper = new MPJLambdaWrapper<>();
        lambdaWrapper.selectAll(PlayCouponDetailsEntity.class);
        lambdaWrapper.selectAll(PlayCouponInfoEntity.class);
        lambdaWrapper.leftJoin(PlayCouponInfoEntity.class, PlayCouponInfoEntity::getId,
                PlayCouponDetailsEntity::getCouponId);
        if (StrUtil.isNotBlank(vo.getCouponId())) {
            lambdaWrapper.eq(PlayCouponDetailsEntity::getCouponId, vo.getCouponId());
        }
        if (StrUtil.isNotBlank(vo.getCustomId())) {
            lambdaWrapper.eq(PlayCouponDetailsEntity::getCustomId, vo.getCustomId());
        }
        if (StrUtil.isNotBlank(vo.getUseState())) {
            lambdaWrapper.eq(PlayCouponDetailsEntity::getUseState, vo.getUseState());
        }
        if (StrUtil.isNotBlank(vo.getObtainingChannels())) {
            lambdaWrapper.eq(PlayCouponDetailsEntity::getObtainingChannels, vo.getObtainingChannels());
        }
        if (vo.getObtainingTime() != null && vo.getObtainingTime().size() == 2) {
            lambdaWrapper.between(PlayCouponDetailsEntity::getObtainingTime, vo.getObtainingTime().get(0),
                    vo.getObtainingTime().get(1));
        }
        if (vo.getUseTime() != null && vo.getUseTime().size() == 2) {
            lambdaWrapper.between(PlayCouponDetailsEntity::getUseTime, vo.getUseTime().get(0), vo.getUseTime().get(1));
        }

        return this.baseMapper.selectJoinPage(new Page<>(vo.getPageNum(), vo.getPageSize()),
                PlayCouponDetailsReturnVo.class, lambdaWrapper);
    }

    /**
     * 查询优惠券详情列表
     *
     * @param playCouponDetails
     *            优惠券详情
     * @return 优惠券详情
     */
    @Override
    public IPage<PlayCouponDetailsEntity> selectPlayCouponDetailsByPage(PlayCouponDetailsEntity playCouponDetails) {
        Page<PlayCouponDetailsEntity> page = new Page<>(1, 10);
        return this.baseMapper.selectPage(page, new LambdaQueryWrapper<PlayCouponDetailsEntity>());
    }

    @Override
    public void create(String customId, String customNickName, String customLevel, String couponId,
            String obtainingChannels, String useState) {
        PlayCouponDetailsEntity couponDetails = new PlayCouponDetailsEntity();
        couponDetails.setId(IdUtils.getUuid());
        couponDetails.setCustomId(customId);
        couponDetails.setCustomNickname(customNickName);
        couponDetails.setCustomLevelId(customLevel);
        couponDetails.setCouponId(couponId);
        couponDetails.setObtainingChannels(obtainingChannels);
        couponDetails.setObtainingTime(LocalDateTime.now());
        couponDetails.setUseState(useState);
        this.create(couponDetails);
    }

    /**
     * 新增优惠券详情
     *
     * @param playCouponDetails
     *            优惠券详情
     * @return 结果
     */
    @Override
    public boolean create(PlayCouponDetailsEntity playCouponDetails) {
        if (StrUtil.isBlankIfStr(playCouponDetails.getId())) {
            playCouponDetails.setId(IdUtils.getUuid());
        }
        return save(playCouponDetails);
    }

    @Override
    public long countActiveByCustomAndCoupon(String couponId, String customId) {
        return lambdaQuery()
                .eq(PlayCouponDetailsEntity::getCouponId, couponId)
                .eq(PlayCouponDetailsEntity::getCustomId, customId)
                .eq(PlayCouponDetailsEntity::getUseState, CouponUseState.UNUSED.getCode())
                .count();
    }

    @Override
    public List<PlayCouponDetailsReturnVo> selectByCustomId(String customId) {
        MPJLambdaWrapper<PlayCouponDetailsEntity> lambdaWrapper = new MPJLambdaWrapper<>();
        lambdaWrapper.selectAll(PlayCouponDetailsEntity.class);
        lambdaWrapper.selectAll(PlayCouponInfoEntity.class);
        lambdaWrapper.eq(PlayCouponDetailsEntity::getCustomId, customId);
        lambdaWrapper.leftJoin(PlayCouponInfoEntity.class, PlayCouponInfoEntity::getId,
                PlayCouponDetailsEntity::getCouponId);
        return this.baseMapper.selectJoinList(PlayCouponDetailsReturnVo.class, lambdaWrapper);
    }

    /**
     * 修改优惠券详情
     *
     * @param playCouponDetails
     *            优惠券详情
     * @return 结果
     */
    @Override
    public boolean update(PlayCouponDetailsEntity playCouponDetails) {
        return updateById(playCouponDetails);
    }

    @Override
    public void updateCouponUseStateByIds(List<String> ids, String useState) {
        LocalDateTime useTime = CouponUseState.USED.getCode().equals(useState) ? LocalDateTime.now() : null;
        for (String id : ids) {
            baseMapper.update(null, com.baomidou.mybatisplus.core.toolkit.Wrappers.<PlayCouponDetailsEntity>lambdaUpdate()
                    .eq(PlayCouponDetailsEntity::getId, id)
                    .set(PlayCouponDetailsEntity::getUseState, useState)
                    .set(PlayCouponDetailsEntity::getUseTime, useTime));
        }
    }

    /**
     * 批量删除优惠券详情
     *
     * @param ids
     *            需要删除的优惠券详情主键
     * @return 结果
     */
    @Override
    public int deletePlayCouponDetailsByIds(String[] ids) {
        return playCouponDetailsMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除优惠券详情信息
     *
     * @param id
     *            优惠券详情主键
     * @return 结果
     */
    @Override
    public int deletePlayCouponDetailsById(String id) {
        return playCouponDetailsMapper.deleteById(id);
    }
}
