package com.ruoyi.web.base.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
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.ProductionProduct;
import com.ruoyi.web.base.domain.ProductionSlaughter;
import com.ruoyi.web.base.domain.ProductionSlaughterGoods;
import com.ruoyi.web.base.domain.param.ProductionSlaughterGoodsRequest;
import com.ruoyi.web.base.service.IProductionSlaughterGoodsService;
import com.ruoyi.web.base.service.IProductionSlaughterGoodsService;
import com.ruoyi.web.base.service.IProductionSlaughterService;
import com.ruoyi.web.base.util.NumUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
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.Date;
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;
import static com.ruoyi.web.base.util.NumUtils.generateString;
import static com.ruoyi.web.base.util.NumUtils.substringToInt;

/**
 * <p>
 * 代宰信息表，存储代宰相关的详细信息 前端控制器
 * </p>
 *
 * @author author
 * @since 2026-03-31
 */
@RestController
@RequestMapping("/production-slaughter-goods")
public class ProductionSlaughterGoodsController {

    @Autowired
    private IProductionSlaughterService slaughterService;
    @Autowired
    private IProductionSlaughterGoodsService goodsService;
    @Autowired
    private TokenService tokenService;



    @ApiOperation("生产计划单添加")
    @PostMapping("/add")
    @Transactional
    public AjaxResult add(@RequestBody ProductionSlaughterGoods productionSlaughterGoods, HttpServletRequest request) throws Exception {
        String loginOrgId = tokenService.getLoginOrgId(request);

        String username = getUsername();
        productionSlaughterGoods.setOrgId(loginOrgId);
        productionSlaughterGoods.setCreateBy(username);
        productionSlaughterGoods.setJingZhong(productionSlaughterGoods.getDzcWeight());


        if (goodsService.count(
                new QueryWrapper<ProductionSlaughterGoods>()
                        .eq("org_id", loginOrgId)
                        .eq("document_number", productionSlaughterGoods.getDocumentNumber())
                        .eq("white_strip_number", productionSlaughterGoods.getWhiteStripNumber())
        ) > 0) {
            throw new Exception("该白条已存在");
        }

        ProductionSlaughter productionSlaughter = slaughterService.getOne(new QueryWrapper<ProductionSlaughter>().eq("document_number", productionSlaughterGoods.getDocumentNumber()));
        productionSlaughter.setCreateTime(new Date());
        productionSlaughter.setActualSlaughterQuantity(
                NumUtils.addStringToDouble(productionSlaughter.getActualSlaughterQuantity(),"0.5"));

        productionSlaughter.setActualSlaughterAmount(
                NumUtils.addStringToDouble( productionSlaughter.getActualSlaughterAmount(),
      String.valueOf( 0.5 *Double.parseDouble(productionSlaughter.getUnitPrice())) )
                );
        productionSlaughter.setUpdateTime(new Date());
        slaughterService.updateById(productionSlaughter);

        return success(goodsService.save(productionSlaughterGoods));
    }

    @ApiOperation("生产计划单修改")
    @PostMapping("/edit")
    @Transactional
    public AjaxResult edit(@RequestBody ProductionSlaughterGoods productionSlaughterGoods, HttpServletRequest request) throws Exception {
        String loginOrgId = tokenService.getLoginOrgId(request);

        String username = getUsername();
        productionSlaughterGoods.setOrgId(loginOrgId);
        productionSlaughterGoods.setCreateBy(username);

        productionSlaughterGoods.setUpdateBy(username);
        productionSlaughterGoods.setUpdateTime(new Date());
        productionSlaughterGoods.setJingZhong(productionSlaughterGoods.getDzcWeight());
        return success(goodsService.updateById(productionSlaughterGoods));
    }

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

    @ApiOperation("生产计划单列表")
    @PostMapping("/list")
    public AjaxResult listAll( HttpServletRequest request,
                               @RequestBody ProductionSlaughterGoods productionSlaughterGoods ){
        QueryWrapper<ProductionSlaughterGoods> query = new QueryWrapper<ProductionSlaughterGoods>()
                .eq("org_id", tokenService.getLoginOrgId(request));
        if(productionSlaughterGoods.getDocumentNumber() != null){
            query.eq("document_number",productionSlaughterGoods.getDocumentNumber());
        }
        List<ProductionSlaughterGoods> org_id = goodsService.list(query);


        return success(org_id);
    }

    @ApiOperation("生产计划单分页")
    @PostMapping("/page")
    public AjaxResult page(
            @RequestBody ProductionSlaughterGoodsRequest productionSlaughterGoodsParam, HttpServletRequest request) {


        QueryWrapper<ProductionSlaughterGoods> wrapper = new QueryWrapper<ProductionSlaughterGoods>().eq("org_id", tokenService.getLoginOrgId(request));

        Page<ProductionSlaughterGoods> org_id =
                goodsService.page(new Page<ProductionSlaughterGoods>(productionSlaughterGoodsParam.getPageNum(), productionSlaughterGoodsParam.getPageSize()),wrapper);


        return success(org_id);
    }

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

}