package com.liang.controller;

import com.github.pagehelper.PageHelper;
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.*;
import com.liang.service.PrjGwtaskService;
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;

/**
 * 付款计划(PrjGwtask)表控制层
 *
 * @author makejava
 * @since 2023-07-09 13:13:10
 */
@Controller
@RequestMapping("prjGwtask")
public class PrjGwtaskController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private PrjGwtaskService prjGwtaskService;

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

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

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

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

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

    @RequestMapping(value = "/init", method = RequestMethod.GET)
    public String init(Model model) {
        SysUserInfo userInfo = getSysUserInfo();
        model.addAttribute("userName", userInfo.getName());
        return "prjproject/list_gwtask";
    }


    @ResponseBody
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public BaseResult list(@RequestParam("page") Integer pageNum,
                           @RequestParam("limit") Integer pageSize,
                           @RequestParam(required = false, defaultValue = "", value = "sortName") String sortName,
                           @RequestParam(required = false, defaultValue = "", value = "sortOrder") String sortOrder) {
        SysUserInfo userInfo = getSysUserInfo();
        PageHelper.startPage(pageNum, pageSize);
        Map<String, String> paraMap = new HashMap<>();
        paraMap.put("sortName", sortName);
        paraMap.put("sortOrder", sortOrder);
        paraMap.put("scr",userInfo.getName());
        List<PrjGwtask> list = prjGwtaskService.getGwTaslList(paraMap);

        PageInfo<PrjGwtask> pageinfo = new PageInfo<>(list);
        //取出查询结果
        List<PrjGwtask> 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 = "/batchDelete", method = RequestMethod.POST)
    @ResponseBody
    public BaseResult batchDelete(@RequestBody String data) {
        List<Integer> idList = JsonTool.parseArray(data, Integer.class);
        int num = 0;
        for (Integer id : idList) {
            boolean flag = prjGwtaskService.deleteById(id);
            if (flag) {
                num++;
            }
        }
        if (num > 0) {
            return BaseResult.success("批量删除成功!");
        } else {
            return BaseResult.failure("批量删除失败!");
        }
    }
}

