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

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.NurseGroupRespVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.NurseGroupSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlyInfoDO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.NurseGroupDO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.NurseGroupDetailsDO;
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
import cn.iocoder.yudao.module.system.dal.mysql.biz.ElderlyInfoMapper;
import cn.iocoder.yudao.module.system.dal.mysql.biz.NurseGroupDetailsMapper;
import cn.iocoder.yudao.module.system.dal.mysql.biz.NurseGroupMapper;
import cn.iocoder.yudao.module.system.dal.mysql.user.AdminUserMapper;
import cn.iocoder.yudao.module.system.service.biz.BuildService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;


/**
 * 护理分组 Service 实现类
 *
 * @author 系统管理员
 */
@Service
@Validated
public class NurseGroupServiceImpl implements NurseGroupService {

    @Resource
    private NurseGroupMapper nurseGroupMapper;

    @Resource
    private NurseGroupDetailsMapper nurseGroupDetailsMapper;

    @Resource
    private ElderlyInfoMapper elderlyInfoMapper;

    @Resource
    private BuildService buildService;

    @Resource
    private AdminUserMapper adminUserMapper;


    @Override
    public Long createNurseGroup(NurseGroupSaveReqVO createReqVO) {
        NurseGroupDO nurseGroup = new NurseGroupDO();
        BeanUtils.copyProperties(createReqVO, nurseGroup);
        nurseGroup.setCreatedBy(SecurityFrameworkUtils.getLoginUserNickname());
        nurseGroup.setCreatedTime(new Date());
        nurseGroupMapper.insert(nurseGroup);

        List<NurseGroupDetailsDO> details = createReqVO.getDetails();
        for (NurseGroupDetailsDO detail : details) {
            detail.setCreatedBy(SecurityFrameworkUtils.getLoginUserNickname());
            detail.setCreatedTime(new Date());
            detail.setNurseShift(String.join(",", detail.getNurseShiftNameArray()));
            detail.setNurseShiftIds(String.join(",", detail.getNurseShiftIdArray()));
            detail.setNursingPersonnel(String.join(",", detail.getNursingPersonnelNameArray()));
            detail.setNursingPersonnelIds(String.join(",", detail.getNursingPersonnelIdsArray()));

            detail.setNurseGroupId(nurseGroup.getId());
            nurseGroupDetailsMapper.insert(detail);

            ElderlyInfoDO elderlyInfo = elderlyInfoMapper.selectById(detail.getElderId());
            if (elderlyInfo != null) {
                elderlyInfo.setNurseGroupId(nurseGroup.getId());
                elderlyInfo.setNurseGroupName(nurseGroup.getNurseGroupName());
                elderlyInfoMapper.updateById(elderlyInfo);
            }


        }
        return nurseGroup.getId();
    }

    @Override
    public NurseGroupRespVO getNurseGroup(Long id) {
        NurseGroupRespVO respVO = new NurseGroupRespVO();
        NurseGroupDO nurseGroupDO = nurseGroupMapper.selectById(id);
        BeanUtils.copyProperties(nurseGroupDO, respVO);

        List<NurseGroupDetailsDO> nurseGroupDetailsList = nurseGroupDetailsMapper.selectList(new LambdaQueryWrapperX<NurseGroupDetailsDO>()
                .eq(NurseGroupDetailsDO::getNurseGroupId, id));

        for (NurseGroupDetailsDO nurseGroupDetailsDO : nurseGroupDetailsList) {
            nurseGroupDetailsDO.setNurseShiftIdArray(Arrays.asList(nurseGroupDetailsDO.getNurseShiftIds().split(",")));
            nurseGroupDetailsDO.setNurseShiftNameArray(Arrays.asList(nurseGroupDetailsDO.getNurseShift().split(",")));
            nurseGroupDetailsDO.setNursingPersonnelNameArray(Arrays.asList(nurseGroupDetailsDO.getNursingPersonnel().split(",")));
            nurseGroupDetailsDO.setNursingPersonnelIdsArray(Arrays.asList(nurseGroupDetailsDO.getNursingPersonnelIds().split(",")));
            nurseGroupDetailsDO.setBedName(buildService.getFullBedName(nurseGroupDetailsDO.getElderId()));
        }
        respVO.setDetails(nurseGroupDetailsList);
        return respVO;
    }

    @Override
    public void deleteNurseGroup(Long id) {
        nurseGroupMapper.deleteById(id);

        nurseGroupDetailsMapper.delete(new LambdaQueryWrapperX<NurseGroupDetailsDO>()
                .eq(NurseGroupDetailsDO::getNurseGroupId, id));
    }

    @Override
    public void updateNurseGroup(NurseGroupSaveReqVO updateReqVO) {
        NurseGroupDO nurseGroup = nurseGroupMapper.selectById(updateReqVO.getId());
        BeanUtils.copyProperties(updateReqVO, nurseGroup);
        nurseGroupMapper.updateById(nurseGroup);

        //先删除在创建
        nurseGroupDetailsMapper.delete(new LambdaQueryWrapperX<NurseGroupDetailsDO>()
                .eq(NurseGroupDetailsDO::getNurseGroupId, updateReqVO.getId()));

        //修改护理组详情
        List<NurseGroupDetailsDO> details = updateReqVO.getDetails();
        for (NurseGroupDetailsDO detail : details) {
            detail.setCreatedBy(SecurityFrameworkUtils.getLoginUserNickname());
            detail.setCreatedTime(new Date());
            detail.setNurseShift(String.join(",", detail.getNurseShiftNameArray()));
            detail.setNurseShiftIds(String.join(",", detail.getNurseShiftIdArray()));
            detail.setNursingPersonnel(String.join(",", detail.getNursingPersonnelNameArray()));
            detail.setNursingPersonnelIds(String.join(",", detail.getNursingPersonnelIdsArray()));

            detail.setNurseGroupId(nurseGroup.getId());
            nurseGroupDetailsMapper.insert(detail);
        }
    }

    @Override
    public List<NurseGroupDO> getListByName(String nurseGroupName) {
        return nurseGroupMapper.selectList(new LambdaQueryWrapperX<NurseGroupDO>()
                .like(NurseGroupDO::getNurseGroupName, nurseGroupName));
    }

    @Override
    public List<AdminUserDO> getNurseGroupByElderId(Long elderId) {
        List<NurseGroupDetailsDO> nurseGroupDetails = nurseGroupDetailsMapper.selectList(new LambdaQueryWrapperX<NurseGroupDetailsDO>()
                .eq(NurseGroupDetailsDO::getElderId, elderId));
        List<AdminUserDO> nurseUserList = new ArrayList<>();
        for (NurseGroupDetailsDO nurseGroupDetail : nurseGroupDetails) {
            String[] split = nurseGroupDetail.getNursingPersonnelIds().split(",");
            for (String userId : split) {
                AdminUserDO adminUserDO = adminUserMapper.selectById(userId);
                nurseUserList.add(adminUserDO);
            }
        }
        return nurseUserList;
    }
}