package com.sqx.modules.travelconf.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sqx.common.utils.DateUtils;
import com.sqx.common.utils.Result;
import com.sqx.modules.travelconf.entity.TravelConf;
import com.sqx.modules.travelconf.service.TravelConfService;
import com.sqx.modules.travelconf.vo.TravelPriceVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;

/**
 * @author bai
 * @date 2024/06/08
 */
@RestController
@Api(value = "出行管理", tags = { "出行管理" })
@RequestMapping(value = "/travelConf")
public class TravelConfController {

    @Autowired
    private TravelConfService service;

    @GetMapping("/list")
    @ApiOperation("获取数据列表")
    public Result list(TravelConf entity, Integer page,Integer limit){
        QueryWrapper<TravelConf> qw = new QueryWrapper<>();

        if(entity != null) {
            //价格类型
            qw.lambda().eq(entity.getPricingType() != null, TravelConf::getPricingType, entity.getPricingType());
            //出行方式
            qw.lambda().eq(entity.getTravelType() != null, TravelConf::getTravelType, entity.getTravelType());
            //时令
            qw.lambda().eq(entity.getSeasonsType() != null, TravelConf::getSeasonsType, entity.getSeasonsType());
            //时段
            qw.lambda().eq(entity.getTimeIntervalType() != null, TravelConf::getTimeIntervalType, entity.getTimeIntervalType());
        }

//        qw.orderByAsc("sort");
        IPage<TravelConf> pageList = service.page(new Page<>(page,limit),qw);
        return Result.success().put("data",pageList);
    }

    @PostMapping(value = "/add")
    @ApiOperation("新增")
    public Result add(@RequestBody TravelConf entity){
        entity.setCreateTime(DateUtil.now());
        entity.setUpdateTime(DateUtil.now());
        return Result.success().put("data",service.save(entity));
    }

    @PostMapping(value = "/update")
    @ApiOperation("修改")
    public Result update(@RequestBody TravelConf entity){
        entity.setUpdateTime(DateUtil.now());
        return Result.success().put("data",service.updateById(entity));
    }

    @RequestMapping("/info/{id}")
    public Result info(@PathVariable("id") Long id){
        TravelConf data = service.getById(id);
        return Result.success().put("data", data);
    }

    @DeleteMapping(value = "/deleteById")
    @ApiOperation("删除")
    public Result deleteById(Long id){
        service.removeById(id);
        return Result.success();
    }

    @DeleteMapping(value = "/deleteByIds")
    @ApiOperation("批量删除")
    public Result deleteByIds(String id){
        if(StringUtils.isNotBlank(id)){
            String[] ids = StringUtils.split(id,",");
            service.removeByIds(Arrays.asList(ids));
        }
        return Result.success();
    }

    @GetMapping(value = "/getTaxiMoney")
    @ApiOperation("测试获取价格")
    public Result getTaxiMoney(Long artificerId, String toLongitude, String toLatitude){
        TravelPriceVo travelPriceVo = service.calcTravelPrice(artificerId, toLongitude, toLatitude, DateUtils.format(new Date()));
        return Result.success().put("data", travelPriceVo);
    }

}