package com.ruoyi.tuya.controller;

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

import com.ruoyi.common.constant.UserConstants;
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.TuyaCategoryInfo;
import com.ruoyi.tuya.service.ITuyaCategoryInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 栏目信息Controller
 *
 * @author zf
 * @date 2022-07-07
 */
@RestController
@RequestMapping("/tuya/categoryinfo")
public class TuyaCategoryInfoController extends BaseController
{
    @Autowired
    private ITuyaCategoryInfoService tuyaCategoryInfoService;

    /**
     * 获取目录下拉树列表
     */
    @GetMapping("/selectlist")
    public AjaxResult selectlist(TuyaCategoryInfo tuyaCategoryInfo)
    {
        List<TuyaCategoryInfo> categorys = tuyaCategoryInfoService.selectTuyaCategoryInfoList(tuyaCategoryInfo);
        return AjaxResult.success(tuyaCategoryInfoService.buildCategoryTreeSelect(categorys));
    }

    /**
     * 查询栏目信息列表
     */
    @PreAuthorize("@ss.hasPermi('tuya:categoryinfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(TuyaCategoryInfo tuyaCategoryInfo)
    {
        startPage();
        List<TuyaCategoryInfo> list = tuyaCategoryInfoService.selectTuyaCategoryInfoList(tuyaCategoryInfo);
        return getDataTable(list);
    }

    /**
     * 导出栏目信息列表
     */
    @PreAuthorize("@ss.hasPermi('tuya:categoryinfo:export')")
    @Log(title = "栏目信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TuyaCategoryInfo tuyaCategoryInfo)
    {
        List<TuyaCategoryInfo> list = tuyaCategoryInfoService.selectTuyaCategoryInfoList(tuyaCategoryInfo);
        ExcelUtil<TuyaCategoryInfo> util = new ExcelUtil<TuyaCategoryInfo>(TuyaCategoryInfo.class);
        util.exportExcel(response, list, "栏目信息数据");
    }

    /**
     * 获取栏目信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('tuya:categoryinfo:query')")
    @GetMapping(value = "/{categoryId}")
    public AjaxResult getInfo(@PathVariable("categoryId") Long categoryId)
    {
        return AjaxResult.success(tuyaCategoryInfoService.selectTuyaCategoryInfoByCategoryId(categoryId));
    }

    /**
     * 新增栏目信息
     */
    @PreAuthorize("@ss.hasPermi('tuya:categoryinfo:add')")
    @Log(title = "栏目信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TuyaCategoryInfo tuyaCategoryInfo)
    {
        if (tuyaCategoryInfo.getParentId() == null){
            tuyaCategoryInfo.setParentId(0L);
        }
        if (UserConstants.NOT_UNIQUE.equals(tuyaCategoryInfoService.checkCategoryNameUnique(tuyaCategoryInfo)))
        {
            return AjaxResult.error("新增栏目'" + tuyaCategoryInfo.getCategoryName() + "'失败，栏目名称已存在");
        }
        tuyaCategoryInfo.setCreateBy(getUsername());
        return toAjax(tuyaCategoryInfoService.insertTuyaCategoryInfo(tuyaCategoryInfo));
    }

    /**
     * 修改栏目信息
     */
    @PreAuthorize("@ss.hasPermi('tuya:categoryinfo:edit')")
    @Log(title = "栏目信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TuyaCategoryInfo tuyaCategoryInfo)
    {
        if (tuyaCategoryInfo.getParentId() == null){
            tuyaCategoryInfo.setParentId(0L);
        }
        if (UserConstants.NOT_UNIQUE.equals(tuyaCategoryInfoService.checkCategoryNameUnique(tuyaCategoryInfo)))
        {
            return AjaxResult.error("修改栏目'" + tuyaCategoryInfo.getCategoryName() + "'失败，栏目名称已存在");
        }
        tuyaCategoryInfo.setUpdateBy(getUsername());
        return toAjax(tuyaCategoryInfoService.updateTuyaCategoryInfo(tuyaCategoryInfo));
    }

    /**
     * 删除栏目信息
     */
    @PreAuthorize("@ss.hasPermi('tuya:categoryinfo:remove')")
    @Log(title = "栏目信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{categoryIds}")
    public AjaxResult remove(@PathVariable Long[] categoryIds)
    {
        for(Long categoryid: categoryIds)
        {
            if (tuyaCategoryInfoService.hasChildByCategoryId(categoryid))
            {
                return AjaxResult.error("存在下级栏目,不允许删除");
            }
        }
        return toAjax(tuyaCategoryInfoService.deleteTuyaCategoryInfoByCategoryIds(categoryIds));
    }
}
