package com.eitc.patient.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
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.core.page.TableDataInfo;
import com.eitc.common.enums.BusinessType;
import com.eitc.common.utils.bean.BeanUtils;
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 com.eitc.patient.vo.ProtocolTemplateVO;
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.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;

/**
 * ProtocolTemplateController
 *
 * @author mtx
 * @date: 2024/05/17 15:51
 */
@Api(tags = "协议模板")
@RestController
@RequestMapping("/protocol-template")
public class ProtocolTemplateController extends BaseController {

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

    /**
     * 查询协议模板列表
     */
    @GetMapping("/list")
    @ApiOperation("查询协议模板列表")
    public TableDataInfo list(ProtocolTemplate protocolTemplate) {
        List<ProtocolTemplateVO> voList = new ArrayList<>();
        startPage();
        LambdaQueryWrapper<ProtocolTemplate> wrapper = Wrappers.<ProtocolTemplate>lambdaQuery()
                .eq(ProtocolTemplate::getProtocolType, protocolTemplate.getProtocolType())
                .like(StringUtils.isNotBlank(protocolTemplate.getProtocolTemplateName()),
                        ProtocolTemplate::getProtocolTemplateName, protocolTemplate.getProtocolTemplateName());
        wrapper.orderByDesc(ProtocolTemplate::getCreateDate);
        List<ProtocolTemplate> list = protocolTemplateService.list(wrapper);
        if (CollectionUtils.isNotEmpty(list)) {
            ProtocolType protocolType = protocolTypeService.getById(protocolTemplate.getProtocolType());
            if (Objects.isNull(protocolType)) {
                protocolType = new ProtocolType();
            }
            for (ProtocolTemplate template : list) {
                ProtocolTemplateVO vo = new ProtocolTemplateVO();
                BeanUtils.copyProperties(template, vo);
                vo.setProtocolTypeCn(protocolType.getProtocolName());
                voList.add(vo);
            }
        }

        return getDataTable(voList);
    }

    /**
     * 获取协议模板详细信息
     */
    @GetMapping(value = "/detail")
    @ApiOperation("获取协议模板详细信息")
    public AjaxResult detail(String id) {
        return success(protocolTemplateService.getById(id));
    }

    /**
     * 新增协议模板
     */
    @Log(title = "新增协议模板", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增协议模板")
    public AjaxResult add(@ApiParam(name = "protocolTemplate") @Valid @RequestBody ProtocolTemplate protocolTemplate) {
        return toAjax(protocolTemplateService.save(protocolTemplate));
    }

    /**
     * 修改协议模板
     */
    @Log(title = "修改协议模板", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改协议模板")
    public AjaxResult edit(@ApiParam(name = "protocolTemplate") @Valid @RequestBody ProtocolTemplate protocolTemplate) {
        return toAjax(protocolTemplateService.updateById(protocolTemplate));
    }

    /**
     * 删除协议模板
     */
    @Log(title = "删除协议模板", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove")
    @ApiOperation("删除协议模板")
    public AjaxResult remove(String ids) {
        List<String> idList = Arrays.asList(ids.split(","));
        return toAjax(protocolTemplateService.removeBatchByIds(idList));
    }

    /**
     * 知情同意书
     */
    @GetMapping("/informed-consent-form")
    @ApiOperation("知情同意书")
    public AjaxResult informedConsentForm() {
        String typeId = protocolTypeService.getProtocolTypes("知情同意书");
        List<ProtocolTemplate> protocolTemplateList = protocolTemplateService.list(Wrappers.<ProtocolTemplate>lambdaQuery()
                .eq(ProtocolTemplate::getProtocolType, typeId)
                .orderByDesc(ProtocolTemplate::getCreateDate));
        return success(protocolTemplateList);
    }

}
