package com.bonus.material.ma.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.convert.Convert;
import com.bonus.common.biz.config.ListPagingUtil;
import com.bonus.common.core.utils.ServletUtils;
import com.bonus.common.log.enums.OperaType;
import com.bonus.material.common.annotation.PreventRepeatSubmit;
import com.bonus.material.ma.domain.vo.MaTypeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.BooleanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
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.bonus.common.log.annotation.SysLog;
import com.bonus.common.security.annotation.RequiresPermissions;
import com.bonus.material.ma.domain.PartType;
import com.bonus.material.ma.service.IPartTypeService;
import com.bonus.common.core.web.controller.BaseController;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.core.utils.poi.ExcelUtil;

/**
 * 配件类型管理Controller
 * 
 * @author xsheng
 * @date 2024-09-27
 */
@Api(tags = "配件类型管理接口")
@RestController
@RequestMapping("/ma_part_type")
public class PartTypeController extends BaseController
{
    @Autowired
    private IPartTypeService partTypeService;

    /**
     * 查询配件类型管理列表
     */
    @ApiOperation(value = "查询配件类型管理列表")
    @RequiresPermissions("ma:type:list")
    @GetMapping("/list")
    public AjaxResult list(PartType partType)
    {
        return partTypeService.selectPartTypeList(partType);
    }

    /**
     * 根据左列表类型id查询右表格
     *
     * @param partType
     * @return
     */
    @ApiOperation(value = "根据左列表类型id查询右表格")
    @GetMapping("/getListByPartType")
    public AjaxResult getListByPartType(PartType partType) {
        List<Integer> parentIds = partTypeService.selectParentId(partType);
        if (CollectionUtils.isEmpty(parentIds)) {
            return AjaxResult.success(new ArrayList<>());
        }
        List<PartType> maTypeVos = new ArrayList<>();
        Integer pageIndex = Convert.toInt(ServletUtils.getParameter("pageNum"), 1);
        Integer pageSize = Convert.toInt(ServletUtils.getParameter("pageSize"), 10);
        for (Integer parentId : parentIds) {
            maTypeVos.addAll(partTypeService.getListByParentId(parentId.longValue(), partType));
        }
        return AjaxResult.success(ListPagingUtil.paging(pageIndex, pageSize, maTypeVos));
    }

    @ApiOperation(value = "配件类型所属上级树")
    @RequiresPermissions("ma:type:query")
    @GetMapping("/getPartTree")
    public AjaxResult getPartTree(PartType dto){
        return partTypeService.getPartTree(dto);
    }

    /**
     * 导出配件类型管理列表
     */
    @ApiOperation(value = "导出配件类型管理列表")
    @PreventRepeatSubmit
    @RequiresPermissions("ma:type:export")
    @SysLog(title = "配件类型管理", businessType = OperaType.EXPORT, logType = 1,module = "仓储管理->导出配件类型管理")
    @PostMapping("/export")
    public void export(HttpServletResponse response, PartType partType)
    {
        List<PartType>  list = partTypeService.getTypeList(partType);
        ExcelUtil<PartType> util = new ExcelUtil<PartType>(PartType.class);
        util.exportExcel(response, list, "配件类型管理数据");
    }

    /**
     * 获取配件类型管理详细信息
     */
    @ApiOperation(value = "获取配件类型管理详细信息")
    @RequiresPermissions("ma:type:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(partTypeService.selectPartTypeByPaId(id));
    }

    /**
     * 新增配件类型管理
     */
    @ApiOperation(value = "新增配件类型管理")
    @PreventRepeatSubmit
    @RequiresPermissions("ma:type:add")
    @SysLog(title = "配件类型管理", businessType = OperaType.INSERT, logType = 1,module = "仓储管理->新增配件类型管理")
    @PostMapping
    public AjaxResult add(@RequestBody PartType partType)
    {
        return partTypeService.insertPartType(partType);
    }

    /**
     * 修改配件类型管理
     */
    @ApiOperation(value = "修改配件类型管理")
    @PreventRepeatSubmit
    @RequiresPermissions("ma:type:edit")
    @SysLog(title = "配件类型管理", businessType = OperaType.UPDATE, logType = 1,module = "仓储管理->修改配件类型管理")
    @PutMapping
    public AjaxResult edit(@RequestBody PartType partType)
    {
        return partTypeService.updatePartType(partType);
    }

    /**
     * 删除配件类型管理
     */
    @ApiOperation(value = "删除配件类型管理")
    @PreventRepeatSubmit
    @RequiresPermissions("ma:type:remove")
    @SysLog(title = "配件类型管理", businessType = OperaType.DELETE, logType = 1,module = "仓储管理->删除配件类型管理")
	@DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        return partTypeService.deletePartTypeByPaId(id);
    }
}
