package com.cpms.project.web.controller.system;

import com.alibaba.fastjson2.JSON;
import com.cpms.common.annotation.Log;
import com.cpms.common.core.controller.BaseController;
import com.cpms.common.core.domain.AjaxResult;
import com.cpms.common.core.domain.entity.SysUser;
import com.cpms.common.core.domain.model.LoginUser;
import com.cpms.common.enums.BusinessType;
import com.cpms.common.utils.ServletUtils;
import com.cpms.common.utils.StringUtils;
import com.cpms.common.utils.poi.ExcelUtil;
import com.cpms.common.utils.spring.SpringUtils;
import com.cpms.framework.web.service.TokenService;
import com.cpms.system.domain.SysPlant;
import com.cpms.system.service.ISysPlantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

/**
 * 装置管理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())
            {
                //直全部权限
                if (currentUser.getDataScope().equals("1")) {
                    list = sysPlantService.selectSysPlantList(sysPlant);
                }else if (currentUser.getDataScope().equals("2")){ //直定义权限
                    sysPlant.setPlantId(currentUser.getUserId());
                    list = sysPlantService.selectSysMyPlantList(sysPlant);
                }else if (currentUser.getDataScope().equals("3")){//本部门
                    sysPlant.setPlantId(currentUser.getUserId());
                    list = sysPlantService.selectSysMyPlantList3(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");
        logger.info(JSON.toJSONString(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));
    }
}
