package org.jeecg.modules.app.controller.commercial;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.app.form.ClassPostponeForm;
import org.jeecg.modules.app.form.CourseQueryUsersForm;
import org.jeecg.modules.app.form.TemporaryCourseForm;
import org.jeecg.modules.app.service.IAppIsinService;
import org.jeecg.modules.app.vo.*;
import org.jeecg.modules.redission.RedissonDelayQueue;
import org.jeecg.modules.system.app.entity.AppCoursesPriceRules;
import org.jeecg.modules.system.app.entity.AppCoursesVerificationRecord;
import org.jeecg.modules.system.app.entity.FamilyMembers;
import org.jeecg.modules.system.app.form.VerifyForm;
import org.jeecg.modules.system.app.service.*;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;
import java.util.stream.Collectors;

@Slf4j
@Tag(name = "App商家端课程相关接口")
@RestController
@AllArgsConstructor
@RequestMapping("/app/course")
public class CommercialController {

    private final IAppOrderService appOrderService;

    private final IAppCoureseService appCoureseService;

    private final IAppCoursesPriceRulesService appCoursesPriceRulesService;

    private final IAppCoursesVerificationRecordService appCoursesVerificationRecordService;

    private final IFamilyMembersService familyMembersService;

    private final IAppIsinService appIsinService;

    /**
     * 查询部门下的课程
     *
     * @param orgCode
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Operation(summary = "查询部门下的课程 ")
    @GetMapping("/getPageCourse")
    public Result<IPage<AppCoursesPageVO>> getPageCourse(@Schema(description = "部门编号") @RequestParam(name = "orgCode") String orgCode,
                                                         @Schema(description = "页数") @RequestParam(name = "pageNo") Integer pageNo,
                                                         @Schema(description = "条数") @RequestParam(name = "pageSize") Integer pageSize) {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String userId = null;
        //如果是教练
        String roleCode = loginUser.getRoleCode();
        if (roleCode.contains("instructor")) {
            userId = loginUser.getId();
        }
        Page<AppCoursesPageVO> page = new Page<>(pageNo, pageSize);
        IPage<AppCoursesPageVO> pageList = appCoureseService.getPageCourse(page,orgCode,userId);
        return Result.OK(pageList);
    }

    /**
     * 课程详情
     *
     * @param courseId
     * @return
     */
    @Operation(summary = "课程详情")
    @GetMapping("/getCourseInfo/{courseId}")
    public Result<List<CoursesPriceRulesVO>> getCourseInfo(@Schema(description = "课程ID") @PathVariable("courseId") String courseId) {
        return Result.OK(appCoureseService.getCourseInfo(courseId));
    }

    /**
     * 拍照验课上传图片
     *
     * @param verifyForm
     * @return
     */
    @Operation(summary = "拍照验课上传图片")
    @PostMapping("/courseUploadImage")
    public Result<Boolean> courseUploadImage(@RequestBody VerifyForm verifyForm) {
        return Result.OK(appCoursesVerificationRecordService.courseUploadImage(verifyForm));
    }

