package com.sooka.system.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.sooka.apply.domain.IntRecord;
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.system.domain.*;
import com.sooka.system.mapper.SysMenuMapper;
import com.sooka.system.service.ISysMenuService;
import com.sooka.system.service.ITUParamsbusinessService;
import com.sooka.system.service.ITUParamspublicService;
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.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.sooka.system.service.ITUInterfaceinfoService;

import javax.annotation.Resource;


/**
 * 【接口应用审批管理、数据应用审批管理】Controller
 *
 * @author lei
 * @date 2021-07-01
 */
@Controller
@RequestMapping("/system/interfaceinfo")
public class TUInterfaceinfoController extends BaseController {
    private String prefix = "system/interfaceinfo";

    @Autowired
    private ITUInterfaceinfoService tUInterfaceinfoService;
    @Resource
    private ITUParamsbusinessService ituParamsbusinessService;
    @Resource
    private ITUParamspublicService ituParamspublicService;
    @Resource
    private SysMenuMapper menuMapper;

    @RequiresPermissions("system:interfaceinfo:view")
    @GetMapping("/{shareType}")
    public String interfaceinfo(@PathVariable("shareType") String shareType, ModelMap mmap) {
        mmap.put("shareType",shareType);
        return prefix + "/interfaceinfo";
    }

    /**
     * 查询【接口应用审批管理】列表
     */
    @RequiresPermissions("system:interfaceinfo:list")
    @RequestMapping(value = {"/list/{shareType}","/list"})
    @ResponseBody
    public TableDataInfo list(@PathVariable(value = "shareType", required = false) String shareType, TUInterfaceinfo tUInterfaceinfo) {
        startPage();
        tUInterfaceinfo.setShareType(shareType);
        List<TUInterfaceinfo> list = tUInterfaceinfoService.selectTUInterfaceinfoList(tUInterfaceinfo);
        return getDataTable(list);
    }

    /**
     * 【接口应用审批管理】根据部门ID获取接口应用
     */
    @GetMapping("/selectInterfaceByDeptId/{deptId}/{shareType1}/{shareType3}")
    public String selectInterfaceByDeptId(@PathVariable("deptId") String deptId,
                                          @PathVariable("shareType1") String shareType1,
                                          @PathVariable("shareType3") String shareType3, ModelMap mmap)
    {
        TUInterfaceinfo tuInterfaceinfo = new TUInterfaceinfo();
        tuInterfaceinfo.setDeptId(deptId);
        //只查询可用的接口
        tuInterfaceinfo.setStatus("0");
        //只查询 市共享和省上报的接口
        tuInterfaceinfo.setShareType1(shareType1);
        tuInterfaceinfo.setShareType3(shareType3);
        startPage();
        List<TUInterfaceinfo> list = tUInterfaceinfoService.selectTUInterfaceinfoList(tuInterfaceinfo);
        mmap.put("list", list.stream().collect(Collectors.toList()));
        return prefix + "/selectInterface";
    }

    /**
     * 【数据应用审批管理】根据部门ID获取数据应用
     */
    @GetMapping("/selectDataByDeptId/{deptId}/{shareType}")
    public String selectDataByDeptId(@PathVariable("deptId") Long deptId, @PathVariable("shareType") String shareType, ModelMap mmap)
    {
        startPage();
        List<SysMenu> menuList = menuMapper.selectMenuListByParentId(deptId);
        List<TUInterfaceinfo> list = new ArrayList();
        for (int i = 0; i < menuList.size(); i++) {
            TUInterfaceinfo tuInterfaceinfo = new TUInterfaceinfo();
            tuInterfaceinfo.setId(menuList.get(i).getMenuId().toString());
            tuInterfaceinfo.setInterfaceName(menuList.get(i).getMenuName());
            tuInterfaceinfo.setInterfaceAddress(menuList.get(i).getUrl());
            tuInterfaceinfo.setShareType(shareType);
            list.add(tuInterfaceinfo);
        }
        mmap.put("list", list.stream().collect(Collectors.toList()));
        return prefix + "/selectData";
    }

    /**
     * 【接口应用审批管理】根据部门ID获取接口
     */
    @RequestMapping("/selectInterfaceByIds")
    @ResponseBody
    public List<TUInterfaceinfo> selectInterfaceByDeptId(String[] ids)
    {
        List<TUInterfaceinfo> list = tUInterfaceinfoService.selectTUInterfaceinfoList(ids);
        return list;
    }

