package shop.alien.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import shop.alien.entity.store.LifeCoupon;
import shop.alien.entity.store.StoreDynamicDiscount;
import shop.alien.entity.store.StoreDynamicDiscountReceiveLog;
import shop.alien.mapper.StoreDynamicDiscountReceiveLogMapper;
import shop.alien.store.service.LifeCouponService;
import shop.alien.store.service.StoreDynamicDiscountReceiveLogService;
import shop.alien.store.service.StoreDynamicDiscountService;
import shop.alien.util.common.UniqueRandomNumGenerator;

/**
 * 商户动态折扣领取记录 服务实现类
 *
 * @author ssk
 * @since 2025-03-18
 */
@Service
@RequiredArgsConstructor
public class StoreDynamicDiscountReceiveLogServiceImpl extends ServiceImpl<StoreDynamicDiscountReceiveLogMapper, StoreDynamicDiscountReceiveLog> implements StoreDynamicDiscountReceiveLogService {

    /**
     * 用户优惠券
     */
    private final LifeCouponService lifeCouponService;

    /**
     * 折扣详情
     */
    private final StoreDynamicDiscountService storeDynamicDiscountService;

    /**
     * 用户参与活动
     *
     * @param discountId 折扣活动id
     * @param userId     用户id
     * @return boolean
     */
    @Override
    public boolean addLog(Integer discountId, Integer userId) {
        StoreDynamicDiscountReceiveLog storeDynamicDiscountReceiveLog = new StoreDynamicDiscountReceiveLog();
        storeDynamicDiscountReceiveLog.setStatus(0);
        storeDynamicDiscountReceiveLog.setDiscountId(discountId);
        storeDynamicDiscountReceiveLog.setUserId(userId);
        storeDynamicDiscountReceiveLog.setDiscountPercent("10");
        return this.save(storeDynamicDiscountReceiveLog);
    }

    /**
     * 用户领取折扣
     *
     * @param discountId 折扣活动id
     * @param userId     用户id
     * @return boolean
     */
    @Override
    public boolean userReceive(Integer discountId, Integer userId) {
        LambdaUpdateWrapper<StoreDynamicDiscountReceiveLog> lambdaQueryWrapper = new LambdaUpdateWrapper<>();
        lambdaQueryWrapper.eq(StoreDynamicDiscountReceiveLog::getDiscountId, discountId)
                .eq(StoreDynamicDiscountReceiveLog::getUserId, userId)
                .set(StoreDynamicDiscountReceiveLog::getStatus, 1);
        boolean stepA = this.update(lambdaQueryWrapper);
        StoreDynamicDiscount byId = storeDynamicDiscountService.getById(discountId);
        LambdaUpdateWrapper<StoreDynamicDiscountReceiveLog> lambdaQueryWrapperB = new LambdaUpdateWrapper<>();
        lambdaQueryWrapper.eq(StoreDynamicDiscountReceiveLog::getDiscountId, discountId)
                .eq(StoreDynamicDiscountReceiveLog::getUserId, userId);
        StoreDynamicDiscountReceiveLog one = this.getOne(lambdaQueryWrapperB);
        //生成优惠券
        String code = UniqueRandomNumGenerator.generateUniqueCode(12);
        LifeCoupon lifeCoupon = new LifeCoupon();
        lifeCoupon.setCouponCode(code);
        lifeCoupon.setName(byId.getActivityName());
        lifeCoupon.setStoreId(byId.getStoreId().toString());
        lifeCoupon.setPrice("0");
        lifeCoupon.setOffprice(one.getDiscountPercent());
        //开始时间
        lifeCoupon.setStartDate(byId.getActivityStartTime());
        //结束时间
        lifeCoupon.setEndDate(byId.getActivityEndTime());
        lifeCoupon.setSingleQty(1);
        lifeCoupon.setStatus(0);
        lifeCoupon.setType(1);
        boolean stepB = lifeCouponService.save(lifeCoupon);
        return stepA && stepB;
    }
}
