package cn.iocoder.yudao.module.rescue.app.controller.admin;

import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.security.core.LoginUser;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.rescue.core.controller.BaseController;
import cn.iocoder.yudao.module.rescue.core.page.TableDataInfo;
import cn.iocoder.yudao.util.RedisCache;
import cn.iocoder.yudao.module.rescue.core.text.HttpStatus;
import cn.iocoder.yudao.module.rescue.domain.DriverInfo;
import cn.iocoder.yudao.module.rescue.domain.RescueInfo;
import cn.iocoder.yudao.module.rescue.domain.RescueInfoDetail;
import cn.iocoder.yudao.module.rescue.domain.RescueRefuelRecord;
import cn.iocoder.yudao.module.rescue.service.IDriverInfoService;
import cn.iocoder.yudao.module.rescue.service.IRescueDriverInfoService;
import cn.iocoder.yudao.module.rescue.service.IRescueInfoService;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static cn.iocoder.yudao.module.rescue.service.impl.RescueDriverInfoServiceImpl.Redis_Driver_Key;

/**
 * app司机端接口
 *
 * @author zcy
 * @date 2023-07-14
 */
@RestController
@RequestMapping("/app/driver")
public class RescueDriverController extends BaseController {
    @Autowired
    private IRescueDriverInfoService rescueDriverInfoService;
    @Autowired
    private IDriverInfoService driverInfoService;
    @Autowired
    private IRescueInfoService rescueInfoService;
    @Autowired
    private RedisCache redisCache2;
    @Autowired
    private AdminUserApi userService;

    //获取司机状态
    @GetMapping("/getStatus")
    public CommonResult getStatus(Long driverId) {
        return success(rescueDriverInfoService.getStatus(driverId));
    }

    //司机数据统计
    @GetMapping("/driverInfoStatistics")
    public CommonResult driverInfoStatistics(Long driverId) {
        return success(rescueDriverInfoService.driverInfoStatistics(driverId));
    }

    //司机待救援列表
    @GetMapping("/driverRescueList")
    public CommonResult driverRescueList() {
        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        DriverInfo driverInfo = driverInfoService.getOne(new LambdaQueryWrapper<DriverInfo>().eq(DriverInfo::getUserId, loginUser.getId()));
        return success(rescueDriverInfoService.driverRescueList(driverInfo.getId()));
    }

    //司机进行中的 已完成的救援信息
    @GetMapping("/driverRescuePage")
    public CommonResult driverRescuePage(RescueInfo rescueInfo,
                                         @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                         @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        DriverInfo driverInfo = driverInfoService.getOne(new LambdaQueryWrapper<DriverInfo>().eq(DriverInfo::getUserId, loginUser.getId()));
        rescueInfo.setDriverId(driverInfo.getId());
        Page<RescueInfo> page = new Page<>(pageNum, pageSize);
        if (rescueInfo.getRescueStatus().equals("2")) {
            IPage<RescueInfo> rescueInfos = rescueDriverInfoService.driverRescueList2(driverInfo.getId(), page);
            return success(rescueInfos);
        } else {
            IPage<RescueInfo> rescueInfos = rescueDriverInfoService.driverRescuePage2(rescueInfo, page);
            return success(rescueInfos);
        }

    }

    //司机待救援列表
    @PostMapping("/updateWork")
    public CommonResult updateWork(Long driverId) throws Exception {
        DriverInfo driverInfo = driverInfoService.getById(driverId);
        if (driverInfo.getDriverStatus().equals("2")) {
            driverInfo.setDriverStatus("1");
        } else {
            LambdaQueryWrapper<RescueInfo> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(RescueInfo::getDriverId, driverId).eq(RescueInfo::getRescueStatus, "3").last("limit 1");
            RescueInfo one = rescueInfoService.getOne(queryWrapper);
            if (!ObjectUtil.isEmpty(one)) {
                throw new Exception("有订单进行中,不可暂停");
            }
            driverInfo.setDriverStatus("2");
        }
        AdminUserRespDTO sysUser = userService.getUser(driverInfo.getUserId());
        //所在顶级机构
        String redisKey = Redis_Driver_Key + sysUser.getDeptId() + ":" + driverId;
        if (!redisCache2.hasKey(redisKey)) {
            redisCache2.setCacheMapValue(redisKey, "status", driverInfo.getDriverStatus());
        }
        driverInfoService.updateById(driverInfo);
        return success();
    }

    //司机接受或拒绝 choose  0拒绝1接受
    @PostMapping("/driverAccept")
    public CommonResult driverAccept(Long rescueDriverId, String choose, String rejectReason) throws Exception {
        rescueDriverInfoService.driverAccept(rescueDriverId, choose, rejectReason);
        return success("查询成功");
    }

    //救援信息详情
    @GetMapping("/rescueDetail")
    public CommonResult rescueDetail(Long rescueId) {
        return success(rescueDriverInfoService.rescueDetail(rescueId));
    }


    //救援信息详情
    @PostMapping("/uploadDetailByDriver")
    public CommonResult uploadDetailByDriver(@RequestBody RescueInfoDetail rescueInfoDetail) throws Exception {
        rescueDriverInfoService.uploadDetailByDriver(rescueInfoDetail);
        return success("成功");
    }

    //设置应收金额
    @PostMapping("/setOrderMoney")
    public CommonResult setOrderMoney(@RequestParam("rescueId") String rescueId, @RequestParam("setMoney") Double setMoney) throws Exception {
        rescueDriverInfoService.setOrderMoney(Long.parseLong(rescueId), setMoney);
        return success("成功");
    }

    //完成救援
    @PostMapping("/endRescue")
    public CommonResult endRescue(@RequestParam("rescueId") String rescueId) throws Exception {
        rescueDriverInfoService.endRescue(Long.parseLong(rescueId));
        return success("成功");
    }

    //上传加油记录
    @PostMapping("/addRefuelRecord")
    public CommonResult addRefuelRecord(@RequestBody RescueRefuelRecord rescueRefuelRecord) throws Exception {
        rescueDriverInfoService.addRefuelRecord(rescueRefuelRecord);
        return success("成功");
    }

    //获取加油记录
    @GetMapping("/listRefuelRecord")
    public CommonResult listRefuelRecord(RescueRefuelRecord rescueRefuelRecord,
                                         @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                         @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<RescueRefuelRecord> page = new Page<>(pageNum, pageSize);
        IPage<RescueRefuelRecord> rescueRefuelRecords = rescueDriverInfoService.listRefuelRecord(rescueRefuelRecord, page);
        return success(rescueRefuelRecords);
    }

    protected TableDataInfo getDataTable(List<?> list) {
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(list);
        rspData.setTotal(new PageInfo(list).getTotal());
        return rspData;
    }
}
