package com.ruoyi.web.base.controller;


import com.ruoyi.web.base.domain.*;
import com.ruoyi.web.base.domain.param.ProductionCompletionRequest;
import com.ruoyi.web.base.service.IProductionCompletionReportGoodsService;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
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.service.IProductionCompletionReportService;
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.List;
import java.util.Map;

import static com.ruoyi.common.core.domain.AjaxResult.success;
import static com.ruoyi.common.utils.SecurityUtils.getUsername;
/**
 * <p>
 * 生产完工报告表，存储生产完工报告的详细信息 前端控制器
 * </p>
 *
 * @author author
 * @since 2026-03-19
 */
@RestController
@RequestMapping("/production-completion-report")
public class ProductionCompletionReportController {
    @Autowired
    private IProductionCompletionReportService productionCompletionReportService;
    @Autowired
    private IProductionCompletionReportGoodsService goodsService;
    @Autowired
    private TokenService tokenService;

    @ApiOperation("生产完工报告添加")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody ProductionCompletionReport report, HttpServletRequest request) throws Exception {
        String loginOrgId = tokenService.getLoginOrgId(request);
        if (productionCompletionReportService.count(
                new QueryWrapper<ProductionCompletionReport>()
                        .eq("org_id", loginOrgId)
                        .eq("report_number", report.getReportNumber())
        ) > 0) {
            throw new Exception("该报告编号已存在");
        }
        report.setOrgId(loginOrgId);
        report.setCreateBy(getUsername());
        List<ProductionCompletionReportGoods> goods = report.getGoods();
        for (ProductionCompletionReportGoods good : goods) {

            good.setReportNumber(report.getReportNumber());
            goodsService.save(good);
        }
        return success(productionCompletionReportService.save(report));
    }

    @ApiOperation("生产完工报告修改")
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody ProductionCompletionReport report, HttpServletRequest request) throws Exception {
        String loginOrgId = tokenService.getLoginOrgId(request);
        if (productionCompletionReportService.count(
                new QueryWrapper<ProductionCompletionReport>()
                        .ne("id", report.getId())
                        .eq("org_id", loginOrgId)
                        .eq("report_number", report.getReportNumber())
        ) > 0) {
            throw new Exception("该报告编号已存在");
        }
        List<ProductionCompletionReportGoods> goods = report.getGoods();
        goodsService.remove(new QueryWrapper<ProductionCompletionReportGoods>().eq("report_number",report.getReportNumber()));
        for (ProductionCompletionReportGoods good : goods) {

            good.setReportNumber(report.getReportNumber());
            goodsService.save(good);
        }

        report.setUpdateBy(getUsername());
        return success(productionCompletionReportService.updateById(report));
    }

    @ApiOperation("生产完工报告删除")
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody Map<String, String> paramsMap) {
        String ids = paramsMap.get("ids");
        for (String id : ids.split(",")) {
            productionCompletionReportService.removeById(id);
        }
        return success();
    }

    @ApiOperation("生产完工报告列表")
    @PostMapping("/list")
    public AjaxResult listAll(HttpServletRequest request) {
        List<ProductionCompletionReport> org_id = productionCompletionReportService.list(
                new QueryWrapper<ProductionCompletionReport>()
                        .eq("org_id", tokenService.getLoginOrgId(request))
        );

        for (ProductionCompletionReport procureQuotation : org_id) {
            procureQuotation.setGoods(goodsService.list(new QueryWrapper<ProductionCompletionReportGoods>().eq("report_number",procureQuotation.getReportNumber())));
        }
        return success(org_id);


    }

    @ApiOperation("生产完工报告分页")
    @PostMapping("/page")
    public AjaxResult page(
           @RequestBody ProductionCompletionRequest regular ,
            HttpServletRequest request
    ) {
        Page<ProductionCompletionReport> org_id = productionCompletionReportService.page(
                new Page<ProductionCompletionReport>(regular.getPageNum(), regular.getPageSize()),
                new QueryWrapper<ProductionCompletionReport>()
                        .eq("org_id", tokenService.getLoginOrgId(request))
        );

        for (ProductionCompletionReport procureQuotation : org_id.getRecords()) {
            procureQuotation.setGoods(goodsService.list(new QueryWrapper<ProductionCompletionReportGoods>().eq("report_number",procureQuotation.getReportNumber())));
        }
        return success(org_id);
    }

    @ApiOperation("生产完工报告详情")
    @PostMapping("/listById")
    public AjaxResult listById(@RequestBody Map<String, String> paramsMap) {
        String id = paramsMap.get("id");
        ProductionCompletionReport byId = productionCompletionReportService.getById(id);
        byId.setGoods(goodsService.list(new QueryWrapper<ProductionCompletionReportGoods>().eq("report_number",byId.getReportNumber())));
        return success(byId);
    }

}
