package com.jeeplus.test.cw.projectBusinessType.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.aop.logging.annotation.ApiLog;
import com.jeeplus.sys.constant.enums.LogTypeEnum;
import com.jeeplus.sys.utils.StringUtils;
import com.jeeplus.test.cw.projectBusinessType.domain.CwProjectBusinessType;
import com.jeeplus.test.cw.projectBusinessType.service.CwProjectBusinessTypeService;
import com.jeeplus.test.cw.projectBusinessType.service.dto.CwProjectBusinessTypeDTO;
import io.swagger.annotations.Api;
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 = "/projectRecords/cw_project_business_type")
public class CwProjectBusinessTypeController {

    @Resource
    private CwProjectBusinessTypeService cwProjectBusinessTypeService;

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

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

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

    /**
     * 保存业务类型
     * @param cwProjectBusinessTypeDTO
     * @return
     */
    @ApiLog(value = "修改/新增业务类型", type = LogTypeEnum.SAVE)
    @PreAuthorize("hasAnyAuthority('cw_project_business_type:add','cw_project_business_type:edit')")
    @PostMapping("save")
    public ResponseEntity save(@Valid @RequestBody CwProjectBusinessTypeDTO cwProjectBusinessTypeDTO) {
        cwProjectBusinessTypeService.saveType(cwProjectBusinessTypeDTO);
        return ResponseEntity.ok ("操作成功");
    }

    /**
     * 删除业务类型
     * @param ids
     * @return
     */
    @ApiLog(value = "删除业务类型", type = LogTypeEnum.SAVE)
    @PreAuthorize ("hasAuthority('cw_project_business_type:del')")
    @DeleteMapping("delete")
    public ResponseEntity delete(String ids) {
        return cwProjectBusinessTypeService.deleteByIds(ids);
    }

}
