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 com.sooka.framework.util.ShiroUtils;
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/examine")
public class DataExamineController extends BaseController {

    private String prefix = "applyData/examine";

    @Autowired
    private IDataRecordService DataRecordService;

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

    /**
     * 查询我的审核
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(DataRecord DataRecord)
    {
        //只查提供部门待审核的数据
        DataRecord.setStatus("4");
        DataRecord.setProvideDeptId(ShiroUtils.getSysUser().getDeptId().toString());
        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, "examine");
    }

    /**
     * 审核页面
     */
    @GetMapping("/examine/{id}")
    public String examine(@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 + "/examineDetail";
    }
}
