package com.ruoyi.web.controller.backstage;


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.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.framework.web.domain.server.Sys;
import com.ruoyi.system.domain.OrderInfo;
import com.ruoyi.system.domain.SysOrder;
import com.ruoyi.system.domain.dto.*;
import com.ruoyi.system.service.IDailySysService;
import com.ruoyi.system.service.IOrderInfoService;
import com.ruoyi.system.service.ISysOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

@RestController
@RequestMapping("/system/market")
public class SysMarketController extends BaseController {

    @Autowired
    private IDailySysService dailySysService;

    @Autowired
    private ISysOrderService sysOrderService;

    @Autowired
    private IOrderInfoService orderInfoService;


    @GetMapping("/getStIncome")
    public AjaxResult getStIncome() {


        AjaxResult success = success();



//        //-------------------------------------------------真实接口-------------------------------------------------------------
//        LocalDate now = LocalDate.now();
//        LocalDate startDate = now.minusDays(2);
//        LocalDate endDate = now.minusDays(1);
//
//        List<SysIncomeDto> sysIncomeDtoList = dailySysService.getSysIncome(startDate, endDate);
//
//        Long income2 = sysIncomeDtoList.get(0).gettIncome();
//        Long income1 = sysIncomeDtoList.get(1).gettIncome();
//
//        Long incomeChange = income1 - income2;
//
//        success.put("income", income1);
//        success.put("incomeChange", incomeChange);
//
//
//
//        Long refund2 = sysIncomeDtoList.get(0).gettRefund();
//        Long refund1 = sysIncomeDtoList.get(1).gettRefund();
//        Long refundChange = refund1 - refund2;
//
//        success.put("refund", refund1);
//        success.put("refundChange", refundChange);
//
//        //---------------------------------------------真实接口end----------------------------------------------------------------


        success.put("income", 100L);
        success.put("incomeChange", 2L);

        success.put("refund", 50L);
        success.put("refundChange", 1L);


        return success ;
    }

    // 根据天数获取每天的收入额
    @GetMapping("/getStIncome/{day}")
    public AjaxResult getStIncome(@PathVariable int day) {

//        //------------------------------------真实接口----------------------------------------------
//        LocalDate now = LocalDate.now();
//        LocalDate startDate = now.minusDays(day+1);
//        LocalDate endDate = now.minusDays(1);
//        List<SysIncomeDto> sysIncomeDtoList = dailySysService.getSysIncome(startDate, endDate);
//        return success(sysIncomeDtoList);
//        //-----------------------------------真实接口end----------------------------------------------

        List<SysIncomeDto> sysIncomeDtoList = new ArrayList<>();
        for (int i = 0; i < day; i++) {
            SysIncomeDto sysIncomeDto = new SysIncomeDto();
            sysIncomeDto.setDate(LocalDate.now().minusDays(i+1));

            Random random = new Random();
            long minValue = 1000L;
            long maxValue = 99999999L;
            long range = maxValue - minValue + 1;
            long randomLong = minValue + (long) (random.nextDouble() * range);

            sysIncomeDto.settIncome(randomLong);
            sysIncomeDtoList.add(sysIncomeDto);
        }
        return success(sysIncomeDtoList);

    }

    // 获取订单列表
    @GetMapping("/getOrderList")
    public TableDataInfo getOrderList(SysOrder sysOrder) {

//        //------------------------------------真实接口----------------------------------------------
//        LocalDate now = LocalDate.now();
//        LocalDate startDate = now.minusDays(7);
//        LocalDate endDate = now.minusDays(1);
//        List<OrderIncomeDto> orderIncomeDtoList = dailySysService.getOrderIncome(startDate, endDate);
//        return success(orderIncomeDtoList);
//        //-----------------------------------真实接口end----------

        startPage();
        List<OrderListDto> orderListDtoList = sysOrderService.getOrderListDto(sysOrder);

        // 根据 order_info_id 查看订单所有的资源位
        for(OrderListDto orderListDto: orderListDtoList){

            String orderInfoId = orderListDto.getOrderInfoId();

            List<OrderInfoDto> orderInfoList = orderInfoService.getOrderInfoDtoList(orderInfoId);

            for (OrderInfoDto orderInfoDto: orderInfoList){
                orderInfoDto.setNickName(orderListDto.getNickName());
                orderInfoDto.setPhoneNumber(orderListDto.getPhonenumber());
                orderInfoDto.setSource(orderInfoDto.getCity()+orderInfoDto.getScenic()+orderInfoDto.getPart()+orderInfoDto.getCoordinate());
                orderInfoDto.setAvatar(orderListDto.getAvatar());
                orderInfoDto.setWay(orderListDto.getWay());
                orderInfoDto.setOrderId(orderListDto.getOrderId());
            }

            orderListDto.setOrderInfoList(orderInfoList);


        }

        return getDataTable(orderListDtoList);
    }

    //获取订单详情
    @Log(title = "营销管理--查看订单详情", businessType = BusinessType.GET)
    @PostMapping("/getOrderDetail")
    public AjaxResult getOrderDetail(@RequestBody GetOrderDetailDto getOrderDetailDto){

        // sys_order 中的 order_id
        Long orderId = getOrderDetailDto.getOrderId();

        // order_info 中的 id
        Long id = getOrderDetailDto.getId();

//        OrderInfoDto orderInfoDto = orderInfoService.getOrderInfoDto(id);

        OrderDetailDto orderDetailDto = sysOrderService.getOrderDetailDto(orderId, id);

        orderDetailDto.setSource(orderDetailDto.getCity()+orderDetailDto.getScenic()+orderDetailDto.getPart()+orderDetailDto.getCoordinate());
        orderDetailDto.setPresent(orderDetailDto.getStartTime() + " ~ " + orderDetailDto.getEndTime());

        Date now = new Date();

        orderDetailDto.setRemainTime(Math.max((orderDetailDto.getEndTime().getTime() - now.getTime()) / 1000, 0));

        return success(orderDetailDto);

    }




}
