package com.jzdsh.auto.biz;

import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.ext.kit.DateKit;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.jzdsh.auto.model.*;
import com.jzdsh.util.CommonCons;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.time.DateUtils;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.Optional;

/**
 * @ProjectName: auto
 * @Package: com.jzdsh.auto.biz
 * @ClassName: CouponService
 * @Author: Jzdsh
 * @Description: 优惠券
 * @Date: 2020/3/23 12:26
 * @Version: 1.0
 */
@Log4j2
public class CouponService {

    @Inject
    UserInfoService userInfoService;
    @Inject
    ShopService shopService;
    @Inject
    ShareInfoService shareInfoService;
    @Inject
    CouponSetService couponSetService;
    @Inject
    MessageService messageService;


    @Before(Tx.class)
    public void saveCommonAndSharerCoupon(int userId, String token) {

        UserInfo user = userInfoService.getByUserId(userId);

        Shop shop = shopService.getByUserId(userId);
        Date date = new Date( );
        //先判断本人是否已有店铺,如果有店铺就直接退出
        if (shop != null) {
            return;
        }

        //判断是分享过来得优惠券 还是正常入驻得优惠券
        // 判断是否 是 分享过来的 token
        ShareInfo shareInfo = shareInfoService.getByToken(token);

        if (shareInfo != null) {
            //分享过来的
            String utoken = shareInfo.getToken( );

            //优惠金额
            Integer money = Optional.ofNullable(shareInfo.getMoney( )).orElse(60);

            //有效期
            String validity = Optional.ofNullable(shareInfo.getValidity( )).orElse("12");
            String validityTime = LocalDate.now( ).plusMonths(Integer.valueOf(validity)).toString( );

            // 到期时间
            Date dateAddOne = DateKit.toDate(validityTime);

            UserInfo p = userInfoService.getByToken(utoken);
            save(p.getId(),money,date,dateAddOne);
            messageService.save(p.getId( ), "恭喜您~ 入驻成功，赠送您一张" + money + "元的优惠券！");

            String sToken = shareInfo.getShareToken( );
            UserInfo p1 = userInfoService.getByToken(sToken);
            save(p1.getId(),money,date,dateAddOne);
            messageService.save(p1.getId( ), "恭喜您~ 您邀请的好友 \"" + user.getNickName( ) + "\" 入驻成功，赠送您一张" + money + "元的优惠券！");

            //更新分享 状态已认领(已认证)
            shareInfo.setStatus(CommonCons.ONE);
            shareInfo.update();

        } else {
            //正常入驻
            CouponSet couponSet = Optional.ofNullable(couponSetService.getByTypeUsed(CommonCons.ONE, CommonCons.ZERO)).orElse(new CouponSet( ));

            Integer money = Optional.ofNullable(couponSet.getMoney( )).orElse(60);
            String validity = Optional.ofNullable(couponSet.getValidity( )).orElse("12");
            String validityTime = LocalDate.now( ).plusMonths(Integer.valueOf(validity)).toString( );
            Date dateAddOne = DateKit.toDate(validityTime);
            save(userId,money,date,dateAddOne);
            messageService.save(userId, "恭喜您~ 入驻成功，赠送您一张" + money + "元的优惠券!");
        }

    }

    /**
     * 新增优惠券
     * @param userId
     * @param money
     * @param createDate
     * @param passDate
     */
    public void save(int userId,int money,Date createDate,Date passDate){
        Coupon coupon = new Coupon( );
        coupon.setUserId(userId);
        //未使用
        coupon.setIsUse(CommonCons.AUTHCOUPON_IS_USE_ZERO);
        //优惠价
        coupon.setMoney(money);
        //来源 0正常入驻  1 分享入驻
        coupon.setSource(CommonCons.AUTHCOUPON_SOURCE_ZERO);
        coupon.setCreateDate(createDate);
        coupon.setPassDate(passDate);
        coupon.save();
    }
}
