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

import com.github.microservice.components.data.base.util.PageEntityUtil;
import com.zhongshu.card.client.model.school.*;
import com.github.microservice.net.ResultContent;
import com.github.microservice.net.ResultMessage;
import com.zhongshu.card.client.service.school.NoticeInfoService;
import com.zhongshu.card.server.core.dao.org.OrganizationDao;
import com.zhongshu.card.server.core.dao.org.UserCountDao;
import com.zhongshu.card.server.core.dao.school.NoticeInfoDao;
import com.zhongshu.card.server.core.dao.school.NoticeInfoViewDao;
import com.zhongshu.card.server.core.domain.school.NoticeInfo;
import com.zhongshu.card.server.core.domain.school.NoticeInfoView;
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 com.zhongshu.card.server.core.util.BeanUtils;
import com.zhongshu.card.server.core.util.CommonUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
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 2024/6/7
 */
@Slf4j
@Service
public class NoticeInfoServiceImpl extends SuperService implements NoticeInfoService {

    @Autowired
    private UserCountDao userCountDao;

    @Autowired
    private OrganizationDao organizationDao;

    @Autowired
    NoticeInfoDao noticeInfoDao;

    @Autowired
    NoticeInfoViewDao noticeInfoViewDao;

    @Autowired
    UserAccountServiceImpl userService;

    @Autowired
    CommonService commonService;

    /**
     * 添加-编辑公告
     *
     * @param param
     * @return
     */
    @Override
    public ResultContent addNoticeInfo(NoticeInfoAddParam param) {
        initDefaultUserAndOid(param);
        NoticeInfo noticeInfo = new NoticeInfo();
        if (StringUtils.isNotEmpty(param.getId())) {
            noticeInfo = noticeInfoDao.findTopById(param.getId());
            if (ObjectUtils.isEmpty(noticeInfo)) {
                return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, param.getId()));
            }
        }
        BeanUtils.copyProperties(param, noticeInfo);
        noticeInfoDao.save(noticeInfo);
        return ResultContent.buildSuccess();
    }

    /**
     * 删除公告
     *
     * @param id
     * @return
     */
    @Override
    public ResultContent deleteNoticeInfo(String id) {
        NoticeInfo noticeInfo = noticeInfoDao.findTopById(id);
        if (ObjectUtils.isEmpty(noticeInfo)) {
            return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, id));
        }
        noticeInfoDao.delete(noticeInfo);
        return ResultContent.buildSuccess();
    }

    /**
     * 置顶
     *
     * @param id
     * @return
     */
    public ResultContent topping(String id) {
        NoticeInfo noticeInfo = noticeInfoDao.findTopById(id);
        if (ObjectUtils.isEmpty(noticeInfo)) {
            return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, id));
        }
        noticeInfo.setIsTopping(Boolean.TRUE);
        noticeInfo.setToppingTime(System.currentTimeMillis());
        noticeInfoDao.save(noticeInfo);
        return ResultContent.buildSuccess();
    }

    /**
     * 查询公告详情
     *
     * @param id
     * @return
     */
    public ResultContent<NoticeInfoModel> getDetail(String id) {
        NoticeInfo noticeInfo = noticeInfoDao.findTopById(id);
        if (ObjectUtils.isEmpty(noticeInfo)) {
            return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, id));
        }
        NoticeInfoModel model = toModel(noticeInfo);
        return ResultContent.buildSuccess(model);
    }

    /**
     * 添加阅读量
     *
     * @param id
     * @return
     */
    @Override
    public ResultContent addView(String id) {
        NoticeInfo noticeInfo = noticeInfoDao.findTopById(id);
        if (ObjectUtils.isEmpty(noticeInfo)) {
            return ResultContent.buildFail(String.format(ResultMessage.DATA_NOT_EXIST, id));
        }
        Long viewTimes = noticeInfo.getViewTimes();
        if (CommonUtil.longIsEmpty(viewTimes)) {
            viewTimes = 0l;
        }
        viewTimes++;
        Map<String, Object> standardData = new HashMap<>();
        standardData.put("id", id);
        standardData.put("viewTimes", viewTimes);
        commonService.updateData(standardData, NoticeInfo.class.getSimpleName());

        NoticeInfoView infoView = new NoticeInfoView();
        initEntity(infoView);
        infoView.setNoticeInfo(noticeInfo);
        infoView.setUserAccount(getCurrentUserAccount());

        return ResultContent.buildSuccess();
    }

    /**
     * 分页查询
     *
     * @param param
     * @param pageable
     * @return
     */
    @Override
    public ResultContent<Page<NoticeInfoModel>> page(NoticeInfoSearchParam param, Pageable pageable) {
        initOidSearchParam(param);
        Page<NoticeInfo> page = noticeInfoDao.page(pageable, param);
        return ResultContent.buildSuccess(PageEntityUtil.concurrent2PageModel(page, this::toModel));
    }

    @Override
    public ResultContent<Page<NoticeInfoViewModel>> pageView(NoticeInfoViewSearch param, Pageable pageable) {
        Page<NoticeInfoView> page = noticeInfoViewDao.page(pageable, param);
        return ResultContent.buildSuccess(PageEntityUtil.concurrent2PageModel(page, this::toViewModel));
    }

    public NoticeInfoModel toModel(NoticeInfo entity) {
        NoticeInfoModel model = new NoticeInfoModel();
        if (ObjectUtils.isNotEmpty(entity)) {
            BeanUtils.copyProperties(entity, model);
        }
        return model;
    }

    public NoticeInfoViewModel toViewModel(NoticeInfoView entity) {
        NoticeInfoViewModel model = new NoticeInfoViewModel();
        if (ObjectUtils.isNotEmpty(entity)) {
            BeanUtils.copyProperties(entity, model);
            model.setUserCount(userService.toModel(entity.getUserAccount()));
        }
        return model;
    }

}
