package com.yonge.cooleshow.teacher.controller;

import com.alibaba.fastjson.JSON;
import com.microsvc.toolkit.common.response.template.R;
import com.yonge.cooleshow.admin.io.request.SysSuggestionVo;
import com.yonge.cooleshow.auth.api.client.SysUserFeignService;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.biz.dal.entity.SysSuggestion;
import com.yonge.cooleshow.biz.dal.entity.SysSuggestionV2;
import com.yonge.cooleshow.biz.dal.enums.ClientEnum;
import com.yonge.cooleshow.biz.dal.service.SysSuggestionService;
import com.yonge.cooleshow.biz.dal.service.SysSuggestionV2Service;
import com.yonge.cooleshow.common.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@Api(tags = "意见反馈")
@RestController
@RequestMapping("${app-config.url.teacher:}")
public class SysSuggestionController extends BaseController {

    @Autowired
    private SysSuggestionService sysSuggestionService;
    @Autowired
    private SysUserFeignService sysUserFeignService;

    @Autowired
    private SysSuggestionV2Service sysSuggestionV2Service;

    @ApiOperation(value = "新增")
    @RequestMapping("sysSuggestion/add")
    public Object add(SysSuggestion sysSuggestion) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed("用户信息获取失败");
        }
        sysSuggestion.setUserId(sysUser.getId().longValue());
        sysSuggestion.setClientType("TEACHER");
        if(StringUtils.isEmpty(sysSuggestion.getMobileNo())){
            sysSuggestion.setMobileNo(sysUser.getPhone());
        }
        sysSuggestionService.insert(sysSuggestion);
        return succeed();
    }


    /**
     * 新增
     *
     * @param suggestion SysSuggestionVo.SysSuggestion
     * @return R<Boolean>
     */
    @ApiOperation(value = "新增", notes = "传入sysSuggestion")
    @PostMapping("sysSuggestion/save")
    public R<Boolean> add(@Validated @RequestBody SysSuggestionVo.SysSuggestion suggestion,
                          @RequestHeader(name = "user-agent") String userAgent) {
        com.yonge.cooleshow.auth.api.entity.SysUser sysUser = sysUserFeignService.queryUserInfo();
        suggestion.setUserId(sysUser.getId());
        String userAgent1 = suggestion.getUserAgent();
        if(StringUtils.isEmpty(userAgent1)){
            userAgent1 = userAgent;
        }
        suggestion.setUserAgent(userAgent1);
        suggestion.setClientType(ClientEnum.TEACHER);

        SysSuggestionV2 sysSuggestion = JSON.parseObject(suggestion.jsonString(), SysSuggestionV2.class);
        sysSuggestion.setCreateTime(new Date());
        return R.from(sysSuggestionV2Service.add(sysSuggestion));
    }

}
