package com.zjt.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.zjt.entity.*;
import com.zjt.mapper.*;
import com.zjt.model.ReturnDate;
import com.zjt.service.MaterialType;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class MaterialTypeImp extends BaseService<type> implements MaterialType  {
    @Resource
    private typeMapper typeMapper;
    @Resource
    private brandMapper brandMapper;
    @Resource
    private groupMapper groupMapper;
    @Resource
    private priceMapper priceMapper;
    @Resource
    private cgmaterialMapper cgmaterialMapper;
    @Override
    public List<type> selectAll() {
        return typeMapper.selectAll();
    }
    public String type(){
        Function<type, ReturnDate> dataMap = (bas_material_type)->{
            ReturnDate ReturnDate=new ReturnDate();
            ReturnDate.setBrandCode(bas_material_type.getMaterialTypeCode());
            ReturnDate.setBrandName(bas_material_type.getMaterialTypeName());
            return ReturnDate;
        };
        List<type> materialType= typeMapper.selectAll();
/*        ReturnDate ReturnDate=new ReturnDate();
        List<ReturnDate> list= new ArrayList<ReturnDate>();*/
        List<ReturnDate> collect = materialType.parallelStream().map(dataMap).collect(Collectors.toList());
        return JSONArray.toJSON(collect).toString();
    }
    public String brand(String materialTypeCode){

        Function<brand,ReturnDate> ReturnDate=(brand)->{
            ReturnDate returnDate =new ReturnDate();
            returnDate.setBrandName(brand.getBrandName());
            returnDate.setBrandCode(brand.getBrandCode());
            return returnDate;
        };
        List<ReturnDate> returnDates = brandMapper.selectBrand(materialTypeCode).stream().map(ReturnDate).collect(Collectors.toList());
        return JSONArray.toJSON(returnDates).toString();
    }
    public String group(String brand){

        Function<group,ReturnDate> ReturnDate=(group)->{
            ReturnDate returnDate =new ReturnDate();
            returnDate.setMaterialGroupName(group.getMaterialGroupName());
            returnDate.setMaterialGroupCode(group.getMaterialGroupCode());
            return returnDate;
        };
        List<ReturnDate> returnDates = groupMapper.selectBrand(brand).stream().map(ReturnDate).collect(Collectors.toList());
        return JSONArray.toJSON(returnDates).toString();
    }

    public String price(String group) throws Exception {

        if("".equals(group)&&null==group){
            throw new Exception("group为空！！！");
        }
        Function<price,ReturnDate> ReturnDate=(price)->{
            ReturnDate returnDate =new ReturnDate();
            returnDate.setMaterialName(price.getMaterialName());
            returnDate.setMaterialCode(price.getMaterialCode());
            return returnDate;
        };
        List<ReturnDate> returnDates = priceMapper.selectPrice(group).stream().map(ReturnDate).collect(Collectors.toList());
        System.out.println(returnDates);
        return JSONArray.toJSON(returnDates).toString();
    }
    public String code(String code){

        Function<price,ReturnDate> ReturnDate=(price)->{
            ReturnDate returnDate =new ReturnDate();
            returnDate.setMaterialName(price.getMaterialName());
            returnDate.setMaterialCode(price.getMaterialCode());
            returnDate.setRetailPrice(price.getRetailPrice());
            return returnDate;
        };
        List<ReturnDate> returnDates = priceMapper.selectCode(code).stream().map(ReturnDate).collect(Collectors.toList());
        System.out.println(returnDates);
        return JSONArray.toJSON(returnDates).toString();
    }

    public String insertCg(cgmaterial cgmaterial){
        return cgmaterialMapper.insert(cgmaterial)+"";
    }

    @Override
    public List<cgmaterial> selectData() {
        return cgmaterialMapper.selectData();
    }

    public int deldate(Long idl, String code){
        cgmaterialMapper.deldate(idl,code);
        return 1;
    }
    @Override
    public List<cgmaterial> selectDataBy(@Param("id")String id, @Param("storeCode")String storeCode, @Param("begin")String begin, @Param("end")String end, @Param("receive")String receive) {
        return cgmaterialMapper.selectDataBy(id,storeCode,begin,end,receive);
    }

}
