package com.ruoyi.web.base.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.web.base.domain.ProductionShipmentNotification;
import com.ruoyi.web.base.domain.param.ProductionRegularPlanRequest;
import com.ruoyi.web.base.service.IProductionShipmentNotificationService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

import static com.ruoyi.common.core.domain.AjaxResult.success;
import static com.ruoyi.common.utils.SecurityUtils.getUsername;

@RestController
@RequestMapping("/production-shipment-notification")
public class ProductionShipmentNotificationController {

    @Autowired
    private IProductionShipmentNotificationService productionShipmentNotificationService;
    @Autowired
    private TokenService tokenService;

    @ApiOperation("出货通知添加")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody ProductionShipmentNotification notification, HttpServletRequest request) throws Exception {
        String loginOrgId = tokenService.getLoginOrgId(request);

        notification.setOrgId(loginOrgId);
        notification.setCreateBy(getUsername());
        return success(productionShipmentNotificationService.save(notification));
    }

    @ApiOperation("出货通知修改")
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody ProductionShipmentNotification notification, HttpServletRequest request) throws Exception {
        String loginOrgId = tokenService.getLoginOrgId(request);

        notification.setUpdateBy(getUsername());
        return success(productionShipmentNotificationService.updateById(notification));
    }

    @ApiOperation("出货通知删除")
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody Map<String, String> paramsMap) {
        String ids = paramsMap.get("ids");
        for (String id : ids.split(",")) {
            productionShipmentNotificationService.removeById(id);
        }
        return success();
    }

    @ApiOperation("出货通知列表")
    @PostMapping("/list")
    public AjaxResult listAll(HttpServletRequest request) {
        return success(productionShipmentNotificationService.list(
                new QueryWrapper<ProductionShipmentNotification>()
                        .eq("org_id", tokenService.getLoginOrgId(request))
        ));
    }

    @ApiOperation("出货通知分页")
    @GetMapping("/page")
    public AjaxResult page(
            @RequestBody ProductionRegularPlanRequest planRequest,
            HttpServletRequest request
    ) {
        return success(productionShipmentNotificationService.page(
                new Page<ProductionShipmentNotification>(planRequest.getPageNum(), planRequest.getPageSize()),
                new QueryWrapper<ProductionShipmentNotification>()
                        .eq("org_id", tokenService.getLoginOrgId(request))
        ));
    }

    @ApiOperation("出货通知详情")
    @PostMapping("/listById")
    public AjaxResult listById(@RequestBody Map<String, String> paramsMap) {
        String id = paramsMap.get("id");
        return success(productionShipmentNotificationService.getById(id));
    }
}
