package com.gic.haoban.manage.web.controller.contact;

import com.gic.api.base.commons.ServiceResponse;
import com.gic.commons.webapi.reponse.RestResponse;
import com.gic.haoban.base.api.common.pojo.dto.WebLoginDTO;
import com.gic.haoban.common.utils.AuthWebRequestUtil;
import com.gic.haoban.manage.api.dto.contact.ContactConfigDTO;
import com.gic.haoban.manage.api.service.contact.ContactConfigApiService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 建联配置
 */
@RestController
@RequestMapping("/contact")
public class ContactController {

    private static Logger logger = LogManager.getLogger(ContactController.class);

    @Autowired
    private ContactConfigApiService contactConfigApiService;

    /**
     * 建联配置
     *
     * @return RestResponse
     */
    @RequestMapping("get")
    @ResponseBody
    public RestResponse<ContactConfigDTO> get() {
        WebLoginDTO user = AuthWebRequestUtil.getLoginUser();
        ServiceResponse<ContactConfigDTO> response = contactConfigApiService.getContactConfig(user.getEnterpriseId());
        if (!response.isSuccess()) {
            return RestResponse.failure(response.getCode(), response.getMessage());
        }
        return RestResponse.successResult(response.getResult());
    }

    /**
     * 保存建联配置
     *
     * @return RestResponse
     */
    @RequestMapping("save")
    @ResponseBody
    public RestResponse<Void> save(@RequestBody ContactConfigDTO configDTO) {
        WebLoginDTO user = AuthWebRequestUtil.getLoginUser();
        configDTO.setEnterpriseId(user.getEnterpriseId());
        ServiceResponse<Void> response = contactConfigApiService.saveContactConfig(configDTO);
        if (!response.isSuccess()) {
            return RestResponse.failure(response.getCode(), response.getMessage());
        }
        return RestResponse.successResult();
    }

}
