package com.ruoyi.project.business.controller;

import java.util.Collections;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.framework.annotation.DataUpdateCheck;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.business.domain.TbCountry;
import com.ruoyi.project.business.service.ITbCountryService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 国家Controller
 * 
 * @author ruoyi
 * @date 2024-04-15
 */
@RestController
@RequestMapping("/user/country")
public class TbCountryController extends BaseController
{
    @Autowired
    private ITbCountryService tbCountryService;

    /**
     * 查询国家列表
     */
    @GetMapping("/list")
    public TableDataInfo list(TbCountry tbCountry)
    {
        startPage();
        List<TbCountry> list = tbCountryService.selectTbCountryList(tbCountry);
        return getDataTable(list);
    }

    /**
     * 查询所有国家列表
     */
    @GetMapping("/getAllNationList")
    public AjaxResult getAllNationList()
    {
        return success(tbCountryService.getAllNationList());
    }

    /**
     * 查询所有国家列表
     */
    @GetMapping("/getAllNationNeedLanguageList/{language}")
    public AjaxResult getAllNationNeedLanguageList(@PathVariable String language)
    {
        return success(tbCountryService.getAllNationNeedLanguageList(language));
    }


    /**
     * 根据区域id获取国家列表
     * 使用数组传参是为了兼容以前的传单值的方式
     * 
     * @param areaIds 区域ids
     */
    @GetMapping("/getCountryByAreaId/{areaIds}")
    public AjaxResult getCountryByAreaIds(@PathVariable Long[] areaIds)
    {
        return success(tbCountryService.getCountryByAreaIds(areaIds));
    }
    
    /**
     * 导出国家列表
     */
    @Log(title = "国家", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbCountry tbCountry)
    {
        List<TbCountry> list = tbCountryService.selectTbCountryList(tbCountry);
        ExcelUtil<TbCountry> util = new ExcelUtil<TbCountry>(TbCountry.class);
        util.exportExcel(response, list, "国家数据");
    }

    /**
     * 获取国家详细信息
     */
    @GetMapping(value = "/{countryId}")
    public AjaxResult getInfo(@PathVariable("countryId") Long countryId)
    {
        return success(tbCountryService.selectTbCountryByCountryId(countryId));
    }

    /**
     * 新增国家
     */
    @Log(title = "国家", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbCountry tbCountry)
    {
        return toAjax(tbCountryService.insertTbCountry(tbCountry));
    }

    /**
     * 修改国家
     */
    @Log(title = "国家", businessType = BusinessType.UPDATE)
    @PutMapping
    @DataUpdateCheck(key = "country_id", tableName = "tb_country")
    public AjaxResult edit(@RequestBody TbCountry tbCountry)
    {
        return toAjax(tbCountryService.updateTbCountry(tbCountry));
    }

    /**
     * 删除国家
     */
    @Log(title = "国家", businessType = BusinessType.DELETE)
	@DeleteMapping("/{countryIds}")
    public AjaxResult remove(@PathVariable Long[] countryIds)
    {
        return toAjax(tbCountryService.deleteTbCountryByCountryIds(countryIds));
    }
}
