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

import com.aliyuncs.utils.StringUtils;
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.exception.RemoteServiceException;
import com.gree.mall.manager.logic.common.CommonLogic;
import com.gree.mall.manager.plus.entity.*;
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.*;

@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 void stockNotice(){
        final int stockNum = 10;
        //基础商品
        List<GoodsSpec> list = goodsSpecService.lambdaQuery()
                .le(GoodsSpec::getStockNum, stockNum)
                .list();

        Map<String,Node> goodsIdMap=new HashMap<>();
        for (GoodsSpec goodsSpec:list
             ) {
            Node node = new Node( goodsSpec.getCompanyWechatId() , goodsSpec.getCompanyName() );
            goodsIdMap.put( goodsSpec.getGoodsId() , node );
        }
        //新增基础商品通知
        this.addNotice(goodsIdMap,2);


        //秒杀商品
        List<SecKillActivitySpec> list2 = secKillActivitySpecService.lambdaQuery()
                .le(SecKillActivitySpec::getSecStockNum, stockNum)
                .list();
        goodsIdMap=new HashMap<>();
        for (SecKillActivitySpec secKillActivitySpec:list2
             ) {
            Node node=new Node(secKillActivitySpec.getCompanyWechatId() , secKillActivitySpec.getCompanyName());
            goodsIdMap.put(secKillActivitySpec.getGoodsId(),node);
        }
        //新增秒杀商品通知
        this.addNotice(goodsIdMap,3);


        //团购商品
        List<PromotionGroupSpec> list3 = promotionGroupSpecService.lambdaQuery()
                .le(PromotionGroupSpec::getStock, stockNum)
                .list();
        goodsIdMap=new HashMap<>();
        for (PromotionGroupSpec promotionGroupSpec:list3
             ) {
            Node node=new Node(promotionGroupSpec.getCompanyWechatId() , promotionGroupSpec.getCompanyName());
            goodsIdMap.put(promotionGroupSpec.getGoodsId(),node);
        }
        //新增团购商品通知
        this.addNotice(goodsIdMap,4);

    }

    private void addNotice(Map<String,Node> goodsIdMap,Integer type){
        if(goodsIdMap.size() == 0){
            return;
        }
        Map<String,String> map = new HashMap<>();
        map.put("2","基础商品");
        map.put("3","秒杀商品");
        map.put("4","团购商品");

        List<Notice> notices = new ArrayList<>();
        Date date = new Date();
        for(String goodsId : goodsIdMap.keySet()) {
            Notice notice = new Notice();
            notice.setObjId(goodsId);
            notice.setTitle("商品库存");
            notice.setType(type);
            notice.setContent(map.get(type.toString())+"id:"+goodsId+",商品库存不足10,请及时补充库存");
            notice.setCreateTime(date);
            notice.setCompanyWechatId(goodsIdMap.get(goodsId).getKey());
            notice.setCompanyName(goodsIdMap.get(goodsId).getValue());
            notices.add(notice);
        }
        noticeService.saveBatch(notices);
    }


    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::getCompanyWechatId,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::getCompanyWechatId , 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) {
        /*List<String> websitNoList = new ArrayList<>();
        AdminWebsitBean adminWebsitBean = CommonUtils.getAdminUser(request);
        if (!adminWebsitBean.getType()) {
            websitNoList = adminWebsitBean.getWeidsitIds().stream().map(WebsitZone::getWebsitNumber).collect(Collectors.toList());
        }*/
        AdminUserCom adminUser = commonLogic.getAdminUser(request);
        String companyWechatId = adminUser.getLoginCompanyWechatId();
        if(StringUtils.isEmpty(companyWechatId)){
            throw new RemoteServiceException("无效微信企业id");
        }

        noticeService.lambdaUpdate()
                //.in(!adminWebsitBean.getType(),Notice::getWebsitNumber,websitNoList)
                .eq(Notice::getCompanyWechatId , companyWechatId)
                .eq(Notice::getReadFlag,false)
                .set(Notice::getReadFlag,true)
                .update();
    }

}
