package com.app.controller.admin.guardian;

import com.app.annotation.platform.OperationLog;
import com.app.constant.SystemConstant;
import com.app.controller.api.GuardianChildRequest;
import com.app.dto.PageResult;
import com.app.dto.Result;
import com.app.dto.account.search.SearchAccountDto;
import com.app.entity.guardian.Guardian;
import com.app.entity.order.OpenCourseOrderItem;
import com.app.service.guardian.GuardianChildService;
import com.app.service.guardian.GuardianService;
import com.app.service.order.OpenCourseOrderService;
import io.swagger.annotations.Api;
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.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 功能描述：家长管理
 * https://www.jianshu.com/p/cf9ad8c3621d
 *
 * @Auther: 梁展鹏
 * @Date: 2018/7/24 11:38
 */
@Api("家长管理")
@Slf4j
@RestController("AdminGuardianController")
@RequestMapping("/admin/guardian")
public class GuardianController {

    @Autowired
    private GuardianService guardianService;

    @Autowired
    private GuardianChildService guardianChildService;

    @Autowired
    private OpenCourseOrderService openCourseOrderService;

    @ApiOperation(value = "查询所有[账号]")
    @PostMapping("/page")
    public ResponseEntity<Result<PageResult<Guardian>>> page(@Valid @RequestBody SearchAccountDto params) {
        return new ResponseEntity(new Result().ok(guardianService.page(params)), HttpStatus.OK);
    }

    @OperationLog("更新 - 关联小孩")
    @ApiOperation(value = "关联小孩")
    @PostMapping("/child/relation")
    public ResponseEntity<Result> relation(@Valid @RequestBody GuardianChildRequest request) {
        guardianChildService.relation(request, request.getGuardianId());
        return new ResponseEntity(new Result().ok(), HttpStatus.OK);
    }

    @OperationLog("更新 - 删除关联小孩")
    @ApiOperation(value = "删除关联小孩")
    @DeleteMapping("/child/{accountChildId}")
    public ResponseEntity<Result> deleteChild(@PathVariable("accountChildId") String accountChildId) {
        guardianChildService.delete(SystemConstant.ADMIN_ID, accountChildId);
        return new ResponseEntity(new Result().ok(), HttpStatus.OK);
    }

    @ApiOperation(value = "查询所有[课程]")
    @PostMapping("/class/{accountId}")
    public ResponseEntity<Result<List<OpenCourseOrderItem>>> classByAccountId(@PathVariable("accountId") String accountId) {
        return new ResponseEntity(new Result().ok(openCourseOrderService.classByAccountId(accountId)), HttpStatus.OK);
    }

}
