package com.ruoyi.web.base.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.web.base.domain.ProductionRecord;
import com.ruoyi.web.base.domain.ProductionRegular;
import com.ruoyi.web.base.domain.ProductionWhiteStripRequisition;
import com.ruoyi.web.base.domain.param.AddProductionWhiteStripRequisition;
import com.ruoyi.web.base.domain.param.WiteStripRequest;
import com.ruoyi.web.base.service.IProductionRecordService;
import com.ruoyi.web.base.service.IProductionRegularService;
import com.ruoyi.web.base.service.IProductionWhiteStripRequisitionService;
import com.ruoyi.web.base.service.IProductionWhiteStripRequisitionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

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-white-strip-requisition")
@Api("分割白条")
public class ProductionWhiteStripRequisitionController {

    @Autowired
    private IProductionWhiteStripRequisitionService productionWhiteStripRequisitionService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private IProductionRegularService regularService;
  


    @ApiOperation("分割白条添加")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody AddProductionWhiteStripRequisition productionWhiteStripRequisition, HttpServletRequest request) throws Exception {
        String loginOrgId = tokenService.getLoginOrgId(request);
        List<ProductionWhiteStripRequisition> goods = productionWhiteStripRequisition.getGoods();
        for (ProductionWhiteStripRequisition good : goods) {
            good.setOrgId(loginOrgId);
            good.setCreateBy(getUsername());
            good.setProductionPlanNum(productionWhiteStripRequisition.getProductionPlanNum());
            ProductionRegular productionRegular = new ProductionRegular();
            productionRegular.setIsFenge(1);
            productionRegular.setId(good.getId());
            regularService.updateById(productionRegular );
        }
        
        return success(productionWhiteStripRequisitionService.saveBatch(productionWhiteStripRequisition.getGoods()));
    }

    @ApiOperation("分割白条修改")
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody AddProductionWhiteStripRequisition productionWhiteStripRequisition, HttpServletRequest request) throws Exception {
  
        
        List<ProductionWhiteStripRequisition> goods = productionWhiteStripRequisition.getGoods();
        for (ProductionWhiteStripRequisition good : goods) {
            good.setUpdateBy(getUsername());
            productionWhiteStripRequisitionService.updateById(good);
        }
        return success();
    }

    @ApiOperation("分割白条删除")
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody Map<String, String> paramsMap) {
        String ids = paramsMap.get("ids");
        for (String id : ids.split(",")) {
            productionWhiteStripRequisitionService.removeById(id);
        }
        return success();
    }

    @ApiOperation("分割白条列表")
    @PostMapping("/list")
    public AjaxResult listAll(   @RequestBody WiteStripRequest planRequest,
                                 HttpServletRequest request) {
        QueryWrapper<ProductionWhiteStripRequisition> wrapper = new QueryWrapper<ProductionWhiteStripRequisition>()
                .eq("org_id", tokenService.getLoginOrgId(request));
//        wrapper.like(StringUtils.isNotEmpty(planRequest.getProductionPlanNumber()),"production_plan_number",planRequest.getProductionPlanNumber())
//                .like(StringUtils.isNotEmpty(planRequest.getProductionRecordNumber()),"production_record_number",planRequest.getProductionRecordNumber());

        return success(productionWhiteStripRequisitionService.list(
                wrapper));
    }

    @ApiOperation("分割白条分页")
    @PostMapping("/page")
    public AjaxResult page(
            @RequestBody WiteStripRequest planRequest,
            HttpServletRequest request
    ) {
        QueryWrapper<ProductionWhiteStripRequisition> wrapper = new QueryWrapper<ProductionWhiteStripRequisition>()
                .eq("org_id", tokenService.getLoginOrgId(request));
//        wrapper.like(StringUtils.isNotEmpty(planRequest.getProductionPlanNumber()),"production_plan_number",planRequest.getProductionPlanNumber())
//                .like(StringUtils.isNotEmpty(planRequest.getProductionRecordNumber()),"production_record_number",planRequest.getProductionRecordNumber());
        return success(productionWhiteStripRequisitionService.page(
                new Page<ProductionWhiteStripRequisition>(planRequest.getPageNum(), planRequest.getPageSize()),
                wrapper
        ));
    }

    @ApiOperation("分割白条详情")
    @PostMapping("/listById")
    public AjaxResult listById(@RequestBody Map<String, String> paramsMap) {
        String id = paramsMap.get("id");
        return success(productionWhiteStripRequisitionService.getById(id));
    }
}
