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

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gree.mall.manager.bean.admin.AdminUserCom;
import com.gree.mall.manager.bean.notice.NoticeWebsitBean;
import com.gree.mall.manager.bean.notice.NoticeWebsitRecordVO;
import com.gree.mall.manager.bean.notice.NoticeWebsitVO;
import com.gree.mall.manager.bean.workorder.WebsitDispatchVO;
import com.gree.mall.manager.commonmapper.NoticeRecordBeanMapper;
import com.gree.mall.manager.enums.IsYesNoEnum;
import com.gree.mall.manager.logic.common.CommonLogic;
import com.gree.mall.manager.plus.entity.*;
import com.gree.mall.manager.plus.service.AdminWebsitService;
import com.gree.mall.manager.plus.service.NoticeWebsitRecordService;
import com.gree.mall.manager.plus.service.NoticeWebsitService;
import com.gree.mall.manager.zfire.bean.ZfireParamBean;
import com.gree.mall.manager.zfire.util.FieldUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

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

@Service
@Slf4j
public class NoticeWebsitLogic {

    @Autowired
    CommonLogic commonLogic;

    @Autowired
    NoticeRecordBeanMapper noticeRecordMapper;

    @Autowired
    NoticeWebsitService noticeWebsitService;

    @Autowired
    AdminWebsitService adminWebsitService;

    @Autowired
    NoticeWebsitRecordService noticeWebsitRecordService;


    public IPage<NoticeWebsitVO> list(Page page, ZfireParamBean zfireParam) {
        //获取当前登录企业id
        AdminUserCom adminUser = commonLogic.getAdminUser();

        List<String> adminCompanyIds = adminUser.getCompanyWechatIds();
        //1.组装查询条件
        zfireParam = FieldUtils.supplyParam(zfireParam, NoticeWebsitVO.class,adminUser);


        String status = adminUser.getType().equals(0)?"SEND":"";

        IPage<NoticeWebsitVO> websitDispatchVOIPage = noticeRecordMapper.listNoticeWebsit(new Page(zfireParam.getPageNum(), zfireParam.getPageSize()), zfireParam, adminCompanyIds,status);

        return websitDispatchVOIPage;
    }

    public NoticeWebsit detail(String id) {
        NoticeWebsit noticeWebsit = noticeWebsitService.getById(id);
        return noticeWebsit;
    }

    public void add(NoticeWebsitBean noticeBean) {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        noticeBean.setCompanyWechatId(adminUser.getCompanyWechatId());
        noticeBean.setIssueNickName(adminUser.getNickName());
        noticeBean.setIssueTime(new Date());
        noticeBean.setIssueUserId(adminUser.getAdminUserId());
        noticeBean.setCompanyWechatName(adminUser.getCompanyName());
        noticeBean.insert();

        List<NoticeWebsitRecord> noticeWebsitRecords = new ArrayList<>();


        for (String websitId : noticeBean.getWebsitIds()) {
            AdminWebsit adminWebsit = adminWebsitService.getById(websitId);
            NoticeWebsitRecord noticeWebsitRecord = new NoticeWebsitRecord();
            noticeWebsitRecord.setNoticeWebsitId(noticeBean.getNoticeWebsitId());
            noticeWebsitRecord.setAdminWebsitId(websitId);
            noticeWebsitRecord.setAdminWebsitName(adminWebsit.getName());
            noticeWebsitRecord.setCompanyWechatId(adminUser.getCompanyWechatId());
            noticeWebsitRecord.setCompanyWechatName(adminUser.getCompanyName());
            noticeWebsitRecord.setIssueTime(noticeBean.getIssueTime());


            noticeWebsitRecords.add(noticeWebsitRecord);
        }

        noticeWebsitRecordService.saveBatch(noticeWebsitRecords);
    }

