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.StaProjectExpenses;
import com.liang.service.StaProjectExpensesService;
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;

/**
 * 项目费用支出(StaProjectExpensesController)表控制层
 */
@Controller
@RequestMapping("staProjectExpenses")
public class StaProjectExpensesController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private StaProjectExpensesService staProjectExpensesService;

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

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

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

    /**
     * 初始化项目费用支出列表
     *
     * @param zb
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/list/{zb}", method = RequestMethod.POST)
    public BaseResult list(@PathVariable Integer zb) {
        List<StaProjectExpenses> list = staProjectExpensesService.getXmfyList(zb);

        PageInfo<StaProjectExpenses> pageinfo = new PageInfo<>(list);
        //取出查询结果
        List<StaProjectExpenses> 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 "staprojectexpenses/add";
    }

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

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

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