package com.app.controller.api;

import com.app.annotation.platform.OperationLog;
import com.app.dto.Result;
import com.app.dto.SearchDto;
import com.app.entity.basics.OpenCourse;
import com.app.entity.order.OpenCourseShoppingCar;
import com.app.service.basics.OpenCourseService;
import com.app.service.order.OpenCourseShoppingCarService;
import com.app.util.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.Optional;

@Api(description = "开课 - 选课单")
@Slf4j
@Validated
@RestController("ApiOpenCourseShoppingCarController")
@RequestMapping("/api/open-course-shopping-car")
public class OpenCourseShoppingCarController {

    @Autowired
    private OpenCourseService openCourseService;

    @Autowired
    private OpenCourseShoppingCarService openCourseShoppingCarService;

    @OperationLog("更新 - 保存选课单")
    @ApiOperation(value = "保存[选课单]")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "accountChildId", value = "小孩标识", dataType = "String", paramType = "path", required = true, defaultValue = "")
    )
    @PostMapping("/child/{accountChildId}")
    public ResponseEntity<Result> save(@PathVariable("accountChildId") String accountChildId, @Valid @RequestBody SearchDto params) {
        String accountId = "";

        /* 判断[开课]是否满足条件 */
        Optional<OpenCourse> optional = openCourseService.findById(params.getId());
        if (optional.isPresent()) {
            OpenCourse _openCourse = optional.get();
            if (!DateUtil.between(_openCourse.getSignUpStartTime(), _openCourse.getSignUpEndTime(), new Date()) || _openCourse.getFreeCourseNumber() <= 0) {
                return new ResponseEntity(new Result().serverError(String.format("[%s]课程已过报名时间或名额已满", _openCourse.getName())), HttpStatus.OK);
            }
            OpenCourseShoppingCar _car = openCourseShoppingCarService.find(params.getId(), accountId, accountChildId);
            if (Optional.ofNullable(_car).isPresent()) {
                return new ResponseEntity(new Result().serverError("选课单已存在该课程"), HttpStatus.OK);
            }

            openCourseShoppingCarService.save(params.getId(), accountId, accountChildId);
            return new ResponseEntity(new Result().ok(), HttpStatus.OK);
        }

        return new ResponseEntity(new Result().serverError(), HttpStatus.INTERNAL_SERVER_ERROR);
    }

    @OperationLog("删除 - 删除选课单")
    @ApiOperation(value = "删除[选课单]")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "accountChildId", value = "小孩标识", dataType = "String", paramType = "path", required = true, defaultValue = ""),
            @ApiImplicitParam(name = "id", value = "开课标识", dataType = "String", paramType = "path", required = true, defaultValue = "")
    })
    @DeleteMapping("/child/{accountChildId}/{id}")
    public ResponseEntity<Result> delete(@PathVariable("accountChildId") String accountChildId, @PathVariable("id") String id) {
        String accountId = "";
        openCourseShoppingCarService.delete(id, accountId, accountChildId);
        return new ResponseEntity(new Result().ok(), HttpStatus.OK);
    }

}
