package com.ruoyi.tuya.controller;

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

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.tuya.domain.TuyaSitesInfo;
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.tuya.domain.TuyaChannelInfo;
import com.ruoyi.tuya.service.ITuyaChannelInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 模块信息Controller
 *
 * @author zf
 * @date 2022-07-06
 */
@RestController
@RequestMapping("/tuya/channelinfo")
public class TuyaChannelInfoController extends BaseController
{
    @Autowired
    private ITuyaChannelInfoService tuyaChannelInfoService;

    /**
     * 获取模块下拉列表
     */
    @GetMapping("/selectlist")
    public AjaxResult selectlist(TuyaChannelInfo tuyaChannelInfo)
    {
        return AjaxResult.success(tuyaChannelInfoService.selectTuyaChannelInfoList(tuyaChannelInfo));
    }
    /**
     * 查询模块信息列表
     */
    @PreAuthorize("@ss.hasPermi('tuya:channelinfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(TuyaChannelInfo tuyaChannelInfo)
    {
        startPage();
        List<TuyaChannelInfo> list = tuyaChannelInfoService.selectTuyaChannelInfoList(tuyaChannelInfo);
        return getDataTable(list);
    }

    /**
     * 导出模块信息列表
     */
    @PreAuthorize("@ss.hasPermi('tuya:channelinfo:export')")
    @Log(title = "模块信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TuyaChannelInfo tuyaChannelInfo)
    {
        List<TuyaChannelInfo> list = tuyaChannelInfoService.selectTuyaChannelInfoList(tuyaChannelInfo);
        ExcelUtil<TuyaChannelInfo> util = new ExcelUtil<TuyaChannelInfo>(TuyaChannelInfo.class);
        util.exportExcel(response, list, "模块信息数据");
    }

    /**
     * 获取模块信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('tuya:channelinfo:query')")
    @GetMapping(value = "/{channelId}")
    public AjaxResult getInfo(@PathVariable("channelId") Long channelId)
    {
        return AjaxResult.success(tuyaChannelInfoService.selectTuyaChannelInfoByChannelId(channelId));
    }

    /**
     * 新增模块信息
     */
    @PreAuthorize("@ss.hasPermi('tuya:channelinfo:add')")
    @Log(title = "模块信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TuyaChannelInfo tuyaChannelInfo)
    {
        if (UserConstants.NOT_UNIQUE.equals(tuyaChannelInfoService.checkChannelNameUnique(tuyaChannelInfo)))
        {
            return AjaxResult.error("新增模块'" + tuyaChannelInfo.getChannelName() + "'失败，模块名称已存在");
        }
        tuyaChannelInfo.setCreateBy(getUsername());
        return toAjax(tuyaChannelInfoService.insertTuyaChannelInfo(tuyaChannelInfo));
    }

    /**
     * 修改模块信息
     */
    @PreAuthorize("@ss.hasPermi('tuya:channelinfo:edit')")
    @Log(title = "模块信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TuyaChannelInfo tuyaChannelInfo)
    {
        if (UserConstants.NOT_UNIQUE.equals(tuyaChannelInfoService.checkChannelNameUnique(tuyaChannelInfo)))
        {
            return AjaxResult.error("修改模块'" + tuyaChannelInfo.getChannelName() + "'失败，模块名称已存在");
        }
        tuyaChannelInfo.setUpdateBy(getUsername());
        return toAjax(tuyaChannelInfoService.updateTuyaChannelInfo(tuyaChannelInfo));
    }

    /**
     * 删除模块信息
     */
    @PreAuthorize("@ss.hasPermi('tuya:channelinfo:remove')")
    @Log(title = "模块信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{channelIds}")
    public AjaxResult remove(@PathVariable Long[] channelIds)
    {
        for(Long channelid: channelIds)
        {
            if (tuyaChannelInfoService.hasChildByChannelId(channelid))
            {
                return AjaxResult.error("存在下级栏目,不允许删除");
            }
        }
        return toAjax(tuyaChannelInfoService.deleteTuyaChannelInfoByChannelIds(channelIds));
    }
}
