package com.bonus.material.tool.controller;

import com.bonus.common.core.utils.poi.ExcelUtil;
import com.bonus.common.core.web.controller.BaseController;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.core.web.page.TableDataInfo;
import com.bonus.common.log.annotation.SysLog;
import com.bonus.common.log.enums.OperaType;
import com.bonus.material.basic.domain.BmSlideShow;
import com.bonus.material.common.annotation.PreventRepeatSubmit;
import com.bonus.material.tool.domain.ToolEntity;
import com.bonus.material.tool.service.ToolService;
import com.bonus.system.api.domain.SysRole;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/tool")
@Api(value = "工具类型管理", tags = "工具类型管理")
public class ToolController extends BaseController {
    @Resource
    private ToolService toolService;

    /**
     * 工具类型树
     *
     * @param entity 实体
     * @return 树结构
     */
    @ApiOperation(value = "工具类型树")
    @GetMapping("/getTreeSelect")
    public AjaxResult getTreeSelect(ToolEntity entity) {
        return toolService.getTreeSelect(entity);
    }


    /**
     * 工具类型表格
     *
     * @param entity 实体
     * @return 表格
     */
    @ApiOperation(value = "工具类型表格")
    @GetMapping("/list")
    public TableDataInfo list(ToolEntity entity) {
        try {
            startPage();
            List<ToolEntity> list = toolService.list(entity);
            return getDataTable(list);
        } catch (Exception e) {
            logger.error(e.toString(), e);
        }
        return getDataTableError(new ArrayList<>());
    }


    /**
     * 工具类型新增
     *
     * @param entity 实体
     * @return 表格
     */
    @ApiOperation(value = "工具类型新增")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody ToolEntity entity) {
        return toolService.add(entity);
    }

    /**
     * 工具类型新增
     *
     * @param entity 实体
     * @return 表格
     */
    @ApiOperation(value = "工具类型修改")
    @PostMapping("/update")
    public AjaxResult update(@RequestBody ToolEntity entity) {
        return toolService.update(entity);
    }


    /**
     * 工具类型新增
     *
     * @param entity 实体
     * @return 表格
     */
    @ApiOperation(value = "工具类型删除")
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody ToolEntity entity) {
        return toolService.delete(entity);
    }


    /**
     * 工具类型新增
     *
     * @param entity 实体
     * @return 表格
     */
    @ApiOperation(value = "工具类型下拉选")
    @PostMapping("/getSelect")
    public AjaxResult getSelect(@RequestBody ToolEntity entity) {
        return toolService.getSelect(entity);
    }

    /**
     * 导出列表
     */
    @ApiOperation(value = "导出工具类型列表", notes = "按条件筛选后导出Excel，支持分页外的所有查询条件")
    @PostMapping("/export")
    public void export(HttpServletResponse response, ToolEntity entity) {
        List<ToolEntity> list = toolService.list(entity);
        ExcelUtil<ToolEntity> util = new ExcelUtil<ToolEntity>(ToolEntity.class);
        util.exportExcel(response, list, "工具类型配置数据");
    }

    @ApiOperation(value = "工具租赁价（批量）修改")
    @PostMapping("/updateLeasePrice")
    public AjaxResult updateLeasePrice(@RequestBody ToolEntity info) {
        if (info.getTypeIds().isEmpty()) {
            return AjaxResult.error("请选择要修改的机具");
        }
        if (info.getLeasePrice() == null) {
            return AjaxResult.error("请输入价格");
        }
        Integer i = toolService.updateLeasePrice(info);
        if (i > 0) {
            return AjaxResult.success("修改成功");
        } else {
            return AjaxResult.error("修改失败");
        }
    }
}
