package com.ruoyi.app.user;

import java.util.List;
import java.util.Objects;
import jakarta.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.system.domain.InfoUser;
import com.ruoyi.system.domain.VipUser;
import com.ruoyi.system.service.IInfoUserService;
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.RiderPosition;
import com.ruoyi.system.service.IRiderPositionService;
import com.ruoyi.common.utils.MessageUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * RiderPositionController
 *
 * @author ruoyi
 * @date 2023-07-31
 */
@RestController
@RequestMapping("/system/position")
public class RiderPositionController extends BaseController
{
    @Autowired
    private IRiderPositionService riderPositionService;
    @Autowired
    private IInfoUserService infoUserService;
    /**
     * 更新验手位置
     * @param token
     * @param riderPosition
     * @return
     */
    @Anonymous
    @Auth
    @RepeatSubmit(interval = 1000, message = "请求过于频繁")
    @PostMapping("/setRiderposition")
    public AjaxResult setRiderposition(@RequestHeader String token, @RequestBody RiderPosition riderPosition) {
        JwtUtil jwtUtil = new JwtUtil();
        String id = jwtUtil.getusid(token);
        System.out.println("setRiderposition更新定位，用户id"+id);
        logger.info("setRiderposition更新定位，用户id"+id);
        InfoUser user = infoUserService.getById(id);
        QueryWrapper<RiderPosition> queryWrapper= new QueryWrapper<>();
        queryWrapper.eq("rider_id",id);
        RiderPosition position = riderPositionService.getOne(queryWrapper);
        RiderPosition rider = new RiderPosition();
        if(position==null){
            rider.setRiderId(Long.valueOf(id));
            rider.setLatitude(riderPosition.getLatitude());
            rider.setLongitude(riderPosition.getLongitude());
            if(user.getCid()!=null && !Objects.equals(user.getCid(), "")){
                rider.setCid(user.getCid());
            }
            System.out.println("setRiderposition更新定位，用户id,新增信息："+ JSON.toJSONString( rider));
            logger.info("setRiderposition更新定位，用户id,新增信息："+ JSON.toJSONString( rider));
            Boolean org = riderPositionService.saveOrUpdate(rider);
            if(org){
                return success();
            }else {
                return error();
            }
        }else {
            rider.setId(position.getId());
            rider.setRiderId(Long.valueOf(id));
            rider.setLatitude(riderPosition.getLatitude());
            rider.setLongitude(riderPosition.getLongitude());
            if(user.getCid()!=null && !Objects.equals(user.getCid(), "")){
                rider.setCid(user.getCid());
            }
            System.out.println("setRiderposition更新定位，用户id,修改信息："+ JSON.toJSONString( rider));
            logger.info("setRiderposition更新定位，用户id,修改信息："+ JSON.toJSONString( rider));
            Boolean org = riderPositionService.saveOrUpdate(rider);
            if(org){
                return success();
            }else {
                return error();
            }
        }
    }

    /**
     * 查询RiderPosition列表
     */
    @PreAuthorize("@ss.hasPermi('system:position:list')")
    @GetMapping("/list")
    public TableDataInfo list(RiderPosition riderPosition)
    {
        startPage();
        List<RiderPosition> list = riderPositionService.selectRiderPositionList(riderPosition);
        return getDataTable(list);
    }

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

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

    /**
     * 新增RiderPosition
     */
    @PreAuthorize("@ss.hasPermi('system:position:add')")
    @Log(title = "RiderPosition", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RiderPosition riderPosition)
    {
        return toAjax(riderPositionService.insertRiderPosition(riderPosition));
    }

    /**
     * 修改RiderPosition
     */
    @PreAuthorize("@ss.hasPermi('system:position:edit')")
    @Log(title = "RiderPosition", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RiderPosition riderPosition)
    {
        return toAjax(riderPositionService.updateRiderPosition(riderPosition));
    }

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