package com.sooka.apply.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.sooka.apply.domain.IntRecord;
import com.sooka.apply.service.IIntRecordService;
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("/apply/examine")
public class ExamineController extends BaseController {

    private String prefix = "apply/examine";

    @Autowired
    private IIntRecordService intRecordService;

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

    /**
     * 查询我的审批
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(IntRecord intRecord)
    {
        //只查提供部门待审批的数据
        intRecord.setStatus("4");
        intRecord.setProvideDeptId(ShiroUtils.getSysUser().getDeptId().toString());
        startPage();
        List<IntRecord> list = intRecordService.selectIntRecordList(intRecord);
        return getDataTable(list);
    }

    /**
     * 导出我的审批
     */
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(IntRecord intRecord)
    {
        List<IntRecord> list = intRecordService.selectIntRecordList(intRecord);
        ExcelUtil<IntRecord> util = new ExcelUtil<IntRecord>(IntRecord.class);
        return util.exportExcel(list, "examine");
    }

    /**
     * 审批页面
     */
    @GetMapping("/examine/{id}")
    public String examine(@PathVariable("id") String id, ModelMap mmap)
    {
        IntRecord intRecord = intRecordService.selectIntRecordById(id);
        JSONArray jsonArray = JSONArray.parseArray(JSON.toJSONString(intRecord.getIntDetailedList()));
        mmap.put("intRecord", intRecord);
        mmap.put("intDetailedList", jsonArray);
        return prefix + "/examineDetail";
    }
}
