package com.zjt.web;

import com.zjt.entity.Tuser;
import com.zjt.entity.cgmaterial;
import com.zjt.service.MaterialType;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/user")
public class MaterialContraller {
    @Resource
    private MaterialType MaterialType;

    @RequestMapping("materialtype")
    public String SelectMaterialType(){
        return MaterialType.selectAll().toString();
    }
    @RequestMapping("type")
    public String type(){
        return MaterialType.type();
    }
    @RequestMapping("brand")
    public String brand(String materialTypeCode) {
        return MaterialType.brand(materialTypeCode);
    }

    @RequestMapping("group")
    public String group(String brand){
        return MaterialType.group(brand);
    }
    @RequestMapping("price")
    public String price(String group) throws Exception {
        return MaterialType.price(group);
    }
    @RequestMapping("code")
    public String code(String code){
        return MaterialType.code(code);
    }
    @RequestMapping("deldate")
    public String deldate(String id, String code){
        id=id.replace(" ","").replace("S","");
        Long idl=Long.parseLong(id.replace(",",""));
        code=code.replace(" ","");
        return MaterialType.deldate(idl,code)+"";

    }
/*    @RequestMapping("select")
    public String select(String store, String date, HttpSession session, Model model){

        Tuser users= (Tuser) session.getAttribute("currentUser");
        Predicate<cgmaterial> predicate = a->{
            if(a.getUsername().equals(users.getTrueName())){
                return true;
            }else{
                return false;
            }
        };
        Predicate<cgmaterial> predicate2=a->{
            if(date!=null){
                String[] split = date.split(" - ");
                String strDateFormat = "yyyy-MM-dd HH:mm:ss";
                SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
                if(sdf.format(a.getCreatetime()).compareTo(split[0])>0&&sdf.format(a.getCreatetime()).compareTo(split[1])<0){
                    return true;
                }else{
                    return false;
                }
            }else{
                return false;
            }
        };
        List<cgmaterial> collect = MaterialType.selectData().stream()
                .filter(a -> a.getUsername().equals(users.getTrueName()))
                .filter(predicate2)
                .collect(Collectors.toList());
        model.addAttribute("listcg",collect);
        return "mypackage/selectcg";
    }*/
}
