package com.ruoyi.database.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUserByPhone;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.database.domain.ParkingBillInfo;
import com.ruoyi.database.domain.vo.ParkingBillInfoVO;
import com.ruoyi.database.service.ParkingBillInfoService;
import com.ruoyi.framework.web.service.TokenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RequestMapping("/ParkingBillInfo")
@Api(tags = "订单管理")
@RestController
@RequiredArgsConstructor
public class ParkingBillInfoController extends BaseController {

    private final ParkingBillInfoService parkingBillInfoService;
    private final TokenService tokenService;

    @GetMapping
    @ApiOperation("查询订单管理")
    public TableDataInfo<ParkingBillInfo> list(ParkingBillInfo parkingBillInfo, HttpServletRequest request) {
        String phone = null;
        try {
            LoginUserByPhone loginUserByPhone = tokenService.getLoginUserByPhone(request);
            phone = loginUserByPhone.getPhone();
        } catch (Exception e) {
        }
        PageUtils.startPage();
        QueryWrapper<ParkingBillInfo> queryWrapper = new QueryWrapper<>(parkingBillInfo);
        if (phone != null && !"".equals(phone)){
            queryWrapper.eq("phone",phone);
        }
        Integer timeRange = parkingBillInfo.getTimeRange();
        if (timeRange != null){
            Date now = new Date();

            if (timeRange == 1) { // 当天
                // 获取当天开始和结束时间
                Date beginOfDay = DateUtil.beginOfDay(now);
                Date endOfDay = DateUtil.endOfDay(now);
                queryWrapper.between("create_time", beginOfDay, endOfDay);

            } else if (timeRange == 2) { // 一个月内
                Date oneMonthAgo = DateUtil.offsetMonth(now, -1);
                queryWrapper.ge("create_time", oneMonthAgo);

            } else if (timeRange == 3) { // 三个月内
                Date threeMonthsAgo = DateUtil.offsetMonth(now, -3);
                queryWrapper.ge("create_time", threeMonthsAgo);
            }
        }
        List<ParkingBillInfo> list = parkingBillInfoService.list(queryWrapper);
        List<ParkingBillInfoVO> parkingBillInfoVOS = new ArrayList<>();
        list.forEach(b -> {
            ParkingBillInfoVO parkingBillInfoVO = new ParkingBillInfoVO();
            BeanUtils.copyProperties(b, parkingBillInfoVO);
            parkingBillInfoVO.setTotalCostYuan(b.getTotalCostYuan().toString() + "元");
            parkingBillInfoVO.setPayMoneyYuan(b.getPayMoneyYuan().toString() + "元");
            parkingBillInfoVO.setDeductMoneyYuan(b.getDeductMoneyYuan().toString() + "元");
            parkingBillInfoVO.setParkPeriodTime(minutesToDHMS(b.getParkPeriodTime()));
            parkingBillInfoVOS.add(parkingBillInfoVO);
        });
        long size = parkingBillInfoService.count(queryWrapper);
        return getDataTableEnhance(parkingBillInfoVOS,size);
    }

    @PostMapping
    @ApiOperation("新增订单管理")
    @Log(title = "订单管理表", businessType = BusinessType.INSERT)
    public AjaxResult insert(@RequestBody ParkingBillInfo ParkingBillInfo) {
        return toAjax(parkingBillInfoService.save(ParkingBillInfo));
    }

    @PutMapping
    @ApiOperation("修改订单管理")
    @Log(title = "订单管理表", businessType = BusinessType.UPDATE)
    public AjaxResult update(@RequestBody ParkingBillInfo ParkingBillInfo) {
        return toAjax(parkingBillInfoService.updateById(ParkingBillInfo));
    }

    @DeleteMapping
    @ApiOperation("删除订单管理")
    @Log(title = "订单管理表", businessType = BusinessType.DELETE)
    public AjaxResult delete(@RequestParam("idList") List<Long> idList) {
        return toAjax(parkingBillInfoService.removeByIds(idList));
    }


    public static String minutesToDHMS(int minutes) {
        int days = minutes / 1440;          // 1440 = 24 * 60
        int hours = (minutes % 1440) / 60;
        int mins = minutes % 60;
        int secs = 0;

        return days + "天" +
                String.format("%02d", hours) + "小时" +
                String.format("%02d", mins) + "分钟" +
                String.format("%02d", secs) + "秒";
    }
}
