package com.liang.controller;

import com.liang.common.JsonTool;
import com.liang.common.base.BaseResult;
import com.liang.entity.StaSchedule;
import com.liang.service.StaScheduleService;
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;

/**
 * 进度安排(StaSchedule)表控制层
 *
 * @author makejava
 * @since 2023-04-09 22:07:01
 */
@RestController
@RequestMapping("staSchedule")
public class StaScheduleController {
    /**
     * 服务对象
     */
    @Resource
    private StaScheduleService staScheduleService;

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

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

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

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

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

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

        List<StaSchedule> planList = JsonTool.parseArray(data, StaSchedule.class);
        int num = staScheduleService.insertBatch(planList);
        if (num > 0) {
            return BaseResult.success("进度安排保存成功！");
        } else {
            return BaseResult.failure("进度安排保存失败！");
        }
    }
}

