package com.eitc.patient.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.eitc.common.annotation.Log;
import com.eitc.common.core.controller.BaseController;
import com.eitc.common.core.domain.AjaxResult;
import com.eitc.common.enums.BusinessType;
import com.eitc.common.utils.bean.BeanUtils;
import com.eitc.patient.domain.DiagnosisType;
import com.eitc.patient.domain.DiagnosisTypeTemplate;
import com.eitc.patient.service.IDiagnosisTypeService;
import com.eitc.patient.service.IDiagnosisTypeTemplateService;
import com.eitc.patient.tree.DiagnosisTypeTree;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * DiagnosisTypeController
 *
 * @author mtx
 * @date: 2024/05/17 15:51
 */
@Api(tags = "诊断分类")
@RestController
@RequestMapping("/system/diagnosis-type")
public class DiagnosisTypeController extends BaseController {

    @Resource
    private IDiagnosisTypeService diagnosisTypeService;
    @Resource
    private IDiagnosisTypeTemplateService diagnosisTypeTemplateService;

    /**
     * 查询诊断分类
     */
    @GetMapping("/list")
    @ApiOperation("查询诊断分类")
    public AjaxResult list(DiagnosisType diagnosisType) {
        return success(diagnosisTypeService.getDiagnosisTypes(diagnosisType));
    }

    @GetMapping("/tree")
    @ApiOperation("查询诊断分类")
    public AjaxResult tree(DiagnosisType diagnosisType) {
        List<DiagnosisType> list = diagnosisTypeService.getDiagnosisTypes(diagnosisType);
        // 封装树形结构
        List<DiagnosisTypeTree> treeBeans = new ArrayList<>();
        list.forEach(t -> {
            DiagnosisTypeTree diagnosisTypeTree = new DiagnosisTypeTree();
            BeanUtils.copyProperties(t, diagnosisTypeTree);
            treeBeans.add(diagnosisTypeTree);
        });
        //获取父节点
        List<DiagnosisTypeTree> collect = diagnosisTypeService.getChildren("0", treeBeans);

        return success(collect);
    }

    @GetMapping("/crumb")
    @ApiOperation("查询诊断分类面包屑")
    public AjaxResult crumb(@RequestParam("id") String id) {
        DiagnosisType service = diagnosisTypeService.getById(id);
        String diagnosisName = service.getDiagnosisName();
        String parentId = service.getParentId();
        String crumbStr = diagnosisTypeService.findCrumb(parentId);
        Object str;
        if (StringUtils.isNotBlank(crumbStr)) {
            str = crumbStr + " > " + diagnosisName;
        } else {
            str = diagnosisName;
        }
        return success(str);
    }

    /**
     * 获取诊断分类详细信息
     */
    @GetMapping(value = "/detail")
    @ApiOperation("获取诊断分类详细信息")
    public AjaxResult detail(String id) {
        return success(diagnosisTypeService.getById(id));
    }

    /**
     * 新增诊断分类
     */
    @Log(title = "新增诊断分类", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增诊断分类")
    public AjaxResult add(@ApiParam(name = "diagnosisType") @Valid @RequestBody DiagnosisType diagnosisType) {
        long count = diagnosisTypeTemplateService.count(Wrappers.<DiagnosisTypeTemplate>lambdaQuery()
                .eq(DiagnosisTypeTemplate::getDiagnosisTypeId, diagnosisType.getParentId()));
        if (count > 0) {
            return error("不允许添加子集");
        }
        return toAjax(diagnosisTypeService.save(diagnosisType));
    }

    /**
     * 修改诊断分类
     */
    @Log(title = "修改诊断分类", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改诊断分类")
    public AjaxResult edit(@ApiParam(name = "diagnosisType") @Valid @RequestBody DiagnosisType diagnosisType) {
        return toAjax(diagnosisTypeService.updateById(diagnosisType));
    }

    /**
     * 删除诊断分类
     */
    @Log(title = "删除诊断分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove")
    @ApiOperation("删除诊断分类")
    public AjaxResult remove(String ids) {
        List<String> idList = Arrays.asList(ids.split(","));
        for (String id : idList) {
            // 判断是否有子集
            long count = diagnosisTypeService.count(Wrappers.<DiagnosisType>lambdaQuery().eq(DiagnosisType::getParentId, id));
            if (count > 0) {
                throw new IllegalArgumentException("请删除子集");
            }
            // 判断是否有模板
            long templateCount = diagnosisTypeTemplateService.count(Wrappers.<DiagnosisTypeTemplate>lambdaQuery()
                    .eq(DiagnosisTypeTemplate::getDiagnosisTypeId, id));
            if (templateCount > 0) {
                throw new IllegalArgumentException("请删除模板");
            }
        }
        return toAjax(diagnosisTypeService.removeBatchByIds(idList));
    }

}
