package cn.iocoder.yudao.module.system.service.sys;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.system.controller.admin.sys.vo.DrugDictionaryPageReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.sys.SysDrugDictionaryDO;
import cn.iocoder.yudao.module.system.dal.mysql.sys.SysDrugDictionaryMapper;
import com.mzt.logapi.context.LogRecordContext;
import com.mzt.logapi.starter.annotation.LogRecord;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.module.system.enums.LogRecordConstants.*;


/**
 * 药品字典 Service 实现类
 *
 * @author 系统管理员
 */
@Service
@Validated
public class SysDrugDictionaryServiceImpl implements SysDrugDictionaryService {

    @Resource
    private SysDrugDictionaryMapper sysDrugDictionaryMapper;

    @Override
    @LogRecord(type = DRUG_DICTIONARY_TYPE, subType = DRUG_DICTIONARY_CREATE_SUB_TYPE, bizNo = "{{#drug.drugId}}",
            success = DRUG_DICTIONARY_CREATE_SUCCESS)
    public void createDrugDictionary(SysDrugDictionaryDO sysDrugDictionaryDO) {
        sysDrugDictionaryDO.setCreatedBy(SecurityFrameworkUtils.getLoginUserNickname());
        sysDrugDictionaryDO.setCreatedTime(LocalDateTime.now());
        sysDrugDictionaryMapper.insert(sysDrugDictionaryDO);
        LogRecordContext.putVariable("drug", sysDrugDictionaryDO);
    }

    @Override
    @LogRecord(type = DRUG_DICTIONARY_TYPE, subType = DRUG_DICTIONARY_UPDATE_SUB_TYPE, bizNo = "{{#drug.drugId}}",
            success = DRUG_DICTIONARY_UPDATE_SUCCESS)
    public void updateDrugDictionary(SysDrugDictionaryDO sysDrugDictionaryDO) {
        sysDrugDictionaryMapper.updateById(sysDrugDictionaryDO);
        LogRecordContext.putVariable("drug", sysDrugDictionaryDO);
    }

    @Override
    @LogRecord(type = DRUG_DICTIONARY_TYPE, subType = DRUG_DICTIONARY_DELETE_SUB_TYPE, bizNo = "{{#drugId}}",
            success = DRUG_DICTIONARY_DELETE_SUCCESS)
    public void deleteDrugDictionary(List<Long> ids) {
        List<SysDrugDictionaryDO> sysDrugDictionaryList = sysDrugDictionaryMapper.selectBatchIds(ids);
        for (Long id : ids) {
            sysDrugDictionaryMapper.deleteById(id);
        }
        LogRecordContext.putVariable("drugId", ids.stream().findFirst().get());
        LogRecordContext.putVariable("removeDrugName", String.join(",", sysDrugDictionaryList
                .stream()
                .map(SysDrugDictionaryDO::getDrugName)
                .collect(Collectors.toList())));

    }

    @Override
    @LogRecord(type = DRUG_DICTIONARY_TYPE, subType = DRUG_DICTIONARY_QUERY_SUB_TYPE, bizNo = "{{#drug.drugId}}",
            success = DRUG_DICTIONARY_QUERY_SUCCESS)
    public SysDrugDictionaryDO getDrugDictionary(Long drugId) {
        SysDrugDictionaryDO sysDrugDictionaryDO = sysDrugDictionaryMapper.selectById(drugId);
        LogRecordContext.putVariable("drug", sysDrugDictionaryDO);
        return sysDrugDictionaryDO;
    }

    @Override
    public PageResult<SysDrugDictionaryDO> getDrugDictionaryPage(DrugDictionaryPageReqVO pageReqVO) {
        return sysDrugDictionaryMapper.selectPage(pageReqVO);
    }


}