package com.ruoyi.mall.service.impl;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.mall.domain.*;
import com.ruoyi.mall.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mall.service.IMallPayLogService;

import javax.annotation.Resource;

/**
 * 支付记录Service业务层处理
 *
 * @author LCL
 * @date 2023-10-26
 */
@Service
public class MallPayLogServiceImpl implements IMallPayLogService
{
    @Resource
    private MallPayLogMapper mallPayLogMapper;
    @Resource
    private MallMemberUserMapper memberUserMapper;
    @Resource
    private VipGuideMapper vipGuideMapper;
    @Resource
    private MemberReportIntelligenceMapper memberReportIntelligenceMapper;
    @Resource
    private BusinessCouponMapper businessCouponMapper;
    @Resource
    private UserCouponMapper userCouponMapper;

    /**
     * 查询支付记录
     *
     * @param payId 支付记录主键
     * @return 支付记录
     */
    @Override
    public MallPayLog selectMallPayLogByPayId(Long payId)
    {
        return mallPayLogMapper.selectMallPayLogByPayId(payId);
    }

    /**
     * 查询支付记录列表
     *
     * @param mallPayLog 支付记录
     * @return 支付记录
     */
    @Override
    public List<MallPayLog> selectMallPayLogList(MallPayLog mallPayLog)
    {
        return mallPayLogMapper.selectMallPayLogList(mallPayLog);
    }

    /**
     * 新增支付记录
     *
     * @param mallPayLog 支付记录
     * @return 结果
     */
    @Override
    public int insertMallPayLog(MallPayLog mallPayLog)
    {
        mallPayLog.setCreateAt(DateUtils.getTime());
        return mallPayLogMapper.insertMallPayLog(mallPayLog);
    }

    /**
     * 修改支付记录
     *
     * @param mallPayLog 支付记录
     * @return 结果
     */
    @Override
    public int updateMallPayLog(MallPayLog mallPayLog)
    {
        mallPayLog.setCreateAt(DateUtils.getTime());
        return mallPayLogMapper.updateMallPayLog(mallPayLog);
    }

    /**
     * 批量删除支付记录
     *
     * @param payIds 需要删除的支付记录主键
     * @return 结果
     */
    @Override
    public int deleteMallPayLogByPayIds(Long[] payIds)
    {
        return mallPayLogMapper.deleteMallPayLogByPayIds(payIds);
    }

    /**
     * 删除支付记录信息
     *
     * @param payId 支付记录主键
     * @return 结果
     */
    @Override
    public int deleteMallPayLogByPayId(Long payId)
    {
        return mallPayLogMapper.deleteMallPayLogByPayId(payId);
    }

    /**
     * 根据支付单号查询支付记录
     *
     * @param outTradeNo 微信官方支付单号
     * @return 支付记录
     */
    @Override
    public MallPayLog selectMallPayLogByOutTradeNo(String outTradeNo) {
        return mallPayLogMapper.selectMallPayLogByOutTradeNo(outTradeNo);
    }

    /**
     * 修改支付记录及用户信息
     *
     * @param outTradeNo 微信官方支付单号
     * @return 结果
     */
    @Override
    public int updateMallPayLogAndMember(String outTradeNo) {
        // 修改会员到期时间
        MallPayLog payLog = mallPayLogMapper.selectMallPayLogByOutTradeNo(outTradeNo);
        VipGuide vipGuide = vipGuideMapper.selectVipGuideByGuideId(payLog.getGuideId());
        if (vipGuide != null) {
            String time = DateUtils.getTime();
            MallMemberUser user = new MallMemberUser();
            user.setId(payLog.getUserId());
            user.setIsOpen("1");
            user.setOpenTypeId(vipGuide.getGuideId());
            user.setOpenAt(time);

            if ("一年".equals(vipGuide.getOwnership())) {
                // 获取当前时间的Calendar实例
                Calendar calendar = Calendar.getInstance();
                calendar.add(Calendar.YEAR, 1);
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String dateStr = sdf.format(calendar.getTime());
                user.setExpireAt(dateStr);
            }
            if ("终身".equals(vipGuide.getOwnership())) {
                user.setExpireAt("终身");
            }
            memberUserMapper.updateMallMemberUser(user);
            //修改支付状态
            int a = updatePayLogStatusByPayId(payLog.getPayId());

            giveMemberCoupon(outTradeNo);
        }
        return 1;
    }

