package com.cyksj.web.controller.group;

import com.cyksj.common.constant.Constant;
import com.cyksj.dto.Result;
import com.cyksj.enums.GatewayResponse;
import com.cyksj.mapper.GoodsDonSkuMapper;
import com.cyksj.model.entity.GroupsRelation;
import com.cyksj.model.manage.views.GroupsRelationView;
import com.cyksj.model.views.GroupsView;
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.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * @author chan
 * @date 2022/9/26 6:31 PM
 */
@Slf4j
@RequestMapping("/applets/group")
@RestController
@RequiredArgsConstructor
public class GroupController {

    private final HttpServletRequest request;

    private final BeanSearcher beanSearcher;

    private final GoodsDonSkuMapper goodsDonSkuMapper;

    @GetMapping("/get")
    @Deprecated
    public Result<SearchResult<GroupsView>> get() {
        SearchResult<GroupsView> search = beanSearcher.search(GroupsView.class, MapUtils.flatBuilder(request.getParameterMap())
                .orderBy(GroupsView::getOrderBy).asc()
                .orderBy(GroupsView::getId).desc().build()
        );
        search.getDataList().forEach((groupsView) -> {
            if (Constant.CHAT_GPT_SKU_IDS.contains(groupsView.getSkuId())) {
                List<GroupsRelationView> relationViewList = beanSearcher.searchAll(GroupsRelationView.class, MapUtils.builder()
                        .field(GroupsRelationView::getGroupsId, groupsView.getId())
                        .field(GroupsRelationView::getStatus, GroupsRelation.Status.outside.name()).op(Operator.NotEqual)
                        .orderBy(GroupsRelationView::getUpdate_time).desc()
                        .build());
                List<GroupsRelationView> all = null;
                //GPT 月付 暗改成20个 展示10个
                if (groupsView.getSkuId().equals(Constant.CHAT_GPT_SKU_IDS.get(0)) && groupsView.getNum() == 20) {
                    all = relationViewList.subList(0, 5);
                    List<GroupsRelationView> after = relationViewList.subList(relationViewList.size() - 5, relationViewList.size());
                    all.addAll(after);
                }
                //GPT 4人月付暗改成6个 展示4个
                else if (groupsView.getSkuId().equals(Constant.CHAT_GPT_SKU_IDS.get(1)) && groupsView.getNum() == 6) {
                    all = relationViewList.subList(0, 2);
                    List<GroupsRelationView> after = relationViewList.subList(relationViewList.size() - 2, relationViewList.size());
                    all.addAll(after);
                }
                //GPT 10人年付暗改成10个
                else if (groupsView.getSkuId().equals(Constant.CHAT_GPT_SKU_IDS.get(2)) && groupsView.getNum() == 12) {
                    all = relationViewList.subList(0, 5);
                    List<GroupsRelationView> after = relationViewList.subList(relationViewList.size() - 5, relationViewList.size());
                    all.addAll(after);
                } else {
                    all = relationViewList;
                }
                groupsView.setRelations(all);
            } else if (Constant.MIDJOURNEY_SKU_IDS.contains(groupsView.getSkuId()) && groupsView.getNum() == 15) {
                List<GroupsRelationView> relationViewList = beanSearcher.searchAll(GroupsRelationView.class, MapUtils.builder()
                        .field(GroupsRelationView::getGroupsId, groupsView.getId())
                        .orderBy(GroupsRelationView::getUpdate_time).desc()
                        .build());
                List<GroupsRelationView> all = null;
                //MIDJOURNEY 月付5个 暗改15个 展示5个
                if (groupsView.getSkuId().equals(Constant.MIDJOURNEY_SKU_IDS.get(0)) && groupsView.getNum() == 15) {
                    all = relationViewList.subList(0, 4);
                    List<GroupsRelationView> after = relationViewList.subList(relationViewList.size() - 1, relationViewList.size());
                    all.addAll(after);
                }
                //MIDJOURNEY 标准会员月付 Pro会员月付;Pro会员年付;标准会员年付;10个 暗改15个 展示10个
                else if ((groupsView.getSkuId().equals(Constant.MIDJOURNEY_SKU_IDS.get(1)) || groupsView.getSkuId().equals(Constant.MIDJOURNEY_SKU_IDS.get(2))
                        || groupsView.getSkuId().equals(Constant.MIDJOURNEY_SKU_IDS.get(3)) || groupsView.getSkuId().equals(Constant.MIDJOURNEY_SKU_IDS.get(4))) && groupsView.getNum() == 15) {
	                all = relationViewList.subList(0, 5);
	                List<GroupsRelationView> after = relationViewList.subList(relationViewList.size() - 5, relationViewList.size());
	                all.addAll(after);
                } else {
	                all = relationViewList;
                }
                groupsView.setRelations(all);
            } else {
                groupsView.setRelations(beanSearcher.searchAll(GroupsRelationView.class, MapUtils.builder()
                        .field(GroupsRelationView::getGroupsId, groupsView.getId())
                        .field(GroupsRelationView::getStatus, GroupsRelation.Status.outside.name()).op(Operator.NotEqual)
                        .build()));
            }

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