package com.ym.mec.student.controller;

import com.ym.mec.biz.dal.dao.SubjectChangeDao;
import com.ym.mec.biz.dal.entity.SubjectChange;
import com.ym.mec.biz.dal.enums.SubjectChangeStatusEnum;
import com.ym.mec.biz.service.SubjectChangeService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.entity.HttpResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
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.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Map;

@RequestMapping("subjectChange")
@Api(tags = "声部更换服务")
@RestController
public class SubjectChangeController extends BaseController {

    @Autowired
    private SubjectChangeService subjectChangeService;
    @Autowired
    private SubjectChangeDao subjectChangeDao;


    @ApiOperation(value = "获取更换详情")
    @GetMapping("/getChangeInfo")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Integer")})
    public HttpResponseResult<SubjectChange> getChangeInfo(Integer id) {
        if (id == null) {
            return failed("参数校验异常");
        }
        return succeed(subjectChangeService.getChangeInfo(id));
    }

    @ApiOperation(value = "支付更换声部费用")
    @PostMapping("/payChange")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "金额", value = "amount", required = true, dataType = "BigDecimal"),
            @ApiImplicitParam(name = "是否使余额", value = "isUseBalancePayment", required = true, dataType = "Boolean")
    })
    public HttpResponseResult<Map> payChange(Integer id, BigDecimal amount, Boolean isUseBalancePayment, Boolean isRepay) throws Exception {
        SubjectChange subjectChange = subjectChangeDao.get(id);
        if (!isRepay && subjectChange.getStatus().equals(SubjectChangeStatusEnum.ING)) {
            return failed(HttpStatus.CONTINUE, "您有待支付的订单");
        }
        return succeed(subjectChangeService.payChange(id, amount, isUseBalancePayment, isRepay));
    }


}
