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.patient.domain.ProtocolTemplate;
import com.eitc.patient.domain.ProtocolType;
import com.eitc.patient.service.IProtocolTemplateService;
import com.eitc.patient.service.IProtocolTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
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.RestController;

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

/**
 * ProtocolTypeController
 *
 * @author mtx
 * @date: 2024/05/17 15:51
 */
@Api(tags = "协议类型配置")
@RestController
@RequestMapping("/protocol-type")
public class ProtocolTypeController extends BaseController {

    @Resource
    private IProtocolTypeService protocolTypeService;
    @Resource
    private IProtocolTemplateService protocolTemplateService;

    /**
     * 查询协议类型
     */
    @GetMapping("/list")
    @ApiOperation("查询协议类型")
    public AjaxResult list() {
        return success(protocolTypeService.list(Wrappers.<ProtocolType>lambdaQuery().orderByDesc(ProtocolType::getCreateDate)));
    }

    /**
     * 新增协议类型
     */
    @Log(title = "新增协议类型", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增协议类型")
    public AjaxResult add(@ApiParam(name = "protocolType") @Valid @RequestBody ProtocolType protocolType) {
        return toAjax(protocolTypeService.save(protocolType));
    }

    /**
     * 修改协议类型
     */
    @Log(title = "修改协议类型", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改协议类型")
    public AjaxResult edit(@ApiParam(name = "protocolType") @Valid @RequestBody ProtocolType protocolType) {
        return toAjax(protocolTypeService.updateById(protocolType));
    }

    /**
     * 删除协议类型
     */
    @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 = protocolTemplateService.count(Wrappers.<ProtocolTemplate>lambdaQuery().eq(ProtocolTemplate::getProtocolType, id));
            if (count > 0) {
                throw new IllegalArgumentException("请删除模板");
            }
        }
        return toAjax(protocolTypeService.removeBatchByIds(idList));
    }

}
