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.ProductionPlanGoods;
import com.ruoyi.web.base.service.IProductionPlanGoodsService;
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-plan-goods")
public class ProductionPlanGoodsController {

    @Autowired
    private IProductionPlanGoodsService productionPlanGoodsService;
    @Autowired
    private TokenService tokenService;

    @ApiOperation("生产计划商品添加")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody ProductionPlanGoods goods, HttpServletRequest request) throws Exception {
        String loginOrgId = tokenService.getLoginOrgId(request);
        if (productionPlanGoodsService.count(
                new QueryWrapper<ProductionPlanGoods>()
                        .eq("org_id", loginOrgId)
                        .eq("order_num", goods.getOrderNum())
        ) > 0) {
            throw new Exception("该计划编号已存在");
        }
        goods.setOrgId(loginOrgId);
        goods.setCreateBy(getUsername());
        return success(productionPlanGoodsService.save(goods));
    }

    @ApiOperation("生产计划商品修改")
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody ProductionPlanGoods goods, HttpServletRequest request) throws Exception {
        String loginOrgId = tokenService.getLoginOrgId(request);
        if (productionPlanGoodsService.count(
                new QueryWrapper<ProductionPlanGoods>()
                        .ne("id", goods.getId())
                        .eq("org_id", loginOrgId)
                        .eq("order_num", goods.getOrderNum())
        ) > 0) {
            throw new Exception("该计划编号已存在");
        }
        goods.setUpdateBy(getUsername());
        return success(productionPlanGoodsService.updateById(goods));
    }

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

    @ApiOperation("生产计划商品列表")
    @PostMapping("/list")
    public AjaxResult listAll(HttpServletRequest request) {
        return success(productionPlanGoodsService.list(
                new QueryWrapper<ProductionPlanGoods>()
                        .eq("org_id", tokenService.getLoginOrgId(request))
        ));
    }

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

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