    /**
     * 拍照验课人员查看
     *
     * @param form
     * @return
     */
    @Operation(summary = "拍照验课人员查看")
    @PostMapping("/courseQueryUsers")
    public Result<List<AppCoursesVerificationRecord>> courseQueryUsers(@RequestBody CourseQueryUsersForm form) {
        List<AppCoursesVerificationRecord> verificationRecords =
                appCoursesVerificationRecordService.list(Wrappers.<AppCoursesVerificationRecord>lambdaQuery()
                        .eq(AppCoursesVerificationRecord::getCoursesPriceRuleId, form.getCoursePriceRulesId())
                        .eq(ObjectUtil.isNotNull(form.getOrPostpone()), AppCoursesVerificationRecord::getOrPostpone, form.getOrPostpone())
                        .eq(ObjectUtil.isNotNull(form.getVerifyStatus()), AppCoursesVerificationRecord::getVerifyStatus, form.getVerifyStatus())
                );
        List<AppCoursesVerificationRecord> list = verificationRecords.stream()
                .collect(Collectors.collectingAndThen(
                        Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(AppCoursesVerificationRecord::getUseUserId))),
                        ArrayList::new
                ));
        return Result.OK(list);
    }

    /**
     * 根据姓名查询用户
     *
     * @param name
     * @return
     */
    @Operation(summary = "根据姓名查询用户 ")
    @GetMapping("/getFamilyMembersByName")
    public Result<List<FamilyMembers>> getFamilyMembersByName(@Schema(description = "页数") @RequestParam(name = "name") String name) {
        List<FamilyMembers> list = familyMembersService.list(Wrappers.<FamilyMembers>lambdaQuery().like(FamilyMembers::getFullName, name));
        return Result.OK(list);
    }

    /**
     * 临时约课
     *
     * @param temporaryCourseForm
     * @return
     */
    @Operation(summary = "临时约课")
    @PostMapping("/temporaryCourse")
    public Result<Boolean> temporaryCourse(@RequestBody TemporaryCourseForm temporaryCourseForm) {
        return Result.OK(appCoureseService.temporaryCourse(temporaryCourseForm));
    }

    /**
     * 课时延期学生列表
     *
     * @param coursePriceRulesId
     * @return
     */
    @Operation(summary = "课时延期学生列表")
    @GetMapping("/getClassPostponeUsers")
    public Result<List<FamilyUserVO>> getClassPostponeUsers(@Schema(description = "课时ID") @RequestParam(name = "coursePriceRulesId") String coursePriceRulesId) {
        return Result.OK(appCoureseService.getClassPostponeUsers(coursePriceRulesId));
    }

    /**
     * 课时延期（创建补课课时）
     *
     * @param form
     * @return
     */
    @Operation(summary = "课时延期（创建补课课时）")
    @PostMapping("/classPostpone")
    public Result<Boolean> classPostpone(@RequestBody ClassPostponeForm form) {
        return Result.OK(appCoureseService.classPostpone(form));
    }

    /**
     * 扫码核销查询订单详情
     *
     * @param orderId
     * @return
     */
    @Operation(summary = "扫码核销查询订单详情")
    @GetMapping("/scanCodeQueryOrder")
    public Result<ScanCodeQueryOrderVO> scanCodeQueryOrder(@Schema(description = "课时ID") @RequestParam(name = "orderId") String orderId) {
        return Result.OK(appOrderService.scanCodeQueryOrder(orderId));
    }

    /**
     * 扫码核销（查询订单详情）
     *
     * @param orderProInfoIds
     * @return
     */
    @Operation(summary = "扫码核销确认")
    @PutMapping("/scanCodeVerification")
    public Result<Boolean> scanCodeVerification(@RequestBody List<String> orderProInfoIds) {
        return Result.OK(appOrderService.scanCodeVerification(orderProInfoIds));
    }

    /**
     * 查询补课课表
     * @return
     */
    @Operation(summary = "查询补课课表")
    @GetMapping("/queryMakeUpClassTable")
    public Result<List<AppCoursesPriceRules>> queryMakeUpClassTable() {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        List<AppCoursesPriceRules> list = appCoursesPriceRulesService.list(Wrappers.<AppCoursesPriceRules>lambdaQuery()
                .eq(AppCoursesPriceRules::getCoursesType, 1)
                .eq(AppCoursesPriceRules::getOrgCode, loginUser.getOrgCode()));
        return Result.OK(list);
    }

    /**
     * 查询部门下的核销记录
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Operation(summary = "查询部门下的使用核销记录 ")
    @GetMapping("/pageAppIsinVerifyRecords")
    public Result<IPage<AppIsinVerifyVO>> pageAppIsinVerifyRecords(@Schema(description = "页数") @RequestParam(name = "pageNo") Integer pageNo,
                                                                   @Schema(description = "条数") @RequestParam(name = "pageSize") Integer pageSize) {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String orgCode = loginUser.getOrgCode();
        Page<AppIsinVerifyVO> page = new Page<>(pageNo, pageSize);
        Page<AppIsinVerifyVO> pageList = appIsinService.pageAppIsinVerifyRecords(page, orgCode);
        return Result.OK(pageList);
    }

    /**
     * 使用核销记录撤销
     *
     * @param appIsinId
     * @return
     */
    @Operation(summary = "使用核销记录撤销 ")
    @PutMapping("/repealVerifyRecord")
    public Result<String> repealVerifyRecord(@Schema(description = "appIsinId") @RequestParam(name = "appIsinId") String appIsinId) throws ParseException {
        return Result.OK(appIsinService.repealVerifyRecord(appIsinId));
    }

    /**
     * 支付异步通知
     *
     * @param request
     * @return
     */
    @PostMapping("/payNotify")
    public String wechatPayNotify(HttpServletRequest request) {
        return "支付回调成功";
    }

    private final RedissonDelayQueue redissonDelayQueue;
    /**
     * 测试超时未支付延时任务
     */
    @GetMapping("/orderTimeOutTask")
    public void orderTimeOutTask() {
        log.info("测试超时未支付延时任务-开始");
        String task = "OrderTimeOutTask_1953753939752968194";
        redissonDelayQueue.offerTask(task, 30);
        log.info("测试超时未支付延时任务-结束");
    }
}
