package com.fjhx.item.controller.product;

import cn.hutool.core.bean.BeanUtil;
import com.fjhx.common.utils.Assert;
import com.fjhx.item.entity.product.ProcessesBo;
import com.fjhx.item.entity.product.dto.ProductBomInfoDto;
import com.fjhx.item.entity.product.po.ProductBomDetail;
import com.fjhx.item.entity.product.po.ProductInfo;
import com.fjhx.item.entity.product.vo.ProductBomDetailVo;
import com.fjhx.item.entity.product.vo.ProductBomInfoVo;
import com.fjhx.item.service.product.ProductBomDetailService;
import com.fjhx.item.service.product.ProductInfoService;
import com.ruoyi.common.core.domain.BaseSelectDto;
import com.ruoyi.common.utils.SecurityUtils;
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 java.util.Date;
import java.util.List;
import java.util.Objects;


/**
 * <p>
 * item_产品BOM明细 前端控制器
 * </p>
 *
 * @author
 * @since 2024-01-07
 */
@RestController
@RequestMapping("/productBomInfo")
public class ProductBomInfoController {

    @Autowired
    private ProductInfoService productInfoService;
    @Autowired
    private ProductBomDetailService productBomDetailService;

    /**
     * 产品BOM信息明细
     */
    @PostMapping("/detail")
    public ProductBomInfoVo detail(@RequestBody BaseSelectDto dto) {
        Assert.notEmpty(dto.getId(), "产品id不能为空");
        ProductInfo productInfo = productInfoService.getById(dto.getId());
        Assert.notEmpty(productInfo, "查询不到产品信息");

        //赋值物料信息
        List<ProductBomDetail> list = productBomDetailService.list(q -> q
                .eq(ProductBomDetail::getProductId, dto.getId())
                .orderByAsc(ProductBomDetail::getType)
        );
        List<ProductBomDetailVo> productBomDetailVos = BeanUtil.copyToList(list, ProductBomDetailVo.class);
        productInfoService.attributeAssign(productBomDetailVos, ProductBomDetail::getMaterialId, (item, material) -> {
            item.setMaterialName(material.getName());
            item.setMaterialCode(material.getCustomCode());
            item.setMaterialLength(material.getLength());
            item.setMaterialWidth(material.getWidth());
            item.setMaterialHeight(material.getHeight());
            item.setMaterialPrice(material.getPrice());
        });

        ProductBomInfoVo productBomInfoVo = new ProductBomInfoVo();
        productBomInfoVo.setId(productInfo.getId());
        productBomInfoVo.setRawMaterialId(productInfo.getRawMaterialId());
        productBomInfoVo.setRawMaterialName(productInfo.getName());
        productBomInfoVo.setRawMaterialName(productInfo.getName());
        productBomInfoVo.setProductBomDetailList(productBomDetailVos);

        //赋值工序列表
        Long technologyId = productInfo.getTechnologyId();
        List<ProcessesBo> processesList = productInfoService.getProcessesByTechnologyId(technologyId);
        productBomInfoVo.setProcessesList(processesList);

        return productBomInfoVo;
    }

    /**
     * 产品BOM信息编辑
     */
    @PostMapping("/edit")
    public void edit(@RequestBody ProductBomInfoDto dto) {
        Assert.notEmpty(dto.getId(), "产品id不能为空");

        List<ProductBomDetail> productBomDetailList = dto.getProductBomDetailList();
        for (ProductBomDetail productBomDetail : productBomDetailList) {
            productBomDetail.setProductId(dto.getId());

            //赋值原材料
            if (Objects.equals(productBomDetail.getType(), 1)) {
                productInfoService.update(q -> q
                        .eq(ProductInfo::getId, dto.getId())
                        .set(ProductInfo::getRawMaterialId, productBomDetail.getMaterialId())
                        .set(ProductInfo::getUpdateTime, new Date())
                        .set(ProductInfo::getUpdateUser, SecurityUtils.getUserId())
                );
            }
        }

        productBomDetailService.editLinked(productBomDetailList, ProductBomDetail::getProductId, dto.getId());
    }

}
