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

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.manager.bean.admin.AdminUserCom;
import com.gree.mall.manager.logic.common.CommonLogic;
import com.gree.mall.manager.plus.entity.Notice;
import com.gree.mall.manager.plus.service.GoodsSpecService;
import com.gree.mall.manager.plus.service.NoticeService;
import com.gree.mall.manager.plus.service.PromotionGroupSpecService;
import com.gree.mall.manager.plus.service.SecKillActivitySpecService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Component
public class NoticeLogic {

    @Autowired
    private NoticeService noticeService;
    @Autowired
    private GoodsSpecService goodsSpecService;
    @Autowired
    private SecKillActivitySpecService secKillActivitySpecService;
    @Autowired
    private PromotionGroupSpecService promotionGroupSpecService;
    @Autowired
    CommonLogic commonLogic;

    private class Node implements Map.Entry<String,String>{

        String key;
        String value;

        public Node(String key,String value){
            this.key=key;
            this.value=value;
        }

        @Override
        public String getKey() {
            return key;
        }

        @Override
        public String getValue() {
            return value;
        }

        @Override
        public String setValue(String value) {
            String oldValue = this.value;
            this.value = value;
            return oldValue;
        }
    }


    public IPage<Notice> page(HttpServletRequest request,Boolean readFlag, Integer pageNum, Integer pageSize) {
       /* List<String> websitNoList = new ArrayList<>();
        AdminWebsitBean adminWebsitBean = CommonUtils.getAdminUser(request);
        if (!adminWebsitBean.getType()) {
            websitNoList = adminWebsitBean.getWeidsitIds().stream().map(WebsitZone::getWebsitNumber).collect(Collectors.toList());
        }*/

        //获取当前登录企业微信id
        AdminUserCom adminUser = commonLogic.getAdminUser(request);
        List<String> companyWechatIds = adminUser.getAdminCompanyIds();

        return noticeService.lambdaQuery()
               // .in(!CollectionUtils.isEmpty(websitNoList),Notice::getWebsitNumber,websitNoList)
                .in(CollectionUtils.isNotEmpty(companyWechatIds),Notice::getCompanyId,companyWechatIds)
                .eq(readFlag!=null,Notice::getReadFlag,readFlag)
                .orderByDesc(Notice::getCreateTime)
                .page(new Page(pageNum, pageSize));
    }

    public Integer unreadCount(HttpServletRequest request) {
       /* List<String> websitNoList = new ArrayList<>();
        AdminWebsitBean adminWebsitBean = CommonUtils.getAdminUser(request);
        if(adminWebsitBean == null){
            return 0;
        }
        if (!adminWebsitBean.getType()) {
            websitNoList = adminWebsitBean.getWeidsitIds().stream().map(WebsitZone::getWebsitNumber).collect(Collectors.toList());
        }*/
        //获取当前登录企业微信id
        AdminUserCom adminUser = commonLogic.getAdminUser(request);
        List<String> companyWechatIds = adminUser.getAdminCompanyIds();

        return noticeService.lambdaQuery()
                //.in(!adminWebsitBean.getType(),Notice::getWebsitNumber,websitNoList)
                .in(CollectionUtils.isNotEmpty(companyWechatIds) , Notice::getCompanyId , companyWechatIds)
                .eq(Notice::getReadFlag,false)
                .count();
    }

    public void markRead(String[] noticeIds) {

        for(String noticeId:noticeIds){
            noticeService.lambdaUpdate()
                    .eq(Notice::getNoticeId,noticeId)
                    .set(Notice::getReadFlag,true)
                    .update();
        }
    }

    public void markReadAll(HttpServletRequest request) {
        noticeService.lambdaUpdate()
                .eq(Notice::getReadFlag,false)
                .set(Notice::getReadFlag,true)
                .update();
    }

}
