package com.openhis.web.check.appservice.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.core.common.core.domain.R;
import com.openhis.check.domain.CheckMethod;
import com.openhis.check.service.ICheckMethodService;
import com.openhis.web.check.appservice.ICheckMethodAppService;
import com.openhis.web.check.dto.CheckMethodDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
public class CheckMethodAppServiceImpl implements ICheckMethodAppService {

    @Resource
    private ICheckMethodService checkMethodService;

    @Override
    public R<?> getCheckMethodList() {
        List<CheckMethod> list = checkMethodService.list();
        return R.ok(list);
    }

    @Override
    public R<?> addCheckMethod(CheckMethodDto checkMethodDto) {
        //1.参数校验
        if(ObjectUtil.isEmpty(checkMethodDto.getName())){
            return R.fail("检查方法名称不能为空");
        }
        if(ObjectUtil.isEmpty(checkMethodDto.getCheckType())){
            return R.fail("检查类型不能为空");
        }
        if(ObjectUtil.isEmpty(checkMethodDto.getCode())){
            return R.fail("代码不能为空");
        }
        //2.判断是否有重复
        LambdaQueryWrapper<CheckMethod> wrapper = new LambdaQueryWrapper<CheckMethod>()
                .eq(CheckMethod::getName, checkMethodDto.getName())
                .eq(CheckMethod::getCheckType, checkMethodDto.getCheckType())
                .eq(CheckMethod::getCode, checkMethodDto.getCode());
        if (checkMethodService.getOne(wrapper) != null){
            return R.fail("检查方法已存在");
        }
        //3.封装实体检查方法
        CheckMethod checkMethod = new CheckMethod();
        checkMethod.setName(checkMethodDto.getName());
        //4.保存添加
        boolean save = checkMethodService.save(checkMethod);
        return R.ok();
    }

    @Override
    public R<?> updateCheckMethod(CheckMethodDto checkMethodDto) {
        //1.参数校验
        if(ObjectUtil.isEmpty(checkMethodDto.getName())){
            return R.fail("检查方法名称不能为空");
        }
        if(ObjectUtil.isEmpty(checkMethodDto.getCheckType())){
            return R.fail("检查类型不能为空");
        }
        if(ObjectUtil.isEmpty(checkMethodDto.getCode())){
            return R.fail("代码不能为空");
        }
        //2.
        return R.ok();
    }

    @Override
    public R<?> removeCheckMethod(Long code) {
        if (ObjectUtil.isEmpty(code)){
            return R.fail("检查方法代码不能为空");
        }
        LambdaQueryWrapper<CheckMethod> wrapper = new LambdaQueryWrapper<CheckMethod>().eq(CheckMethod::getCode, code);
        boolean remove = checkMethodService.remove(wrapper);
        return R.ok(remove);
    }


}
