package com.bonus.material.ma.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;

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 com.bonus.material.ma.domain.vo.MaTypeSelectVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.BooleanUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import com.bonus.common.log.annotation.SysLog;
import com.bonus.common.security.annotation.RequiresPermissions;
import com.bonus.material.ma.domain.Type;
import com.bonus.material.ma.service.ITypeService;
import com.bonus.common.core.web.controller.BaseController;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.core.utils.poi.ExcelUtil;
import com.bonus.common.core.web.page.TableDataInfo;

/**
 * 物资类型管理Controller
 * @author syruan
 */
@Api(tags = "物资类型管理接口")
@RestController
@RequestMapping("/ma_type")
public class TypeController extends BaseController {

    @Resource
    private ITypeService typeService;

    /**
     * 查询物资类型管理列表
     */
    @ApiOperation(value = "查询物资类型管理列表")
    @RequiresPermissions("ma:type:list")
    @GetMapping("/list")
    public TableDataInfo list(MaTypeVo type) {
        startPage();
        List<MaTypeVo> list = typeService.selectTypeListAndParentInfo(type);
        return getDataTable(list);
    }

//    @ApiOperation(value = "查询物资类型管理列表(无分页)")
//    @RequiresPermissions("ma:type:list")
//    @GetMapping("/listNoPage")
//    public AjaxResult listNoPage(MaTypeVo type) {
//        List<MaTypeVo> list = typeService.selectTypeListAndParentInfo(type);
//        return typeService.getMyTypeAndBindUsers(list);
//    }

