package com.zjt.web;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.zjt.entity.*;
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 org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.function.Consumer;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xxu
 * @since 2025-09-25
 */
@RestController
@RequestMapping("/entity")
public class ProductionVarietyDetailController {
    @Autowired
    private IProductionVarietyDetailService productionVarietyDetailService;
    @RequestMapping("varietyLists")
    public String selectvarietyList() {
        List<ProductionVarietyDetail> productionVarietyDetails = productionVarietyDetailService.selectvarietyList();
        return  JSONArray.toJSON(productionVarietyDetails).toString();
    }
    @RequestMapping("varietyDetailsLists")
    public String selectvarietyDetails(String variety) {
        if(variety!=null||variety!=""){
            List<ProductionVarietyDetail> productionVarietyDetails = productionVarietyDetailService.selectvarietyDetails(variety);
            return  JSONArray.toJSON(productionVarietyDetails).toString();
        }else{
            return  JSONArray.toJSON("").toString();
        }
    }

    @RequestMapping("productionheader")
    public String group(@RequestParam("id") String id){
        if(id!=null||id!=""){
            long l = Long.parseLong(id);
            List<Productionheader> productionVarietyDetails = productionVarietyDetailService.selectProductionheader(l);
            System.out.println("输出的值为："+productionVarietyDetails);
            return  JSONArray.toJSON(productionVarietyDetails).toString();
        }else{
            return  JSONArray.toJSON("").toString();
        }
    }
    @RequestMapping("selectColour")
    public String selectColour(@RequestParam("id") String id){
        if(id!=null||id!=""){
            long l = Long.parseLong(id);
            List<ProductionVarietyDetailColour> productionVarietyDetails = productionVarietyDetailService.selectColour(l);
            return  JSONArray.toJSON(productionVarietyDetails).toString();
        }else{
            return  JSONArray.toJSON("").toString();
        }
    }
    @RequestMapping("selectChannel")
    public String selectChannel(@RequestParam("id") String id){
        if(id!=null||id!=""){
            long l = Long.parseLong(id);
            List<ProductionVarietyTd>  ProductionVarietyTdDetails= productionVarietyDetailService.selectChannel(l);
            return  JSONArray.toJSON(ProductionVarietyTdDetails).toString();
        }else{
            return  JSONArray.toJSON("").toString();
        }
    }
    @RequestMapping("insert")
    public String insert(@RequestParam("list") String userList, HttpSession session) {
        List<SaleorderHeader> SaleorderHeaders = JSON.parseArray(userList, SaleorderHeader.class);
        Tuser user = (Tuser) session.getAttribute("currentUser");
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
        String s = user.getId() + "" + df.format(new Date());

        Consumer<SaleorderHeader> consumer = productionVarietyDetailService::insertSh;

        SaleorderHeaders.stream()
                .filter(a -> a.getMaterialCode() != null && a.getPosition() != null)
                .map(a -> {
                    a.setOrderNumber(s);
//                    a.setUsername(user.getTrueName());
                    a.setCreateTime(new Date());
//                    a.setStore(user.getUserName());
                    return a;
                })
                .forEach(consumer);

        return "mypackage/varietyList";
    }
}
