package com.bonus.sgzb.base.controller;

import com.bonus.sgzb.base.domain.MaType;
import com.bonus.sgzb.base.service.ITypeService;
import com.bonus.sgzb.common.core.constant.HttpStatus;
import com.bonus.sgzb.common.core.web.controller.BaseController;
import com.bonus.sgzb.common.core.web.domain.AjaxResult;
import com.bonus.sgzb.common.core.web.page.TableDataInfo;
import com.bonus.sgzb.common.log.annotation.Log;
import com.bonus.sgzb.common.log.enums.BusinessType;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 工机具类型管理控制层
 */
@RestController
@Api(value = "工机具类型管理控制层")
@RequestMapping("/type")
public class MaTypeController extends BaseController {

    @Autowired
    private ITypeService iTypeService;


    /**
     * 根据类型名称查询类型
     * @return 结果
     */
    @ApiOperation(value = "根据类型名称查询类型")
    @GetMapping("/getMaTypeList")
    public AjaxResult getMaTypeList(String typeName){
        return AjaxResult.success(iTypeService.getMaTypeList(typeName));

    }

    /**
     * 根据左列表类型id查询右表格
     * @param typeId
     * @return
     */
    @ApiOperation(value = "根据左列表类型id查询右表格")
    @GetMapping("/getListByMaType")
    public TableDataInfo getListByMaType(@RequestParam(required = false) Long typeId,
                                         @RequestParam(required = false) String typeName,
                                         @RequestParam(required = false) Integer pageSize,
                                         @RequestParam(required = false) Integer pageNum){
        if(typeId==null){
            return null;
        }
        List<MaType> listByMaType = iTypeService.getListByMaType(typeId, typeName);
        TableDataInfo rspData = new TableDataInfo();
        rspData.setTotal(listByMaType.size());
        rspData.setCode(HttpStatus.SUCCESS);
        listByMaType = listByMaType.stream().skip((long) (pageNum - 1) * pageSize).limit(pageSize).collect(Collectors.toList());
        rspData.setRows(listByMaType);
        rspData.setMsg("查询成功");

        return rspData;
    }

    /**
     * 获取机具类型管理ma_type详细信息
     */
    @ApiOperation(value = "获取机具类型管理ma_type详细信息")
    @GetMapping(value = "/{typeId}")
    public AjaxResult getInfo(@PathVariable("typeId") Long typeId)
    {
        return success(iTypeService.selectMaTypeByTypeId(typeId));
    }

    /**
     * 新增机具类型管理ma_type
     */
    @ApiOperation(value = "新增机具类型管理ma_type")
    @Log(title = "机具类型管理ma_type", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MaType maType)
    {
        return toAjax(iTypeService.insertMaType(maType));
    }

    /**
     * 修改机具类型管理ma_type
     */
    @ApiOperation(value = "修改机具类型管理ma_type")
    @Log(title = "机具类型管理ma_type", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MaType maType)
    {
        return toAjax(iTypeService.updateMaType(maType));
    }

    /**
     * 删除机具类型管理ma_type
     */
    @ApiOperation(value = "删除机具类型管理ma_type")
    @Log(title = "机具类型管理ma_type", businessType = BusinessType.DELETE)
    @DeleteMapping("/{typeId}")
    public AjaxResult remove(@PathVariable Long typeId)
    {
        return toAjax(iTypeService.deleteMaTypeByTypeId(typeId));
    }


}
