package com.ruoyi.applet.contoller;


import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.model.domain.*;
import com.ruoyi.model.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Api(description = "订单模块 - 打卡记录")
@RestController
@RequestMapping("/applet/orderItemLog")
public class ApiAppletOrderItemLogController {

    @Autowired
    private IAppletOrderItemDateLogService appletOrderItemDateLogService;

    @Autowired
    private IAppletOrderService appletOrderService;
    @Autowired
    private IAppletOrderItemService appletOrderItemService;

    @Autowired
    private IAppletOrderItemDateService appletOrderItemDateService;

    @Autowired
    private IAppUsersService appUsersService;
    @Autowired
    private IAppletAmountLogService appletAmountLogService;



    //增加或者修改打卡记录
    @ApiOperation("增加或者修改打卡记录")
    @PostMapping("/addOrUpdate")
    @Transactional
    public AjaxResult addOrUpdate(@RequestBody AppletOrderItemDateLog appletOrderItemDateLog)
    {



        if(appletOrderItemDateLog.getId()!=null){
            int i = appletOrderItemDateLogService.updateAppletOrderItemDateLog(appletOrderItemDateLog);

        }else{

            int i = appletOrderItemDateLogService.insertAppletOrderItemDateLog(appletOrderItemDateLog);

        }

        if(appletOrderItemDateLog.getSubmitFlag()!= null){

            //修改日订单状态
            AppletOrderItemDate appletOrderItem = appletOrderItemDateService.selectAppletOrderItemDateById(appletOrderItemDateLog.getItemDateId());
            if(appletOrderItem !=null){
                appletOrderItem.setStatus(appletOrderItemDateLog.getSubmitFlag());
                appletOrderItemDateService.updateAppletOrderItemDate(appletOrderItem);
            }




            //修改总订单标识
            AppletOrderItem appletOrder = appletOrderItemService.selectAppletOrderItemById(appletOrderItemDateLog.getItemOrderId());

            if(appletOrder !=null){
                //查询该订单下的所有日订单
                AppletOrderItemDate date = new AppletOrderItemDate();
                date.setOrderId(appletOrder.getId());
                List<AppletOrderItemDate> appletOrderItemDateList = appletOrderItemDateService.selectAppletOrderItemDateList(date);
                //总日订单数量
                int count = appletOrderItemDateList.size();


                AppletOrderItemDate date1 = new AppletOrderItemDate();
                date1.setOrderId(appletOrder.getId());
                date1.setStatus(0);
                List<AppletOrderItemDate> appletOrderItemDateListDate1 = appletOrderItemDateService.selectAppletOrderItemDateList(date1);
                //状态为0的订单数量
                int count1 = appletOrderItemDateListDate1.size();


                AppletOrderItemDate date2 = new AppletOrderItemDate();
                date2.setOrderId(appletOrder.getId());
                date2.setStatus(1);
                List<AppletOrderItemDate> appletOrderItemDateListDate2 = appletOrderItemDateService.selectAppletOrderItemDateList(date2);
                //状态为1的订单数量
                int count2 = appletOrderItemDateListDate2.size();

                System.out.println("总日订单数量"+count);
                System.out.println("状态为0的订单数量"+count1);
                System.out.println("状态为1的订单数量"+count2);


                //如果count = count2
                if(count == count2){
                    //修改总订单状态
                    System.out.println("OrderStatus:"+2);
                    appletOrder.setOrderStatus(2);
                    appletOrderItemService.updateAppletOrderItem(appletOrder);


                    //根据订单中的用户id查询用户信息
                    AppUsers user = appUsersService.selectAppUsersByUserId(appletOrder.getUserId());
                    //增加伴宠师流水

                    // 记录日志
                    AppletAmountLog moneyLog = new AppletAmountLog();
                    moneyLog.setUserId(user.getUserId());
                    moneyLog.setTitle("伴宠师佣金");
                    moneyLog.setAmount(appletOrder.getOrderGivePrice());
                    moneyLog.setType(0); //收入
                    moneyLog.setState(1); //到账
                    moneyLog.setCreateTime(LocalDateTime.now());
                    moneyLog.setMoneyType(1);//钱包类型
                    int i = appletAmountLogService.insertAppletAmountLog(moneyLog);
                    user.setPrice(user.getPrice().add(appletOrder.getOrderGivePrice()));
                    appUsersService.updateAppUsers(user);


                }
                //如果count2 >0 并且 count1 >0
                if(count2 >0 && count1 >0){
                    //修改总订单状态
                    System.out.println("OrderStatus:"+1);
                    appletOrder.setOrderStatus(1);
                    appletOrderItemService.updateAppletOrderItem(appletOrder);
                }
            }

        }
        return AjaxResult.success("打卡成功");
    }


    //根据订单标识查询打卡记录
    @ApiOperation("根据订单标识查询打卡记录")
    @GetMapping(value = "/getByOrderId/{itemDateId}")
    public AjaxResult getInfo(@PathVariable("itemDateId") Long itemDateId) {
        AppletOrderItemDateLog appletOrderItemDateLog = new AppletOrderItemDateLog();
        appletOrderItemDateLog.setItemDateId(itemDateId);
        AppletOrderItemDateLog appletOrderItemDateLog1 = appletOrderItemDateLogService.selectAppletOrderItemDateLogByItemDateId(itemDateId);
        return AjaxResult.success(appletOrderItemDateLog1);
    }













}
