package com.fs.users.controller;

import com.alibaba.fastjson.JSON;
import com.fs.common.annotation.Log;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.domain.R;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.enums.BusinessType;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.store.domain.FsUserAddress;
import com.fs.store.dto.AddressInfoDTO;
import com.fs.store.service.IFsUserAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户地址Controller
 *
 * @author fs
 * @date 2022-03-15
 */
@RestController
@RequestMapping("/store/userAddress")
public class FsUserAddressController extends BaseController
{
    @Autowired
    private IFsUserAddressService fsUserAddressService;

    /**
     * 查询用户地址列表
     */
    @GetMapping("/list")
    public TableDataInfo list(FsUserAddress fsUserAddress)
    {
        startPage();
        List<FsUserAddress> list = fsUserAddressService.selectFsUserAddressList(fsUserAddress);
        return getDataTable(list);
    }

    /**
     * 导出用户地址列表
     */
    @Log(title = "用户地址", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FsUserAddress fsUserAddress)
    {
        List<FsUserAddress> list = fsUserAddressService.selectFsUserAddressList(fsUserAddress);
        ExcelUtil<FsUserAddress> util = new ExcelUtil<FsUserAddress>(FsUserAddress.class);
        return util.exportExcel(list, "userAddress");
    }

    /**
     * 获取用户地址详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(fsUserAddressService.selectFsUserAddressById(id));
    }

    /**
     * 新增用户地址
     */
    @Log(title = "用户地址", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FsUserAddress fsUserAddress)
    {
        return toAjax(fsUserAddressService.insertFsUserAddress(fsUserAddress));
    }

    /**
     * 修改用户地址
     */
    @Log(title = "用户地址", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FsUserAddress fsUserAddress)
    {
        return toAjax(fsUserAddressService.updateFsUserAddress(fsUserAddress));
    }

    /**
     * 删除用户地址
     */
    @Log(title = "用户地址", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(fsUserAddressService.deleteFsUserAddressByIds(ids));
    }
    @GetMapping("/getAddressList")
    public R getAddressList(FsUserAddress fsUserAddress)
    {
        fsUserAddress.setIsDel(0);
        List<FsUserAddress> list = fsUserAddressService.selectFsUserAddressList(fsUserAddress);
        return R.ok().put("data", list);
    }

    @GetMapping(value = "/getAddress/{address}")
    public AjaxResult getAddress(@PathVariable("address") String address)
    {
        String kdnAddress = fsUserAddressService.getKdnAddress(address);
        AddressInfoDTO addressInfoDTO = JSON.parseObject(kdnAddress, AddressInfoDTO.class);
        AddressInfoDTO.AddressData data = addressInfoDTO.getData();
        String provinceName = data.getProvinceName();
        if (!provinceName.contains("省") && !provinceName.contains("区")){
            data.setProvinceName(data.getProvinceName()+"市");
            if (data.getExpAreaName().contains("县")){
                data.setCityName("县");
            }else {
                data.setCityName("市辖区");
            }
        }
        return AjaxResult.success(addressInfoDTO);
    }
}
