package com.cyl.h5.service;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.cyl.h5.pojo.request.H5MemberCouponRequest;
import com.cyl.h5.pojo.response.WechatMemberCouponResponse;
import com.cyl.manager.marketing.domain.WechatMemberCoupon;
import com.cyl.manager.marketing.mapper.WechatMemberCouponMapper;
import com.ruoyi.common.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Transactional
@Slf4j
public class H5MemberCouponService {

    @Autowired
    private WechatMemberCouponMapper wechatMemberCouponMapper;

    public List<WechatMemberCoupon> selectMemberCouponList(H5MemberCouponRequest h5MemberCouponRequest) {
        WechatMemberCoupon wechatMemberCoupon = new WechatMemberCoupon();
        wechatMemberCoupon.setMemberId(h5MemberCouponRequest.getMemberId());
        wechatMemberCoupon.setPhone(h5MemberCouponRequest.getPhone());
        wechatMemberCoupon.setCouponState(wechatMemberCoupon.getCouponState());
        //查询可用状态优惠券
        wechatMemberCoupon.setCouponState("SENDED");

        return wechatMemberCouponMapper.selectList2(wechatMemberCoupon);
    }

    public List<WechatMemberCouponResponse> selectMemberCouponListForOrder(Long memberId) {
        return wechatMemberCouponMapper.selectListForOrder(memberId);
    }

    public List<WechatMemberCouponResponse> selectMemberCouponById(Integer couponId) {
        return wechatMemberCouponMapper.selectMemberCouponById(couponId);
    }
    public void processExpiredCoupon() {
        String now = DateUtils.getTime();

        //String sql = "update wechat_member_coupon set coupon_state = 'EXPIRED' and update_time = '" + now + "' where expire_time < '" + now + "' and coupon_state = 'SENDED'";
        log.info("【优惠券过期处理任务】now：" + now);
        int result = wechatMemberCouponMapper.updateCouponState(now);


        log.info("【优惠券过期处理任务】更新结果：" + result);
    }
}
