package com.org.hktshop.controller.user;

import com.org.hktshop.base.BaseResult;
import com.org.hktshop.controller.HktBaseController;
import com.org.hktshop.entity.user.HktUserContacts;
import com.org.hktshop.service.user.HktUserContactsService;
import com.org.hktshop.utils.HttpCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shop/userContacts")
@Api(tags = "用户联系人接口")
public class HktUserContactsController extends HktBaseController {

    @Autowired
    private HktUserContactsService userContactsService;

    @PostMapping("/saveUserContacts")
    @ApiOperation(value = "新增用户联系人", notes = "新增用户联系人")
    public BaseResult saveUserContacts(@RequestBody HktUserContacts hktUserContacts) {
        try {
            hktUserContacts.setUserId(userInfo.getId());
            userContactsService.saveHktUserContacts(hktUserContacts);
            return BaseResult.success();
        } catch (Exception ex) {
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode(), HttpCode.INTERNAL_SERVER_ERROR.getMsg());
        }
    }

    @PostMapping("/updateUserContacts")
    @ApiOperation(value = "更新用户联系人", notes = "更新用户联系人")
    public BaseResult updateUserContacts(@RequestBody HktUserContacts hktUserContacts) {
        try {
            hktUserContacts.setUserId(userInfo.getId());
            userContactsService.updateHktUserContacts(hktUserContacts);
            return BaseResult.success();
        } catch (Exception ex) {
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode(), HttpCode.INTERNAL_SERVER_ERROR.getMsg());
        }
    }

    @GetMapping("/deleteUserContacts")
    @ApiOperation(value = "通过id删除用户联系人", notes = "通过id删除用户联系人")
    @ApiImplicitParam(name = "id", value = "主键id", required = true, paramType = "query")
    public BaseResult deleteUserContacts(@RequestParam("id") String id) {
        try {
            HktUserContacts hktUserContacts=new HktUserContacts();
            hktUserContacts.setUserId(userInfo.getId());
            userContactsService.deleteHktUserContacts(hktUserContacts);
            return BaseResult.success();
        } catch (Exception ex) {
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode(), HttpCode.INTERNAL_SERVER_ERROR.getMsg());
        }
    }

    @GetMapping("/getUserContacts")
    @ApiOperation(value = "获取用户联系人信息", notes = "获取用户联系人信息")
    public BaseResult getUserContacts() {
        try {
            List<HktUserContacts> hktUserContacts = userContactsService.gethktHktUserContacts();
            return BaseResult.successWithData(hktUserContacts);
        } catch (Exception ex) {
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode(), HttpCode.INTERNAL_SERVER_ERROR.getMsg());
        }
    }
}
