package com.yuchen.saas.park.service.controller.pldoor;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.yuchen.saas.park.api.dto.pldoor.PlDoorPointDTO;
import com.yuchen.saas.park.api.dto.pldoor.PlPsOrderDTO;
import com.yuchen.saas.park.api.entity.pldoor.PlPsOrder;
import com.yuchen.saas.park.api.entity.pldoor.PlPsOrderImg;
import com.yuchen.saas.park.api.vo.request.PlDoorPointReq;
import com.yuchen.saas.park.api.vo.request.PlPsOrderReq;
import com.yuchen.saas.park.service.service.pldoor.PlPsOrderImgService;
import com.yuchen.saas.park.service.service.pldoor.PlPsOrderService;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.nest.springwrap.core.mp.support.NestPage;
import org.nest.springwrap.core.mp.support.Query;
import org.nest.springwrap.core.tool.api.R;
import org.nest.springwrap.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 智慧配送订单 前端控制器
 * </p>
 *
 * @author miaoqs
 * @since 2024-07-31
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/plPsOrder")
public class PlPsOrderController {

    private final PlPsOrderService plPsOrderService;

    private final PlPsOrderImgService plPsOrderImgService;

    /**
     * 智慧配送订单 新增或修改
     */
    @PostMapping("/saveOrUpdate")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "智慧配送订单 新增或修改", notes = "智慧配送订单 新增或修改")
    public R saveOrUpdate(@Valid @RequestBody PlPsOrder plPsOrder) {
        if(Func.isEmpty(plPsOrder)) {
            return R.error("智慧配送订单不能为空");
        }
        return R.data(plPsOrderService.saveOrUpdate(plPsOrder));
    }

    /**
     * 获取详情
     * @param id
     * @return
     */
    @GetMapping("/detail")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "智慧配送订单 详情", notes = "智慧配送订单详情")
    public R detail(Long id) {
        return R.data(plPsOrderService.getById(id));
    }

    /**
     * 根据主表id删除智慧配送订单
     * @param plPsOrder
     * @return
     */
    @PostMapping("/removeId")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "根据主表id 智慧配送订单 删除", notes = "根据主表id 智慧配送订单 删除")
    public R removeId(@RequestBody PlPsOrder plPsOrder) {
        List<PlPsOrderImg> list = plPsOrderImgService.list(new LambdaQueryWrapper<PlPsOrderImg>().eq(PlPsOrderImg::getPlPsOrderId, plPsOrder.getId()));
        if(Func.isNotEmpty(list)) {
            return R.error("该智慧配送订单已绑定到分组，无法删除");
        }
        plPsOrderService.removeById(plPsOrder.getId());
        return R.success();
    }

    /**
     * 分页 智慧配送订单
     */
    @GetMapping("/pageByparam")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "分页 智慧配送订单", notes = "分页 智慧配送订单")
    public R<NestPage<PlPsOrder>> pageByparam(PlPsOrderReq plPsOrderReq, Query query) {
        IPage<PlPsOrder> pages = plPsOrderService.pageByparam(plPsOrderReq, query);
        return  R.data(NestPage.of(pages));
    }

    @PostMapping("/updateOrderState")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "修改启用状态", notes = "修改启用状态")
    public R updateOrderState(@Valid @RequestBody PlPsOrderDTO plPsOrderDTO) {
        if(Func.isEmpty(plPsOrderDTO) || Func.isEmpty(plPsOrderDTO.getOrderState())) {
            return R.error("启用状态不能为空");
        }
        PlPsOrder plPsOrder = new PlPsOrder();
        plPsOrder.setId(plPsOrderDTO.getId());
        plPsOrder.setOrderState(plPsOrderDTO.getOrderState());
        return R.data(plPsOrderService.updateById(plPsOrder));
    }


}