    private int updatePayLogStatusByPayId(Long payId){
        MallPayLog payLog = new MallPayLog();
        payLog.setPayId(payId);
        //
        payLog.setStatus("1");
        return mallPayLogMapper.updateMallPayLog(payLog);
    }

    /**修改支付记录及报告审核类型
     *
     * @param outTradeNo 微信官方支付单号
     * @return
     */
    @Override
    public int updateMallPayLogAndReport(String outTradeNo) {
        //修改支付状态
        MallPayLog payLog = mallPayLogMapper.selectMallPayLogByOutTradeNo(outTradeNo);
        int a = updatePayLogStatusByPayId(payLog.getPayId());
        //修改报告审核类型
        MemberReportIntelligence memberReportIntelligence = new MemberReportIntelligence();
        memberReportIntelligence.setReportId(payLog.getGuideId());
        memberReportIntelligence.setCheckType("1");
        int b = memberReportIntelligenceMapper.updateMemberReportIntelligence(memberReportIntelligence);
        return b;
    }

    /**
     * 用户返优惠券
     *
     * @param outTradeNo 微信官方支付单号
     * @return 结果
     */
    @Override
    public int giveMemberCoupon(String outTradeNo) {
        MallPayLog payLog = mallPayLogMapper.selectMallPayLogByOutTradeNo(outTradeNo);

        // 给上级返分享优惠券
        MallMemberUser myUser = memberUserMapper.selectMallMemberUserById(payLog.getUserId());// 我的信息
        // 有上级就返分享优惠券
        if (myUser != null && myUser.getIntroUser() != 0 && myUser.getIntroUser() != null) {
            BusinessCoupon businessCoupon = new BusinessCoupon();
            businessCoupon.setCouponType("2");
            businessCoupon.setDelFlag("0");
            List<BusinessCoupon> couponList = businessCouponMapper.selectBusinessCouponList(businessCoupon);
            if (couponList.size() > 0) {
                // 给用户返
                for (BusinessCoupon coupon : couponList) {

                    Date endTime = coupon.getEndTime();
                    Date nowTime = new Date();

                    if (nowTime.compareTo(endTime) < 1) {
                        UserCoupon userCoupon = new UserCoupon();
                        userCoupon.setUserId(myUser.getIntroUser());
                        userCoupon.setCouponId(coupon.getCouponId());
                        userCoupon.setFromUid(payLog.getUserId());
                        userCoupon.setTitle(coupon.getTitle());
                        userCoupon.setCouponMoney(coupon.getCouponMoney());
                        userCoupon.setCouponType(coupon.getCouponType());
                        userCoupon.setStartTime(coupon.getStartTime());
                        userCoupon.setEndTime(coupon.getEndTime());
                        userCoupon.setCreateTime(nowTime);
                        userCoupon.setStatus("1");
                        userCouponMapper.insertUserCoupon(userCoupon);
                    }
                }
            }
        }
        return 1;
    }

    /**
     * 修改用户优惠券状态
     *
     * @param outTradeNo 微信官方支付单号
     * @return 结果
     */
    @Override
    public int updateMemberCouponStatus(String outTradeNo) {
        MallPayLog mallPayLog = mallPayLogMapper.selectMallPayLogByOutTradeNo(outTradeNo);
        if (mallPayLog != null && mallPayLog.getResId() != null) {
            UserCoupon userCoupon = userCouponMapper.selectUserCouponByCuId(Long.parseLong(mallPayLog.getResId()));
            if (userCoupon != null) {
                userCoupon.setStatus("2");
                userCoupon.setUpdateTime(DateUtils.getNowDate());
                userCouponMapper.updateUserCoupon(userCoupon);
            }
        }
        return 1;
    }
}
