package com.ruoyi.app.order;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.DictUtils;
import jakarta.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.system.domain.PosOrder;
import com.ruoyi.system.mapper.InfoAddressMapper;
import com.ruoyi.system.utils.Auth;
import com.ruoyi.system.utils.JwtUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.InfoAddress;
import com.ruoyi.system.service.IInfoAddressService;
import com.ruoyi.common.utils.MessageUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * addressController
 *
 * @author ruoyi
 * @date 2023-05-24
 */
@RestController
@RequestMapping("/system/address")
public class InfoAddressController extends BaseController
{
    @Autowired
    private IInfoAddressService infoAddressService;
    @Autowired
    private InfoAddressMapper infoAddressMapper;

    /**
     * 返回谷歌地图key
     * @return
     */
    @Anonymous
    @GetMapping("/getGoogleMapKey")
    public AjaxResult getGoogleMapKey(){
        List<SysDictData> dictData= DictUtils.getDictCache("sys_googlemap_key");
        if (dictData != null && !dictData.isEmpty()) {
            return success("成功", dictData.get(0).getDictValue());
        }
        return error("未找到Google地图配置");
    }
    //删除收货地址
    @Anonymous
    @GetMapping("/deleaddress")
    public AjaxResult deleaddress(@RequestParam Long id)
    {
        return toAjax(infoAddressService.deleteById(id));
    }

    //获取用户收货地址详请
    @Anonymous
    @GetMapping("/getaddressxq")
    public AjaxResult getaddressxq(@RequestParam Long id)
    {
        InfoAddress address = infoAddressService.getById(id);
        return success(MessageUtils.message("no.obtained.success"), address);
    }
    //根据距离获取我的地址
    @Anonymous
    @Auth
    @GetMapping("/getzuijinaddress")
    public AjaxResult getzuijinaddress(@RequestHeader String token,
                                       @RequestParam BigDecimal longitude,
                                       @RequestParam BigDecimal latitude)
    {
        JwtUtil jwtUtil = new JwtUtil();
        String id = jwtUtil.getusid(token);
        List<InfoAddress> list = infoAddressMapper.getmyaddress(longitude,latitude, Integer.valueOf(id));
        return success(MessageUtils.message("no.obtained.success"), list);
    }
    //获取用户收货地址信息
    @Anonymous
    @Auth
    @GetMapping("/getaddress")
    public AjaxResult getaddress(@RequestHeader String token)
    {
        JwtUtil jwtUtil = new JwtUtil();
        String id = jwtUtil.getusid(token);
        QueryWrapper<InfoAddress> queryWrapper= new QueryWrapper<>();
        queryWrapper.eq("user_id",id);
        List<InfoAddress> list = infoAddressService.list(queryWrapper);
        return success(MessageUtils.message("no.obtained.success"), list);
    }

    //添加用户收货地址信息
    @Anonymous
    @Auth
    @PostMapping("/address")
    public AjaxResult address(@RequestHeader String token,@RequestBody InfoAddress infoAddress)
    {
        JwtUtil jwtUtil = new JwtUtil();
        String id = jwtUtil.getusid(token);
        infoAddress.setUserId(Long.valueOf(id));
        boolean org = infoAddressService.saveOrUpdate(infoAddress);
        if(org){
            return success();
        }else {
            return error();
        }

    }


    /**
     * 查询address列表
     */
    @PreAuthorize("@ss.hasPermi('system:address:list')")
    @GetMapping("/list")
    public TableDataInfo list(InfoAddress infoAddress)
    {
        startPage();
        List<InfoAddress> list = infoAddressService.selectInfoAddressList(infoAddress);
        return getDataTable(list);
    }

    /**
     * 导出address列表
     */
    @PreAuthorize("@ss.hasPermi('system:address:export')")
    @Log(title = "address", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, InfoAddress infoAddress)
    {
        List<InfoAddress> list = infoAddressService.selectInfoAddressList(infoAddress);
        ExcelUtil<InfoAddress> util = new ExcelUtil<InfoAddress>(InfoAddress.class);
        util.exportExcel(response, list, MessageUtils.message("no.export.excel.address"));
    }

    /**
     * 获取address详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:address:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(infoAddressService.selectInfoAddressById(id));
    }

    /**
     * 新增address
     */
    @PreAuthorize("@ss.hasPermi('system:address:add')")
    @Log(title = "address", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody InfoAddress infoAddress)
    {
        return toAjax(infoAddressService.insertInfoAddress(infoAddress));
    }

    /**
     * 修改address
     */
    @PreAuthorize("@ss.hasPermi('system:address:edit')")
    @Log(title = "address", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody InfoAddress infoAddress)
    {
        return toAjax(infoAddressService.updateInfoAddress(infoAddress));
    }

    /**
     * 删除address
     */
    @PreAuthorize("@ss.hasPermi('system:address:remove')")
    @Log(title = "address", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(infoAddressService.deleteInfoAddressByIds(ids));
    }
}
