package com.yijia.station.controller;

import java.util.Arrays;
import java.util.List;

import com.yijia.common.core.domain.entity.SysDept;
import com.yijia.common.core.domain.entity.SysUser;
import com.yijia.common.core.domain.model.LoginUser;
import com.yijia.common.utils.SecurityUtils;
import com.yijia.system.service.ISysDeptService;
import com.yijia.system.service.ISysUserService;
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.yijia.common.annotation.Log;
import com.yijia.common.core.controller.BaseController;
import com.yijia.common.core.domain.AjaxResult;
import com.yijia.common.enums.BusinessType;
import com.yijia.station.domain.StationDeviceManage;
import com.yijia.station.service.IStationDeviceManageService;
import com.yijia.common.utils.poi.ExcelUtil;
import com.yijia.common.core.page.TableDataInfo;

/**
 * 油站设备管理Controller
 *
 * @author yijia
 * @date 2021-01-13
 */
@RestController
@RequestMapping("/station/manage")
public class StationDeviceManageController extends BaseController
{
    @Autowired
    private IStationDeviceManageService stationDeviceManageService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ISysDeptService deptService;
    /**
     * 查询油站设备管理列表
     */
    @GetMapping("/list")
    public TableDataInfo list(StationDeviceManage stationDeviceManage)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        SysDept dept =new SysDept();
        dept.setDeptId(currentUser.getUser().getDeptId());
        List<String> list = deptService.selectDeptId(dept);
        if(list!=null && list.size()>0){
            stationDeviceManage.setStationIdList(list);
            stationDeviceManage.setStationId(null);
        }
        startPage();
        List<StationDeviceManage> manageList = stationDeviceManageService.selectStationDeviceManageList(stationDeviceManage);
        return getDataTable(manageList);
    }

    /**
     * 导出油站设备管理列表
     */
    @Log(title = "油站设备管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(StationDeviceManage stationDeviceManage)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        SysDept dept =new SysDept();
        dept.setDeptId(currentUser.getUser().getDeptId());
        List<String> list = deptService.selectDeptId(dept);
        if(list!=null && list.size()>0){
            stationDeviceManage.setStationIdList(list);
            stationDeviceManage.setStationId(null);
        }
        List<StationDeviceManage> manageList = stationDeviceManageService.selectStationDeviceManageList(stationDeviceManage);
        ExcelUtil<StationDeviceManage> util = new ExcelUtil<StationDeviceManage>(StationDeviceManage.class);
        return util.exportExcel(manageList, "manage");
    }

    /**
     * 获取油站设备管理详细信息
     */
    @GetMapping(value = "/{deviceId}")
    public AjaxResult getInfo(@PathVariable("deviceId") Long deviceId)
    {
        return AjaxResult.success(stationDeviceManageService.selectStationDeviceManageById(deviceId));
    }

    /**
     * 新增油站设备管理
     */
    @Log(title = "油站设备管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody StationDeviceManage stationDeviceManage)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        stationDeviceManage.setStationId(currentUser.getUser().getDeptId());
        stationDeviceManage.setStationName(currentUser.getUser().getDept().getDeptName());
        return toAjax(stationDeviceManageService.insertStationDeviceManage(stationDeviceManage));
    }

    /**
     * 修改油站设备管理
     */
    @Log(title = "油站设备管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody StationDeviceManage stationDeviceManage)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        stationDeviceManage.setStationId(currentUser.getUser().getDeptId());
        stationDeviceManage.setStationName(currentUser.getUser().getDept().getDeptName());
        return toAjax(stationDeviceManageService.updateStationDeviceManage(stationDeviceManage));
    }

    /**
     * 删除油站设备管理
     */
    @Log(title = "油站设备管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{deviceIds}")
    public AjaxResult remove(@PathVariable Long[] deviceIds)
    {
        return toAjax(stationDeviceManageService.deleteStationDeviceManageByIds(deviceIds));
    }

    /**
     * 状态修改
     */
    @Log(title = "油站设备管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeManageStatus")
    public AjaxResult changeManageStatus(@RequestBody StationDeviceManage stationDeviceManage)
    {
        return toAjax(stationDeviceManageService.updateStationDeviceManage(stationDeviceManage));
    }
}
