package com.flossom.system.service.impl;

import com.flossom.common.core.domain.entity.WxNursingLog;
import com.flossom.common.core.domain.entity.WxUserInstrument;
import com.flossom.common.core.domain.entity.WxUserMember;
import com.flossom.common.core.domain.entity.WxUserTag;
import com.flossom.common.core.domain.req.UserNursingLogReq;
import com.flossom.common.core.domain.req.UserNursingLogVm;
import com.flossom.common.core.domain.req.WxUserMemberVm;
import com.flossom.common.core.enums.BindingStatusEnums;
import com.flossom.common.core.enums.TagTypeStatusEnum;
import com.flossom.common.core.mapper.WxNursingLogMapper;
import com.flossom.common.core.mapper.WxUserInstrumentMapper;
import com.flossom.common.core.mapper.WxUserMemberMapper;
import com.flossom.common.core.mapper.WxUserTagMapper;
import com.flossom.common.core.utils.DateUtils;
import com.flossom.common.core.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.flossom.system.service.IWxNursingLogService;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户护理日志Service业务层处理
 *
 * @author flossom
 * @date 2024-02-21
 */
@Service
public class WxNursingLogServiceImpl implements IWxNursingLogService {

    @Autowired
    private WxNursingLogMapper wxNursingLogMapper;

    @Autowired
    private WxUserMemberMapper wxUserMemberMapper;

    @Autowired
    private WxUserTagMapper wxUserTagMapper;

    @Autowired
    private WxUserInstrumentMapper wxUserInstrumentMapper;

    /**
     * 查询用户护理日志
     *
     * @param id 用户护理日志主键
     * @return 用户护理日志
     */
    @Override
    public WxNursingLog selectWxNursingLogById(Long id) {
        return wxNursingLogMapper.selectWxNursingLogById(id);
    }

    /**
     * 查询用户护理日志列表
     */
    @Override
    public List<WxNursingLog> selectWxNursingLogList(UserNursingLogReq userNursingLogReq, boolean isPage) {
        // 查询条件
        UserNursingLogVm userNursingLogVm = new UserNursingLogVm();
        WxUserMemberVm wxUserMemberVm = new WxUserMemberVm();
        if (!(userNursingLogReq.getUserIdList() == null && userNursingLogReq.getNickname() == null
                && userNursingLogReq.getMobile() == null && userNursingLogReq.getProvinceId() == null
                && userNursingLogReq.getCityId() == null && userNursingLogReq.getAreaId() == null
                && userNursingLogReq.getWecomTagList() == null && userNursingLogReq.getMiniProgramTagList() == null)) {
            wxUserMemberVm.setId(userNursingLogReq.getUserIdList());
            wxUserMemberVm.setNickname(userNursingLogReq.getNickname());
            wxUserMemberVm.setMobile(userNursingLogReq.getMobile());
            wxUserMemberVm.setProvinceId(userNursingLogReq.getProvinceId());
            wxUserMemberVm.setCityId(userNursingLogReq.getCityId());
            wxUserMemberVm.setAreaId(userNursingLogReq.getAreaId());
            if (userNursingLogReq.getWecomTagList() != null && userNursingLogReq.getWecomTagList().size() > 0) {
                wxUserMemberVm.setWecomTags(userNursingLogReq.getWecomTagList().stream().map(Long::intValue).collect(Collectors.toList()));
            }
            if (userNursingLogReq.getMiniProgramTagList() != null && userNursingLogReq.getMiniProgramTagList().size() > 0) {
                wxUserMemberVm.setMiniProgramTags(userNursingLogReq.getMiniProgramTagList().stream().map(Long::intValue).collect(Collectors.toList()));
            }
            List<WxUserMember> wxUserMemberList = wxUserMemberMapper.selectWxUserMemberListByVm(wxUserMemberVm);

            if (wxUserMemberList != null && wxUserMemberList.size() > 0) {
                userNursingLogVm.setUserIdList(wxUserMemberList.stream().map(WxUserMember::getId).collect(Collectors.toList()));
            } else {
                userNursingLogVm.setUserIdList(Arrays.asList(0L));
            }
        }
        userNursingLogVm.setInstrumentIdList(userNursingLogReq.getInstrumentIdList());
        userNursingLogVm.setModeIdList(userNursingLogReq.getModeIdList());
        userNursingLogVm.setNursingTimeRange(userNursingLogReq.getNursingTimeRange());
        userNursingLogVm.setUpdateTimeRange(userNursingLogReq.getUpdateTimeRange());

        if(isPage) {
            PageUtils.startPage();
        }
        List<WxNursingLog> wxNursingLogList = wxNursingLogMapper.selectNursingLogListByVm(userNursingLogVm);
        if (wxNursingLogList != null && wxNursingLogList.size() > 0) {
            wxNursingLogList.forEach(wxNursingLog -> {
                WxUserMember wxUserMember = wxUserMemberMapper.selectWxUserMemberById(wxNursingLog.getUserId());
                if (wxUserMember != null) {
                    // 用户信息
                    wxNursingLog.setHeadimg(wxUserMember.getHeadimg());
                    wxNursingLog.setMobile(wxUserMember.getMobile());
                    wxNursingLog.setNickname(wxUserMember.getNickname());
                    wxNursingLog.setUserMemberCreateTime(wxUserMember.getCreateTime());
                    // 查询标签
                    WxUserTag wxUserTag = new WxUserTag();
                    wxUserTag.setUserId(wxUserMember.getId());
                    wxUserTag.setType(TagTypeStatusEnum.MINI_PROGRAM.getCode());
                    List<WxUserTag> miniProgramTagList = wxUserTagMapper.selectWxUserTagList(wxUserTag);
                    if (miniProgramTagList != null && miniProgramTagList.size() > 0) {
                        wxNursingLog.setMiniProgramTagListStr(miniProgramTagList.stream().map(WxUserTag::getTagName).collect(Collectors.joining(",")));
                    }
                    wxUserTag.setType(TagTypeStatusEnum.ENTERPRISE_WECHAT.getCode());
                    List<WxUserTag> wecomTagList = wxUserTagMapper.selectWxUserTagList(wxUserTag);
                    if (wecomTagList != null && wecomTagList.size() > 0) {
                        wxNursingLog.setWecomTagListStr(wecomTagList.stream().map(WxUserTag::getTagName).collect(Collectors.joining(",")));
                    }
                    // 仪器绑定时间
                    WxUserInstrument query = new WxUserInstrument();
                    query.setUserId(wxUserMember.getId());
                    query.setInstrumentId(wxNursingLog.getInstrumentId());
                    query.setBindingStatus(BindingStatusEnums.BINDED.getCode());
                    List<WxUserInstrument> wxUserInstrumentList = wxUserInstrumentMapper.selectListByUserIdAndInstrumentId(query);
                    if (wxUserInstrumentList != null && wxUserInstrumentList.size() == 1) {
                        WxUserInstrument wxUserInstrument = wxUserInstrumentList.get(0);
                        wxNursingLog.setInstrumentBindingTime(wxUserInstrument.getUpdateTime());
                    }
                }
            });
        }
        return wxNursingLogList;
    }

