package com.liang.controller;

import com.liang.entity.BaseSubjectname;
import com.liang.service.BaseSubjectnameService;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (BaseSubjectname)表控制层
 *
 * @author makejava
 * @since 2023-03-04 10:54:02
 */
@Controller
@RequestMapping("baseSubjectname")
public class BaseSubjectnameController {
    /**
     * 服务对象
     */
    @Resource
    private BaseSubjectnameService baseSubjectnameService;

    /**
     * 分页查询
     *
     * @param baseSubjectname 筛选条件
     * @param pageRequest     分页对象
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<Page<BaseSubjectname>> queryByPage(BaseSubjectname baseSubjectname, PageRequest pageRequest) {
        return ResponseEntity.ok(this.baseSubjectnameService.queryByPage(baseSubjectname, pageRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<BaseSubjectname> queryById(@PathVariable("id") String id) {
        return ResponseEntity.ok(this.baseSubjectnameService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param baseSubjectname 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<BaseSubjectname> add(BaseSubjectname baseSubjectname) {
        return ResponseEntity.ok(this.baseSubjectnameService.insert(baseSubjectname));
    }

    /**
     * 编辑数据
     *
     * @param baseSubjectname 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<BaseSubjectname> edit(BaseSubjectname baseSubjectname) {
        return ResponseEntity.ok(this.baseSubjectnameService.update(baseSubjectname));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(String id) {
        return ResponseEntity.ok(this.baseSubjectnameService.deleteById(id));
    }

    @ApiOperation(value = "页面初始化", notes = "页面初始化")
    @RequestMapping(value = "/init", method = RequestMethod.GET)
    public String init() {
        return "basesubjectname/list";
    }

    /**
     * 显示根节点
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Object list() {
//        List<BaseSubjectname> list = baseSubjectnameService.queryListById("0");
        List<BaseSubjectname> list = baseSubjectnameService.queryAll();

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("code", 0);
        resultMap.put("msg", "");
        resultMap.put("count", list.size());
        resultMap.put("data", list);

        return resultMap;
    }

    /**
     * 显示子节点
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/expand/{id}", method = RequestMethod.GET)
    public Object expand(@PathVariable String id) {
        List<BaseSubjectname> list = baseSubjectnameService.queryListById(id);

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("code", 0);
        resultMap.put("msg", "");
        resultMap.put("count", list.size());
        resultMap.put("data", list);

        return resultMap;
    }
}

