package com.liang.controller;

import com.liang.common.JsonTool;
import com.liang.common.base.BaseResult;
import com.liang.entity.StaBudget;
import com.liang.service.StaBudgetService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

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

/**
 * 经费预算(StaBudget)表控制层
 *
 * @author makejava
 * @since 2023-04-09 22:07:01
 */
@RestController
@RequestMapping("staBudget")
public class StaBudgetController {
    /**
     * 服务对象
     */
    @Resource
    private StaBudgetService staBudgetService;

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

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

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

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

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

    /**
     * 获取经费预算列表
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/list/{xm}", method = RequestMethod.POST)
    public Object list(@PathVariable Integer xm) {
        List<StaBudget> list = staBudgetService.getBudgetList(xm);
        String strData = JsonTool.toJsonString(list);
        return strData;
    }

    /**
     * 保存数据（经费预算）
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/doUpdateJfys", method = RequestMethod.POST)
    public BaseResult doUpdateJfys(@RequestBody String data) {
        List<StaBudget> staBudgets = JsonTool.parseArray(data, StaBudget.class);
        int num = staBudgetService.updateBatch(staBudgets);
        if (num > 0) {
            return BaseResult.success("经费预算保存成功!");
        } else {
            return BaseResult.failure("经费预算保存失败!");
        }
    }
}

