package com.ruoyi.project.business.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.business.domain.TpCaseElevator;
import com.ruoyi.project.business.dto.TpCaseElevatorDto;
import com.ruoyi.project.business.dto.TpParaRelatedCaseDto;
import com.ruoyi.project.business.dto.TpParaValidationDto;
import com.ruoyi.project.business.enums.DelFlagEnum;
import com.ruoyi.project.business.service.ITpCaseElevatorService;
import com.ruoyi.project.business.service.ITpParaRelatedCaseService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @author jubs
 */
@RestController
@RequestMapping("/business/case")
public class TpParaRelatedCaseController extends BaseController {
    @Resource
    private ITpParaRelatedCaseService tpParaRelatedCaseService;
    @Resource
    private ITpCaseElevatorService tpCaseElevatorService;


    @PostMapping("saveOrUpdate")
    public AjaxResult saveOrUpdate(@RequestBody TpParaRelatedCaseDto data) {
        return AjaxResult.success(tpParaRelatedCaseService.saveOrUpdateDto(data));
    }

    @GetMapping("getRelatedCaseAll")
    public AjaxResult getRelatedCaseAll(String lastUpdate) {
        return AjaxResult.success(tpParaRelatedCaseService.getRelatedCaseAll(lastUpdate));
    }


    @GetMapping("getTableList")
    public TableDataInfo getTableList(TpParaRelatedCaseDto data) {
        List<Long> relatedModelIds = data.getRelatedModelIds();
        if (CollectionUtils.isNotEmpty(relatedModelIds)){
            data.setIds(tpCaseElevatorService.list(Wrappers.<TpCaseElevator>lambdaQuery()
                    .in(TpCaseElevator::getElevatorModelId, relatedModelIds)
                    .eq(TpCaseElevator::getDelFlag, DelFlagEnum.NORMAL.getValue()))
                    .stream().map(TpCaseElevator::getRelatedCaseId).distinct()
                    .collect(Collectors.toList()));
            if (CollectionUtils.isEmpty(data.getIds())) {
                data.setIds(Lists.newArrayList(-1L));
            }
        }

        startPage();
//        return getDataTable(tpParaRelatedCaseService.getTableList(data));
        return tpParaRelatedCaseService.getTableList(data);
    }

    @GetMapping("getInfoById/{id}")
    public AjaxResult getInfoById(@PathVariable Long id) {
        return AjaxResult.success(tpParaRelatedCaseService.getInfoById(id));
    }

    @PostMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody TpParaRelatedCaseDto param) {
        return AjaxResult.success(tpParaRelatedCaseService.changeStatus(param));
    }

    @GetMapping("/initRelation")
    public TableDataInfo initRelation(TpParaValidationDto param) {
        startPage();
        List<TpCaseElevatorDto> relationEnters = tpParaRelatedCaseService.initData(param.getId());
        return getDataTable(relationEnters);
    }

    @PostMapping("/batchChangeRelation")
    public AjaxResult batchChangeRelation(@RequestBody TpParaValidationDto param) {
        return AjaxResult.success(tpParaRelatedCaseService.batchChangeRelation(param));
    }

    @GetMapping("/getInfoByTpElevatorModelId/{elevatorModelId}")
    public AjaxResult getInfoByTpElevatorModelId(@PathVariable Long elevatorModelId) {
        return AjaxResult.success(tpParaRelatedCaseService.getInfoByTpElevatorModelId(elevatorModelId));
    }

    @DeleteMapping("/delete")
    public AjaxResult delete(@RequestParam("id") Long id, @RequestParam(value = "updateTime",required = false) Date updateTime) {
        return AjaxResult.success(tpParaRelatedCaseService.deleteById(id, updateTime));
    }


}
