package com.huigou.topsun.proofing.service.impl;

import com.huigou.topsun.product.domain.ProductLooked;
import com.huigou.topsun.product.domain.ProductPublishedConf;
import com.huigou.topsun.product.repository.ProductLookedRepository;
import com.huigou.topsun.product.repository.ProductPublishedConfRepository;
import com.huigou.topsun.proofing.domain.model.ProofingApply;
import com.huigou.topsun.proofing.domain.model.ProofingMake;
import com.huigou.topsun.proofing.domain.vo.ProofingMakeVo;
import com.huigou.topsun.proofing.repository.ProofingApplyRepository;
import com.huigou.topsun.proofing.repository.ProofingMakeRepository;
import com.huigou.topsun.proofing.service.ProofingMakeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ProofingMakeServiceImpl implements ProofingMakeService {

    @Autowired
    ProofingMakeRepository proofingMakeRepository;
    @Autowired
    ProofingApplyRepository proofingApplyRepository;
    @Autowired
    ProductLookedRepository productLookedRepository;
    @Autowired
    ProductPublishedConfRepository productPublishedConfRepository;



    @Override
    public List<ProofingMakeVo> getProofingMakeVoList(){
        List<ProofingMake> makeAll = proofingMakeRepository.findAll();
        List<ProofingMakeVo> makeVoList = new ArrayList<>();

        for (ProofingMake proofingMake : makeAll) {
            ProofingApply proofingApply = proofingApplyRepository.findByProofingApplyId(proofingMake.getProofingApplyId());
            ProofingMakeVo proofingMakeVo = getProofingMakeVo(proofingMake, proofingApply);
            makeVoList.add(proofingMakeVo);
        }

        return makeVoList;
    }

    @Override
    public ProofingMakeVo getProofingMakeVoById(String proofingMakeId) {
        ProofingMake proofingMake = proofingMakeRepository.findOne(proofingMakeId);
        ProofingApply proofingApply = proofingApplyRepository.findByProofingApplyId(proofingMake.getProofingApplyId());
        ProductLooked productLooked = productLookedRepository.findByProductId(proofingApply.getProductId());
        ProductPublishedConf productPublishedConf = productPublishedConfRepository.getProductPublishedConfByProductId(proofingApply.getProductId());
        return this.getProofingMakeVo(proofingApply,proofingMake,productLooked,productPublishedConf);
    }

    /**
     * 分解ProofingMake
     * @param proofingMake
     */
    @Override
    public void saveProofingMakeVo(ProofingMake proofingMake) {
        proofingMakeRepository.save(proofingMake);
    }

    @Override
    public void deleteProofingMakeVo(String proofingMakeId) {
        proofingMakeRepository.delete(proofingMakeId);
    }

    /**
     *   生成ProofingMakeVo类
     * @return 填充过数据后的ProofingMakeVo
     */
    public ProofingMakeVo getProofingMakeVo(Object... objs){
        ProofingMakeVo proofingMakeVo = new ProofingMakeVo();
        for (Object obj : objs) {
            if(obj!=null){
                BeanUtils.copyProperties(obj, proofingMakeVo);
            }
        }
        return proofingMakeVo;
    }


}
