package com.cyksj.web.controller.manage.corp;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cyksj.config.corp.YHLXWxCorpConfig;
import com.cyksj.dto.Result;
import com.cyksj.enums.GatewayResponse;
import com.cyksj.model.entity.CorpMsgFollowAssociation;
import com.cyksj.model.manage.views.CorpMsgFollowAssociationView;
import com.cyksj.service.corp.CorpMsgFollowAssociationService;
import com.cyksj.service.corp.WxCorpOps;
import com.ejlchina.searcher.BeanSearcher;
import com.ejlchina.searcher.SearchResult;
import com.ejlchina.searcher.param.Operator;
import com.ejlchina.searcher.util.MapUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

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

/**
 * @author chan
 * @date 2024/1/18 12:14
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/manage/msg/follow")
public class CorpMsgFollowController {

    private final CorpMsgFollowAssociationService corpMsgFollowAssociationService;

    private final BeanSearcher beanSearcher;

    private final HttpServletRequest request;

    private final WxCorpOps wxCorpOps;

    private final YHLXWxCorpConfig corpConfig;

    /**
     *  会话存档开启成员列表
     * @author chan
     * @date 2024-01-18 17:27
     */
    @GetMapping
    public Result<SearchResult<CorpMsgFollowAssociationView>> getPermitUserList() throws Exception {
        List<String> res = wxCorpOps.getPermitUserList(corpConfig.getCorpId(), null);
        SearchResult<CorpMsgFollowAssociationView> search = beanSearcher.search(CorpMsgFollowAssociationView.class, MapUtils.flatBuilder(request.getParameterMap()).field(CorpMsgFollowAssociationView::getUserid, res).op(Operator.InList).build());
        search.getDataList().forEach((data)->{
            List<CorpMsgFollowAssociation> corpMsgFollowAssociations = beanSearcher.searchAll(CorpMsgFollowAssociation.class, MapUtils.builder().field(CorpMsgFollowAssociation::getFollowId, data.getUserid()).build());
            List<String> followIds = corpMsgFollowAssociations.stream().map(CorpMsgFollowAssociation::getAssociationFollowId).collect(Collectors.toList());
            if(followIds.size() > 0 ){
                List<CorpMsgFollowAssociationView> corpMsgFollowAssociationViews = beanSearcher.searchAll(CorpMsgFollowAssociationView.class, MapUtils.builder().field(CorpMsgFollowAssociationView::getUserid, followIds).op(Operator.InList).build());
                data.setAssociationList(corpMsgFollowAssociationViews);
            }else {
                data.setAssociationList(new ArrayList<>());
            }
        });
        return GatewayResponse.SUCCESS.newBuilder().toResult(search);
    }

    /**
     *
     * @return
     */
    @GetMapping("/all")
    public Result<List<CorpMsgFollowAssociationView>> getAll() throws Exception {
        List<String> res = wxCorpOps.getPermitUserList(corpConfig.getCorpId(), null);
        List<CorpMsgFollowAssociationView> search = beanSearcher.searchAll(CorpMsgFollowAssociationView.class, MapUtils.flatBuilder(request.getParameterMap()).field(CorpMsgFollowAssociationView::getUserid, res).op(Operator.InList).build());

        return GatewayResponse.SUCCESS.newBuilder().toResult(search);
    }

    /**
     * 新增会话存档关系
     * @author chan
     * @date 2024-01-18 17:26
     */
    @PostMapping
    public Result<String> addAssociation(@RequestBody CorpMsgFollowAssociation corpMsgFollowAssociation){
        corpMsgFollowAssociationService.save(corpMsgFollowAssociation);
        return GatewayResponse.SUCCESS.newBuilder().toResult();
    }

    /**
     * 删除会话存档关系
     * @author chan
     * @date 2024-01-18 17:26
     */
    @DeleteMapping
    public Result<String> delAssociation(@RequestBody CorpMsgFollowAssociation corpMsgFollowAssociation){
        corpMsgFollowAssociationService.remove(Wrappers.lambdaQuery(CorpMsgFollowAssociation.class).eq(CorpMsgFollowAssociation::getAssociationFollowId,corpMsgFollowAssociation.getAssociationFollowId()).eq(CorpMsgFollowAssociation::getFollowId,corpMsgFollowAssociation.getFollowId()));
        return GatewayResponse.SUCCESS.newBuilder().toResult();
    }
}
