package com.jeeplus.finance.workClientInfo.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeeplus.finance.workClientInfo.domain.CwManageLevelType;
import com.jeeplus.finance.workClientInfo.service.CwManageLevelTypeService;
import com.jeeplus.finance.workClientInfo.service.dto.CwManageLevelTypeDTO;
import com.jeeplus.logging.annotation.ApiLog;
import com.jeeplus.logging.constant.enums.LogTypeEnum;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Api("客户配置管理-管理级次分类")
@RestController
@RequestMapping(value = "/cw_work_client/manage_level_type")
public class CwManageLevelTypeController {

    @Resource
    private CwManageLevelTypeService cwManageLevelTypeService;

    /**
     * 查询管理级次分类列表
     * @param cwManageLevelTypeDTO
     * @param page
     * @return
     * @throws Exception
     */
    @ApiLog("查询管理级次分类列表")
    @PreAuthorize("hasAuthority('cw_work_client:manage_level_type:list')")
    @GetMapping("list")
    public ResponseEntity<IPage<CwManageLevelTypeDTO>> data(CwManageLevelTypeDTO cwManageLevelTypeDTO, Page<CwManageLevelTypeDTO> page) throws Exception {
        page.setSize(-1);
        IPage<CwManageLevelTypeDTO> result = new Page<CwManageLevelTypeDTO>();
        if(ObjectUtil.isNotEmpty(cwManageLevelTypeDTO)){
            if(StringUtils.isNotBlank(cwManageLevelTypeDTO.getName())){
                //根据type模糊查询
                List<CwManageLevelType> cwManageLevelTypeList = cwManageLevelTypeService.selectByName(cwManageLevelTypeDTO);
//                List<CwManageLevelType> cwManageLevelTypeList = cwManageLevelTypeService.list(new QueryWrapper<CwManageLevelType>().lambda()
//                        .like(StringUtils.isNotBlank(cwManageLevelTypeDTO.getName()), CwManageLevelType::getName, cwManageLevelTypeDTO.getName())
//                );
                List<String> idList = new ArrayList<>();
                cwManageLevelTypeList.stream().forEach(item->{
                    if (StringUtils.isNotBlank(item.getParentIds())){
                        String[] split = item.getParentIds().split(",");
                        for (String s : split) {
                            idList.add(s);
                        }
                    }
                    List<CwManageLevelType> list = cwManageLevelTypeService.selectByParentIds(new QueryWrapper<CwManageLevelType>().lambda().like(CwManageLevelType::getParentIds, "," + item.getId() + ","));
//                    List<CwManageLevelType> list = cwManageLevelTypeService.list(new QueryWrapper<CwManageLevelType>().lambda().like(CwManageLevelType::getParentIds, "," + item.getId() + ","));
                    idList.addAll(list.stream().map(CwManageLevelType::getId).collect(Collectors.toList()));
                    idList.add(item.getId());
                });
                List<String> ids = idList.stream().distinct().filter(item->{
                    if (!"0".equals(item)){
                        return true;
                    }
                    return false;
                }).collect(Collectors.toList());
                if(CollectionUtil.isNotEmpty(ids)){
                    QueryWrapper<CwManageLevelType> wrapper = new QueryWrapper<CwManageLevelType>()
                            .in("cw_mlt.id",idList);
                    result = cwManageLevelTypeService.findList (page,wrapper);
                }
            }else{
                result = cwManageLevelTypeService.findList (page,new QueryWrapper<CwManageLevelType>());
            }
        }
        return ResponseEntity.ok (result);
    }

    /**
     * 查询管理级次分类数据
     * @param id
     * @return
     */
    @ApiLog("查询管理级次分类数据")
    @PreAuthorize ("hasAnyAuthority('cw_work_client:manage_level_type:view','cw_work_client:manage_level_type:add','cw_work_client:manage_level_type:edit')")
    @GetMapping("queryById")
    public ResponseEntity queryById(@RequestParam("id") String id) {
        CwManageLevelTypeDTO cwManageLevelTypeDTO = cwManageLevelTypeService.queryById ( id );
        return ResponseEntity.ok (cwManageLevelTypeDTO);
    }

    /**
     * 查询全部一级管理级次分类
     * @return
     */
    @ApiLog("查询全部一级管理级次分类")
    @GetMapping("getList")
    public ResponseEntity<List<CwManageLevelTypeDTO>> getList() {
        List<CwManageLevelTypeDTO> list = cwManageLevelTypeService.getList();
        return ResponseEntity.ok (list);
    }

    /**
     * 保存管理级次分类
     * @param cwManageLevelTypeDTO
     * @return
     */
    @ApiLog(value = "修改/新增管理级次分类", type = LogTypeEnum.SAVE)
    @PreAuthorize("hasAnyAuthority('cw_work_client:manage_level_type:save','cw_work_client:manage_level_type:edit')")
    @PostMapping("save")
    public ResponseEntity save(@Valid @RequestBody CwManageLevelTypeDTO cwManageLevelTypeDTO) {
        cwManageLevelTypeService.saveType(cwManageLevelTypeDTO);
        return ResponseEntity.ok ("操作成功");
    }

    /**
     * 删除管理级次分类
     * @param ids
     * @return
     */
    @ApiLog(value = "删除管理级次分类", type = LogTypeEnum.SAVE)
    @PreAuthorize ("hasAuthority('cw_work_client:manage_level_type:del')")
    @DeleteMapping("delete")
    public ResponseEntity delete(String ids) {
        return cwManageLevelTypeService.deleteByIds(ids);
    }

}
