package com.org.hktshop.controller.global;

import com.org.hktshop.Interface.global.IHktLanguageService;
import com.org.hktshop.base.BaseResult;
import com.org.hktshop.controller.BaseController;
import com.org.hktshop.entity.admin.HktAdmin;
import com.org.hktshop.entity.global.HktLanguage;
import com.org.hktshop.utils.HttpCode;
import com.org.hktshop.utils.Ret;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

@RestController
@RequestMapping("/Comm/language")
public class HktLanguageController extends BaseController {

    @Autowired
    IHktLanguageService languageService;

    @ApiOperation(value = "新增/修改语言")
    @PostMapping(value = "/updateLanguage")
    public Object updateLanguage(@RequestBody HktLanguage language, HttpServletRequest request) {
        try {
            HktAdmin admin = (HktAdmin) getSessionUserId(request);
            Ret ret = languageService.updateLanguage(admin, language);
            if (ret.isFail()) return BaseResult.fail(HttpCode.BAD_REQUEST.getCode(), ret.getStr("msg"));
            return BaseResult.success();
        } catch (Exception ex) {
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode());
        }
    }

    @ApiOperation(value = "删除语言")
    @PostMapping(value = "/deleteLanguage")
    public Object deleteLanguage(@RequestBody HktLanguage language, HttpServletRequest request) {
        try {
            HktAdmin admin = (HktAdmin) getSessionUserId(request);
            Ret ret = languageService.deleteLanguage(admin, language);
            if (ret.isFail()) return BaseResult.fail(HttpCode.BAD_REQUEST.getCode(), ret.getStr("msg"));
            return BaseResult.success();
        } catch (Exception ex) {
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode());
        }
    }

    @ApiOperation(value = "删除语言")
    @GetMapping(value = "/queryList")
    public Object queryList() {
        try {
            List<HktLanguage> list = languageService.queryList();
            return BaseResult.successWithData(list);
        } catch (Exception ex) {
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode());
        }
    }
}
