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

import cn.hutool.core.collection.CollectionUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.ByodRegistrationDrugRespVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.ByodRegistrationPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.ByodRegistrationRespVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.ByodRegistrationSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ByodRegistrationDO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ByodRegistrationDrugDO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlyInfoDO;
import cn.iocoder.yudao.module.system.dal.dataobject.sys.SysDrugDictionaryDO;
import cn.iocoder.yudao.module.system.dal.mysql.biz.ByodRegistrationDrugMapper;
import cn.iocoder.yudao.module.system.dal.mysql.biz.ByodRegistrationMapper;
import cn.iocoder.yudao.module.system.dal.mysql.biz.ElderlyInfoMapper;
import cn.iocoder.yudao.module.system.dal.mysql.sys.SysDrugDictionaryMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 长者自带药登记 Service 实现类
 *
 * @author 系统管理员
 */
@Service
@Validated
public class ByodRegistrationServiceImpl implements ByodRegistrationService {


    @Resource
    private BuildService buildService;

    @Resource
    private ElderlyInfoMapper elderlyInfoMapper;

    @Resource
    private ByodRegistrationMapper byodRegistrationMapper;

    @Resource
    private SysDrugDictionaryMapper sysDrugDictionaryMapper;

    @Resource
    private ByodRegistrationDrugMapper byodRegistrationDrugMapper;


    @Override
    public Long createByodRegistration(ByodRegistrationSaveReqVO createReqVO) {
        ByodRegistrationDO byodRegistration = new ByodRegistrationDO();
        BeanUtils.copyProperties(createReqVO, byodRegistration);
        byodRegistration.setCreatedTime(LocalDateTime.now());
        byodRegistration.setCreatedBy(SecurityFrameworkUtils.getLoginUserNickname());
        if (byodRegistration.getFiles().equals("[]")) {
            byodRegistration.setFiles(null);
        }
        byodRegistrationMapper.insert(byodRegistration);

        List<ByodRegistrationDrugDO> drugList = createReqVO.getDrugList();
        for (ByodRegistrationDrugDO byodRegistrationDrugDO : drugList) {
            byodRegistrationDrugDO.setCreatedTime(LocalDateTime.now());
            byodRegistrationDrugDO.setByodRegistrationId(byodRegistration.getId());
            byodRegistrationDrugDO.setCreatedBy(SecurityFrameworkUtils.getLoginUserNickname());
            byodRegistrationDrugMapper.insert(byodRegistrationDrugDO);

            //创建到药品字典里面
            if (byodRegistrationDrugDO.getIsNew()) {
                SysDrugDictionaryDO sysDrugDictionary = new SysDrugDictionaryDO();
                sysDrugDictionary.setDrugName(byodRegistrationDrugDO.getDrugName());
//                sysDrugDictionary.setDrugCode();
//                sysDrugDictionary.setPinYinCode();
//                sysDrugDictionary.setBatch();
//                sysDrugDictionary.setManufacturer();
                sysDrugDictionary.setSpec(byodRegistrationDrugDO.getSpec());
                sysDrugDictionary.setCreatedTime(LocalDateTime.now());
                sysDrugDictionary.setCreatedBy(SecurityFrameworkUtils.getLoginUserNickname());
                sysDrugDictionaryMapper.insert(sysDrugDictionary);
            }
        }
        return byodRegistration.getId();
    }

    @Override
    public ByodRegistrationRespVO getByodRegistration(Long id) {
        ByodRegistrationDO byodRegistrationDO = byodRegistrationMapper.selectById(id);
        ByodRegistrationRespVO respVO = new ByodRegistrationRespVO();
        if (byodRegistrationDO != null) {
            BeanUtils.copyProperties(byodRegistrationDO, respVO);
            List<ByodRegistrationDrugDO> byodRegistrationDrugList = byodRegistrationDrugMapper.selectList(new LambdaQueryWrapperX<ByodRegistrationDrugDO>()
                    .eq(ByodRegistrationDrugDO::getByodRegistrationId, byodRegistrationDO.getId()));
            respVO.setDrugList(byodRegistrationDrugList);


            ElderlyInfoDO elderlyInfoDO = elderlyInfoMapper.selectById(byodRegistrationDO.getElderId());
            respVO.setElderName(elderlyInfoDO.getElderName());
            respVO.setElderSex(elderlyInfoDO.getElderSex());
            respVO.setBedName(buildService.getFullBedName(elderlyInfoDO.getId()));

        }
        return respVO;
    }

