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.CollectCostDetails;
import com.eitc.patient.domain.CollectCostType;
import com.eitc.patient.service.ICollectCostDetailsService;
import com.eitc.patient.service.ICollectCostTypeService;
import com.eitc.patient.tree.CollectCostTypeTree;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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;
import java.util.Objects;

/**
 * CollectCostTypeController
 *
 * @author mtx
 * @date: 2024/05/17 15:51
 */
@Api(tags = "收费分类")
@RestController
@RequestMapping("/collect-cost-type")
public class CollectCostTypeController extends BaseController {

    @Resource
    private ICollectCostTypeService collectCostTypeService;
    @Resource
    private ICollectCostDetailsService collectCostDetailsService;

    /**
     * 收费分类
     */
    @GetMapping("/list")
    @ApiOperation("查询收费分类")
    public AjaxResult list(CollectCostType collectCostType) {
        return success(collectCostTypeService.getList(collectCostType));
    }

    @GetMapping("/tree")
    @ApiOperation("查询收费分类树")
    public AjaxResult tree(CollectCostType collectCostType) {
        List<CollectCostType> list = collectCostTypeService.getList(collectCostType);
        // 封装树形结构
        List<CollectCostTypeTree> treeBeans = new ArrayList<>();
        list.forEach(t -> {
            CollectCostTypeTree tree = new CollectCostTypeTree();
            BeanUtils.copyProperties(t, tree);
            treeBeans.add(tree);
        });
        //获取父节点
        List<CollectCostTypeTree> collect = collectCostTypeService.getChildren("0", treeBeans);

        return success(collect);
    }

    @GetMapping("/crumb")
    @ApiOperation("查询收费分类面包屑")
    public AjaxResult crumb(@RequestParam("id") String id) {
        CollectCostType collectCostType = collectCostTypeService.getById(id);
        Object str = "";
        if (Objects.isNull(collectCostType)) {
            return success(str);
        }
        String costTypeName = collectCostType.getCostTypeName();
        String parentId = collectCostType.getParentId();
        String crumbStr = collectCostTypeService.findCrumb(parentId);
        if (StringUtils.isNotBlank(crumbStr)) {
            str = crumbStr + " > " + costTypeName;
        } else {
            str = costTypeName;
        }
        return success(str);
    }

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

    /**
     * 新增收费分类
     */
    @Log(title = "新增收费分类", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增收费分类")
    public AjaxResult add(@Valid @RequestBody CollectCostType collectCostType) {
        long count = collectCostDetailsService.count(Wrappers.<CollectCostDetails>lambdaQuery()
                .eq(CollectCostDetails::getCollectCostTypeId, collectCostType.getParentId()));
        if (count > 0) {
            return error("不允许添加子集");
        }
        return toAjax(collectCostTypeService.save(collectCostType));
    }

    /**
     * 修改收费分类
     */
    @Log(title = "修改收费分类", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改收费分类")
    public AjaxResult edit(@Valid @RequestBody CollectCostType collectCostType) {
        return toAjax(collectCostTypeService.updateById(collectCostType));
    }

    /**
     * 删除收费分类
     */
    @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 = collectCostTypeService.count(Wrappers.<CollectCostType>lambdaQuery().eq(CollectCostType::getParentId, id));
            if (count > 0) {
                throw new IllegalArgumentException("请删除子集");
            }
            // 判断是否有模板
            long templateCount = collectCostDetailsService.count(Wrappers.<CollectCostDetails>lambdaQuery()
                    .eq(CollectCostDetails::getCollectCostTypeId, id));
            if (templateCount > 0) {
                throw new IllegalArgumentException("请删除模板");
            }
        }
        return toAjax(collectCostTypeService.removeBatchByIds(idList));
    }

}
