package com.tzld.piaoquan.api.service.contentplatform.impl;

import com.tzld.piaoquan.api.common.enums.ExceptionEnum;
import com.tzld.piaoquan.api.common.enums.contentplatform.PlanStatusEnum;
import com.tzld.piaoquan.api.common.exception.CommonException;
import com.tzld.piaoquan.api.dao.mapper.contentplatform.ContentPlatformIllegalMsgMapper;
import com.tzld.piaoquan.api.model.config.LoginUserContext;
import com.tzld.piaoquan.api.model.param.contentplatform.NoticeListParam;
import com.tzld.piaoquan.api.model.param.contentplatform.NoticeReadParam;
import com.tzld.piaoquan.api.model.po.contentplatform.ContentPlatformAccount;
import com.tzld.piaoquan.api.model.po.contentplatform.ContentPlatformIllegalMsg;
import com.tzld.piaoquan.api.model.po.contentplatform.ContentPlatformIllegalMsgExample;
import com.tzld.piaoquan.api.model.vo.contentplatform.NoticeItemVO;
import com.tzld.piaoquan.api.service.contentplatform.ContentPlatformNoticeService;
import com.tzld.piaoquan.growth.common.utils.page.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class ContentPlatformNoticeServiceImpl implements ContentPlatformNoticeService {

    @Autowired
    private ContentPlatformIllegalMsgMapper msgMapper;

    @Override
    public Page<NoticeItemVO> noticeList(NoticeListParam param) {
        ContentPlatformAccount loginAccount = LoginUserContext.getUser();
        ContentPlatformIllegalMsgExample example = new ContentPlatformIllegalMsgExample();
        example.createCriteria().andAccountIdEqualTo(loginAccount.getId());
        example.setOrderByClause("create_timestamp desc");
        Page<NoticeItemVO> page = new Page<>(param.getPageNum(), param.getPageSize());
        example.setPage(page);
        long totalCount = msgMapper.countByExample(example);
        page.setTotalSize((int) totalCount);
        List<ContentPlatformIllegalMsg> list = msgMapper.selectByExample(example);
        List<NoticeItemVO> records = new ArrayList<>();
        for (ContentPlatformIllegalMsg item : list) {
            NoticeItemVO vo = new NoticeItemVO();
            vo.setId(item.getId());
            vo.setTitle(item.getIllegalTitle());
            vo.setMsg(item.getIllegalContent());
            vo.setStatus(item.getStatus());
            vo.setCreateTimestamp(item.getCreateTimestamp());
            records.add(vo);
        }
        page.setObjs(records);
        return page;
    }

    @Override
    public Long getNotReadCount() {
        ContentPlatformAccount loginAccount = LoginUserContext.getUser();
        ContentPlatformIllegalMsgExample example = new ContentPlatformIllegalMsgExample();
        example.createCriteria().andAccountIdEqualTo(loginAccount.getId()).andStatusEqualTo(PlanStatusEnum.DELETED.getVal());
        return msgMapper.countByExample(example);
    }

    @Override
    public void noticeRead(NoticeReadParam param) {
        ContentPlatformAccount loginAccount = LoginUserContext.getUser();
        ContentPlatformIllegalMsg msg = msgMapper.selectByPrimaryKey(param.getId());
        if (Objects.isNull(msg)) {
            return;
        }
        if (!Objects.equals(loginAccount.getId(), msg.getAccountId())) {
            throw new CommonException(ExceptionEnum.PARAM_ERROR);
        }
        ContentPlatformIllegalMsg illegalMsg = new ContentPlatformIllegalMsg();
        illegalMsg.setId(param.getId());
        illegalMsg.setStatus(1);
        illegalMsg.setUpdateTimestamp(System.currentTimeMillis());
        msgMapper.updateByPrimaryKeySelective(illegalMsg);
    }

    @Override
    public void noticeReadAll() {
        ContentPlatformAccount loginAccount = LoginUserContext.getUser();
        ContentPlatformIllegalMsgExample example = new ContentPlatformIllegalMsgExample();
        example.createCriteria().andAccountIdEqualTo(loginAccount.getId()).andStatusEqualTo(PlanStatusEnum.DELETED.getVal());
        ContentPlatformIllegalMsg record = new ContentPlatformIllegalMsg();
        record.setStatus(1);
        record.setUpdateTimestamp(System.currentTimeMillis());
        msgMapper.updateByExampleSelective(record, example);
    }
}
