package com.ym.mec.student.controller;

import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.dao.StudentDao;
import com.ym.mec.biz.dal.dao.SysConfigDao;
import com.ym.mec.biz.dal.dto.DegreeFeeDto;
import com.ym.mec.biz.dal.dto.DegreePayDto;
import com.ym.mec.biz.dal.entity.ChildrenDayDegreeDetail;
import com.ym.mec.biz.dal.entity.ChildrenDayReserve;
import com.ym.mec.biz.dal.enums.YesOrNoEnum;
import com.ym.mec.biz.service.ChildrenDayDegreeDetailService;
import com.ym.mec.biz.service.ChildrenDayReserveService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.entity.HttpResponseResult;
import com.ym.mec.common.exception.BizException;
import com.ym.mec.util.date.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

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

@RequestMapping("childrenDay")
@Api(tags = "6.1儿童节活动")
@RestController
public class ChildrenDayController extends BaseController {
    @Autowired
    private SysUserFeignService sysUserFeignService;
    @Autowired
    private ChildrenDayReserveService childrenDayReserveService;
    @Autowired
    private StudentDao studentDao;
    @Autowired
    private ChildrenDayDegreeDetailService childrenDayDegreeDetailService;
    @Autowired
    private SysConfigDao sysConfigDao;

    @ApiOperation(value = "占位")
    @PostMapping("/reserve")
    public HttpResponseResult<ChildrenDayReserve> reserve() {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (Objects.isNull(user)) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        if (user.getOrganId().equals(43)) {
            return failed("您不可参与该活动，请联系乐团主管");
        }
        return succeed(childrenDayReserveService.addReserve(user, YesOrNoEnum.YES));
    }

    @ApiOperation(value = "获取占位信息")
    @GetMapping("/getReserve")
    public HttpResponseResult<ChildrenDayReserve> getReserve() {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (Objects.isNull(user)) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        return succeed(childrenDayReserveService.getUserReserve(user.getId()));
    }


    @ApiOperation(value = "获取考级相关的价格")
    @GetMapping("/getFeeInfo")
    public HttpResponseResult<DegreeFeeDto> getFeeInfo() {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (Objects.isNull(user)) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        return succeed(childrenDayReserveService.getFeeInfo(user.getOrganId()));
    }

    @ApiOperation(value = "6.1考级支付")
    @PostMapping("/pay")
    public HttpResponseResult pay(@RequestBody DegreePayDto degreePayDto) throws Exception {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            throw new BizException("用户信息获取失败，请重新登陆");
        }
        if (sysUser.getOrganId().equals(43) &&
                (degreePayDto.getVip1v1() || degreePayDto.getVip1v2() || degreePayDto.getTheoryCourse())
        ) {
            return failed("您不可参与该活动，请联系乐团主管");
        }
        Date nowTime = new Date();
        String endTimeStr = sysConfigDao.findConfigValue("children_day_end_time");
        Date endTime = DateUtil.stringToDate(endTimeStr);
        if (endTime.compareTo(nowTime) <= 0) {
            throw new BizException("活动已结束，谢谢关注");
        }
        studentDao.lockUser(sysUser.getId());

        degreePayDto.setUserId(sysUser.getId());
        degreePayDto.setOrganId(sysUser.getOrganId());
        degreePayDto.setUser(sysUser);
        List<ChildrenDayDegreeDetail> ingDetails = childrenDayDegreeDetailService.getByUserIdAndStatus(sysUser.getId(), 1);
        if (!degreePayDto.getRePay() && ingDetails.size() > 0) {
            return failed(HttpStatus.CONTINUE, "您有待支付的订单");
        }

        Map payMap = childrenDayReserveService.pay(degreePayDto);
        if (payMap.containsKey("tradeState")) {
            return failed(HttpStatus.CREATED, payMap, "恭喜您,支付成功!");
        }
        return succeed(payMap);
    }

}