    @Override
    public PageResult<ByodRegistrationRespVO> getByodRegistrationPage(ByodRegistrationPageReqVO pageReqVO) {
        Page<ByodRegistrationRespVO> page = new Page<>(pageReqVO.getPageNo(), pageReqVO.getPageSize());
        List<ByodRegistrationRespVO> list = byodRegistrationMapper.getByodRegistrationPage(page, pageReqVO);
        for (ByodRegistrationRespVO respVO : list) {
            respVO.setBedName(buildService.getFullBedName(respVO.getElderId()));
        }
        return new PageResult<>(list, page.getTotal());
    }

    @Override
    public void deleteByodRegistration(List<Long> ids) {
        for (Long id : ids) {
            byodRegistrationMapper.deleteById(id);
            byodRegistrationDrugMapper.delete(new LambdaQueryWrapperX<ByodRegistrationDrugDO>()
                    .eq(ByodRegistrationDrugDO::getByodRegistrationId, id));
        }
    }

    @Override
    public void updateByodRegistration(ByodRegistrationSaveReqVO updateReqVO) {
        ByodRegistrationDO byodRegistration = byodRegistrationMapper.selectById(updateReqVO.getId());
        BeanUtils.copyProperties(updateReqVO, byodRegistration);

        byodRegistrationDrugMapper.delete(new LambdaQueryWrapperX<ByodRegistrationDrugDO>()
                .eq(ByodRegistrationDrugDO::getByodRegistrationId, byodRegistration.getId()));

        List<ByodRegistrationDrugDO> drugList = updateReqVO.getDrugList();
        for (ByodRegistrationDrugDO byodRegistrationDrugDO : drugList) {
            byodRegistrationDrugDO.setId(null);
            byodRegistrationDrugDO.setCreatedTime(LocalDateTime.now());
            byodRegistrationDrugDO.setCreatedBy(SecurityFrameworkUtils.getLoginUserNickname());
            byodRegistrationDrugMapper.insert(byodRegistrationDrugDO);

            //创建到药品字典里面
            if (byodRegistrationDrugDO.getIsNew()) {
                SysDrugDictionaryDO sysDrugDictionary = new SysDrugDictionaryDO();
                sysDrugDictionary.setDrugName(byodRegistrationDrugDO.getDrugName());
//                sysDrugDictionary.setDrugCode();
//                sysDrugDictionary.setPinYinCode();
//                sysDrugDictionary.setBatch();
//                sysDrugDictionary.setManufacturer();
                sysDrugDictionary.setSpec(byodRegistrationDrugDO.getSpec());
                sysDrugDictionary.setCreatedTime(LocalDateTime.now());
                sysDrugDictionary.setCreatedBy(SecurityFrameworkUtils.getLoginUserNickname());
                sysDrugDictionaryMapper.insert(sysDrugDictionary);
            }

        }
    }

    @Override
    public Map<String, Object> validDrugs(List<ByodRegistrationDrugDO> drugList) {
        List<ByodRegistrationDrugDO> collect = drugList.stream().filter(e -> {
            List<SysDrugDictionaryDO> sysDrugDictionaryList = sysDrugDictionaryMapper
                    .selectList(new LambdaQueryWrapperX<SysDrugDictionaryDO>()
                            .eq(SysDrugDictionaryDO::getDrugName, e.getDrugName()));
            if (CollectionUtil.isEmpty(sysDrugDictionaryList)) {
                return true;
            }
            return false;
        }).collect(Collectors.toList());
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("newDrugList", collect);
        dataMap.put("drugName", collect.stream().map(ByodRegistrationDrugDO::getDrugName).collect(Collectors.joining(",")));
        return dataMap;
    }

    @Override
    public PageResult<ByodRegistrationDrugRespVO> getByodRegistrationDrugPage(ByodRegistrationPageReqVO pageReqVO) {
        Page<ByodRegistrationDrugRespVO> page = new Page<>(pageReqVO.getPageNo(), pageReqVO.getPageSize());
        List<ByodRegistrationDrugRespVO> list = byodRegistrationMapper.getByodRegistrationDrugPage(page, pageReqVO);
        return new PageResult<>(list, page.getTotal());
    }

    @Override
    public List<ByodRegistrationDrugDO> getByodRegistrationDrugList(Long elderId) {
        List<ByodRegistrationDO> byodRegistrationList = byodRegistrationMapper.selectList(new LambdaQueryWrapperX<ByodRegistrationDO>()
                .eq(ByodRegistrationDO::getElderId, elderId));

        List<ByodRegistrationDrugDO> dataList = new ArrayList<>();
        for (ByodRegistrationDO byodRegistrationDO : byodRegistrationList) {
            List<ByodRegistrationDrugDO> byodRegistrationDrugs = byodRegistrationDrugMapper.selectList(new LambdaQueryWrapperX<ByodRegistrationDrugDO>()
                    .eq(ByodRegistrationDrugDO::getByodRegistrationId, byodRegistrationDO.getId()));

            dataList.addAll(byodRegistrationDrugs);
        }
        return dataList;
    }

}