package com.ruoyi.mall.controller;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.mall.domain.MallCompany;
import com.ruoyi.mall.domain.MallCompanyUser;
import com.ruoyi.mall.service.IMallCompanyService;
import com.ruoyi.mall.service.IMallCompanyUserService;
import com.ruoyi.system.domain.SysUserRole;
import com.ruoyi.system.mapper.SysUserRoleMapper;
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.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.ruoyi.mall.domain.MallDevice;
import com.ruoyi.mall.service.IMallDeviceService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 设备Controller
 *
 * @author LCL
 * @date 2024-03-17
 */
@RestController
@RequestMapping("/mall/mallDevice")
public class MallDeviceController extends BaseController
{
    @Resource
    private IMallDeviceService mallDeviceService;
    @Resource
    private IMallCompanyService mallCompanyService;
    @Resource
    private IMallCompanyUserService mallCompanyUserService;
    @Resource
    private SysUserRoleMapper sysUserRoleMapper;

    /**
     * 查询设备列表
     */
    @PreAuthorize("@ss.hasPermi('mall:mallDevice:list')")
    @GetMapping("/list")
    public TableDataInfo list(MallDevice mallDevice)
    {
        // 获取登陆用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUserRole sysUserRole = sysUserRoleMapper.selectSysUserRoleByUserId(loginUser.getUserId());

        if (sysUserRole != null) {
            // 公司角色
            if (sysUserRole.getRoleId() == 100) {
                MallCompany company = mallCompanyService.selectMallCompanyByCompanySysUserId(loginUser.getUserId());
                if (company != null) {
                    mallDevice.setCompanyId(company.getCompanyId());
                }
            }
            // 基站管理员角色
            if (sysUserRole.getRoleId() == 101) {
                MallCompanyUser companyUser = mallCompanyUserService.selectMallCompanyUserByCompanySysUserId(loginUser.getUserId());
                if (companyUser != null) {
                    mallDevice.setUserId(companyUser.getCompanyUserId());
                }
            }
        }

        startPage();
        List<MallDevice> list = mallDeviceService.selectMallDeviceList(mallDevice);
        return getDataTable(list);
    }

    /**
     * 导出设备列表
     */
    @PreAuthorize("@ss.hasPermi('mall:mallDevice:export')")
    @Log(title = "设备", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MallDevice mallDevice)
    {
        List<MallDevice> list = mallDeviceService.selectMallDeviceList(mallDevice);
        ExcelUtil<MallDevice> util = new ExcelUtil<MallDevice>(MallDevice.class);
        util.exportExcel(response, list, "设备数据");
    }

    /**
     * 获取设备详细信息
     */
    @PreAuthorize("@ss.hasPermi('mall:mallDevice:query')")
    @GetMapping(value = "/{deviceId}")
    public AjaxResult getInfo(@PathVariable("deviceId") Long deviceId)
    {
        return AjaxResult.success(mallDeviceService.selectMallDeviceByDeviceId(deviceId));
    }

    /**
     * 新增设备
     */
    @PreAuthorize("@ss.hasPermi('mall:mallDevice:add')")
    @Log(title = "设备", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MallDevice mallDevice)
    {
        return toAjax(mallDeviceService.insertMallDevice(mallDevice));
    }

    /**
     * 修改设备
     */
    @PreAuthorize("@ss.hasPermi('mall:mallDevice:edit')")
    @Log(title = "设备", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MallDevice mallDevice)
    {
        return toAjax(mallDeviceService.updateMallDevice(mallDevice));
    }

    /**
     * 删除设备
     */
    @PreAuthorize("@ss.hasPermi('mall:mallDevice:remove')")
    @Log(title = "设备", businessType = BusinessType.DELETE)
	@DeleteMapping("/{deviceIds}")
    public AjaxResult remove(@PathVariable Long[] deviceIds)
    {
        return toAjax(mallDeviceService.deleteMallDeviceByDeviceIds(deviceIds));
    }
}
