package com.zd.smartlock.controller;

import com.zd.common.core.domain.per.PerFun;
import com.zd.common.core.domain.per.PerPrefix;
import com.zd.common.core.utils.poi.ExcelUtil;
import com.zd.common.core.web.controller.BaseController;
import com.zd.common.core.web.domain.AjaxResult;
import com.zd.common.core.web.domain.ResultData;
import com.zd.common.core.web.page.TableDataInfo;
import com.zd.common.log.annotation.Log;
import com.zd.common.log.enums.BusinessType;
import com.zd.common.security.annotation.PreAuthorize;
import com.zd.common.security.service.TokenService;
import com.zd.smartlock.domain.SlOpendoorApply;
import com.zd.smartlock.domain.vo.SlOpendoorApplyVo;
import com.zd.smartlock.service.ISlOpendoorApplyService;
import com.zd.system.api.domain.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * 申请开门Controller
 *
 * @author xxf
 * @date 2022-09-03
 */
@RestController
@Api(tags = "【申请开门】")
@RequestMapping("/openDoorApply")
public class SlOpendoorApplyController extends BaseController
{
    @Autowired
    private ISlOpendoorApplyService slOpendoorApplyService;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询申请开门列表
     */
    @PreAuthorize(hasPermi = PerPrefix.SMARTLOCK_OPENDOORAPPLY+ PerFun.LIST)
    @GetMapping("/list")
    @ApiOperation(value = "查询申请开门列表")
    public TableDataInfo<SlOpendoorApplyVo> list(SlOpendoorApplyVo slOpendoorApply)
    {
        //获取登录用户
        SysUser sysUser = tokenService.getLoginUser().getSysUser();
        startPage();
        List<SlOpendoorApplyVo> list = slOpendoorApplyService.selectSlOpendoorApplyList(slOpendoorApply);
        for (SlOpendoorApplyVo vo:list) {
                if(vo.getLoseTime().getTime()<System.currentTimeMillis()){
                    vo.setApplyStatus(2L);
                }
        }
        return getDataTable(list);
    }

    /**
     * 授权申请
     */
    //@PreAuthorize(hasPermi = PerPrefix.SMARTLOCK_OPENDOORAPPLY+ "applyList")
    @GetMapping("/applyList")
    @ApiOperation(value = "查询申请开门列表")
    public TableDataInfo<SlOpendoorApplyVo> applylist(SlOpendoorApplyVo slOpendoorApply)
    {
        //获取登录用户
        SysUser sysUser = tokenService.getLoginUser().getSysUser();
        slOpendoorApply.setUserId(sysUser.getUserId());
        startPage();
        List<SlOpendoorApplyVo> list = slOpendoorApplyService.selectSlOpendoorApplyList(slOpendoorApply);
        for (SlOpendoorApplyVo vo:list) {
            if(vo.getLoseTime().getTime()<System.currentTimeMillis()){
                vo.setApplyStatus(2L);
            }
        }
        return getDataTable(list);
    }

    /**
     * 查询申请开门列表(学生端)
     */
    @GetMapping("/listStudent")
    @ApiOperation(value = "查询申请开门列表(学生端)")
    public TableDataInfo<SlOpendoorApplyVo> listStudent(SlOpendoorApplyVo slOpendoorApply)
    {
        //获取登录用户
        SysUser sysUser = tokenService.getLoginUser().getSysUser();
        slOpendoorApply.setUserId(sysUser.getUserId());

        startPage();
        List<SlOpendoorApplyVo> list = slOpendoorApplyService.selectSlOpendoorApplyListStudent(slOpendoorApply);
        for (SlOpendoorApplyVo vo:list) {
            if(vo.getLoseTime().getTime()<System.currentTimeMillis()){
                vo.setApplyStatus(2L);
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出申请开门列表
     */
    @ApiOperation(value = "导出申请开门列表")
    @PreAuthorize(hasPermi = PerPrefix.SMARTLOCK_OPENDOORAPPLY+ PerFun.EXPORT)
    @Log(title = "申请开门", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SlOpendoorApplyVo slOpendoorApply) throws IOException
    {
        List<SlOpendoorApplyVo> list = slOpendoorApplyService.selectSlOpendoorApplyList(slOpendoorApply);
        ExcelUtil<SlOpendoorApplyVo> util = new ExcelUtil<SlOpendoorApplyVo>(SlOpendoorApplyVo.class);
        util.exportExcel(response, list, "申请开门数据");
    }

    /**
     * 获取申请开门详细信息
     */
    @ApiOperation(value = "获取申请开门详细信息")
    @PreAuthorize(hasPermi = PerPrefix.SMARTLOCK_OPENDOORAPPLY+ PerFun.QUERY)
    @GetMapping(value = "/{id}")
    public ResultData<SlOpendoorApply> getInfo(@PathVariable("id") Long id)
    {
        return ResultData.success(slOpendoorApplyService.selectSlOpendoorApplyById(id));
    }

    /**
     * 新增申请开门
     */
    @ApiOperation(value = "新增申请开门")
    @PreAuthorize(hasPermi = PerPrefix.SMARTLOCK_OPENDOORAPPLY+ PerFun.ADD)
    @Log(title = "申请开门", businessType = BusinessType.INSERT)
    @PostMapping
    public ResultData add(@RequestBody SlOpendoorApply slOpendoorApply)
    {
        /*user_id 申请人id
        user_id2 人员2 id
        subject_id 实验室id
        subject_name 实验室名称
        position
        */

        return ResultData.result(slOpendoorApplyService.insertSlOpendoorApply(slOpendoorApply));
    }

    /**
     * 新增申请开门
     */
    @ApiOperation(value = "新增申请开门")
    @Log(title = "申请开门", businessType = BusinessType.INSERT)
    @PostMapping("/addStudent")
    public ResultData addStudent(@RequestBody SlOpendoorApply slOpendoorApply)
    {
        return ResultData.result(slOpendoorApplyService.insertSlOpendoorApply(slOpendoorApply));
    }


    /**
     * 修改申请开门
     */
    @ApiOperation(value = "修改申请开门")
    @PreAuthorize(hasPermi = PerPrefix.SMARTLOCK_OPENDOORAPPLY+ PerFun.EDIT)
    @Log(title = "申请开门", businessType = BusinessType.UPDATE)
    @PutMapping
    public ResultData edit(@RequestBody SlOpendoorApply slOpendoorApply)
    {
        return  ResultData.result(slOpendoorApplyService.updateSlOpendoorApply(slOpendoorApply));
    }

    /**
     * 删除申请开门
     */
    @ApiOperation(value = "删除申请开门")
    @PreAuthorize(hasPermi = PerPrefix.SMARTLOCK_OPENDOORAPPLY+ PerFun.REMOVE)
    @Log(title = "申请开门", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public ResultData remove(@PathVariable Long[] ids)
    {
        return ResultData.result(slOpendoorApplyService.deleteSlOpendoorApplyByIds(ids));
    }

    /***
     * 重新授权
     * @param id 申请主键
     * @return
     */
    @ApiOperation(value = "重新授权")
    @GetMapping("/auth/{id}")
    public AjaxResult againAuth(@PathVariable Long id)
    {
        return AjaxResult.success(slOpendoorApplyService.againAuth(id));
    }
}
