package com.zjt.web;

import com.zjt.entity.ProductionVariety;
import com.zjt.service.IProductionVarietyDetailService;
import com.zjt.service.IProductionVarietyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xxu
 * @since 2025-09-25
 */
@Controller
@RequestMapping("/entity/productionheader")
public class ProductionheaderController {

    private static final String VIEW_VARIETY_LIST = "mypackage/varietyList";

    @Autowired
    private IProductionVarietyService productionVarietyService;
    @Autowired
    private IProductionVarietyDetailService productionVarietyDetailService;

    @RequestMapping("list")
    public String selectVarietyAll() {
        productionVarietyService.varietyList();
        productionVarietyDetailService.selectvarietyList();
        return VIEW_VARIETY_LIST;
    }

    @RequestMapping("add")
    public String varietyAdd(String materialTypeCode) {
        if (materialTypeCode == null || materialTypeCode.isEmpty()) {
            // 可以抛异常或返回错误页面，这里简化处理
            return VIEW_VARIETY_LIST;
        }
        ProductionVariety productionVariety = createProductionVariety(materialTypeCode);
        productionVarietyService.save(productionVariety);
        return VIEW_VARIETY_LIST;
    }

    @RequestMapping("update")
    public String varietyUpdate(String materialTypeCode) {
        if (materialTypeCode == null || materialTypeCode.isEmpty()) {
            return VIEW_VARIETY_LIST;
        }
        ProductionVariety productionVariety = createProductionVariety(materialTypeCode);
        productionVarietyService.saveOrUpdate(productionVariety);
        return VIEW_VARIETY_LIST;
    }

    private ProductionVariety createProductionVariety(String materialTypeCode) {
        ProductionVariety productionVariety = new ProductionVariety();
        productionVariety.setVariety(materialTypeCode);
        return productionVariety;
    }
}
