package com.crm.custom.controller;

import com.crm.rely.backend.core.constant.FeignClientAnnotation;
import com.crm.rely.backend.core.dto.country.SysCountryDto;
import com.crm.rely.backend.core.entity.country.SysCountryEntity;
import com.crm.custom.service.SysCountryService;
import com.crm.rely.backend.core.dto.base.BaseResultDto;
import com.crm.rely.backend.util.GetIpAndMac;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author: houn
 */
@RestController
@RequestMapping("/country")
public class SysCountryController {

    @Autowired
    private SysCountryService sysCountryService;

    /**
     * 根据pid获取列表
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/get")
    public BaseResultDto getAllCountry(@RequestBody  SysCountryEntity entity) throws Exception {

        if (entity.getPid() == null) {
            entity.setPid(0L);
        }
        List<SysCountryDto> sysCountryDtos = sysCountryService.getByPid(entity);

        return BaseResultDto.success(sysCountryDtos);
    }

    @PostMapping("/manager/get")
    @FeignClientAnnotation
    public BaseResultDto getManagerAllCountry(@RequestBody  SysCountryEntity entity) throws Exception {

        if (entity.getPid() == null) {
            entity.setPid(0L);
        }
        List<SysCountryDto> sysCountryDtos = sysCountryService.getByPid(entity);

        return BaseResultDto.success(sysCountryDtos);
    }

    @PostMapping("/getCodeByIp")
    public BaseResultDto<String> getCodeByIp(HttpServletRequest request) throws Exception{
        String ip = GetIpAndMac.getIp(request);
//        String ip = "114.101.199.90";
        return BaseResultDto.success(sysCountryService.getCodeByIp(ip));
    }


}
