package com.yingying.tourist.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.yingying.tourist.business.BusinessConstant;
import com.yingying.tourist.common.Util;
import com.yingying.tourist.domain.Config;
import com.yingying.tourist.domain.Question;
import com.yingying.tourist.domain.User;
import com.yingying.tourist.exception.ErrorCode;
import com.yingying.tourist.result.MessageResult;
import com.yingying.tourist.service.ConfigService;
import com.yingying.tourist.service.QuestionService;
import com.yingying.tourist.service.UserService;
import com.yingying.tourist.vo.QuestionVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class QuestionController {

    @Autowired
    private QuestionService questionService;

    @Autowired
    private UserService userService;

    @Autowired
    private ConfigService configService;

    @GetMapping("question/pick")
    public MessageResult<QuestionVo> question(String openid, String pick){
        if (Util.isNull(pick)) {
            return new MessageResult<QuestionVo>().failure(ErrorCode.PARAMETERS_ERROR);
        }
        QuestionVo questionVo = new QuestionVo();
        String[] pickList = pick.split(",");
        String pickLast = pickList[3];
        String pickBegin = pickList[0] + pickList[1] + pickList[2];
        EntityWrapper<Question> questionWrapper = new EntityWrapper<>();
        questionWrapper.eq("question_path", pickBegin);
        Question question = questionService.selectOne(questionWrapper);
        BeanUtils.copyProperties(question, questionVo);
        questionVo.setResultDestinationInfo(BusinessConstant.destinationMap.get(question.getResultOrder()));
        questionVo.setResultDescribe(BusinessConstant.describeMap.get(Util.getRandomInt(1,16)));
        questionVo.setResultIdentity(BusinessConstant.Identity.getValue(pickLast));
        EntityWrapper<Config> configWrapper = new EntityWrapper<>();
        configWrapper.eq("config_key", "h5_supernatant_show");
        Config config = configService.selectOne(configWrapper);
        questionVo.setShow("1".equals(config.getConfigValue()));
        if (Util.isNotNull(openid)) {
            EntityWrapper<User> userWrapper = new EntityWrapper<>();
            userWrapper.eq("openid", openid);
            User user = userService.selectOne(userWrapper);
            questionVo.setNickname(user.getNickname());
        }
        return new MessageResult<QuestionVo>().ok(questionVo);
    }
}
