package com.ruoyi.project.system.controller;

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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.framework.security.LoginUser;
import com.ruoyi.framework.security.service.TokenService;
import com.ruoyi.project.system.domain.SysUser;
import org.springframework.security.access.prepost.PreAuthorize;
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.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.domain.SysPlant;
import com.ruoyi.project.system.service.ISysPlantService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 装置管理Controller
 *
 * @author ruoyi
 * @date 2021-06-23
 */
@RestController
@RequestMapping("/system/plant")
public class SysPlantController extends BaseController
{
    @Autowired
    private ISysPlantService sysPlantService;

    /**
     * 查询装置管理列表
     */
    @GetMapping("/list")
    public AjaxResult list(SysPlant sysPlant)
    {
        List<SysPlant> list = sysPlantService.selectSysPlantList(sysPlant);
        return AjaxResult.success(list);
    }

    /**
     * 查询装置管理列表
     */
    @GetMapping("/myList")
    public AjaxResult myList(SysPlant sysPlant)
    {
        List<SysPlant> list = new ArrayList<>();
        // 获取当前的用户
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        if (StringUtils.isNotNull(loginUser))
        {
            SysUser currentUser = loginUser.getUser();
            // 如果是超级管理员，则不过滤数据
            if (StringUtils.isNotNull(currentUser) && !currentUser.isAdmin())
            {
                sysPlant.setPlantId(currentUser.getUserId());
                list = sysPlantService.selectSysMyPlantList(sysPlant);
            }else {
                list = sysPlantService.selectSysPlantList(sysPlant);
            }
        }

        return AjaxResult.success(list);
    }

    @RequestMapping(value = "unitList")
    public List<SysPlant> unitList(@RequestBody Map<String, Object> params) {
        List plantIds = (List) params.get("plantIds");
        List<SysPlant> list = sysPlantService.selectSysPlantByPlants(plantIds);
        return list;
    }
    /**
     * 导出装置管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:plant:export')")
    @Log(title = "装置管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SysPlant sysPlant)
    {
        List<SysPlant> list = sysPlantService.selectSysPlantList(sysPlant);
        ExcelUtil<SysPlant> util = new ExcelUtil<SysPlant>(SysPlant.class);
        return util.exportExcel(list, "plant");
    }

    /**
     * 获取装置管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:plant:query')")
    @GetMapping(value = "/{plantId}")
    public AjaxResult getInfo(@PathVariable("plantId") Long plantId)
    {
        return AjaxResult.success(sysPlantService.selectSysPlantById(plantId));
    }

    /**
     * 新增装置管理
     */
    @PreAuthorize("@ss.hasPermi('system:plant:add')")
    @Log(title = "装置管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysPlant sysPlant)
    {
        return toAjax(sysPlantService.insertSysPlant(sysPlant));
    }

    /**
     * 修改装置管理
     */
    @PreAuthorize("@ss.hasPermi('system:plant:edit')")
    @Log(title = "装置管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysPlant sysPlant)
    {
        return toAjax(sysPlantService.updateSysPlant(sysPlant));
    }

    /**
     * 删除装置管理
     */
    @PreAuthorize("@ss.hasPermi('system:plant:remove')")
    @Log(title = "装置管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{plantIds}")
    public AjaxResult remove(@PathVariable Long[] plantIds)
    {
        return toAjax(sysPlantService.deleteSysPlantByIds(plantIds));
    }
}
