package com.org.hktshop.service.encapsulation.impl;

import com.org.hktshop.base.BaseException;
import com.org.hktshop.base.BaseService;
import com.org.hktshop.entity.Page;
import com.org.hktshop.entity.encapsulation.FcMaterial;
import com.org.hktshop.entity.encapsulation.FcPackage;
import com.org.hktshop.service.encapsulation.FcMaterialService;
import com.org.hktshop.service.encapsulation.FcPackageService;
import com.org.hktshop.utils.DateUtil;
import com.org.hktshop.utils.ExecSqlLib;

import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class FcPackageServiceImpl extends BaseService<FcPackage> implements FcPackageService {
    @Resource
    private FcMaterialService materialService;

    @Override
    public void saveEncapsulation(FcPackage fcPackage) {
//        FcMaterial fcMaterial = new FcMaterial();
//        fcMaterial.setLevel(1);
//        List<FcMaterial> materials = materialService.findAll(fcMaterial);
//
//        Set<String> materialIds = materials.stream().map(FcMaterial::getId).collect(Collectors.toSet());
//        StringBuilder ids = new StringBuilder();
//        for (String materialId : materialIds) {
//            ids.append(materialId).append(",");
//        }
//        ids.delete(ids.length() - 1, ids.length());
//        fcPackage.setMaterialIds(ids.toString());
        this.updateJdbc(fcPackage);
    }

    @Override
    public void deleteEncapsulation(String id) {
        if (StringUtils.isEmpty(id)) {
            throw new BaseException("id 不能为空");
        }
        this.deleteById(id, new FcPackage());
    }

    @Override
    public void updateEncapsulation(FcPackage fcPackage) {
        if (StringUtils.isEmpty(fcPackage.getId())) {
            throw new BaseException("id 不能为空");
        }
        fcPackage.setUpdateTime(DateUtil.getDateTime());
        this.updateJdbc(fcPackage);
    }

    @Override
    public FcPackage getEncapsulation(String id) {
        if (StringUtils.isEmpty(id)) {
            throw new BaseException("id 不能为空");
        }
        return this.queryByIdJbdc(id, new FcPackage());
    }

    @Override
    public FcPackage getEncapsulation(FcPackage fcPackage) {
        return this.baseJdbcDao.query(ExecSqlLib.GetQuerySql(fcPackage),fcPackage);
    }

    @Override
    public Page<FcPackage> getHktEncapsulationPage(FcPackage fcPackage) {
        Page<FcPackage> fcPackagePage = baseJdbcDao.queryPage(ExecSqlLib.GetQuerySql(fcPackage).replace("order by id", "order by sort,id"), fcPackage);
//
//        if (!CollectionUtils.isEmpty(fcPackagePage.getRecords())) {
//            List<String> materialIdsList = fcPackagePage.getRecords().stream().map(FcPackage::getMaterialIds).collect(Collectors.toList());
//            Set<String> materialIds = new HashSet<>();
//            for (String ids : materialIdsList) {
//                if(!StringUtils.isEmpty(ids)){
//                    String[] idsSplit = ids.split(",");
//                    materialIds.addAll(Arrays.asList(idsSplit));
//                }
//            }
//            if(!CollectionUtils.isEmpty(materialIds)){
//                List<FcMaterial> fcMaterialList = materialService.findByIds(materialIds);
//                Map<String, List<FcMaterial>> fcMaterialsMap = fcMaterialList.stream()
//                        .collect(Collectors.groupingBy(FcMaterial::getId));
//                for (FcPackage fcPackage1 : fcPackagePage.getRecords()) {
//                    StringBuilder name=new StringBuilder();
//
//                }
//            }
//        }
        return fcPackagePage;
    }

    @Override
    public List<FcPackage> findAll() {

        String sql = ExecSqlLib.GetQuerySql(new FcPackage()).replace("order by id", "order by sort,id");
        return this.baseJdbcDao.queryList(sql,new FcPackage());
    }

//    @Override
//    public List<FcPackage> findByIds(List<String> idsList) {
//        StringBuilder sql = new StringBuilder();
//        sql.append(ExecSqlLib.GetQuerySql(new FcPackage()).replace("order by id", ""));
//        StringBuilder ids = new StringBuilder();
//        for (String id : idsList) {
//            ids.append("'").append(id).append("',");
//        }
//        ids.deleteCharAt(ids.length() - 1);
//        sql.append(String.format(" WHERE id in (%s)", ids));
//        return this.baseJdbcDao.queryList(sql.toString(), new FcPackage());
//    }
}
