package com.fs.store.controller;

import java.util.List;

import com.fs.common.core.domain.R;
import com.fs.store.utils.CityTreeUtil;
import com.fs.store.vo.CityVO;
import com.google.common.collect.Lists;
import io.swagger.annotations.ApiOperation;
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.fs.common.annotation.Log;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.enums.BusinessType;
import com.fs.store.domain.FsCity;
import com.fs.store.service.IFsCityService;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.common.core.page.TableDataInfo;
import springfox.documentation.annotations.Cacheable;

import javax.servlet.http.HttpServletRequest;

/**
 * 城市Controller
 * 
 * @author fs
 * @date 2022-03-15
 */
@RestController
@RequestMapping("/store/city")
public class FsCityController extends BaseController
{
    @Autowired
    private IFsCityService fsCityService;

    /**
     * 查询城市列表
     */
    @PreAuthorize("@ss.hasPermi('store:city:list')")
    @GetMapping("/list")
    public TableDataInfo list(FsCity fsCity)
    {
        startPage();
        List<FsCity> list = fsCityService.selectFsCityList(fsCity);
        return getDataTable(list);
    }

    /**
     * 导出城市列表
     */
    @PreAuthorize("@ss.hasPermi('store:city:export')")
    @Log(title = "城市", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FsCity fsCity)
    {
        List<FsCity> list = fsCityService.selectFsCityList(fsCity);
        ExcelUtil<FsCity> util = new ExcelUtil<FsCity>(FsCity.class);
        return util.exportExcel(list, "city");
    }

    /**
     * 获取城市详细信息
     */
    @PreAuthorize("@ss.hasPermi('store:city:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(fsCityService.selectFsCityById(id));
    }

    /**
     * 新增城市
     */
    @PreAuthorize("@ss.hasPermi('store:city:add')")
    @Log(title = "城市", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FsCity fsCity)
    {
        return toAjax(fsCityService.insertFsCity(fsCity));
    }

    /**
     * 修改城市
     */
    @PreAuthorize("@ss.hasPermi('store:city:edit')")
    @Log(title = "城市", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FsCity fsCity)
    {
        return toAjax(fsCityService.updateFsCity(fsCity));
    }

    /**
     * 删除城市
     */
    @PreAuthorize("@ss.hasPermi('store:city:remove')")
    @Log(title = "城市", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(fsCityService.deleteFsCityByIds(ids));
    }


    @GetMapping("/getAllList")
    public R getAllList(FsCity fsCity)
    {
        List<FsCity> list = fsCityService.selectFsCityList(fsCity);
        return R.ok().put("data",list);
    }


    @ApiOperation("获取城市数据")
    @GetMapping("/getCitys")
    @Cacheable("citys")
    public R getCitys(HttpServletRequest request){
        List<FsCity> list=fsCityService.selectFsCitys();
        List<CityVO> cityVOS = Lists.newArrayList();
        for (FsCity city : list){
            CityVO cityVO = new CityVO();
            cityVO.setValue(city.getCityId());
            cityVO.setLabel(city.getName());
            cityVO.setPid(city.getParentId());
            cityVOS.add(cityVO);
        }
        return R.ok().put("data", CityTreeUtil.list2TreeConverter(cityVOS, 0));

    }


}
