package com.sooka.system.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

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.TempPerUser;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
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.domain.TTempPermission;
import com.sooka.system.service.ITTempPermissionService;


/**
 * tempPerController
 *
 * @author lei
 * @date 2022-08-09
 */
@Controller
@RequestMapping("/system/tempPer")
public class TTempPermissionController extends BaseController
{
    private String prefix = "system/tempPer";

    @Autowired
    private ITTempPermissionService tTempPermissionService;

    @Value("${dept.zsjDeptId}")
    private Long zsjDeptId;

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

    /**
     * 查询tempPer列表
     */
    @RequiresPermissions("system:tempPer:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TTempPermission tTempPermission)
    {
        startPage();
        List<TTempPermission> list = tTempPermissionService.selectTTempPermissionList(tTempPermission);
        return getDataTable(list);
    }

    @PostMapping("/expirationList")
    @ResponseBody
    public List<TTempPermission> expirationList(TTempPermission tTempPermission)
    {
        tTempPermission.setEndTime(new Date());
        /** 只有政数局人员可接收到临期提醒 **/
        if(ShiroUtils.getSysUser().getDeptId().equals(zsjDeptId)){
            return tTempPermissionService.selectTTempPermissionList(tTempPermission);
        }
        return new ArrayList<>();
    }

    @RequiresPermissions("system:tempPer:add")
    @PostMapping("/listUser")
    @ResponseBody
    public List<TempPerUser> listUser()
    {
        List<TempPerUser> list = tTempPermissionService.listUser();
        return list;
    }

    /**
     * 导出tempPer列表
     */
    @RequiresPermissions("system:tempPer:export")
    @Log(title = "tempPer", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TTempPermission tTempPermission)
    {
        List<TTempPermission> list = tTempPermissionService.selectTTempPermissionList(tTempPermission);
        ExcelUtil<TTempPermission> util = new ExcelUtil<TTempPermission>(TTempPermission.class);
        return util.exportExcel(list, "tempPer");
    }

    /**
     * 新增tempPer
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存tempPer
     */
    @RequiresPermissions("system:tempPer:add")
    @Log(title = "tempPer", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TTempPermission tTempPermission)
    {
        return toAjax(tTempPermissionService.insertTTempPermission(tTempPermission));
    }

    /**
     * 修改tempPer
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        TTempPermission tTempPermission = tTempPermissionService.selectTTempPermissionById(id);
        mmap.put("tTempPermission", tTempPermission);
        return prefix + "/edit";
    }

    /**
     * 修改保存tempPer
     */
    @RequiresPermissions("system:tempPer:edit")
    @Log(title = "tempPer", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TTempPermission tTempPermission)
    {
        return toAjax(tTempPermissionService.updateTTempPermission(tTempPermission));
    }

    /**
     * 删除tempPer
     */
    @RequiresPermissions("system:tempPer:remove")
    @Log(title = "tempPer", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tTempPermissionService.deleteTTempPermissionByIds(ids));
    }
}
