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 com.sooka.system.domain.SysUser;
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)
    {
        SysUser sysUser = ShiroUtils.getSysUser();
        String deptId = sysUser.getDeptId().toString();
        // @Excel(name = "接口来源", readConverterExp = "share_type_1=市共享,share_type_2=市归集,share_type_3=省上报,share_type_4=省共享")
        // 政数局通过1，提供部门通过2，政数局待审批3，提供部门待审批4，政数局驳回5，提供部门驳回6
        // share_type_4 = 省共享 新建0，政数局待审批3，政数局通过1
        // share_type_1 = 市共享 新建0，政数局待审批3，提供部门待审批4，提供部门通过2
        if(deptId.equals("103")){
            //只查政数局待审批的数据
            DataRecord.setStatus("3");
        }else {
            //只查当前用户部门待审批的数据
            DataRecord.setProvideDeptId(sysUser.getDeptId().toString());
            DataRecord.setStatus("4");
        }
//        SysUser sysUser = ShiroUtils.getSysUser();
//        //只查当前用户部门的数据
//        DataRecord.setProvideDeptId(sysUser.getDeptId().toString());
//        //只查政数局待审批的数据
//        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";
    }
}
