package cn.com.qmth.examcloud.core.questions.api.controller;

import cn.com.qmth.examcloud.api.commons.security.bean.User;
import cn.com.qmth.examcloud.commons.exception.StatusException;
import cn.com.qmth.examcloud.core.questions.base.Constants;
import cn.com.qmth.examcloud.core.questions.dao.entity.Property;
import cn.com.qmth.examcloud.core.questions.dao.entity.dto.PropertyDto;
import cn.com.qmth.examcloud.core.questions.service.PropertyService;
import cn.com.qmth.examcloud.web.support.ControllerSupport;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author weiwenhai
 * @describle 属性
 * @date 2017.11.8
 */
@Controller
@RequestMapping("${api_cqb}/")
public class PropertyController extends ControllerSupport {
    @Autowired
    private PropertyService propertyService;

    @ApiOperation(value = "查询所有属性(树结构)")
    @GetMapping(value = "/property/all/{coursePropertyId}")
    public ResponseEntity<Object> findAllProperty(@PathVariable String coursePropertyId) {
        User user = getAccessUser();
        if (user == null) {
            throw new StatusException(Constants.SYS_CODE_500, "请先登录！");
        }

        List<PropertyDto> properties = propertyService.findParentPropertyDtoList(coursePropertyId, user.getRootOrgId());
        return new ResponseEntity<>(properties, HttpStatus.OK);
    }

    @ApiOperation(value = "新增或更新属性")
    @RequestMapping(value = "/property/save", method = {RequestMethod.POST, RequestMethod.PUT})
    public ResponseEntity<Object> saveProperty(@RequestBody PropertyDto property) {
        User user = getAccessUser();
        if (user == null) {
            throw new StatusException(Constants.SYS_CODE_500, "请先登录！");
        }

        property.setOrgId(user.getRootOrgId());
        propertyService.saveProperty(property);

        return new ResponseEntity<>(HttpStatus.OK);
    }

    @ApiOperation(value = "删除属性")
    @DeleteMapping(value = "/property/delete/{propertyId}/{coursePropertyId}")
    public ResponseEntity<Object> deleteProperty(@PathVariable String propertyId, @PathVariable String coursePropertyId) {
        User user = getAccessUser();
        if (user == null) {
            throw new StatusException(Constants.SYS_CODE_500, "请先登录！");
        }

        propertyService.deleteProperty(propertyId, coursePropertyId, user.getRootOrgId());
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @ApiOperation(value = "查询所有属性")
    @GetMapping(value = "/property/{coursePropertyId}")
    public ResponseEntity<Object> findAll(@PathVariable String coursePropertyId) {
        User user = getAccessUser();
        if (user == null) {
            throw new StatusException(Constants.SYS_CODE_500, "请先登录！");
        }

        List<Property> properties = propertyService.findProperties(coursePropertyId, user.getRootOrgId());
        return new ResponseEntity<>(properties, HttpStatus.OK);
    }

    @ApiOperation(value = "根据一级查询二级所有属性")
    @GetMapping(value = "/property/second/{propertyId}")
    public ResponseEntity<Object> findSons(@PathVariable String propertyId) {
        User user = getAccessUser();
        if (user == null) {
            throw new StatusException(Constants.SYS_CODE_500, "请先登录！");
        }

        List<Property> properties = propertyService.findSonProperties(propertyId);
        return new ResponseEntity<>(properties, HttpStatus.OK);
    }

    @ApiOperation(value = "根据课程属性id所有一级属性")
    @GetMapping(value = "/property/first/{coursePropertyId}")
    public ResponseEntity<Object> findParents(@PathVariable String coursePropertyId) {
        User user = getAccessUser();
        if (user == null) {
            throw new StatusException(Constants.SYS_CODE_500, "请先登录！");
        }

        List<Property> properties = propertyService.findParentProperties(coursePropertyId, user.getRootOrgId());
        return new ResponseEntity<>(properties, HttpStatus.OK);
    }

    @ApiOperation(value = "上移")
    @PutMapping(value = "/property/moveUp")
    public ResponseEntity<Object> moveUp(@RequestBody PropertyDto property) {
        User user = getAccessUser();
        if (user == null) {
            throw new StatusException(Constants.SYS_CODE_500, "请先登录！");
        }

        property.setOrgId(user.getRootOrgId());
        propertyService.moveUp(property);

        return new ResponseEntity<>(HttpStatus.OK);
    }

    @ApiOperation(value = "下移")
    @PutMapping(value = "/property/moveDown")
    public ResponseEntity<Object> moveDown(@RequestBody PropertyDto property) {
        User user = getAccessUser();
        if (user == null) {
            throw new StatusException(Constants.SYS_CODE_500, "请先登录！");
        }

        property.setOrgId(user.getRootOrgId());
        propertyService.moveDown(property);

        return new ResponseEntity<>(HttpStatus.OK);
    }

}