package com.sw.mes.yb.pc.service.impl;

import com.sw.mes.core.base.BaseServiceImpl;
import com.sw.mes.core.utils.RequestPage;
import com.sw.mes.base.factory.utils.PageUtils;
import com.sw.mes.yb.pc.dao.CrucibleCheckDataDAO;
import com.sw.mes.yb.pc.dao.CrucibleCheckDataItemDAO;
import com.sw.mes.yb.pc.dto.CrucibleCheckDataDto;
import com.sw.mes.yb.pc.dto.CrucibleCheckDataItemDto;
import com.sw.mes.yb.pc.entity.CrucibleCheckData;
import com.sw.mes.yb.pc.entity.CrucibleCheckDataItem;
import com.sw.mes.yb.pc.service.CrucibleCheckDataService;
import com.sw.mes.yb.pc.service.StoreMixInfoService;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class CrucibleCheckDataServiceImpl extends BaseServiceImpl<CrucibleCheckData> implements CrucibleCheckDataService {

    CrucibleCheckDataDAO dao;

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

    @Autowired
    ModelMapper modelMapper;

    @Autowired
    CrucibleCheckDataItemDAO crucibleCheckDataItemDAO;

    @Autowired
    StoreMixInfoService storeMixInfoService;

    @Override
    public List<CrucibleCheckData> findByExample(Specification<CrucibleCheckData> specification, RequestPage page) {
        org.springframework.data.domain.Page<CrucibleCheckData> springDataPage = dao.findAll(specification, PageUtils.createPageable(page));
        page.setTotal(springDataPage.getTotalElements());
        return springDataPage.getContent();
    }

    @Override
    public void saveOrUpdate(@Valid CrucibleCheckDataDto crucibleCheckDataDto) throws RuntimeException {

        CrucibleCheckData crucibleCheckData = modelMapper.map(crucibleCheckDataDto, CrucibleCheckData.class);

        List<CrucibleCheckDataItemDto> itemDtos = crucibleCheckDataDto.getCrucibleCheckDataItemDtos();
        Boolean pass = true;
        List<CrucibleCheckDataItem> itemList = new ArrayList<>();
        if (itemDtos != null && itemDtos.size() > 0) {
            for (CrucibleCheckDataItemDto dataItemDto: itemDtos) {
                if (dataItemDto.getConfirmResult() != null && "2".equals(dataItemDto.getConfirmResult())) {
                    pass = false;
                }
                CrucibleCheckDataItem crucibleCheckDataItem = modelMapper.map(dataItemDto, CrucibleCheckDataItem.class);
                crucibleCheckDataItem.setCheckData(crucibleCheckData);

                itemList.add(crucibleCheckDataItem);
            }
        }
        crucibleCheckData.setPass(pass);
        //如果可用，修改库存及领料记录
        if (pass) {
            String code = crucibleCheckData.getCode();
            try {
                storeMixInfoService.StoreOutput(code);
            } catch (Exception e) {
                e.printStackTrace();
                throw new RuntimeException(e.getMessage());
            }
        }

        crucibleCheckData.setOperateTime(new Date());
        saveOrUpdate(crucibleCheckData);
        if (itemList.size() > 0) {
            crucibleCheckDataItemDAO.saveAll(itemList);
        }
    }

    @Override
    public CrucibleCheckData getByCode(String code) {
        List<CrucibleCheckData> crucibleCheckDataList = dao.findByCode(code);
        if (crucibleCheckDataList != null && crucibleCheckDataList.size() > 0) {
            return crucibleCheckDataList.get(0);
        }
        return null;
    }

    @Override
    public void updateLotNo(String code, String lotNo) {
        dao.updateLotNo(code, lotNo);
    }

}
