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.ProductionSegmentedProduct;
import com.ruoyi.web.base.service.IProductionSegmentedProductService;
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-segmented-product")
public class ProductionSegmentedProductController {

    @Autowired
    private IProductionSegmentedProductService productionSegmentedProductService;
    @Autowired
    private TokenService tokenService;

    @ApiOperation("生产分段产品添加")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody ProductionSegmentedProduct product, HttpServletRequest request) throws Exception {
        String loginOrgId = tokenService.getLoginOrgId(request);

        product.setOrgId(loginOrgId);
        product.setCreateBy(getUsername());
        return success(productionSegmentedProductService.save(product));
    }

    @ApiOperation("生产分段产品修改")
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody ProductionSegmentedProduct product, HttpServletRequest request) throws Exception {
        String loginOrgId = tokenService.getLoginOrgId(request);
        product.setUpdateBy(getUsername());
        return success(productionSegmentedProductService.updateById(product));
    }

    @ApiOperation("生产分段产品删除")
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody Map<String, String> paramsMap) {
        String ids = paramsMap.get("ids");
        for (String id : ids.split(",")) {
            productionSegmentedProductService.removeById(id);
        }
        return success();
    }

    @ApiOperation("生产分段产品列表")
    @PostMapping("/list")
    public AjaxResult listAll(HttpServletRequest request) {
        return success(productionSegmentedProductService.list(
                new QueryWrapper<ProductionSegmentedProduct>()
                        .eq("org_id", tokenService.getLoginOrgId(request))
        ));
    }

    @ApiOperation("生产分段产品分页")
    @GetMapping("/page")
    public AjaxResult page(
            @RequestParam("pageNum") Integer pageNum,
            @RequestParam("pageSize") Integer pageSize,
            HttpServletRequest request
    ) {
        return success(productionSegmentedProductService.page(
                new Page<ProductionSegmentedProduct>(pageNum, pageSize),
                new QueryWrapper<ProductionSegmentedProduct>()
                        .eq("org_id", tokenService.getLoginOrgId(request))
        ));
    }

    @ApiOperation("生产分段产品详情")
    @PostMapping("/listById")
    public AjaxResult listById(@RequestBody Map<String, String> paramsMap) {
        String id = paramsMap.get("id");
        return success(productionSegmentedProductService.getById(id));
    }
}
