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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.core.common.utils.ChineseConvertUtils;
import com.core.common.utils.MessageUtils;
import com.core.common.utils.StringUtils;
import com.openhis.administration.domain.Account;
import com.openhis.administration.domain.Location;
import com.openhis.administration.mapper.LocationMapper;
import com.openhis.administration.service.ILocationService;
import com.openhis.clinical.domain.ConditionNoDefinition;
import com.openhis.clinical.mapper.ConditionNoDefinitionMapper;
import com.openhis.clinical.service.IConditionNoDefinitionService;
import com.openhis.common.constant.CommonConstants;
import com.openhis.common.constant.PromptMsgConstant;
import com.openhis.common.enums.*;
import com.openhis.common.utils.HisPageUtils;
import com.openhis.common.utils.HisQueryUtils;
import com.openhis.web.basedatamanage.dto.LocationAddOrEditDto;
import com.openhis.web.basedatamanage.dto.LocationInfoDto;
import com.openhis.web.datadictionary.dto.DeviceManageDto;
import com.openhis.web.datadictionary.dto.ICDCodeAddDto;
import com.openhis.web.datadictionary.dto.ICDCodeDto;
import com.openhis.web.datadictionary.dto.ICDCodeUpdateDto;
import com.openhis.workflow.domain.SupplyRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.core.common.core.domain.R;
import com.openhis.web.datadictionary.appservice.ICDCodeService;

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

@Service
public class ICDCodeServiceImpl implements ICDCodeService {

    @Resource
    private ConditionNoDefinitionMapper conditionNoDefinitionMapper;

    @Resource
    private ICDCodeService icdCodeService;

    @Resource
    private IConditionNoDefinitionService conditionNoDefinitionService;

    /**
     *
     * @param searchKey
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public R<?> getICDCodePage(String searchKey, Integer pageNo, Integer pageSize) {
        // 构建查询条件
        QueryWrapper<ConditionNoDefinition> queryWrapper = HisQueryUtils.buildQueryWrapper(null,searchKey,
                new HashSet<>(Arrays.asList("gl_no", "gl_name", "icd10_no", "icd10_name")), null);
        Page<ICDCodeDto> icdCodeDtoPage =
                HisPageUtils.selectPage(conditionNoDefinitionMapper, queryWrapper, pageNo, pageSize, ICDCodeDto.class);
        return R.ok(icdCodeDtoPage);
    }

    /**
     * 添加
     * @param icdCodeAddDto
     * @return
     */
    @Override
    public R<?> addICDInformation(ICDCodeAddDto icdCodeAddDto) {
        ConditionNoDefinition conditionNoDefinition = new ConditionNoDefinition();
        conditionNoDefinition.setIcd10No(icdCodeAddDto.getIcd10No());
        conditionNoDefinition.setIcd10Name(icdCodeAddDto.getIcd10Name());
        conditionNoDefinition.setGlNo(icdCodeAddDto.getGlNo());
        conditionNoDefinition.setGlName(icdCodeAddDto.getGlName());
        boolean result = conditionNoDefinitionService.save(conditionNoDefinition);
        if(result){
            return R.ok("ok");
        }
        return R.fail("no");
    }

    /**
     * 删除
     * @param glNo
     * @return
     */
    @Override
    public R<?> deleteICDInformation(String glNo) {
        boolean result =
                conditionNoDefinitionService.remove(new LambdaQueryWrapper<ConditionNoDefinition>().likeRight(ConditionNoDefinition::getGlNo, glNo));
        return result ? R.ok(null, MessageUtils.createMessage(PromptMsgConstant.Common.M00005, new Object[] {"国临编码信息删除"}))
                : R.fail(MessageUtils.createMessage(PromptMsgConstant.Common.M00006, new Object[] {"国临编码信息删除"}));
    }

    @Override
    public R<?> saveOrUpdateICDInformation(ICDCodeUpdateDto icdCodeUpdateDto) {
        ConditionNoDefinition conditionNoDefinition = new ConditionNoDefinition();
        conditionNoDefinition.setId(icdCodeUpdateDto.getId());
        conditionNoDefinition.setIcd10No(icdCodeUpdateDto.getIcd10No());
        conditionNoDefinition.setIcd10Name(icdCodeUpdateDto.getIcd10Name());
        conditionNoDefinition.setGlNo(icdCodeUpdateDto.getGlNo());
        conditionNoDefinition.setGlName(icdCodeUpdateDto.getGlName());
        boolean result=conditionNoDefinitionService.saveOrUpdate(conditionNoDefinition);
        if(result){
            return R.ok("ok");
        }
        return R.fail("no");
    }


}
