package com.bonus.sgzb.base.controller;

import com.bonus.sgzb.base.api.domain.MaType;
import com.bonus.sgzb.base.domain.vo.TreeSelect;
import com.bonus.sgzb.base.mapper.MaTypeMapper;
import com.bonus.sgzb.base.service.ITypeService;
import com.bonus.sgzb.common.core.web.controller.BaseController;
import com.bonus.sgzb.common.core.web.domain.AjaxResult;
import com.bonus.sgzb.common.log.annotation.Log;
import com.bonus.sgzb.common.log.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

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

    @Autowired
    private ITypeService iTypeService;

    @Resource
    private MaTypeMapper maTypeMapper;


    /**
     * 根据类型名称查询类型
     * @return 结果
     */
    @ApiOperation(value = "根据类型名称查询类型")
    @GetMapping("/getMaTypeList")
    public AjaxResult getMaTypeList(@RequestParam(required = false, defaultValue = "", value = "typeName") String typeName, @RequestParam(required = false, defaultValue = "", value = "parentId") String parentId){
        List<TreeSelect> maTypeList = iTypeService.getMaTypeList(typeName, parentId);
        return AjaxResult.success(maTypeList);
    }

    /**
     * 工机具类型下拉树
     * @return 结果
     */
    @ApiOperation(value = "工机具类型下拉树")
    @GetMapping("/getMaTypeSelect")
    public AjaxResult getMaTypeSelect(@RequestParam(required = false, defaultValue = "", value = "typeName") String typeName, @RequestParam(required = false, defaultValue = "", value = "parentId") String parentId){
        List<TreeSelect> maTypeList = iTypeService.getMaTypeSelect(typeName, parentId);
        return AjaxResult.success(maTypeList);
    }

    @ApiOperation(value = "获取机具设备的具体规格")
    @GetMapping(value = "/selectMaTypeTreeByLevel")
    public AjaxResult selectMaTypeTreeByLevel(@RequestParam(value = "typeId") String typeId) {
        List<MaType> maTypeList = maTypeMapper.selectMaTypeTreeByLevel(typeId);
        return AjaxResult.success(maTypeList);
    }

    /**
     * 根据左列表类型id查询右表格
     * @param typeId
     * @return
     */
    @ApiOperation(value = "工器具类型")
    @GetMapping("/equipmentType")
    public AjaxResult equipmentType(@RequestParam(required = false) Long typeId,
                                    @RequestParam(required = false) String typeName){
        List<MaType> listByMaType = iTypeService.getEquipmentType(typeId,typeName);
        return success(listByMaType);
    }

    /**
     * 获取规格层级为3的设备列表
     * @return 结果
     */
    @ApiOperation(value = "获取规格层级的设备列表")
    @GetMapping("/selectMaTypeListByLevelIndex")
    public AjaxResult selectMaTypeListByLevelIndex(@RequestParam(value = "level", required = false) Integer level, @RequestParam(value = "type") Integer type, @RequestParam(value = "parentId", required = false) Integer parentId, @RequestParam(value = "keyword", required = false) String keyword){
        if (type == null) { return AjaxResult.error("参数错误,类型不能为空"); }
        if (type == 0) {
            // 查询全部机具类型
            if (level == 0 || level == 1) {
                // 查询全部等级、一级
                List<TreeSelect> treeSelectList = iTypeService.buildDeptTreeSelect(maTypeMapper.selectMaTypeListByLevelNotFour(null,null));
                List<TreeSelect> children = treeSelectList.get(0).getChildren();
                TreeSelect zero = new TreeSelect();
                zero.setLevel(2);
                zero.setId(0L);
                zero.setLabel("全部");
                zero.setParentId(0L);
                children.add(0, zero);
                return AjaxResult.success(treeSelectList);
            }
            if (level == 2) {
                // 查询二级
                if (parentId == 0){
                    List<MaType> maTypes = maTypeMapper.selectMaTypeListByLevelNotFour(null, keyword);
                    return AjaxResult.success(maTypes);
                }else {
                    return AjaxResult.success(maTypeMapper.selectMaTypeListByLevelNotFour(parentId.toString(),keyword));
                }


                // List<TreeSelect> treeSelectList = iTypeService.buildDeptTreeSelect(maTypeMapper.selectMaTypeListByLevelNotFour(parentId.toString()));
                //
                // // 遍历剔除不符合的三级规格
                // for (TreeSelect treeSelect : treeSelectList) {
                //     List<TreeSelect> children = treeSelect.getChildren();
                //     if (children != null) {
                //         for (TreeSelect child : children) {
                //             List<TreeSelect> grandChildren = child.getChildren();
                //             if (grandChildren!= null) {
                //                 grandChildren.removeIf(obj -> !Objects.equals(obj.getParentId(), Long.valueOf(parentId)));
                //             }
                //         }
                //     }
                // }
                // return AjaxResult.success(treeSelectList);
            }
        }
        if (type == 1) {
            // 查询施工机具
            List<TreeSelect> treeSelectList;
            if (level != 2) {
                treeSelectList = iTypeService.buildDeptTreeSelect(maTypeMapper.selectMaTypeListByLevelNotFour(null,null));
//                treeSelectList.removeIf(item -> item.getId() == 1L);
                //List<TreeSelect> treeSelectList1 = treeSelectList.stream().filter(item -> Objects.equals(item.getId(), 1L)).collect(Collectors.toList());
                return AjaxResult.success(treeSelectList);
            } else {
                return AjaxResult.success(maTypeMapper.selectMaTypeListByLevelNotFour(parentId.toString(),keyword));
                // treeSelectList = iTypeService.buildDeptTreeSelect(maTypeMapper.selectMaTypeListByLevelNotFour(parentId.toString()));
                // // 先过滤出施工机具
                // treeSelectList = treeSelectList.stream().filter(item -> Objects.equals(item.getId(), 1L)).collect(Collectors.toList());
                // // 再过滤出不符合的三级规格
                // if (!treeSelectList.isEmpty()) {
                //     for (TreeSelect treeSelect : treeSelectList) {
                //         List<TreeSelect> children = treeSelect.getChildren();
                //         if (children != null) {
                //             for (TreeSelect child : children) {
                //                 List<TreeSelect> grandChildren = child.getChildren();
                //                 if (grandChildren != null) {
                //                     grandChildren.removeIf(obj -> !Objects.equals(obj.getParentId(), Long.valueOf(parentId)));
                //                 }
                //
                //             }
                //         }
                //     }
                // }
                // return AjaxResult.success(treeSelectList);
            }
        }
        if (type == 2) {
            List<TreeSelect> treeSelectList = iTypeService.buildDeptTreeSelect(maTypeMapper.selectMaTypeListByLevelNotFour(null,null));
            //treeSelectList = treeSelectList.stream().filter(item -> Objects.equals(item.getId(), 2L)).collect(Collectors.toList());
            // 查询安全工器具
            if (level == 2) {
                return AjaxResult.success(maTypeMapper.selectMaTypeListByLevelNotFour(parentId.toString(),keyword));
                // if (!treeSelectList.isEmpty()) {
                //     for (TreeSelect treeSelect : treeSelectList) {
                //         List<TreeSelect> children = treeSelect.getChildren();
                //         if (children != null) {
                //             for (TreeSelect child : children) {
                //                 List<TreeSelect> grandChildren = child.getChildren();
                //                 if (grandChildren != null) {
                //                     grandChildren.removeIf(obj -> !Objects.equals(obj.getParentId(), Long.valueOf(parentId)));
                //                 }
                //             }
                //         }
                //     }
                // }
            }
            return AjaxResult.success(treeSelectList);
        }
        return AjaxResult.success(maTypeMapper.selectMaTypeListByLevelNotFour(null,null));
    }

    /**
     * 根据左列表类型id查询右表格
     * @param typeId
     * @return
     */
    @ApiOperation(value = "根据左列表类型id查询右表格")
    @GetMapping("/getListByMaType")
    public AjaxResult getListByMaType(@RequestParam(required = false) Long typeId,
                                      @RequestParam(required = false) String typeName){
        List<MaType> listByMaType = iTypeService.getListByParentId(typeId,typeName);
        return success(listByMaType);
    }

    /**
     * 根据左列表类型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)
    {
        int result = iTypeService.insertMaType(maType);
        if (result > 0) {
            return success(toAjax(iTypeService.insertMaType(maType)));
        } else {
            return AjaxResult.error("未新增成功,请查看同一层级是否有重名的类型!");
        }
    }

    /**
     * 修改机具类型管理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) throws Exception {
        return toAjax(iTypeService.deleteMaTypeByTypeId(typeId));
    }


}
