package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.Apply;
import com.ruoyi.system.domain.ApplyChild;
import com.ruoyi.system.domain.Dangan;
import com.ruoyi.system.service.IApplyChildService;
import com.ruoyi.system.service.IApplyService;
import com.ruoyi.system.service.IDanganService;
import com.ruoyi.system.service.ISysUserService;
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 ruoyi
 * @date 2022-10-26
 */
@Controller
@RequestMapping("/system/apply")
public class ApplyController extends BaseController
{
    private String prefix = "system/apply";

    @Autowired
    private IApplyService applyService;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private IDanganService danganService;
    @Autowired
    private IApplyChildService childService;

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

    /**
     * 查询开通申请列表
     */
    @RequiresPermissions("system:apply:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Apply apply)
    {
        startPage();
        List<Apply> list = applyService.selectApplyList(apply);
        return getDataTable(list);
    }

    /**
     * 导出开通申请列表
     */
    @RequiresPermissions("system:apply:export")
    @Log(title = "开通申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Apply apply)
    {
        List<Apply> list = applyService.selectApplyList(apply);
        ExcelUtil<Apply> util = new ExcelUtil<Apply>(Apply.class);
        return util.exportExcel(list, "apply");
    }

    /**
     * 新增开通申请
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        //获取用户列表
       List<SysUser> userList= userService.selectUserList(new SysUser());
       mmap.put("userList",userList);
       //获取档案列表
        List<Dangan> danganList = danganService.selectDanganList(new Dangan());
        mmap.put("danganList",danganList);
        return prefix + "/add";
    }

    /**
     * 新增保存开通申请
     */
    @RequiresPermissions("system:apply:add")
    @Log(title = "开通申请", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Apply apply)
    {
        apply.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(applyService.insertApply(apply));
    }

    /**
     * 修改开通申请
     */
    @GetMapping("/edit/{applyId}")
    public String edit(@PathVariable("applyId") Long applyId, ModelMap mmap)
    {
        Apply apply = applyService.selectApplyById(applyId);
        mmap.put("apply", apply);
        return prefix + "/edit";
    }

    /**
     * 修改保存开通申请
     */
    @RequiresPermissions("system:apply:edit")
    @Log(title = "开通申请", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Apply apply)
    {
        return toAjax(applyService.updateApply(apply));
    }

    /**
     * 删除开通申请
     */
    @RequiresPermissions("system:apply:remove")
    @Log(title = "开通申请", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(applyService.deleteApplyByIds(ids));
    }

    /**
     * 查看
     */
    @GetMapping("/look/{applyId}")
    public String look(@PathVariable("applyId") Long applyId, ModelMap mmap)
    {
        Apply apply = applyService.selectApplyById(applyId);
        mmap.put("apply", apply);
        //获取子系统列表
        ApplyChild child = new ApplyChild();
        child.setApplyId(applyId);
        List<ApplyChild> childList = childService.selectApplyChildList(child);
        mmap.put("childList", childList);

        return prefix + "/look";
    }

    /**
     * 处理
     */
    @Log(title = "处理", businessType = BusinessType.UPDATE)
    @PostMapping("/dealStatus")
    @ResponseBody
    public AjaxResult dealStatus(Apply apply)
    {
        return toAjax(applyService.updateApply(apply));
    }


}
