package com.hzu.labbooking.controller;

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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hzu.labbooking.constant.LabStatusConstansts;
import com.hzu.labbooking.domain.vo.LabUseVO;
import com.hzu.labbooking.domain.vo.LabVO;
import com.hzu.labbooking.domain.vo.MyLabVO;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.hzu.labbooking.domain.Lab;
import com.hzu.labbooking.service.ILabService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 实验室信息Controller
 *
 * @author yezihao
 * @date 2021-06-14
 */
@RestController
@RequestMapping("/labbooking/lab")
public class LabController extends BaseController {

    @Autowired
    private ILabService labService;
    @Autowired
    private ISysDeptService deptService;
    @Autowired
    private TokenService tokenService;

    /**
     * 查询实验室信息列表
     */
    @GetMapping("/bookLabList")
    public TableDataInfo bookLabList(Lab lab) {
        startPage();
        List<LabVO> list = labService.selectBookLabList(lab);
        return getDataTable(list);
    }

    /**
     * 管理员查询实验室信息列表
     */
    @GetMapping("/labList")
    public TableDataInfo labList(Lab lab) {
        startPage();
        List<MyLabVO> list = labService.selectLabList(lab);
        return getDataTable(list);
    }

    /**
     * 查询实验室信息列表
     */
    @GetMapping("/labUse")
    public AjaxResult labUse(Long year, Long term) {
        List<LabUseVO> voList = labService.getLabUseRate(year, term);
        return AjaxResult.success(voList);
    }

    /**
     * 导出实验室信息列表
     */
    @Log(title = "实验室信息", businessType = BusinessType.EXPORT)
    @GetMapping("/bookLabExport")
    public AjaxResult bookLabExport(Lab lab) {
        List<LabVO> list = labService.selectBookLabList(lab);
        ExcelUtil<LabVO> util = new ExcelUtil<LabVO>(LabVO.class);
        return util.exportExcel(list, "实验室信息数据");
    }

    /**
     * 管理员导出实验室信息列表
     */
    @Log(title = "实验室信息", businessType = BusinessType.EXPORT)
    @GetMapping("/labExport")
    public AjaxResult labExport(Lab lab) {
        List<MyLabVO> list = labService.selectLabList(lab);
        ExcelUtil<MyLabVO> util = new ExcelUtil<>(MyLabVO.class);
        return util.exportExcel(list, "实验室信息数据");
    }

    /**
     * 获取实验室信息详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(labService.selectLabById(id));
    }

    /**
     * 新增实验室信息
     */
    @Log(title = "实验室信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Lab lab) {
        int status = labService.insertLab(lab);
        if (status == -1) {
            return AjaxResult.error("实验室名称或者实验室位置已经存在");
        } else {
            return toAjax(status);
        }
    }

    /**
     * 修改实验室信息
     */
    @Log(title = "实验室信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Lab lab) {
        int status = labService.updateLab(lab);
        if (status == -1) {
            return AjaxResult.error("实验室位置已存在");
        } else if (status == -2) {
            return AjaxResult.error("实验室名称已经存在");
        } else if (status == -3) {
            return AjaxResult.error("实验室名称或者实验室位置已经存在");
        } else if (status == -4) {
            return AjaxResult.error("未知异常");
        } else {
            return toAjax(status);
        }
    }

    /**
     * 删除实验室信息
     */
    @Log(title = "实验室信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        Lab lab = new Lab();
        lab.setStatus((long) LabStatusConstansts.DELETED);
        QueryWrapper<Lab> wrapper = new QueryWrapper<>();
        wrapper.in("id", (Object[]) ids);
        return toAjax(labService.update(lab, wrapper));
    }

    /**
     * 修改实验室状态
     */
    @Log(title = "预约状态修改", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody Lab lab) {
        return toAjax(labService.updateLabStatus(lab));
    }

    /**
     * 获取当前用户学院信息
     */
    @GetMapping("/getDept")
    public R getDept() {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Long deptId = loginUser.getUser().getDeptId();
        List<Long> list = new ArrayList<>();
        list.add(deptId);
        //SysDept sysDept = deptService.selectDeptById(deptId);
        //Long parentId = sysDept.getParentId();
        //if (parentId != 0) list.add(parentId);
        List<SysDept> sysDepts = deptService.selectDeptNameByIds(list);
        return R.ok(sysDepts);
    }

}
