package com.ruoyi.tuya.controller;

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

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
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.TuyaSitesInfo;
import com.ruoyi.tuya.service.ITuyaSitesInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.web.multipart.MultipartFile;


/**
 * 平台信息Controller
 *
 * @author ruoyi
 * @date 2022-07-01
 */
@RestController
@RequestMapping("/tuya/sitesinfo")
public class TuyaSitesInfoController extends BaseController
{
    @Autowired
    private ITuyaSitesInfoService tuyaSitesInfoService;

    /**
     * 获取平台下拉树列表
     */
    @GetMapping("/treeselect")
    public AjaxResult treeselect(TuyaSitesInfo tuyaSitesInfo)
    {
        List<TuyaSitesInfo> sites = tuyaSitesInfoService.selectTuyaSitesInfoList(tuyaSitesInfo);
        return AjaxResult.success(tuyaSitesInfoService.buildDeptTreeSelect(sites));
    }

    /**
     * 获取平台下拉列表
     */
    @GetMapping("/selectlist")
    public AjaxResult selectlist(TuyaSitesInfo tuyaSitesInfo)
    {
        return AjaxResult.success(tuyaSitesInfoService.sitesInfoSelect(tuyaSitesInfo));
    }

    /**
     * 查询平台信息列表
     */
    @PreAuthorize("@ss.hasPermi('tuya:sitesinfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(TuyaSitesInfo tuyaSitesInfo)
    {
        startPage();
        List<TuyaSitesInfo> list = tuyaSitesInfoService.selectTuyaSitesInfoList(tuyaSitesInfo);
        return getDataTable(list);
    }
    /**
     * 导出平台信息列表
     */
    @PreAuthorize("@ss.hasPermi('tuya:sitesinfo:export')")
    @Log(title = "平台信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TuyaSitesInfo tuyaSitesInfo)
    {
        List<TuyaSitesInfo> list = tuyaSitesInfoService.selectTuyaSitesInfoList(tuyaSitesInfo);
        ExcelUtil<TuyaSitesInfo> util = new ExcelUtil<TuyaSitesInfo>(TuyaSitesInfo.class);
        util.exportExcel(response, list, "平台信息数据");
    }

    /**
     * 获取平台信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('tuya:sitesinfo:query')")
    @GetMapping(value = "/{siteId}")
    public AjaxResult getInfo(@PathVariable("siteId") Long siteId)
    {
        return AjaxResult.success(tuyaSitesInfoService.selectTuyaSitesInfoBySiteId(siteId));
    }

    /**
     * 新增平台信息
     */
    @PreAuthorize("@ss.hasPermi('tuya:sitesinfo:add')")
    @Log(title = "平台信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TuyaSitesInfo tuyaSitesInfo)
    {
        if (UserConstants.NOT_UNIQUE.equals(tuyaSitesInfoService.checkSiteNameUnique(tuyaSitesInfo)))
        {
            return AjaxResult.error("新增平台'" + tuyaSitesInfo.getSiteNameCn() + "'失败，平台中文名称已存在");
        }
        else if (UserConstants.NOT_UNIQUE.equals(tuyaSitesInfoService.checkSiteNameEnUnique(tuyaSitesInfo)))
        {
            return AjaxResult.error("新增平台'" + tuyaSitesInfo.getSiteNameEn() + "'失败，平台英文名称已存在");
        }
        tuyaSitesInfo.setCreateBy(getUsername());
        return toAjax(tuyaSitesInfoService.insertTuyaSitesInfo(tuyaSitesInfo));
    }

    /**
     * 修改平台信息
     */
    @PreAuthorize("@ss.hasPermi('tuya:sitesinfo:edit')")
    @Log(title = "平台信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TuyaSitesInfo tuyaSitesInfo)
    {
        if (UserConstants.NOT_UNIQUE.equals(tuyaSitesInfoService.checkSiteNameUnique(tuyaSitesInfo)))
        {
            return AjaxResult.error("新增平台'" + tuyaSitesInfo.getSiteNameCn() + "'失败，平台中文名称已存在");
        }
        else if (UserConstants.NOT_UNIQUE.equals(tuyaSitesInfoService.checkSiteNameEnUnique(tuyaSitesInfo)))
        {
            return AjaxResult.error("新增平台'" + tuyaSitesInfo.getSiteNameEn() + "'失败，平台英文名称已存在");
        }
        tuyaSitesInfo.setUpdateBy(getUsername());
        return toAjax(tuyaSitesInfoService.updateTuyaSitesInfo(tuyaSitesInfo));
    }

    /**
     * 删除平台信息
     */
    @PreAuthorize("@ss.hasPermi('tuya:sitesinfo:remove')")
    @Log(title = "平台信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{siteIds}")
    public AjaxResult remove(@PathVariable Long[] siteIds)
    {
        for(Long siteId: siteIds)
        {
            if (tuyaSitesInfoService.hasChildBySiteId(siteId))
            {
                return AjaxResult.error("存在下级模块,不允许删除");
            }
        }
        return toAjax(tuyaSitesInfoService.deleteTuyaSitesInfoBySiteIds(siteIds));
    }

    /**
     * logo上传
     */
    @Log(title = "平台LOGO", businessType = BusinessType.UPDATE)
    @PostMapping("/sitelogo")
    public AjaxResult sitelogo(@RequestParam("logofile") MultipartFile file) throws Exception
    {
        if (!file.isEmpty())
        {
            String avatar = FileUploadUtils.upload(RuoYiConfig.getAvatarPath(), file, MimeTypeUtils.IMAGE_EXTENSION);
            AjaxResult ajax = AjaxResult.success();
            ajax.put("imgUrl", avatar);
            return ajax;
        }
        return AjaxResult.error("上传图片异常，请联系管理员");
    }
}