    public void update(NoticeWebsitBean noticeBean) {

        AdminUserCom adminUser = commonLogic.getAdminUser();
        noticeBean.setCompanyWechatId(adminUser.getCompanyWechatId());
        noticeBean.setCompanyWechatName(adminUser.getCompanyName());

        noticeBean.updateById();

        List<NoticeWebsitRecord> noticeWebsitRecords = new ArrayList<>();

        noticeWebsitRecordService.lambdaUpdate()
                .eq(NoticeWebsitRecord::getNoticeWebsitId,noticeBean.getNoticeWebsitId())
                .remove();

        for (String websitId : noticeBean.getWebsitIds()) {
            AdminWebsit adminWebsit = adminWebsitService.getById(websitId);
            NoticeWebsitRecord noticeWebsitRecord = new NoticeWebsitRecord();
            noticeWebsitRecord.setAdminWebsitId(websitId);
            noticeWebsitRecord.setNoticeWebsitId(noticeBean.getNoticeWebsitId());
            noticeWebsitRecord.setAdminWebsitName(adminWebsit.getName());
            noticeWebsitRecord.setCompanyWechatId(adminUser.getCompanyWechatId());
            noticeWebsitRecord.setCompanyWechatName(adminUser.getCompanyName());

            noticeWebsitRecords.add(noticeWebsitRecord);
        }

        noticeWebsitRecordService.saveBatch(noticeWebsitRecords);
    }

    public void ofOrSend(String id, String status) {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        NoticeWebsit noticeWebsit = noticeWebsitService.getById(id);
        noticeWebsit.setStatus(status);


        noticeWebsit.updateById();

        noticeWebsitRecordService.lambdaUpdate()
                .eq(NoticeWebsitRecord::getNoticeWebsitId,id)
                .set(NoticeWebsitRecord::getIssueTime,new Date()).update();
    }

    public IPage<NoticeWebsitRecordVO> listWebsit(Page page, ZfireParamBean zfireParam) {
        //获取当前登录企业id
        AdminUserCom adminUser = commonLogic.getAdminUser();

        List<String> adminCompanyIds = adminUser.getCompanyWechatIds();
        //1.组装查询条件
        zfireParam = FieldUtils.supplyParam(zfireParam, NoticeWebsitRecordVO.class,adminUser);

        String status = adminUser.getType().equals(0)?"SEND":"";

        IPage<NoticeWebsitRecordVO> noticeWebsitRecordVOIPage = noticeRecordMapper.listWebsit(new Page(zfireParam.getPageNum(), zfireParam.getPageSize()), zfireParam, adminCompanyIds,status);

        return noticeWebsitRecordVOIPage;
    }

    public Object unreadCount(HttpServletRequest request, String readFlag) {
        AdminUserCom adminUser = commonLogic.getAdminUser();

        List<String> collect = noticeWebsitService.lambdaQuery()
                .eq(NoticeWebsit::getStatus, "SEND").select(NoticeWebsit::getNoticeWebsitId)
                .list().stream().map(NoticeWebsit::getNoticeWebsitId).collect(Collectors.toList());

        if (CollectionUtils.isEmpty(collect)) {
            return 0;
        }

        Integer count = noticeWebsitRecordService.lambdaQuery()
                .in(NoticeWebsitRecord::getNoticeWebsitId,collect)
                .eq(NoticeWebsitRecord::getReadFlag, readFlag)
                .in(!CollectionUtils.isEmpty(adminUser.getAdminWebsitIds()),NoticeWebsitRecord::getAdminWebsitId, adminUser.getAdminWebsitIds())
                .count();

        return count;
    }

    @Transactional(rollbackFor = Exception.class)
    public void markRead(List<String> noticeIds) {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        noticeWebsitRecordService.lambdaUpdate()
                .in(NoticeWebsitRecord::getNoticeWebsitId,noticeIds)
                .eq(NoticeWebsitRecord::getReadFlag,IsYesNoEnum.NO.getKey())
                .in(!CollectionUtils.isEmpty(adminUser.getAdminWebsitIds()),NoticeWebsitRecord::getAdminWebsitId, adminUser.getAdminWebsitIds())
                .set(NoticeWebsitRecord::getReadFlag, IsYesNoEnum.YES.getKey())
                .set(NoticeWebsitRecord::getReadTime, new Date())
                .update();

        for (String id : noticeIds) {



            NoticeWebsit one = noticeWebsitService.getById(id);
            if (one != null){

                noticeWebsitService.lambdaUpdate()
                        .eq(NoticeWebsit::getNoticeWebsitId,one.getNoticeWebsitId())
                        .set(NoticeWebsit::getNum,noticeWebsitRecordService.lambdaQuery()
                                .eq(NoticeWebsitRecord::getNoticeWebsitId,id)
                                .eq(NoticeWebsitRecord::getReadFlag,IsYesNoEnum.YES.getKey()).count()).update();
            }
        }


    }

