package com.ruoyi.applet.contoller;


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

@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;






    //增加或者修改打卡记录
    @ApiOperation("增加或者修改打卡记录")
    @PostMapping("/addOrUpdate")
    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){
            //修改总订单标识
            AppletOrderItem appletOrder = appletOrderItemService.selectAppletOrderItemById(appletOrderItemDateLog.getItemOrderId());

            if(appletOrder !=null){
                appletOrder.setOrderStatus(appletOrderItemDateLog.getSubmitFlag());
                appletOrderItemService.updateAppletOrderItem(appletOrder);
            }



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


        }
        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);
    }










}