    /**
     * 【数据应用审批管理】根据部门ID获取数据应用
     */
    @RequestMapping("/selectInterfaceDataByIds")
    @ResponseBody
    public List<TUInterfaceinfo> selectInterfaceDataByIds(String[] ids)
    {
        List<TUInterfaceinfo> list = tUInterfaceinfoService.selectTUInterfaceDataList(ids);
        return list;
    }


    /**
     * 导出【接口应用审批管理】列表
     */
    @RequiresPermissions("system:interfaceinfo:export")
    @Log(title = "省共享接口", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TUInterfaceinfo tUInterfaceinfo) {
        List<TUInterfaceinfo> list = tUInterfaceinfoService.selectTUInterfaceinfoList(tUInterfaceinfo);
        ExcelUtil<TUInterfaceinfo> util = new ExcelUtil<TUInterfaceinfo>(TUInterfaceinfo.class);
        return util.exportExcel(list, "interfaceinfo");
    }

    /**
     * 新增【接口应用审批管理】
     */
    @GetMapping("/add/{shareType}")
    public String add(@PathVariable("shareType") String shareType, ModelMap mmap) {
        mmap.put("shareType",shareType);
        return prefix + "/add";
    }

    /**
     * 新增保存【接口应用审批管理】
     */
    @RequiresPermissions("system:interfaceinfo:add")
    @Log(title = "【接口应用审批管理】", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TUInterfaceinfo tUInterfaceinfo) {
        return toAjax(tUInterfaceinfoService.insertTUInterfaceinfo(tUInterfaceinfo));
    }

    /**
     * 修改【接口应用审批管理】
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap) {
        TUInterfaceinfo tUInterfaceinfo = tUInterfaceinfoService.selectTUInterfaceinfoById(id);
        String shareType = tUInterfaceinfo.getShareType();
        boolean showSecretKey = shareType.equals("share_type_2") || shareType.equals("share_type_3");
        mmap.put("tUInterfaceinfo", tUInterfaceinfo);
        mmap.put("hideSecretKey", !showSecretKey);
        return prefix + "/edit";
    }

    /**
     * 修改保存【接口应用审批管理】
     */
    @RequiresPermissions("system:interfaceinfo:edit")
    @Log(title = "【接口应用审批管理】", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TUInterfaceinfo tUInterfaceinfo) {
        return toAjax(tUInterfaceinfoService.updateTUInterfaceinfo(tUInterfaceinfo));
    }

    /**
     * 删除【接口应用审批管理】
     */
    @RequiresPermissions("system:interfaceinfo:remove")
    @Log(title = "【接口应用审批管理】", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(tUInterfaceinfoService.deleteTUInterfaceinfoByIds(ids));
    }

    //【接口应用审批管理】
    @GetMapping("/settingParams/{id}")
    public String settingParams(@PathVariable("id") String id, ModelMap mmap) {
        mmap.put("interfaceinfo_id", id);
        return "system/paramsbusiness/paramsbusiness";
    }
    //【接口应用审批管理】
    @GetMapping("/execInterface/{id}")
    public String execInterface(@PathVariable("id") String id, ModelMap mmap) {
        mmap.put("interfaceinfo_id", id);
        //按接口id查询接口基本信息
        TUInterfaceinfo tuInterfaceinfo = tUInterfaceinfoService.selectTUInterfaceinfoById(id);
        //按接口id查询公共参数
        List params_public = ituParamspublicService.selectTUParamspublicList(new TUParamspublic());
        //按接口id查询业务参数
        TUParamsbusiness tuParamsbusiness = new TUParamsbusiness();
        tuParamsbusiness.setInterfaceinfoId(id);
        List params_business = ituParamsbusinessService.selectTUParamsbusinessList(tuParamsbusiness);

        mmap.put("tuInterfaceinfo",tuInterfaceinfo);
        mmap.put("params_public_list",params_public);
        mmap.put("params_business_list",params_business);
        return "system/interfaceinfo/execInterface";
    }

    /**
     * 【接口应用审批管理】状态修改
     */
    @Log(title = "接口管理", businessType = BusinessType.UPDATE)
    @RequiresPermissions("system:interfaceinfo:edit")
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(TUInterfaceinfo interfaceinfo)
    {
        return toAjax(tUInterfaceinfoService.updateTUInterfaceinfo(interfaceinfo));
    }

    /**
     * 【接口应用审批管理】查询接口日志台账
     */
    @PostMapping("/interfaceLogList")
    @ResponseBody
    public TableDataInfo interfaceLogList(TUInterfaceinfo interfaceinfo)
    {
        startPage();
        List<TUInterfaceinfo> list = tUInterfaceinfoService.interfaceLogList(interfaceinfo);
        return getDataTable(list);
    }

}
