package com.ruoyi.mall.controller;

import java.util.HashMap;
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.domain.MallDevice;
import com.ruoyi.mall.service.IMallCompanyService;
import com.ruoyi.mall.service.IMallCompanyUserService;
import com.ruoyi.mall.service.IMallDeviceService;
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.MallBaseStation;
import com.ruoyi.mall.service.IMallBaseStationService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 基站Controller
 *
 * @author LCL
 * @date 2024-03-17
 */
@RestController
@RequestMapping("/mall/mallBaseStation")
public class MallBaseStationController extends BaseController
{
    @Resource
    private IMallBaseStationService mallBaseStationService;
    @Resource
    private IMallDeviceService mallDeviceService;
    @Resource
    private IMallCompanyService mallCompanyService;
    @Resource
    private IMallCompanyUserService mallCompanyUserService;
    @Resource
    private SysUserRoleMapper sysUserRoleMapper;

    /**
     * 查询基站列表
     */
    @PreAuthorize("@ss.hasPermi('mall:mallBaseStation:list')")
    @GetMapping("/list")
    public TableDataInfo list(MallBaseStation mallBaseStation)
    {
        // 获取登陆用户
        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) {
                    mallBaseStation.setCompanyId(company.getCompanyId());
                }
            }
            // 基站管理员角色
            if (sysUserRole.getRoleId() == 101) {
                MallCompanyUser companyUser = mallCompanyUserService.selectMallCompanyUserByCompanySysUserId(loginUser.getUserId());
                if (companyUser != null) {
                    mallBaseStation.setUserId(companyUser.getCompanyUserId());
                }
            }
        }

        startPage();
        List<MallBaseStation> list = mallBaseStationService.selectMallBaseStationList(mallBaseStation);
        return getDataTable(list);
    }

    /**
     * 导出基站列表
     */
    @PreAuthorize("@ss.hasPermi('mall:mallBaseStation:export')")
    @Log(title = "基站", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MallBaseStation mallBaseStation)
    {
        List<MallBaseStation> list = mallBaseStationService.selectMallBaseStationList(mallBaseStation);
        ExcelUtil<MallBaseStation> util = new ExcelUtil<MallBaseStation>(MallBaseStation.class);
        util.exportExcel(response, list, "基站数据");
    }

    /**
     * 获取基站详细信息
     */
    @PreAuthorize("@ss.hasPermi('mall:mallBaseStation:query')")
    @GetMapping(value = "/{baseStationId}")
    public AjaxResult getInfo(@PathVariable("baseStationId") Long baseStationId)
    {
        return AjaxResult.success(mallBaseStationService.selectMallBaseStationByBaseStationId(baseStationId));
    }

    /**
     * 新增基站
     */
    @PreAuthorize("@ss.hasPermi('mall:mallBaseStation:add')")
    @Log(title = "基站", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MallBaseStation mallBaseStation)
    {
        if (mallBaseStation.getCompanyId() != null) {
            mallBaseStation.setBindCompany("1");
        }
        if (mallBaseStation.getUserId() != null) {
            mallBaseStation.setBindUser("1");
        }
        mallBaseStationService.insertMallBaseStation(mallBaseStation);

        MallDevice mallDevice = new MallDevice();
        mallDevice.setBaseStationId(mallBaseStation.getBaseStationId());
        mallDevice.setDeviceStatus("0");
        mallDevice.setDeviceOpen("0");
        mallDevice.setDeviceDefence("0");

        // 设备类型：0-无，1-报警主机，2-摄像头，3-弹箱，4-语音喇叭，5-电源
        for (int i = 1; i < 6; i++) {
            mallDevice.setDeviceType(String.valueOf(i));
            if (i == 3) {
                mallDevice.setBoxUse("0");
                mallDevice.setBoxVoice("0");
                mallDevice.setBoxTrigger("0");
            }
            if (i == 5) {
                mallDevice.setBatteryWork("100");
                mallDevice.setBatteryUseType("0");
            }
            mallDevice.setCreateAt(mallBaseStation.getCreateAt());
            mallDeviceService.insertMallDevice(mallDevice);
            mallDevice.setBoxUse(null);
            mallDevice.setBoxVoice(null);
            mallDevice.setBoxTrigger(null);
            mallDevice.setBatteryWork(null);
            mallDevice.setBatteryUseType(null);
        }

        return AjaxResult.success();
    }

    /**
     * 修改基站
     */
    @PreAuthorize("@ss.hasPermi('mall:mallBaseStation:edit')")
    @Log(title = "基站", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MallBaseStation mallBaseStation)
    {
        if (mallBaseStation.getCompanyId() != null) {
            mallBaseStation.setBindCompany("1");
        }
        if (mallBaseStation.getUserId() != null) {
            mallBaseStation.setBindUser("1");
        }
        return toAjax(mallBaseStationService.updateMallBaseStation(mallBaseStation));
    }

    /**
     * 删除基站
     */
    @PreAuthorize("@ss.hasPermi('mall:mallBaseStation:remove')")
    @Log(title = "基站", businessType = BusinessType.DELETE)
	@DeleteMapping("/{baseStationIds}")
    public AjaxResult remove(@PathVariable Long[] baseStationIds)
    {
        return toAjax(mallBaseStationService.deleteMallBaseStationByBaseStationIds(baseStationIds));
    }

    /**
     * 获取基站列表
     *
     * @param
     * @return 获取基站列表集合
     */
    @GetMapping("/getBaseStation")
    public AjaxResult getBaseStation() {
        HashMap<String,Object> map = new HashMap<>();
        MallBaseStation mallBaseStation = new MallBaseStation();
        mallBaseStation.setDelFlag("0");

        // 获取登陆用户
        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) {
                    mallBaseStation.setCompanyId(company.getCompanyId());
                }
            }
            // 基站管理员角色
            if (sysUserRole.getRoleId() == 101) {
                MallCompanyUser companyUser = mallCompanyUserService.selectMallCompanyUserByCompanySysUserId(loginUser.getUserId());
                if (companyUser != null) {
                    mallBaseStation.setUserId(companyUser.getCompanyUserId());
                }
            }
        }
        List<MallBaseStation> list = mallBaseStationService.selectMallBaseStationList(mallBaseStation);
        map.put("data",list);
        return AjaxResult.success(map);
    }
}
