package com.ym.mec.student.controller;


import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.dao.DegreeRegistrationDao;
import com.ym.mec.biz.dal.dao.SporadicChargeInfoDao;
import com.ym.mec.biz.dal.dto.SporadicPayDto;
import com.ym.mec.biz.dal.entity.DegreeRegistration;
import com.ym.mec.biz.dal.entity.Employee;
import com.ym.mec.biz.dal.entity.SporadicChargeInfo;
import com.ym.mec.biz.dal.enums.DealStatusEnum;
import com.ym.mec.biz.dal.enums.SporadicChargeTypeEnum;
import com.ym.mec.biz.dal.page.SporadicChargeInfoQueryInfo;
import com.ym.mec.biz.service.DegreeRegistrationService;
import com.ym.mec.biz.service.SporadicChargeInfoService;
import com.ym.mec.biz.service.StudentPaymentOrderService;
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.idcard.IdcardValidator;
import com.ym.mec.util.validator.CommonValidator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RequestMapping("degree")
@Api(tags = "考级报名服务")
@RestController
public class DegreeController extends BaseController {

    @Autowired
    private StudentPaymentOrderService studentPaymentOrderService;

    @Autowired
    private DegreeRegistrationDao degreeRegistrationDao;

    @Autowired
    private DegreeRegistrationService degreeRegistrationService;

    @Autowired
    private SporadicChargeInfoDao sporadicChargeInfoDao;

    @Autowired
    private SporadicChargeInfoService sporadicChargeInfoService;


    @ApiOperation(value = "考级报名支付")
    @PostMapping("/pay")
    public HttpResponseResult pay(DegreeRegistration degreeRegistration) throws Exception {
        if (degreeRegistration == null ||
                degreeRegistration.getSporadicId() == null ||
                degreeRegistration.getName() == null ||
                degreeRegistration.getGender() == null ||
                degreeRegistration.getIdcard() == null ||
                degreeRegistration.getCity() == null ||
                degreeRegistration.getSchool() == null ||
                degreeRegistration.getSubject() == null ||
                degreeRegistration.getTheoryLevel() == null ||
                degreeRegistration.getMobile() == null

        ) {
            throw new BizException("必填参数不能为空");
        }
        if (!CommonValidator.isMobileNo(degreeRegistration.getMobile())) {
            throw new BizException("手机号码不正确，请核对");
        }

        IdcardValidator idcardValidator = new IdcardValidator();
        if (!idcardValidator.isValidatedAllIdcard(degreeRegistration.getIdcard())) {
            throw new BizException("身份证号不正确，请核对");
        }

        SporadicChargeInfo info = sporadicChargeInfoDao.get(degreeRegistration.getSporadicId());
        if (info == null || info.getDelFlag().equals(1)) {
            return failed("报名信息不存在");
        }
        if (info.getOrganId() != 42 || !info.getChargeType().equals(SporadicChargeTypeEnum.LEVEL)) {
            return failed("报名信息不存在");
        }

        DegreeRegistration degree = degreeRegistrationDao.findByMobileAndSporadicId(degreeRegistration.getMobile(), degreeRegistration.getSporadicId());

        if (degree != null && degree.getStatus().equals(2)) {
            return failed("您本次已报名，请勿重复报名");
        }

        if (!degreeRegistration.getIsRepay() && degree != null) {
            Integer ingOrder = studentPaymentOrderService.findOrderByGroupType(degree.getId(), degreeRegistration.getSporadicId(), "SPORADIC", DealStatusEnum.ING);
            if (ingOrder != null && ingOrder > 0) {
                return failed(HttpStatus.CONTINUE, "您有待支付的订单");
            }
        }
        Map payMap = degreeRegistrationService.pay(degreeRegistration);
        if (payMap.containsKey("tradeState")) {
            return failed(HttpStatus.CREATED, "恭喜您,支付成功!");
        }
        return succeed(payMap);
    }


    @ApiOperation(value = "查询收费列表")
    @GetMapping("/queryList")
    public HttpResponseResult queryPage(SporadicChargeInfoQueryInfo queryInfo) {
        queryInfo.setOrganId("42");
        queryInfo.setChargeType(SporadicChargeTypeEnum.LEVEL);
        queryInfo.setRows(99);
        queryInfo.setOpenFlag(0);
        queryInfo.setSort("amount_");
        queryInfo.setOrder("ASC");
        return succeed(sporadicChargeInfoService.queryDetailPage(queryInfo));
    }

}
