package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.model.WeChatUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.CpsContentInfo;
import com.ruoyi.system.domain.CpsMeetingEnterpriseTripInfo;
import com.ruoyi.system.domain.CpsMeetingUsr;
import com.ruoyi.system.mapper.CpsMeetingEnterpriseTripInfoMapper;
import com.ruoyi.system.mapper.CpsMeetingUsrMapper;
import com.ruoyi.system.service.ICpsMeetingUsrService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * cps_meeting_usr与会人员信息Service业务层处理
 *
 * @author ruoyi
 * @date 2024-08-18
 */
@Service
public class CpsMeetingUsrServiceImpl implements ICpsMeetingUsrService {
    @Autowired
    private CpsMeetingUsrMapper cpsMeetingUsrMapper;

    @Autowired
    private CpsMeetingEnterpriseTripInfoMapper cpsMeetingTripService;

    /**
     * 查询cps_meeting_usr与会人员信息
     *
     * @param id cps_meeting_usr与会人员信息主键
     * @return cps_meeting_usr与会人员信息
     */
    @Override
    public CpsMeetingUsr selectCpsMeetingUsrById(Long id) {
        return cpsMeetingUsrMapper.selectCpsMeetingUsrById(id);
    }

    /**
     * 查询cps_meeting_usr与会人员信息列表
     *
     * @param cpsMeetingUsr cps_meeting_usr与会人员信息
     * @return cps_meeting_usr与会人员信息
     */
    @Override
    public List<CpsMeetingUsr> selectCpsMeetingUsrList(CpsMeetingUsr cpsMeetingUsr) {
        return cpsMeetingUsrMapper.selectCpsMeetingUsrList(cpsMeetingUsr);
    }

    /**
     * 新增cps_meeting_usr与会人员信息
     *
     * @param cpsMeetingUsr cps_meeting_usr与会人员信息
     * @return 结果
     */
    @Override
    public int insertCpsMeetingUsr(CpsMeetingUsr cpsMeetingUsr) {
        cpsMeetingUsr.setCreateTime(DateUtils.getNowDate());
        return cpsMeetingUsrMapper.insertCpsMeetingUsr(cpsMeetingUsr);
    }

    /**
     * 修改cps_meeting_usr与会人员信息
     *
     * @param cpsMeetingUsr cps_meeting_usr与会人员信息
     * @return 结果
     */
    @Override
    public int updateCpsMeetingUsr(CpsMeetingUsr cpsMeetingUsr) {
        cpsMeetingUsr.setUpdateTime(DateUtils.getNowDate());
        return cpsMeetingUsrMapper.updateCpsMeetingUsr(cpsMeetingUsr);
    }

    /**
     * 批量删除cps_meeting_usr与会人员信息
     *
     * @param ids 需要删除的cps_meeting_usr与会人员信息主键
     * @return 结果
     */
    @Override
    public int deleteCpsMeetingUsrByIds(Long[] ids) {
        return cpsMeetingUsrMapper.deleteCpsMeetingUsrByIds(ids);
    }

    /**
     * 删除cps_meeting_usr与会人员信息信息
     *
     * @param id cps_meeting_usr与会人员信息主键
     * @return 结果
     */
    @Override
    public int deleteCpsMeetingUsrById(Long id) {
        return cpsMeetingUsrMapper.deleteCpsMeetingUsrById(id);
    }

    @Override
    public Boolean existCpsMeetingUsr(String tel) {
        return cpsMeetingUsrMapper.exitUser(tel) > 0;
    }

    @Override
    public CpsMeetingUsr getWeChatUser(WeChatUser wxUser) {
        if (!existCpsMeetingUsr(wxUser.getTel())) {
            CpsMeetingUsr cpsMeetingUsr = new CpsMeetingUsr() {{
                setUsrName(wxUser.getNickName());
                setOpenId(wxUser.getOpenid());
                setTel(wxUser.getTel());
                setAvatarUrl(wxUser.getAvatarUrl());
            }};
            insertCpsMeetingUsr(cpsMeetingUsr);
            return cpsMeetingUsr;
        }
        return selectCpsMeetingUsrList(new CpsMeetingUsr() {{
            setTel(wxUser.getTel());
        }}).get(0);
    }

    @Override
    public void usrRegister(CpsMeetingUsr user) {
        CpsMeetingUsr cpsMeetingUsr = cpsMeetingUsrMapper.qryUsrByTel(user.getTel());
        if (ObjectUtils.isNotEmpty(cpsMeetingUsr)) {
            cpsMeetingUsrMapper.updateCpsMeetingUsrByTel(user);
            return;
        }
        insertCpsMeetingUsr(user);
    }

    @Override
    public List<CpsContentInfo> qryUsrCollectContent(String openId, String keyword) {
        return cpsMeetingUsrMapper.qryUsrCollectContent(openId, keyword);
    }

    @Override
    public CpsMeetingEnterpriseTripInfo qryUsrTripContent(String openId) {
        CpsMeetingUsr cpsMeetingUsr = cpsMeetingUsrMapper.selectCpsMeetingUsrByOpenId(openId);
        if (ObjectUtils.isEmpty(cpsMeetingUsr)) {
            return null;
        }
        return cpsMeetingTripService.selectCpsMeetingEnterpriseTripInfoById(cpsMeetingUsr.getTripId());
    }
}
