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.StaActualexpenses;
import com.liang.service.StaActualexpensesService;
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.ui.Model;
import org.springframework.web.bind.annotation.*;

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

/**
 * (StaActualexpenses)表控制层
 *
 * @author makejava
 * @since 2023-05-30 11:38:53
 */
@Controller
@RequestMapping("staActualexpenses")
public class StaActualexpensesController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private StaActualexpensesService staActualexpensesService;

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

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

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

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

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

    /**
     * 初始化差旅费列表
     *
     * @param zb
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/list/{zb}", method = RequestMethod.POST)
    public BaseResult list(@PathVariable Integer zb) {
        List<StaActualexpenses> list = staActualexpensesService.getClfList(zb);

        PageInfo<StaActualexpenses> pageinfo = new PageInfo<>(list);
        //取出查询结果
        List<StaActualexpenses> 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(Model model) {
        return "statravelexpense/add";
    }

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

        List<StaActualexpenses> expenseList = JsonTool.parseArray(data, StaActualexpenses.class);
        for (int i = 0; i < expenseList.size(); i++) {
            expenseList.get(i).setZb(zb);
        }
        if (expenseList.size() > 0) {
            int num = staActualexpensesService.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 "statravelexpense/update";
    }

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

