package org.jeecg.modules.system.app.service.impl;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.system.app.entity.AppCoursesVerificationRecord;
import org.jeecg.modules.system.app.entity.AppOrder;
import org.jeecg.modules.system.app.entity.AppOrderProInfo;
import org.jeecg.modules.system.app.entity.FamilyMembers;
import org.jeecg.modules.system.app.form.CoursesVerificationRecordForm;
import org.jeecg.modules.system.app.form.VerifyForm;
import org.jeecg.modules.system.app.mapper.AppCoursesVerificationRecordMapper;
import org.jeecg.modules.system.app.mapper.AppOrderMapper;
import org.jeecg.modules.system.app.mapper.AppOrderProInfoMapper;
import org.jeecg.modules.system.app.mapper.FamilyMembersMapper;
import org.jeecg.modules.system.app.service.IAppCoursesVerificationRecordService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @Description: nm_courses_verification_record
 * @Author: jeecg-boot
 * @Date: 2025-08-04
 * @Version: V1.0
 */
@Service
public class AppCoursesVerificationRecordServiceImpl extends ServiceImpl<AppCoursesVerificationRecordMapper, AppCoursesVerificationRecord> implements IAppCoursesVerificationRecordService {

    @Resource
    private FamilyMembersMapper  familyMembersMapper;

    @Resource
    private AppOrderMapper appOrderMapper;

    @Resource
    private AppOrderProInfoMapper appOrderProInfoMapper;

    @Override
    public Boolean courseUploadImage(VerifyForm verifyForm) {

        //获取登录用户信息
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

        for (CoursesVerificationRecordForm coursesVerificationRecordForm : verifyForm.getFormList()) {
            List<AppCoursesVerificationRecord> verificationRecordList = baseMapper.selectList(Wrappers.<AppCoursesVerificationRecord>lambdaQuery()
                    .eq(AppCoursesVerificationRecord::getCoursesPriceRuleId, verifyForm.getCoursePriceRulesId())
                    .eq(AppCoursesVerificationRecord::getUseUserId, coursesVerificationRecordForm.getId())
                    .eq(AppCoursesVerificationRecord::getVerifyStatus, CommonConstant.STATUS_NORMAL)
            );

            if(CollUtil.isEmpty(verificationRecordList)){
                FamilyMembers familyMembers = familyMembersMapper.selectById(coursesVerificationRecordForm.getId());
                throw new JeecgBootException("当前用户"+familyMembers.getFullName()+"没有应验课程");
            }
            verificationRecordList.forEach(verificationRecord -> {
                verificationRecord.setVerifyStatus(CommonConstant.STATUS_1_INT);
                verificationRecord.setVerifyUserId(sysUser.getId());
                verificationRecord.setVerifyUserName(sysUser.getRealname());
                verificationRecord.setVerifyImage(coursesVerificationRecordForm.getVerifyImage());
                baseMapper.updateById(verificationRecord);
                //修改订单状态
                String useUserId = verificationRecord.getUseUserId();
                String coursesId = verificationRecord.getCoursesId();
                AppOrderProInfo orderProInfo = appOrderProInfoMapper.selectOne(Wrappers.<AppOrderProInfo>lambdaQuery().eq(AppOrderProInfo::getFamilyUserId, useUserId).eq(AppOrderProInfo::getProductId, coursesId).last("limit 1"));
                if (ObjectUtil.isNotEmpty(orderProInfo)){
                    orderProInfo.setOrderStatus(CommonConstant.ORDER_STATUS_2);
                    appOrderProInfoMapper.updateById(orderProInfo);
                }
                String orderId = orderProInfo.getOrderId();
                AppOrder appOrder = appOrderMapper.selectById(orderId);
                if (ObjectUtil.isNotEmpty(appOrder)){
                    List<AppOrderProInfo> proInfoList = appOrderProInfoMapper.selectList(Wrappers.<AppOrderProInfo>lambdaQuery().eq(AppOrderProInfo::getOrderId, orderId).eq(AppOrderProInfo::getType, CommonConstant.ORDER_PRO_INFO_TYPE_5));
                    long count = proInfoList.stream().filter(info -> Objects.equals(info.getOrderStatus(), CommonConstant.ORDER_STATUS_2)).count();
                    if(count == proInfoList.size()){
                        appOrder.setOrderStatus(CommonConstant.ORDER_STATUS_2);
                        appOrderMapper.updateById(appOrder);
                    }
                }
            });
        }
        return Boolean.TRUE;
    }
}
