package com.sooka.applyData.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.sooka.applyData.domain.DataRecord;
import com.sooka.applyData.service.IDataRecordService;
import com.sooka.common.core.controller.BaseController;
import com.sooka.common.core.domain.AjaxResult;
import com.sooka.common.core.page.TableDataInfo;
import com.sooka.common.utils.poi.ExcelUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 政数局 -- 我的审批Controller
 *
 * @author limeng
 * @date 2022-10-19
 */
@Controller
@RequestMapping("/applyData/approval")
public class DataApprovalController extends BaseController {

    private String prefix = "applyData/approval";

    @Autowired
    private IDataRecordService DataRecordService;

    @RequiresPermissions("apply:approval:view")
    @GetMapping()
    public String approval()
    {
        return prefix + "/approval";
    }

    /**
     * 查询我的审批
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(DataRecord DataRecord)
    {
        //只查政数局待审批的数据
        DataRecord.setStatus("3");
        startPage();
        List<DataRecord> list = DataRecordService.selectDataRecordList(DataRecord);
        return getDataTable(list);
    }

    /**
     * 导出我的审批
     */
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(DataRecord DataRecord)
    {
        List<DataRecord> list = DataRecordService.selectDataRecordList(DataRecord);
        ExcelUtil<DataRecord> util = new ExcelUtil<DataRecord>(DataRecord.class);
        return util.exportExcel(list, "approval");
    }

    /**
     * 审批页面
     */
    @GetMapping("/approval/{id}")
    public String approval(@PathVariable("id") String id, ModelMap mmap)
    {
        DataRecord DataRecord = DataRecordService.selectDataRecordById(id);
        JSONArray jsonArray = JSONArray.parseArray(JSON.toJSONString(DataRecord.getDataDetailedList()));
        mmap.put("DataRecord", DataRecord);
        mmap.put("DataDetailedList", jsonArray);
        return prefix + "/approvalDetail";
    }
}
