package com.jeeplus.finance.program.serviceType.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.program.serviceType.domain.ProgramServiceType;
import com.jeeplus.finance.program.serviceType.service.ProgramServiceTypeService;
import com.jeeplus.finance.program.serviceType.service.dto.ProgramServiceTypeDTO;
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 = "/program/service_type")
public class ProgramServiceTypeController {

    @Resource
    private ProgramServiceTypeService programServiceTypeService;

    /**
     * 查询业务类型列表
     * @param programServiceTypeDTO
     * @param page
     * @return
     * @throws Exception
     */
    @GetMapping("list")
    public ResponseEntity<IPage<ProgramServiceTypeDTO>> data(ProgramServiceTypeDTO programServiceTypeDTO, Page<ProgramServiceTypeDTO> page) throws Exception {
        page.setSize(-1);
        IPage<ProgramServiceTypeDTO> result = new Page<ProgramServiceTypeDTO>();
        if(ObjectUtil.isNotEmpty(programServiceTypeDTO)){
            if(StringUtils.isNotBlank(programServiceTypeDTO.getName())){
                //根据type模糊查询
                List<ProgramServiceType> programServiceTypeList = programServiceTypeService.list(new QueryWrapper<ProgramServiceType>().lambda()
                        .like(StringUtils.isNotBlank(programServiceTypeDTO.getName()), ProgramServiceType::getName, programServiceTypeDTO.getName())
                );
                List<String> idList = new ArrayList<>();
                programServiceTypeList.stream().forEach(item->{
                    if (StringUtils.isNotBlank(item.getParentIds())){
                        String[] split = item.getParentIds().split(",");
                        for (String s : split) {
                            idList.add(s);
                        }
                    }
                    List<ProgramServiceType> list = programServiceTypeService.list(new QueryWrapper<ProgramServiceType>().lambda().like(ProgramServiceType::getParentIds, "," + item.getId() + ","));
                    idList.addAll(list.stream().map(ProgramServiceType::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<ProgramServiceType> wrapper = new QueryWrapper<ProgramServiceType>()
                            .in("pst.id",idList);
                    result = programServiceTypeService.findList (page,wrapper);
                }
            }else{
                result = programServiceTypeService.findList (page,new QueryWrapper<ProgramServiceType>());
            }
        }
        return ResponseEntity.ok (result);
    }

    /**
     * 查询业务类型数据
     * @param id
     * @return
     */
    @PreAuthorize ("hasAnyAuthority('program_service_type:view','program_service_type:add','program_service_type:edit')")
    @GetMapping("queryById")
    public ResponseEntity queryById(@RequestParam("id") String id) {
        ProgramServiceTypeDTO programServiceTypeDTO = programServiceTypeService.queryById ( id );
        return ResponseEntity.ok (programServiceTypeDTO);
    }

    /**
     * 保存业务类型
     * @param programServiceTypeDTO
     * @return
     */
    @PreAuthorize("hasAnyAuthority('program_service_type:add','program_service_type:edit')")
    @PostMapping("save")
    public ResponseEntity save(@Valid @RequestBody ProgramServiceTypeDTO programServiceTypeDTO) {
        programServiceTypeService.saveType(programServiceTypeDTO);
        return ResponseEntity.ok ("操作成功");
    }

    /**
     * 删除业务类型
     * @param ids
     * @return
     */
    @PreAuthorize ("hasAuthority('program_service_type:del')")
    @DeleteMapping("delete")
    public ResponseEntity delete(String ids) {
        return programServiceTypeService.deleteByIds(ids);
    }

    /**
     * 查询全部一级业务类型
     * @return
     */
    @GetMapping("getList")
    public ResponseEntity<List<ProgramServiceTypeDTO>> getList() {
        List<ProgramServiceTypeDTO> list = programServiceTypeService.getList();
        return ResponseEntity.ok (list);
    }
}
