package com.ruoyi.web.controller.liandao;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.query.LambdaQueryWrapperX;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.liandao.domain.*;
import com.ruoyi.liandao.dto.complaintSuggestions.ComplaintSuggestionsAddReqDto;
import com.ruoyi.liandao.dto.questionnaire.QuestionnaireDto;
import com.ruoyi.liandao.dto.questionnaire.QuestionnaireSubmitReqDto;
import com.ruoyi.liandao.dto.tourGuide.TourGuideAppointmentAddReqDto;
import com.ruoyi.liandao.dto.tourGuide.TourGuidePageReqDto;
import com.ruoyi.liandao.dto.website.ArticlePageReqDto;
import com.ruoyi.liandao.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 前端网站接口
 * @Author : chenzhengyu
 * @create 2024/3/20 14:02
 */
@Api("中业官网接口")
@Anonymous
@RestController
@RequestMapping("/zy/website")
public class WebsiteController extends BaseController {

    @Autowired
    private ResourcesConfigService resourcesConfigService;
    @Autowired
    private ArticleContentService articleContentService;
    @Autowired
    private TourGuideInfoService tourGuideInfoService;
    @Autowired
    private TourGuideAppointmentService tourGuideAppointmentService;
    @Autowired
    private QuestionnaireConfigService questionnaireConfigService;
    @Autowired
    private QuestionnaireSubmitService questionnaireSubmitService;
    @Autowired
    private ComplaintSuggestionsService complaintSuggestionsService;
    @Autowired
    private TicketCheckService ticketCheckService;
    @Autowired
    private ArticleTypeService articleTypeService;

    /**
     * 根据所属类别获取资源集合
     * @param category 所属类别
     * @return
     */
    @ApiOperation("根据所属类别获取资源集合")
    @GetMapping("/resources/selectByCategory")
    public AjaxResult selectResourcesByCategory(@RequestParam String category){
        List<ResourcesConfig> result = resourcesConfigService.list(new LambdaQueryWrapperX<ResourcesConfig>()
                .eq(ResourcesConfig::getCategory, category)
                .orderByAsc(ResourcesConfig::getName)
                .orderByDesc(ResourcesConfig::getUpdateTime));
        return success(result);
    }


    /**
     * 根据资源名称和类型获取资源
     *
     * @param name 名称
     * @return
     */
    @ApiOperation("根据资源名称和类型获取资源")
    @GetMapping("/resources/getByName")
    public AjaxResult getResourcesByName(@RequestParam String name) {
        ResourcesConfig one = resourcesConfigService.getOne(new LambdaQueryWrapperX<ResourcesConfig>()
                .eq(ResourcesConfig::getName, name));
        return success(one);
    }

    /**
     * 文章分页查询
     * @param dto
     * @return
     */
    @ApiOperation("文章分页查询")
    @GetMapping("/article/page")
    public TableDataInfo pageArticle(ArticlePageReqDto dto){
        ArticleType type = articleTypeService.getOne(new LambdaQueryWrapperX<ArticleType>()
                .eq(ArticleType::getName, dto.getType()));
        if(StringUtils.isBlank(dto.getLanguage())){
            dto.setLanguage("zh");
        }
        LambdaQueryWrapperX<ArticleContent> wrapperX = new LambdaQueryWrapperX<>();
        wrapperX.eqIfPresent(ArticleContent::getTittle, dto.getTittle());
        if (type != null) {
            List<Long> typeIdList = articleTypeService.getChildTypeIdList(type.getId());
            wrapperX.inIfPresent(ArticleContent::getArticleTypeId, typeIdList);
        } else {
            wrapperX.eqIfPresent(ArticleContent::getArticleType, dto.getType());
        }
        if (StringUtils.isNotBlank(dto.getSearchKey())) {
            wrapperX.and(wrapper ->
                    wrapper.like(ArticleContent::getTittle, dto.getSearchKey()).or()
                            .like(ArticleContent::getContent, dto.getSearchKey()));
        }
        wrapperX.eq(ArticleContent::getLanguage, dto.getLanguage());
        wrapperX.orderByDesc(ArticleContent::getSubmitTime);
        IPage<ArticleContent> iPage = articleContentService.page((IPage<ArticleContent>) dto.getPage(), wrapperX);
        return getDataTable(iPage);
    }

    /**
     * 文章根据id查询详情
     *
     * @param id
     * @return
     */
    @ApiOperation("文章根据id查询详情")
    @GetMapping("/article/detail")
    public AjaxResult detail(@RequestParam Long id) {
        ArticleContent result = articleContentService.getById(id);
        return success(result);
    }

    /**
     * 导游信息分页查询
     * @param dto
     * @return
     */
    @GetMapping("/tourGuide/info/page")
    public TableDataInfo infoPage(TourGuidePageReqDto dto){
        IPage<TourGuideInfo> iPage = tourGuideInfoService.getPage(dto);
        return getDataTable(iPage);
    }

    /**
     * 导游预约新增
     * @param dto
     * @return
     */
    @PostMapping("/tourGuide/appointment/add")
    public AjaxResult appointmentAdd(@RequestBody TourGuideAppointmentAddReqDto dto){
        tourGuideAppointmentService.add(dto);
        return success();
    }

    /**
     * 获取问卷
     *
     * @return
     */
    @GetMapping("/questionnaire/getQuestionnaire")
    public AjaxResult getQuestionnaire() {
        List<QuestionnaireDto> result = questionnaireConfigService.getQuestionnaire();
        return success(result);
    }

    /**
     * 提交问卷
     *
     * @param dto
     * @return
     */
    @PostMapping("/questionnaire/submitQuestionnaire")
    public AjaxResult submitQuestionnaire(@RequestBody QuestionnaireSubmitReqDto dto) {
        questionnaireSubmitService.submitQuestionnaire(dto.getQuestionnaireList());
        return success();
    }

    /**
     * 提交投诉建议
     * @param dto
     * @return
     */
    @PostMapping("/complaint/suggestions/add")
    public AjaxResult complaintSuggestionsAdd(@RequestBody ComplaintSuggestionsAddReqDto dto){
        ComplaintSuggestions complaintSuggestions = Convert.convert(ComplaintSuggestions.class, dto);
        complaintSuggestionsService.save(complaintSuggestions);
        return success();
    }

    /**
     * 获取游客实时数量
     * @return
     */
    @GetMapping("/getTheRealTimeNumberOfVisitors")
    public AjaxResult getTheRealTimeNumberOfVisitors(){
        try {
            Long qty = ticketCheckService.getTheRealTimeNumberOfVisitors();
            return success(qty);
        } catch (Exception e) {
            return success(0);
        }
    }
}
