package com.ruoyi.busi.service.impl;

import java.util.List;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.busi.domain.BusiNotice;
import com.ruoyi.busi.mapper.BusiNoticeMapper;
import com.ruoyi.busi.query.AppNoticeSign;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.member.service.IMemberCardService;
import com.ruoyi.member.service.IMemberUserService;
import com.ruoyi.member.vo.MemberUserVO;
import com.ruoyi.system.service.ISysUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.busi.mapper.BusiNoticeSignMapper;
import com.ruoyi.busi.domain.BusiNoticeSign;
import com.ruoyi.busi.service.IBusiNoticeSignService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 通告报名Service业务层处理
 *
 * @author 朱春云
 * @date 2025-03-17
 */
@Service
public class BusiNoticeSignServiceImpl extends ServiceImpl<BusiNoticeSignMapper,BusiNoticeSign>  implements IBusiNoticeSignService
{
    @Autowired
    private BusiNoticeSignMapper busiNoticeSignMapper;
    @Autowired
    private ISysUserService userService;
    @Resource
    private IMemberCardService cardService;
    @Autowired
    private BusiNoticeMapper noticeMapper;
    @Autowired
    private IMemberUserService memberUserService;

    @Override
    public IPage<BusiNoticeSign> queryListPage(BusiNoticeSign pageReqVO, Page<BusiNoticeSign> page) {
        return busiNoticeSignMapper.queryListPage(pageReqVO, page);
    }

    /**
     * 用户报名
     * @param appNoticeSign 报名实体
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void userSign(AppNoticeSign appNoticeSign) throws Exception {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        //当前用户是否已报名该通告
        if(busiNoticeSignMapper.selectOne(new LambdaQueryWrapper<BusiNoticeSign>().eq(BusiNoticeSign::getNoticeId, appNoticeSign.getNoticeId()).eq(BusiNoticeSign::getUserId, loginUser.getUserId()).last("limit 1"))!=null){
            throw new Exception("请勿重复报名！");
        }
        //判断报名是否到上线
        BusiNotice busiNotice = noticeMapper.selectById(appNoticeSign.getNoticeId());

        LambdaQueryWrapper<BusiNoticeSign> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BusiNoticeSign::getNoticeId, appNoticeSign.getNoticeId());
        if(null!=busiNotice.getNeedNum()&&busiNoticeSignMapper.selectCount(queryWrapper)>=busiNotice.getNeedNum()){
            throw new Exception("报名人数已满！");
        }
        SysUser sysUser = userService.selectUserById(loginUser.getUserId());
        //先使用赠送次数
        MemberUserVO memberUserVO = memberUserService.queryByUserId(loginUser.getUserId(), "02");
        if(memberUserVO.getSendReportNum()>=appNoticeSign.getCardList().size()){
            //直接扣赠送次数即可
            memberUserVO.setSendReportNum(memberUserVO.getSendReportNum()-appNoticeSign.getCardList().size());
            memberUserService.updateById(memberUserVO);
        }else{
            //先扣赠送次数 再扣权益次数
          long lastNum =   appNoticeSign.getCardList().size() - memberUserVO.getSendReportNum();
          cardService.dealMemberRights(loginUser.getUserId(),null,"report",Integer.valueOf(Long.toString(lastNum)));
            memberUserVO.setSendReportNum(0L);
            memberUserService.updateById(memberUserVO);
        }
        for (JSONObject cardInfo : appNoticeSign.getCardList()) {
            BusiNoticeSign busiNoticeSign =new BusiNoticeSign();
            busiNoticeSign.setNoticeId(appNoticeSign.getNoticeId());
            busiNoticeSign.setUserId(loginUser.getUserId());
            busiNoticeSign.setNickname(sysUser.getNickName());
            busiNoticeSign.setAvatar(sysUser.getAvatar());
            busiNoticeSign.setStatus("01");
            busiNoticeSign.setFormData(JSONArray.toJSONString(appNoticeSign.getCustomForm()));
            busiNoticeSign.setCardId(cardInfo.getString("id"));
            busiNoticeSign.setPrice(cardInfo.getBigDecimal("price"));
            busiNoticeSign.setRemark(cardInfo.getString("remark"));
            busiNoticeSign.setTel(cardInfo.getString("tel"));
            busiNoticeSign.setIsSuper(appNoticeSign.getIsSuper());
            busiNoticeSign.setAddrId(appNoticeSign.getAddressId());
            busiNoticeSignMapper.insert(busiNoticeSign);
        }

    }

    /**
     * 选中报名通过
     * @param signIds
     */
    @Override
    public void chooseSign(String[]  signIds) {
        for (String signId : signIds) {
            BusiNoticeSign noticeSign = this.getById(signId);
            noticeSign.setStatus("02");
            this.updateById(noticeSign);
        }
    }
}