    /**
     * 新增用户护理日志
     *
     * @param wxNursingLog 用户护理日志
     * @return 结果
     */
    @Override
    public int insertWxNursingLog(WxNursingLog wxNursingLog) {
        wxNursingLog.setCreateTime(DateUtils.getNowDate());
        return wxNursingLogMapper.insertWxNursingLog(wxNursingLog);
    }

    /**
     * 修改用户护理日志
     *
     * @param wxNursingLog 用户护理日志
     * @return 结果
     */
    @Override
    public int updateWxNursingLog(WxNursingLog wxNursingLog) {
        wxNursingLog.setUpdateTime(DateUtils.getNowDate());
        return wxNursingLogMapper.updateWxNursingLog(wxNursingLog);
    }

    /**
     * 批量删除用户护理日志
     *
     * @param ids 需要删除的用户护理日志主键
     * @return 结果
     */
    @Override
    public int deleteWxNursingLogByIds(Long[] ids) {
        return wxNursingLogMapper.deleteWxNursingLogByIds(ids);
    }

    /**
     * 删除用户护理日志信息
     *
     * @param id 用户护理日志主键
     * @return 结果
     */
    @Override
    public int deleteWxNursingLogById(Long id) {
        return wxNursingLogMapper.deleteWxNursingLogById(id);
    }


    @Override
    public List<WxNursingLog> selectWxNursingLogByIdList(List<Integer> idList) {
        List<WxNursingLog> wxNursingLogList = wxNursingLogMapper.selectWxNursingLogByIdList(idList);
        if (wxNursingLogList != null && wxNursingLogList.size() > 0) {
            wxNursingLogList.forEach(wxNursingLog -> {
                WxUserMember wxUserMember = wxUserMemberMapper.selectWxUserMemberById(wxNursingLog.getUserId());
                if (wxUserMember != null) {
                    // 用户信息
                    wxNursingLog.setHeadimg(wxUserMember.getHeadimg());
                    wxNursingLog.setMobile(wxUserMember.getMobile());
                    wxNursingLog.setNickname(wxUserMember.getNickname());
                    wxNursingLog.setUserMemberCreateTime(wxUserMember.getCreateTime());
                    // 查询标签
                    WxUserTag wxUserTag = new WxUserTag();
                    wxUserTag.setUserId(wxUserMember.getId());
                    wxUserTag.setType(TagTypeStatusEnum.MINI_PROGRAM.getCode());
                    List<WxUserTag> miniProgramTagList = wxUserTagMapper.selectWxUserTagList(wxUserTag);
                    if (miniProgramTagList != null && miniProgramTagList.size() > 0) {
                        wxNursingLog.setMiniProgramTagListStr(miniProgramTagList.stream().map(WxUserTag::getTagName).collect(Collectors.joining(",")));
                    }
                    wxUserTag.setType(TagTypeStatusEnum.ENTERPRISE_WECHAT.getCode());
                    List<WxUserTag> wecomTagList = wxUserTagMapper.selectWxUserTagList(wxUserTag);
                    if (wecomTagList != null && wecomTagList.size() > 0) {
                        wxNursingLog.setWecomTagListStr(wecomTagList.stream().map(WxUserTag::getTagName).collect(Collectors.joining(",")));
                    }
                    // 仪器绑定时间
                    WxUserInstrument query = new WxUserInstrument();
                    query.setUserId(wxUserMember.getId());
                    query.setInstrumentId(wxNursingLog.getInstrumentId());
                    query.setBindingStatus(BindingStatusEnums.BINDED.getCode());
                    List<WxUserInstrument> wxUserInstrumentList = wxUserInstrumentMapper.selectListByUserIdAndInstrumentId(query);
                    if (wxUserInstrumentList != null && wxUserInstrumentList.size() == 1) {
                        WxUserInstrument wxUserInstrument = wxUserInstrumentList.get(0);
                        wxNursingLog.setInstrumentBindingTime(wxUserInstrument.getUpdateTime());
                    }
                }
            });
        }
        return wxNursingLogList;
    }
}
