package com.liang.controller;

import com.github.pagehelper.PageInfo;
import com.liang.common.JsonTool;
import com.liang.common.base.BaseController;
import com.liang.common.base.BaseResult;
import com.liang.entity.StaDeviceexpenses;
import com.liang.service.StaDeviceexpensesService;
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;

/**
 * 仪器设备使用费(StaDeviceexpenses)表控制层
 *
 * @author makejava
 * @since 2023-07-18 21:51:46
 */
@Controller
@RequestMapping("staDeviceexpenses")
public class StaDeviceexpensesController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private StaDeviceexpensesService staDeviceexpensesService;

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

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

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

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

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

    /**
     * 初始化列表
     *
     * @param zb
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/list/{zb}", method = RequestMethod.POST)
    public BaseResult list(@PathVariable Integer zb) {
        List<StaDeviceexpenses> list = staDeviceexpensesService.getSbsyfList(zb);

        PageInfo<StaDeviceexpenses> pageinfo = new PageInfo<>(list);
        //取出查询结果
        List<StaDeviceexpenses> rows = pageinfo.getList();
        int total = (int) pageinfo.getTotal();
        Map<String, Object> result = new HashMap<>();
        result.put(RESULT_ROWS, rows);
        result.put(RESULT_TOTAL, total);

        return BaseResult.success(result);
    }

    /**
     * 新建数据
     *
     * @return
     */
    @RequestMapping(value = "/forAdd", method = RequestMethod.GET)
    public String forAdd() {
        return "deviceexpense/add";
    }

    /**
     * 批量保存
     *
     * @param data
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/batchInsert/{zb}", method = RequestMethod.POST)
    public BaseResult batchInsert(@RequestBody String data, @PathVariable Integer zb) {
        //删除
        staDeviceexpensesService.deleteByZb(zb);

        List<StaDeviceexpenses> expenseList = JsonTool.parseArray(data, StaDeviceexpenses.class);
        for (int i = 0; i < expenseList.size(); i++) {
            expenseList.get(i).setZb(zb);
        }
        if (expenseList.size() > 0) {
            int num = staDeviceexpensesService.insertBatch(expenseList);
            if (num == expenseList.size()) {
                return BaseResult.success("仪器设备使用费保存成功！");
            } else {
                return BaseResult.failure("仪器设备使用费保存失败！");
            }
        } else {
            return BaseResult.success("没有要保存的信息");
        }
    }

    /**
     * 编辑数据
     *
     * @return
     */
    @RequestMapping(value = "/forUpdate", method = RequestMethod.GET)
    public String forUpdate() {
        return "deviceexpense/update";
    }

    /**
     * 查看数据
     *
     * @return
     */
    @RequestMapping(value = "/forView", method = RequestMethod.GET)
    public String forView() {
        return "deviceexpense/view";
    }
}