    @Transactional(rollbackFor = Exception.class)
    public void markReadAll(HttpServletRequest request) {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        if (!CollectionUtils.isEmpty(adminUser.getAdminWebsitIds())) {

            List<String> collect = noticeWebsitService.lambdaQuery()
                    .eq(NoticeWebsit::getStatus, "SEND").select(NoticeWebsit::getNoticeWebsitId)
                    .list().stream().map(NoticeWebsit::getNoticeWebsitId).collect(Collectors.toList());

            if (CollectionUtils.isEmpty(collect)) {
                return ;
            }

            List<NoticeWebsitRecord> list = noticeWebsitRecordService.lambdaQuery()
                    .in(NoticeWebsitRecord::getNoticeWebsitId,collect)
                    .eq(NoticeWebsitRecord::getReadFlag,IsYesNoEnum.NO.getKey())
                    .in(!CollectionUtils.isEmpty(adminUser.getAdminWebsitIds()), NoticeWebsitRecord::getAdminWebsitId, adminUser.getAdminWebsitIds())
                    .list();
            for (NoticeWebsitRecord websitRecord : list) {

                websitRecord.setReadFlag(IsYesNoEnum.YES.getKey());
                websitRecord.setReadTime(new Date());
                websitRecord.updateById();
                NoticeWebsit one = noticeWebsitService.getById(websitRecord.getNoticeWebsitId());
                if (one != null){
                    noticeWebsitService.lambdaUpdate()
                            .eq(NoticeWebsit::getNoticeWebsitId,one.getNoticeWebsitId())
                            .set(NoticeWebsit::getNum,noticeWebsitRecordService.lambdaQuery()
                                    .eq(NoticeWebsitRecord::getNoticeWebsitId,one.getNoticeWebsitId())
                                    .eq(NoticeWebsitRecord::getReadFlag,IsYesNoEnum.YES.getKey()).count()).update();
                }


            }

        }
    }

    public void readOne(List<String> id) {

        AdminUserCom adminUser = commonLogic.getAdminUser();
        noticeWebsitRecordService.lambdaUpdate()
                .in(NoticeWebsitRecord::getId,id)
                .eq(NoticeWebsitRecord::getReadFlag,IsYesNoEnum.NO.getKey())
                .in(!CollectionUtils.isEmpty(adminUser.getAdminWebsitIds()),NoticeWebsitRecord::getAdminWebsitId, adminUser.getAdminWebsitIds())
                .set(NoticeWebsitRecord::getReadFlag, IsYesNoEnum.YES.getKey())
                .set(NoticeWebsitRecord::getReadTime, new Date())
                .update();

        for (String idOne : id) {

            NoticeWebsitRecord noticeWebsitRecord = noticeWebsitRecordService.getById(idOne);
            NoticeWebsit one = noticeWebsitService.getById(noticeWebsitRecord.getNoticeWebsitId());
            if (one != null){
                noticeWebsitService.lambdaUpdate()
                        .in(NoticeWebsit::getNoticeWebsitId,one.getNoticeWebsitId())
                        .set(NoticeWebsit::getNum,noticeWebsitRecordService.lambdaQuery()
                                .eq(NoticeWebsitRecord::getNoticeWebsitId,one.getNoticeWebsitId())
                                .eq(NoticeWebsitRecord::getReadFlag,IsYesNoEnum.YES.getKey()).count()).update();
            }
        }
    }
}
