package com.zd.laboratory.controller;

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

import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.zd.common.core.domain.per.PerFun;
import com.zd.common.core.domain.per.PerPrefix;
import com.zd.common.core.web.domain.ResultData;
import com.zd.laboratory.mqtt.constants.MqttConstants;
import com.zd.laboratory.mqtt.service.impl.CommonSend;
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.zd.common.log.annotation.Log;
import com.zd.common.log.enums.BusinessType;
import com.zd.common.security.annotation.PreAuthorize;
import com.zd.laboratory.domain.LabRotationChart;
import com.zd.laboratory.service.ILabRotationChartService;
import com.zd.common.core.web.controller.BaseController;
import com.zd.common.core.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.zd.common.swagger.config.Knife4jConfiguration;
import com.zd.common.core.web.page.TableDataInfo;

/**
 * 轮播图Controller
 *
 * @author zd
 * @date 2021-12-28
 */
@RestController
@Api(tags = "【轮播图】")
//作者手动加入 ，在接口上加作者@ApiOperationSupport(author = "XXX")
@ApiSupport(author = Knife4jConfiguration.Author.CYL)
@RequestMapping("/chart")
public class LabRotationChartController extends BaseController
{
    @Autowired
    private ILabRotationChartService labRotationChartService;
    @Autowired
    private CommonSend commonSend;

    /**
     * 查询轮播图列表
     */
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_CHART + PerFun.LIST)
    @GetMapping("/list")
    @ApiOperation(value = "查询轮播图列表")
    public TableDataInfo<LabRotationChart> list(LabRotationChart labRotationChart)
    {
        startPage();
        List<LabRotationChart> list = labRotationChartService.selectLabRotationChartList(labRotationChart);
        return getDataTable(list);
    }

    /**
     * 导出轮播图列表
     */
    @ApiOperation(value = "导出轮播图列表")
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_CHART + PerFun.EXPORT)
    @Log(title = "轮播图", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LabRotationChart labRotationChart) throws IOException
    {
        List<LabRotationChart> list = labRotationChartService.selectLabRotationChartList(labRotationChart);
        ExcelUtil<LabRotationChart> util = new ExcelUtil<LabRotationChart>(LabRotationChart.class);
        util.exportExcel(response, list, "轮播图数据");
    }

    /**
     * 获取轮播图详细信息
     */
    @ApiOperation(value = "获取轮播图详细信息")
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_CHART + PerFun.QUERY)
    @GetMapping(value = "/{id}")
    public ResultData<LabRotationChart> getInfo(@PathVariable("id") Long id)
    {
        return ResultData.success(labRotationChartService.selectLabRotationChartById(id));
    }

    /**
     * 新增轮播图
     */
    @ApiOperation(value = "新增轮播图")
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_CHART + PerFun.ADD)
    @Log(title = "轮播图", businessType = BusinessType.INSERT)
    @PostMapping
    public ResultData add(@RequestBody LabRotationChart labRotationChart)
    {
        int num = labRotationChartService.insertLabRotationChart(labRotationChart);

        commonSend.send(MqttConstants.LAB_ROTATION, "新增了一个轮播图");
        return ResultData.result(num);
    }

    /**
     * 修改轮播图
     */
    @ApiOperation(value = "修改轮播图")
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_CHART + PerFun.EDIT)
    @Log(title = "轮播图", businessType = BusinessType.UPDATE)
    @PutMapping
    public ResultData edit(@RequestBody LabRotationChart labRotationChart)
    {
        int count = labRotationChartService.updateLabRotationChart(labRotationChart);
        commonSend.send(MqttConstants.LAB_ROTATION, "修改了一个轮播图");
        return  ResultData.result(count);
    }

    /**
     * 删除轮播图
     */
    @ApiOperation(value = "删除轮播图")
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_CHART + PerFun.REMOVE)
    @Log(title = "轮播图", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public ResultData remove(@PathVariable Long[] ids)
    {
        int count = labRotationChartService.deleteLabRotationChartByIds(ids);
        commonSend.send(MqttConstants.LAB_ROTATION, "删除了一个轮播图");
        return ResultData.result(count);
    }

    /**
     * 轮播图开关操作
     */
    @ApiOperation(value = "轮播图开关操作")
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_CHART + PerFun.EDIT)
    @Log(title = "轮播图", businessType = BusinessType.UPDATE)
    @PutMapping("/editRotationType/{id}")
    public ResultData editRotationType(@PathVariable("id") Long id)
    {
        LabRotationChart labRotationChart = labRotationChartService.selectLabRotationChartById(id);
        if(labRotationChart.getImgType()==1){
            labRotationChart.setImgType(2);
        }else{
            labRotationChart.setImgType(1);
        }
        labRotationChartService.updateLabRotationChart(labRotationChart);
        commonSend.send(MqttConstants.LAB_ROTATION, "打开或关闭轮播图");
        return  ResultData.success(labRotationChart.getImgType());
    }

}
