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.PlPsOrderDTO;
import com.yuchen.saas.park.api.entity.pldoor.PlPsOrderImg;
import com.yuchen.saas.park.api.entity.pldoor.PlPsOrderImg;
import com.yuchen.saas.park.api.vo.request.PlPsOrderReq;
import com.yuchen.saas.park.service.service.pldoor.PlPsOrderImgService;
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("/plPsOrderImg")
public class PlPsOrderImgController {

    private final PlPsOrderImgService plPsOrderImgService;

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

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

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

    /**
     * 分页 智慧配送订单
     */
    @GetMapping("/pageByparam")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "分页 智慧配送订单", notes = "分页 智慧配送订单")
    public R<NestPage<PlPsOrderImg>> pageByparam(PlPsOrderReq plPsOrderReq, Query query) {
        IPage<PlPsOrderImg> pages = plPsOrderImgService.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("启用状态不能为空");
        }
        PlPsOrderImg plPsOrderImg = new PlPsOrderImg();
        plPsOrderImg.setId(plPsOrderDTO.getId());
        return R.data(plPsOrderImgService.updateById(plPsOrderImg));
    }


}
