package com.xinda.data.controller;

import com.xinda.common.annotation.Log;
import com.xinda.common.core.controller.BaseController;
import com.xinda.common.core.domain.AjaxResult;
import com.xinda.common.core.page.TableDataInfo;
import com.xinda.common.enums.BusinessType;
import com.xinda.common.utils.SecurityUtils;
import com.xinda.common.utils.poi.ExcelUtil;
import com.xinda.iot.domain.Standard;
import com.xinda.iot.service.IStandardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

/**
 * 产品分类Controller
 *
 * @author kerwincui
 * @date 2021-12-16
 */
@Api(tags = "产品分类")
@RestController
@RequestMapping("/iot/standard")
public class StandardController extends BaseController
{
    @Autowired
    private IStandardService standardService;

    /**
     * 查询产品分类列表
     */
    @PreAuthorize("@ss.hasPermi('iot:standard:list')")
    @GetMapping("/list")
    @ApiOperation("分类分页列表")
    public TableDataInfo list(Standard standard)
    {
        Boolean showSenior = standard.getShowSenior();
        if (Objects.isNull(showSenior)){
            standard.setShowSenior(true); //默认展示上级品类
        }
        Long deptUserId = getLoginUser().getUser().getDept().getDeptUserId();
        standard.setAdmin(SecurityUtils.isAdmin(deptUserId));
        standard.setDeptId(getLoginUser().getDeptId());
        standard.setTenantId(deptUserId);
        startPage();
        return getDataTable(standardService.selectStandardList(standard));
    }

    /**
     * 查询产品简短分类列表
     */
    @PreAuthorize("@ss.hasPermi('iot:standard:list')")
    @GetMapping("/shortlist")
    @ApiOperation("分类简短列表")
    public AjaxResult shortlist(Standard standard)
    {
        Boolean showSenior = standard.getShowSenior();
        if (Objects.isNull(showSenior)){
            standard.setShowSenior(true); //默认展示上级品类
        }
        Long deptUserId = getLoginUser().getUser().getDept().getDeptUserId();
        standard.setAdmin(SecurityUtils.isAdmin(deptUserId));
        standard.setDeptId(getLoginUser().getDeptId());
        standard.setTenantId(deptUserId);
        startPage();
        return AjaxResult.success(standardService.selectStandardShortList(standard));
    }

    /**
     * 导出产品分类列表
     */
    @PreAuthorize("@ss.hasPermi('iot:standard:export')")
    @Log(title = "产品分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出分类")
    public void export(HttpServletResponse response, Standard standard)
    {
        List<Standard> list = standardService.selectStandardList(standard);
        ExcelUtil<Standard> util = new ExcelUtil<Standard>(Standard.class);
        util.exportExcel(response, list, "产品分类数据");
    }

    /**
     * 获取产品分类详细信息
     */
    @ApiOperation("获取分类详情")
    @PreAuthorize("@ss.hasPermi('iot:standard:query')")
    @GetMapping(value = "/{standardId}")
    public AjaxResult getInfo(@PathVariable("standardId") Long standardId)
    {
        return AjaxResult.success(standardService.selectStandardByStandardId(standardId));
    }

    /**
     * 新增产品分类
     */
    @PreAuthorize("@ss.hasPermi('iot:standard:add')")
    @Log(title = "产品分类", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("添加分类")
    public AjaxResult add(@RequestBody Standard standard)
    {
        return toAjax(standardService.insertStandard(standard));
    }


    @PostMapping("/clone")
    @ApiOperation("克隆分类")
    public AjaxResult clone(@RequestBody Standard standard)
    {
        return toAjax(standardService.cloneStandard(standard));
    }

    /**
     * 修改产品分类
     */
    @PreAuthorize("@ss.hasPermi('iot:standard:edit')")
    @Log(title = "产品分类", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改分类")
    public AjaxResult edit(@RequestBody Standard standard)
    {
        return toAjax(standardService.updateStandard(standard));
    }

    /**
     * 删除产品分类
     */
    @PreAuthorize("@ss.hasPermi('iot:standard:remove')")
    @Log(title = "产品分类", businessType = BusinessType.DELETE)
	@DeleteMapping("/{standardIds}")
    @ApiOperation("批量删除分类")
    public AjaxResult remove(@PathVariable Long[] standardIds)
    {
        return standardService.deleteStandardByStandardIds(standardIds);
    }
}
