package com.zhongshu.card.server.core.service.tip;

import com.github.microservice.components.data.base.util.PageEntityUtil;
import com.github.microservice.net.ResultContent;
import com.github.microservice.net.ResultMessage;
import com.zhongshu.card.client.model.tip.TipInfoModel;
import com.zhongshu.card.client.model.tip.TipInfoParam;
import com.zhongshu.card.client.model.tip.TipInfoSearch;
import com.zhongshu.card.client.model.tip.TipStaticModel;
import com.zhongshu.card.client.utils.DateUtils;
import com.zhongshu.card.server.core.dao.org.UserCountDao;
import com.zhongshu.card.server.core.dao.tip.TipInfoDao;
import com.zhongshu.card.server.core.domain.org.UserAccount;
import com.zhongshu.card.server.core.domain.tip.TipInfo;
import com.zhongshu.card.server.core.service.base.CommonService;
import com.zhongshu.card.server.core.service.base.SuperService;
import com.zhongshu.card.server.core.service.user.UserAccountServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author TRX
 * @date 2025/2/19
 */
@Slf4j
@Service
public class TipInfoService extends SuperService {

    @Autowired
    private TipInfoDao tipInfoDao;

    @Autowired
    private UserAccountServiceImpl userAccountService;

    @Autowired
    private UserCountDao userCountDao;

    @Autowired
    private CommonService commonService;

    /**
     * 保存访客申请
     *
     * @param param
     * @return
     */
    public ResultContent saveInfo(TipInfoParam param) {
        TipInfo entity = new TipInfo();
        BeanUtils.copyProperties(param, entity);
        if (StringUtils.isNotEmpty(param.getFromUserId())) {
            UserAccount fromUserAccount = userCountDao.findTopByUserId(param.getFromUserId());
            entity.setFromUserAccount(fromUserAccount);
            if (ObjectUtils.isNotEmpty(fromUserAccount)) {
                entity.setFromUserName(fromUserAccount.getName());
            }
        }
        if (StringUtils.isNotEmpty(param.getFromUserId())) {
            UserAccount toUserAccount = userCountDao.findTopByUserId(param.getToUserId());
            entity.setToUserAccount(toUserAccount);
            if (ObjectUtils.isNotEmpty(toUserAccount)) {
                entity.setToUserName(toUserAccount.getName());
            }
        }
        tipInfoDao.save(entity);
        return ResultContent.buildSuccess();
    }

    public ResultContent getInfo(String id) {
        TipInfo entity = tipInfoDao.findTopById(id);
        if (entity == null) {
            return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, id));
        }
        return ResultContent.buildSuccess(toModel(entity));
    }

    /**
     * 删除数据
     *
     * @param id
     * @return
     */
    public ResultContent deleteInfo(String id) {
        TipInfo entity = tipInfoDao.findTopById(id);
        if (entity == null) {
            return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, id));
        }
        tipInfoDao.delete(entity);
        return ResultContent.buildSuccess();
    }

    /**
     * 分页列表
     *
     * @param param
     * @param pageable
     * @return
     */
    public ResultContent<Page<TipInfoModel>> page(TipInfoSearch param, Pageable pageable) {
        Page<TipInfo> page = tipInfoDao.page(pageable, param);
        if (param.getIsDesen() != null && param.getIsDesen()) {
            return ResultContent.buildSuccess(PageEntityUtil.concurrent2PageModel(page, this::toDesenModel));
        }
        return ResultContent.buildSuccess(PageEntityUtil.concurrent2PageModel(page, this::toModel));
    }

    public ResultContent markRead(String id) {
        TipInfo entity = tipInfoDao.findTopById(id);
        if (entity == null) {
            return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, id));
        }
        entity.setIsRead(Boolean.TRUE);
        entity.setReadTime(System.currentTimeMillis());
        entity.setReadTimeStr(DateUtils.paresTime(System.currentTimeMillis(), DateUtils.FORMAT_LONG));
        return ResultContent.buildSuccess(toModel(entity));
    }

    /**
     * 标记当前用户所有份已读
     *
     * @return
     */
    public ResultContent markCurrentAllRead() {
        return markUserAllRead(getCurrentUserId());
    }

    /**
     * 标记用户所有的未读 为已读
     *
     * @param userId
     * @return
     */
    private ResultContent markUserAllRead(String userId) {
        java.util.Map<String, Object> where = new java.util.HashMap<>();
        where.put("isRead", Boolean.FALSE);
        where.put("toUserId", userId);

        Map<String, Object> standardData = new HashMap<String, Object>();
        standardData.put("isRead", Boolean.TRUE);
        standardData.put("readTime", System.currentTimeMillis());
        standardData.put("readTimeStr", DateUtils.paresTime(System.currentTimeMillis(), DateUtils.FORMAT_LONG));
        commonService.updateData(where, standardData, TipInfo.class.getSimpleName());
        return ResultContent.buildSuccess();
    }

    public ResultContent staticCurrentUnRead(String projectOid) {
        if (StringUtils.isEmpty(projectOid)) {
            projectOid = getCurrentProjectOid();
        }
        if (StringUtils.isEmpty(projectOid)) {
            return ResultContent.buildFail("项目oid信息为空");
        }
        TipInfoSearch param = new TipInfoSearch();
        param.setProjectOid(projectOid);
        param.setIsRead(Boolean.FALSE);
        param.setToUserId(getCurrentUserId());
        return staticTipCount(param);
    }

    /**
     * 统计数量
     *
     * @return
     */
    public ResultContent staticTipCount(TipInfoSearch param) {
        TipStaticModel model = new TipStaticModel();
        Long unReadCount = tipInfoDao.count(param);
        model.setUnReadCount(unReadCount);
        return ResultContent.buildSuccess(model);
    }

    public TipInfoModel toDesenModel(TipInfo entity) {
        TipInfoModel model = null;
        if (ObjectUtils.isNotEmpty(entity)) {
            model = new TipInfoModel();
            BeanUtils.copyProperties(entity, model);
            model.setFromUserAccount(userAccountService.toSimpleDesenModel(entity.getFromUserAccount()));
            model.setToUserAccount(userAccountService.toSimpleDesenModel(entity.getToUserAccount()));
        }
        return model;
    }

    public TipInfoModel toModel(TipInfo entity) {
        TipInfoModel model = null;
        if (ObjectUtils.isNotEmpty(entity)) {
            model = new TipInfoModel();
            BeanUtils.copyProperties(entity, model);

            model.setFromUserAccount(userAccountService.toSimpleModel(entity.getFromUserAccount()));
            model.setToUserAccount(userAccountService.toSimpleModel(entity.getToUserAccount()));
        }
        return model;
    }
}
