package com.gree.mall.miniapp.logic.notice;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gree.mall.miniapp.bean.notice.NoticeRecordVO;
import com.gree.mall.miniapp.bean.user.CurrentCompanyWechat;
import com.gree.mall.miniapp.logic.common.CommonLogic;
import com.gree.mall.miniapp.plus.entity.CommonFile;
import com.gree.mall.miniapp.plus.entity.Notice;
import com.gree.mall.miniapp.plus.entity.NoticeRecord;
import com.gree.mall.miniapp.plus.service.NoticeRecordService;
import com.gree.mall.miniapp.plus.service.NoticeService;
import com.gree.mall.miniapp.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class NoticeLogic {

    @Autowired
    NoticeRecordService noticeRecordService;

    @Autowired
    CommonLogic commonLogic;

    @Autowired
    NoticeService noticeService;

    public IPage<NoticeRecordVO> listNotice(String type, Integer pageNo, Integer pageSize) {
        CurrentCompanyWechat currentCompanyWechat = commonLogic.getCurrentCompanyWechat();

        Page<NoticeRecord> page = noticeRecordService.lambdaQuery()
                .eq(NoticeRecord::getAdminUserId, currentCompanyWechat.getUserId())
                .eq(NoticeRecord::getType, type)
                .orderByDesc(NoticeRecord::getCreateTime)
                .page(new Page<>(pageNo, pageSize));

        List<NoticeRecordVO> noticeRecords = BeanUtil.copyToList(page.getRecords(), NoticeRecordVO.class);

        for (NoticeRecordVO noticeRecord : noticeRecords) {
            List<CommonFile> commonFiles = commonLogic.queryFileByObjId(noticeRecord.getNoticeId(), "notice_file");


            noticeRecord.setFile(commonFiles);
        }

        Page<NoticeRecordVO> pageList = new Page<>();
        pageList.setRecords(noticeRecords);
        pageList.setSize(page.getSize());
        pageList.setTotal(page.getTotal());
        pageList.setCurrent(page.getCurrent());
        pageList.setPages(page.getPages());




        return pageList;
    }

    public NoticeRecordVO detailNotice(String id) {

        NoticeRecordVO noticeRecordVO = BeanUtil.toBean(noticeRecordService.getById(id), NoticeRecordVO.class);
        List<CommonFile> commonFiles = commonLogic.queryFileByObjId(noticeRecordVO.getNoticeId(), "notice_file");


        noticeRecordVO.setFile(commonFiles);
        return noticeRecordVO;
    }

    public Integer unreadCount(HttpServletRequest request, String noticeType, String readFlag) {
        CurrentCompanyWechat currentCompanyWechat = commonLogic.getCurrentCompanyWechat();

        List<String> type=new ArrayList<>();
        if (!StringUtil.isEmpty(noticeType) && noticeType.equals("SYSTEM")){
            type.add("WB");
            type.add("SYSTEM");
        }else {
            type.add("NOTICE");
        }

        return noticeRecordService.lambdaQuery()
                .eq(NoticeRecord::getAdminUserId,currentCompanyWechat.getUserId())
                .eq(!StringUtil.isEmpty(readFlag),NoticeRecord::getReadFlag, readFlag)
                .in(!StringUtil.isEmpty(noticeType),NoticeRecord::getNoticeType, type)

                .count();
    }


    public void markRead(List<String> noticeIds) {
        CurrentCompanyWechat currentCompanyWechat = commonLogic.getCurrentCompanyWechat();

        if (noticeRecordService.lambdaQuery()
                .eq(NoticeRecord::getAdminUserId,currentCompanyWechat.getUserId())
                .in(NoticeRecord::getId,noticeIds)
                .count() < 1) {
            return;
        }
        noticeRecordService.lambdaUpdate()
                .set(NoticeRecord::getReadFlag, "YES")
                .set(NoticeRecord::getReadTime,new Date())
                .eq(NoticeRecord::getAdminUserId,currentCompanyWechat.getUserId())
                .in(NoticeRecord::getId,noticeIds)
                .update();

        for (String noticeId : noticeIds) {
            NoticeRecord not = noticeRecordService.lambdaQuery()
                    .eq(NoticeRecord::getAdminUserId,currentCompanyWechat.getUserId())
                    .eq(NoticeRecord::getId, noticeId).last("limit 1").one();

            Notice one = noticeService.lambdaQuery()
                    .eq(Notice::getNoticeId, not.getNoticeId()).last("limit 1").one();
            if (one != null){
                one.setNum(one.getNum()+1);
                one.updateById();
            }
        }

    }

    public void markReadAll(HttpServletRequest request) {
        CurrentCompanyWechat currentCompanyWechat = commonLogic.getCurrentCompanyWechat();

        List<NoticeRecord> list = noticeRecordService.lambdaQuery()
                .eq(NoticeRecord::getReadFlag, "NO")
                .eq(NoticeRecord::getAdminUserId,currentCompanyWechat.getUserId())
                .list();
        for (NoticeRecord noticeRecord : list) {

            Notice one = noticeService.lambdaQuery()
                    .eq(Notice::getNoticeId, noticeRecord.getNoticeId()).last("limit 1").one();
            if (one != null){
                one.setNum(one.getNum()+1);
                one.updateById();
            }
        }


        noticeRecordService.lambdaUpdate()
                .set(NoticeRecord::getReadFlag,"YES")
                .set(NoticeRecord::getReadTime,new Date())
                .eq(NoticeRecord::getReadFlag, "NO")
                .eq(NoticeRecord::getAdminUserId,currentCompanyWechat.getUserId())
                .update();
    }


}
