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.PrjSubtopicplan;
import com.liang.service.PrjSubtopicplanService;
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;

/**
 * (PrjSubtopicplan)表控制层
 *
 * @author makejava
 * @since 2023-04-05 09:15:18
 */
@Controller
@RequestMapping("prjSubtopicplan")
public class PrjSubtopicplanController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private PrjSubtopicplanService prjSubtopicplanService;

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

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

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

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

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

    /**
     * 子课题计划
     *
     * @param zkt
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/list/{zkt}", method = RequestMethod.POST)
    public BaseResult list(@PathVariable Integer zkt) {
        List<PrjSubtopicplan> list = prjSubtopicplanService.getSubtopicPlanList(zkt);

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

    /**
     * 批量保存子课题计划
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/batchInsert/{zkt}", method = RequestMethod.POST)
    @ResponseBody
    public BaseResult batchInsert(@RequestBody String data, @PathVariable Integer zkt) {
        //删除子课题计划
        prjSubtopicplanService.deleteByZkt(zkt);

        List<PrjSubtopicplan> planList = JsonTool.parseArray(data, PrjSubtopicplan.class);
        if (planList.size() > 0) {
            int num = prjSubtopicplanService.insertBatch(planList);
            if (num == planList.size()) {
                return BaseResult.success("子课题计划保存成功！");
            } else {
                return BaseResult.failure("子课题计划保存失败！");
            }
        } else {
            return BaseResult.success("没有要保存的信息");
        }
    }

    @RequestMapping(value = "/forUpdate", method = RequestMethod.GET)
    public String forUpdate() {
        return "prjsubtopicplan/update";
    }

    @RequestMapping(value = "/forView", method = RequestMethod.GET)
    public String forView() {
        return "prjsubtopicplan/view";
    }
}

