package com.fdkankan.ucenter.controller;


import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fdkankan.ucenter.common.BaseController;
import com.fdkankan.ucenter.common.Result;
import com.fdkankan.ucenter.common.ResultData;
import com.fdkankan.ucenter.entity.Article;
import com.fdkankan.ucenter.entity.Case;
import com.fdkankan.ucenter.service.IArticleService;
import com.fdkankan.ucenter.service.ICaseService;
import com.fdkankan.ucenter.service.IFeedbackOptionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 
 * @since 2022-10-13
 */
@RestController
@RequestMapping("/ucenter/article")
public class ArticleController extends BaseController {

    @Autowired
    ICaseService caseService;
    @Autowired
    IFeedbackOptionService feedbackOptionService;
    /**
     * 行业解决方案-案例展示
     */
    @PostMapping("/detail")
    public Result detail(@RequestBody Case caseEntity) throws Exception {
        if(ObjectUtils.isEmpty(caseEntity.getId())){
            return Result.success();
        }
        return Result.success(caseService.getById(caseEntity.getId()));
    }

    @PostMapping("/allList")
    public Result allList(@RequestBody Case caseEntity) throws Exception {
        String lang = getLang();
        if("zh".equals(lang)){
            lang = "cn";
        }
        caseEntity.setLanguage(lang);

        return Result.success(caseService.allList(caseEntity));
    }

    @GetMapping("/feedbackOptionList")
    public Result feedbackOptionList(){
        Case caseEntity = new Case();
        String lang = getLang();
        if("zh".equals(lang)){
            lang = "cn";
        }
        caseEntity.setLanguage(lang);
        List<Case> cases = caseService.allList(caseEntity);
        List<Integer> ids = new ArrayList<>();
        for (Case aCase : cases) {
            if(StringUtils.isNotBlank(aCase.getFeedbackOptionIds())){
                JSONArray jsonArray = JSONArray.parseArray(aCase.getFeedbackOptionIds());
                for (Object object : jsonArray) {
                    ids.add((Integer) object);
                }
            }
        }
        if(ids.isEmpty()){
            return Result.success();
        }

        return Result.success(feedbackOptionService.listByIds(ids));
    }
}

