package com.sw.mes.fb.checkout.service.impl;

import com.sw.mes.core.base.BaseServiceImpl;
import com.sw.mes.fb.checkout.dao.CooperateDataDao;
import com.sw.mes.fb.checkout.entity.CooperateData;
import com.sw.mes.fb.checkout.entity.FbCheckOutRecord;
import com.sw.mes.fb.checkout.service.CooperateDataService;
import com.sw.mes.mcs.dto.RecutDto;
import com.sw.mes.mcs.dto.RecutInfoFbDetailInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * @Author
 * @Description：
 * @Date: Create in
 * @Modified By:
 */
@Service
@Transactional
public class CooperateDataServiceImpl extends BaseServiceImpl<CooperateData> implements CooperateDataService {
    CooperateDataDao dao;

    @Autowired
    public void setDao(CooperateDataDao dao) {
        super.setBaseDao(dao);
        this.dao = dao;
    }

    @Override
    public CooperateData findByCooperateNo(String cooperateNo) {
        List<CooperateData> cooperateDataList = dao.findByCooperateNo(cooperateNo);
        if (cooperateDataList.size() > 0) {
            return cooperateDataList.get(0);
        }
        return null;
    }

    @Override
    public List<CooperateData> findBySectionNoFb(String sectionNoFb) {
        return dao.findBySectionNoFb(sectionNoFb);
    }

    @Override
    public void saveInfo(FbCheckOutRecord fbCheckOutRecord, RecutDto recutDto) {
        List<RecutInfoFbDetailInfo> detail = recutDto.getDetail();
        if (null != detail && detail.size() > 0) {
            String fbSectionNo = fbCheckOutRecord.getFbSectionNo();

            List<CooperateData> cooperateDataList = new ArrayList<>();
            for (RecutInfoFbDetailInfo recutInfoFbDetailInfo : detail) {
                String segmentNo = recutInfoFbDetailInfo.getSegmentNo();

                CooperateData cooperateData = new CooperateData();
                cooperateData.setEapDiameterMax(fbCheckOutRecord.getEapDiameterMax());
                cooperateData.setEapDiameterMin(fbCheckOutRecord.getEapDiameterMin());
                cooperateData.setEapSideMax(fbCheckOutRecord.getEapSideMax());
                cooperateData.setEapSideMin(fbCheckOutRecord.getEapSideMin());
                cooperateData.setVerticalityMax(fbCheckOutRecord.getVerticalityMax());
                cooperateData.setVerticalityMin(fbCheckOutRecord.getVerticalityMin());
                cooperateData.setDiameterMax(fbCheckOutRecord.getDiameterMax());
                cooperateData.setDiameterMin(fbCheckOutRecord.getDiameterMin());
                cooperateData.setProjectionMax(fbCheckOutRecord.getProjectionMax());
                cooperateData.setProjectionMin(fbCheckOutRecord.getProjectionMin());
                cooperateData.setSideLengthMax(fbCheckOutRecord.getSideLengthMax());
                cooperateData.setSideLengthMin(fbCheckOutRecord.getSideLengthMin());

                cooperateData.setSectionNoFb(fbSectionNo);
                cooperateData.setCooperateNo(segmentNo);
                cooperateData.setCreateTime(new Date());
                cooperateDataList.add(cooperateData);

            }
            dao.saveAll(cooperateDataList);

        }

    }
}
