package com.ruoyi.wx.web.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.WxUser;
import com.ruoyi.common.core.domain.model.LoginWxUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.framework.recovery.domain.WxCoupons;
import com.ruoyi.framework.recovery.domain.WxCredit;
import com.ruoyi.framework.recovery.mapper.WxCouponsMapper;
import com.ruoyi.framework.recovery.mapper.WxCreditMapper;
import com.ruoyi.framework.recovery.mapper.WxUserMapper;
import com.ruoyi.wx.web.domain.dto.CreditDto;
import com.ruoyi.wx.web.domain.vo.WxCreditVo;
import com.ruoyi.wx.web.domain.vo.WxUserVo;
import com.ruoyi.wx.web.service.IWxPersonCenterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WxPersonCenterServiceImpl implements IWxPersonCenterService {
    @Autowired
    private WxCreditMapper wxCreditMapper;

    @Autowired
    private WxCouponsMapper couponsMapper;

    @Autowired
    private WxUserMapper wxUserMapper;

    @Override
    public AjaxResult getIndex() {
        LoginWxUser loginWxUser = SecurityUtils.getLoginWxUser();
        if (loginWxUser == null) {
            throw new SecurityException("请登录");
        }
        WxUser wxUser = loginWxUser.getWxUser();
        WxUserVo userVo = new WxUserVo();
        BeanUtils.copyBeanProp(userVo, wxUser);
        Map<String, Object> data = new HashMap<>();
        // 积分情况
        List<WxCredit> credits = wxCreditMapper.selectWxCreditList(new WxCredit());
        Date start = DateUtil.beginOfDay(new Date());
        Date end = DateUtil.endOfDay(new Date());
        List<Long> ids = wxCreditMapper.selectWxCreditUser(wxUser.getWxOpenid(), start, end);
        WxCreditVo wxCreditVo = new WxCreditVo();
        wxCreditVo.setCredits(credits);
        wxCreditVo.setRemoveCreateIds(ids);
        // 优惠卷情况
        List<WxCoupons> wxCoupons = couponsMapper.selectWxCouponsList(new WxCoupons());
        // 用户信息
        data.put("userVo", userVo);
        // 积分信息
        data.put("credits", wxCreditVo);
        // 优惠卷信息
        data.put("wxCoupons", wxCoupons);
        return AjaxResult.success(data);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addCredit(CreditDto creditDto) {
        LoginWxUser loginWxUser = SecurityUtils.getLoginWxUser();
        if (loginWxUser == null) {
            throw new SecurityException("请登录");
        }
        WxUser wxUser = loginWxUser.getWxUser();
        wxCreditMapper.insertWxCreditUser(wxUser.getWxOpenid(), creditDto.getId(), new Date());
        wxUser.setPoints(wxUser.getPoints() + creditDto.getCreditNum());
        int result = wxUserMapper.updateWxUser(wxUser);
        return result > 0;
    }
}