    /**
     * 根据左列表类型id查询右表格
     *
     * @param maTypeVo
     * @return
     */
    @ApiOperation(value = "根据左列表类型id查询右表格")
    @GetMapping("/getListByMaType")
    public AjaxResult getListByMaType(MaTypeVo maTypeVo) {
        List<Integer> parentIds = typeService.selectParentId(maTypeVo);
        if (CollectionUtils.isEmpty(parentIds)) {
            return AjaxResult.success(new ArrayList<>());
        }
        List<MaTypeVo> 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(typeService.getListByParentId(parentId.longValue(), maTypeVo));
        }
        if (BooleanUtils.isTrue(maTypeVo.getDisplayBindRelationship())) {
            List<MaTypeVo> finalMaTypeVos = typeService.getMyTypeAndBindUsers(maTypeVos);
            return AjaxResult.success(ListPagingUtil.paging(pageIndex, pageSize, finalMaTypeVos));
        } else {
            return AjaxResult.success(ListPagingUtil.paging(pageIndex, pageSize, maTypeVos));
        }
    }

    /**
     * 根据物资仓库ID查询施工类型
     */
    @ApiOperation(value = "根据物资仓库ID查询施工类型")
    @RequiresPermissions("ma:type:list")
    @GetMapping("/selectMaTypeListByHouseId")
    public AjaxResult selectMaTypeListByHouseId(@NotNull(message = "物资仓库ID不能为空") Long houseId) {
        List<MaTypeSelectVo> list = typeService.selectMaTypeListByHouseId(houseId);
        return success(list);
    }

    /**
     * 根据物资类型ID查询子级下拉列表 --不进行递归穿透
     */
    @ApiOperation(value = "根据物资类型ID-查询子级下拉列表")
    @RequiresPermissions("ma:type:list")
    @GetMapping("/selectMaTypeListByTypeId")
    public AjaxResult selectMaTypeListByTypeId(@NotNull(message = "物资类型ID不能为空") Long typeId) {
        List<MaTypeSelectVo> list = typeService.getMaTypeSelectVoListByParentId(typeId);
        return success(list);
    }

    /**
     * 查询物资类型4级规格型号--前端联动式下拉框
     *
     * @param typeId 规格型号
     */
    @ApiOperation(value = "获取物资类型连动式下拉框")
    @GetMapping("/equipmentType")
    public AjaxResult equipmentType(@RequestParam(required = false) Long typeId, @RequestParam(required = false) String typeName) {
        List<Type> listByMaType = typeService.getEquipmentType(typeId, typeName);
        return success(listByMaType);
    }


    /**
     * TODO : 后续优化代码逻辑
     * 物资类型下拉树
     */
    @ApiOperation(value = "物资类型下拉树")
    @RequiresPermissions("ma:type:list")
    @GetMapping("/getMaTypeTreeSelect")
    public AjaxResult getMaTypeTreeSelect(@RequestParam(required = false, defaultValue = "", value = "typeName") String typeName,
                                          @RequestParam(required = false, defaultValue = "", value = "parentId") String parentId) {
        return typeService.getMaTypeTreeSelect(typeName, parentId);
    }

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

    /**
     * 导出物资类型管理列表(库管员)
     */
    @ApiOperation(value = "导出物资类型列表(库管员)")
    @PreventRepeatSubmit
    @RequiresPermissions("ma:type:export")
    @SysLog(title = "物资类型管理(库管员)", businessType = OperaType.EXPORT, module = "仓储管理->导出物资类型(库管员)")
    @PostMapping("/export4Keeper")
    public void export4Keeper(HttpServletResponse response, Type type) {
        List<Type> list = typeService.selectTypeList4Keeper(type);
        ExcelUtil<Type> util = new ExcelUtil<Type>(Type.class);
        util.exportExcel(response, list, "物资类型管理数据(库管员)");
    }

    /**
     * 导出物资类型管理列表(维修员)
     */
    @ApiOperation(value = "导出物资类型列表(维修员)")
    @PreventRepeatSubmit
    @RequiresPermissions("ma:type:export")
    @SysLog(title = "物资类型管理(维修员)", businessType = OperaType.EXPORT, module = "仓储管理->导出物资类型(维修员)")
    @PostMapping("/export4Repair")
    public void export4Repair(HttpServletResponse response, Type type) {
        List<Type> list = typeService.selectTypeList4Repair(type);
        ExcelUtil<Type> util = new ExcelUtil<Type>(Type.class);
        util.exportExcel(response, list, "物资类型管理数据(维修员)");
    }

    /**
     * 获取物资类型管理详细信息
     */
    @ApiOperation(value = "获取物资类型详细信息")
    @RequiresPermissions("ma:type:query")
    @GetMapping(value = "/{typeId}")
    public AjaxResult getInfo(@PathVariable("typeId") Long typeId) {
        return success(typeService.selectTypeByTypeId(typeId));
    }

    /**
     * 新增物资类型管理
     */
    @ApiOperation(value = "新增物资类型")
    @PreventRepeatSubmit
    @RequiresPermissions("ma:type:add")
    @SysLog(title = "物资类型管理", businessType = OperaType.INSERT, module = "仓储管理->新增物资类型")
    @PostMapping
    public AjaxResult add(@RequestBody Type type) {
        return toAjax(typeService.insertType(type));
    }

    /**
     * 修改物资类型管理
     */
    @ApiOperation(value = "修改物资类型")
    @PreventRepeatSubmit
    @RequiresPermissions("ma:type:edit")
    @SysLog(title = "物资类型管理", businessType = OperaType.UPDATE, module = "仓储管理->修改物资类型")
    @PutMapping
    public AjaxResult edit(@RequestBody Type type) {
        return toAjax(typeService.updateType(type));
    }

    /**
     * 删除物资类型管理
     */
    @ApiOperation(value = "删除物资类型")
    @PreventRepeatSubmit
    @RequiresPermissions("ma:type:remove")
    @SysLog(title = "物资类型管理", businessType = OperaType.DELETE, module = "仓储管理->删除物资类型")
	@DeleteMapping("/{typeId}")
    public AjaxResult remove(@PathVariable Long typeId) {
        return typeService.deleteTypeByTypeId(typeId);
    }

    /**
     * 查询物资类型配置右侧列表
     */
//    @ApiOperation(value = "查询物资类型配置右侧列表")
//    @RequiresPermissions("ma:typeConfig:list")
//    @GetMapping("/getMaTypeConfigList")
//    public AjaxResult getMaTypeConfigList(MaTypeConfigDto maTypeConfigDto) {
//        // 调用service处理业务逻辑
//        return typeService.getMaTypeConfigList(maTypeConfigDto);
//    }


    /**
     * 根据左列表类型id查询右表格 --- 暂未启用，代码有问题!!
     * TODO: 待完善
     * @param typeId 左列表类型id

     @ApiOperation(value = "根据左列表类型id查询右表格")
     @GetMapping("/getListByMaType")
     public AjaxResult getListByMaType(Long typeId, @RequestParam(required = false) String typeName, Integer level) {
     List<Integer> parentIds = typeService.selectParentId(typeId, level);
     List<Type> listByMaType = new ArrayList<>();
     for (Integer parentId : parentIds) {
     listByMaType.addAll(typeService.getListByParentId(parentId.longValue(), typeName));
     }
     return null;
     }*/
}
