package com.sooka.applyData.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sooka.applyData.domain.DataDetailed;
import com.sooka.applyData.domain.DataRecord;
import com.sooka.applyData.service.IDataRecordService;
import com.sooka.common.annotation.Log;
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.enums.BusinessType;
import com.sooka.common.utils.poi.ExcelUtil;
import com.sooka.framework.util.ShiroUtils;
import com.sooka.system.domain.SysMenu;
import com.sooka.system.domain.SysUser;
import com.sooka.system.mapper.SysMenuMapper;
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 javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 申请记录Controller
 *
 * @author yhliang
 * @date 2021-07-06
 */
@Controller
@RequestMapping("/applyData/record")
public class DataRecordController extends BaseController
{
    private String prefix = "applyData/record";

    @Autowired
    private IDataRecordService DataRecordService;

    @Resource
    private SysMenuMapper sysMenuMapper;

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

    /**
     * 查询申请记录列表
     */
    @RequiresPermissions("apply:record:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(DataRecord DataRecord)
    {
        DataRecord.setApplyUserId(ShiroUtils.getUserId());
        startPage();
        List<DataRecord> list = DataRecordService.selectDataRecordList(DataRecord);
        return getDataTable(list);
    }

    /**
     * 新增申请记录
     */
    @RequiresPermissions("apply:record:add")
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        SysUser sysUser = ShiroUtils.getSysUser();
        mmap.put("applyDeptName", sysUser.getDept().getDeptName());
        mmap.put("applyUserName", sysUser.getUserName());
        return prefix + "/add";
    }

    /**
     * 新增保存申请记录
     */
    @Log(title = "申请记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(DataRecord DataRecord)
    {
        SysMenu sysDept = sysMenuMapper.selectMenuById(Long.valueOf(DataRecord.getProvideDeptId()));
        DataRecord.setProvideDeptId(sysDept.getDeptId().toString());
        DataRecord.setDataDetailedList(structureSubData(DataRecord.getSubData()));
        return toAjax(DataRecordService.insertDataRecord(DataRecord));
    }

    private List<DataDetailed> structureSubData(String subData){
        JSONArray jsonArray = JSONArray.parseArray(subData);
        List<DataDetailed> list = new ArrayList<>();
        for(int i=0;i< jsonArray.size();i++){
            JSONArray jsonArray1 = JSONArray.parseArray(jsonArray.get(i).toString());
            DataDetailed DataDetailed = new DataDetailed();
            for(int j=0;j<jsonArray1.size();j++){
                JSONObject jsonObject = JSONObject.parseObject(jsonArray1.get(j).toString());
                String name = jsonObject.getString("name");
                if(name.equals("intId")){
                    DataDetailed.setIntId(jsonObject.getString("value"));
                }else if(name.equals("purpose")){
                    DataDetailed.setPurpose(jsonObject.getString("value"));
                }else if(name.equals("file")){
                    DataDetailed.setFile(jsonObject.getString("value"));
                }
            }
            list.add(DataDetailed);
        }
        return list;
    }

    /**
     * 附件单独用iframe
     */
    @GetMapping(value={"/uploadFrame","/uploadFrame/{subId}"})
    public String uploadFrame(@PathVariable(value = "subId", required = false) String subId, ModelMap mmap)
    {
        if(subId != null){
            DataDetailed DataDetailed = DataRecordService.selectDataRecordDetailedBySubId(subId);
            mmap.put("file",DataDetailed.getFile());
        }
        return prefix + "/uploadFrame";
    }

    /**
     * 附件单独用iframe
     */
    @GetMapping(value={"/uploadFrameDisabled/{subId}"})
    public String uploadFrameDisabled(@PathVariable(value = "subId", required = false) String subId, ModelMap mmap)
    {
        if(subId != null){
            DataDetailed DataDetailed = DataRecordService.selectDataRecordDetailedBySubId(subId);
            mmap.put("file",DataDetailed.getFile());
        }
        return prefix + "/uploadFrameDisabled";
    }

    /**
     * 修改申请记录
     */
    @RequiresPermissions("apply:record:edit")
    @GetMapping("/edit/{id}")
    public String edit(@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 + "/edit";
    }

    /**
     * 修改保存申请记录
     */
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(DataRecord DataRecord)
    {
        DataRecord.setDataDetailedList(structureSubData(DataRecord.getSubData()));
        return toAjax(DataRecordService.editDataRecord(DataRecord));
    }

    /**
     * 保存驳回信息字段
     */
    @PostMapping("/reject")
    @ResponseBody
    public AjaxResult reject(DataRecord DataRecord)
    {
        return toAjax(DataRecordService.rejectDataRecord(DataRecord));
    }

    /**
     * 导出申请记录列表
     */
    @RequiresPermissions("apply:record:export")
    @Log(title = "申请记录", businessType = BusinessType.EXPORT)
    @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, "record");
    }

    /**
     * 删除申请记录
     */
    @RequiresPermissions("apply:record:remove")
    @Log(title = "申请记录", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(DataRecordService.deleteDataRecordByIds(ids));
    }

    /**
     * 修改状态
     */
    @PostMapping("/updateStatus")
    @ResponseBody
    public AjaxResult updateStatus(String id,String status)
    {
        return toAjax(DataRecordService.updateStatus(id,status));
    }

    /**
     * 查看详情
     */
    @GetMapping("/detail/{id}")
    public String detail(@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 + "/detail";
    }

    /**
     * 驳回原因弹框
     */
    @GetMapping("/rejectDialog/{id}")
    public String rejectDialog(@PathVariable("id") String id, ModelMap mmap)
    {
        DataRecord DataRecord = DataRecordService.selectDataRecordById(id);
        mmap.put("DataRecord", DataRecord);
        return prefix + "/rejectDialog";
    }






/***********************以下为一期原有方法******************************/

    /**
     * 查询申请记录列表
     */
    @RequiresPermissions("apply:record:list")
    @PostMapping("/DataDetailedList")
    @ResponseBody
    public TableDataInfo DataDetailedList(DataDetailed DataDetailed)
    {
        startPage();
        List<DataDetailed> list = DataRecordService.selectDataDetailedList(DataDetailed);
        return getDataTable(list);
    }

    /**
     * 状态修改
     */
    @Log(title = "接口管理", businessType = BusinessType.UPDATE)
    @RequiresPermissions("apply:record:edit")
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(DataDetailed DataDetailed)
    {
        return toAjax(DataRecordService.changeStatus(DataDetailed));
    }


}
