package com.qianhe.zqbf.controller;

import com.qianhe.common.core.controller.BaseController;
import com.qianhe.common.core.domain.AjaxResult;
import com.qianhe.common.core.page.TableDataInfo;
import com.qianhe.zqbf.domain.ZqbfXmzBfxm;
import com.qianhe.zqbf.domain.ZqbfXmzBfxmQuery;
import com.qianhe.zqbf.service.ZqbfXmzBfxmService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/zqbf/bfxm")
public class ZqbfXmzBfxmController extends BaseController {
    @Autowired
    private ZqbfXmzBfxmService zqbfXmzBfxmService;

    /**
     * 分页查询
     */
    @GetMapping("/list")
    public TableDataInfo list(ZqbfXmzBfxmQuery zqbfXmzBfxmQuery) {
        startPage();
        return getDataTable(zqbfXmzBfxmService.list(zqbfXmzBfxmQuery));
    }

    /**
     * 新增
     */
    @PostMapping
    public AjaxResult add(@RequestBody ZqbfXmzBfxm zqbfXmzBfxm) {
        try {
            return toAjax(zqbfXmzBfxmService.add(zqbfXmzBfxm, getUsername()));
        } catch (Exception e) {
            log.error(e.getMessage());
            return error(e.getMessage());
        }
    }

    /**
     * 修改
     */
    @PutMapping
    public AjaxResult edit(@RequestBody ZqbfXmzBfxm zqbfXmzBfxm) {
        try {
            return toAjax(zqbfXmzBfxmService.edit(zqbfXmzBfxm, getUsername()));
        } catch (Exception e) {
            log.error(e.getMessage());
            return error(e.getMessage());
        }
    }

    /**
     * 删除
     */
    @DeleteMapping("/{ids}")
    public AjaxResult delete(@PathVariable Long[] ids) {
        try {
            return toAjax(zqbfXmzBfxmService.delete(ids));
        } catch (Exception e) {
            log.error(e.getMessage());
            return error(e.getMessage());
        }
    }

    /**
     * 帮扶项目树
     */
    @GetMapping("/tree")
    public AjaxResult tree() {
        try {
            return success(zqbfXmzBfxmService.tree());
        } catch (Exception e) {
            log.error(e.getMessage());
            return error(e.getMessage());
        }
    }
}
