package com.liang.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liang.common.annotation.LogAround;
import com.liang.common.base.BaseController;
import com.liang.common.base.BaseResult;
import com.liang.entity.ViewApproveRecord;
import com.liang.service.ViewApproveRecordService;
import io.swagger.annotations.ApiOperation;
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;

/**
 * (ViewApproveRecord)表控制层
 *
 * @author makejava
 * @since 2023-05-03 12:25:53
 */
@Controller
@RequestMapping("viewApproveRecord")
public class ViewApproveRecordController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private ViewApproveRecordService viewApproveRecordService;

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

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

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

    /**
     * 数据分页查询
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @LogAround("数据分页查询")
    @ApiOperation(value = "数据分页查询", notes = "数据分页查询")
    @ResponseBody
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public BaseResult list(@RequestParam("page") Integer pageNum,
                           @RequestParam("limit") Integer pageSize,
                           @RequestParam(required = false, defaultValue = "", value = "XM") String xm,
                           @RequestParam(required = false, defaultValue = "", value = "ZT") String zt,
                           @RequestParam(required = false, defaultValue = "", value = "TJRDW") String tjrdw,
                           @RequestParam(required = false, defaultValue = "", value = "TJRName") String tjrname,
                           @RequestParam(required = false, defaultValue = "", value = "SHRName") String shrname,
                           @RequestParam(required = false, defaultValue = "", value = "SHRDW") String shrdw) {
        PageHelper.startPage(pageNum, pageSize);

        Map<String, String> paraMap = new HashMap<>();
        paraMap.put("XM", xm);
        paraMap.put("ZT", zt);
        paraMap.put("TJRDW", tjrdw);
        paraMap.put("TJRName", tjrname);
        paraMap.put("SHRName", shrname);
        paraMap.put("SHRDW", shrdw);

        List<ViewApproveRecord> viewApproveRecordList = viewApproveRecordService.getApproveRecordList(paraMap);

        PageInfo<ViewApproveRecord> pageinfo = new PageInfo<>(viewApproveRecordList);
        // 取出查询结果
        List<ViewApproveRecord> 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);
    }
}

