package com.sw.mes.sap.service.impl;


import com.sw.mes.core.base.BaseServiceImpl;
import com.sw.mes.core.context.BaseContextHandler;
import com.sw.mes.core.response.Result;
import com.sw.mes.core.response.ResultGenerator;
import com.sw.mes.sap.dao.ReportErpOutputRecordDetailDao;
import com.sw.mes.sap.dto.ReportErpOutputRecordDetailDto;
import com.sw.mes.sap.entity.ReportErpOutputRecordDetail;
import com.sw.mes.sap.service.ReportErpOutputRecordDetailService;
import com.sw.mes.yb.production.entity.LongCrystal;
import com.sw.mes.yb.production.service.LongCrystalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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


/**
* @Author
* @Description：
* @Date: Create in
* @Modified By:
*/
@Service
@Transactional
public class ReportErpOutputRecordDetailServiceImpl extends BaseServiceImpl<ReportErpOutputRecordDetail> implements ReportErpOutputRecordDetailService {

    @Autowired
    private LongCrystalService longCrystalService;

    @Autowired
    private ReportErpOutputRecordDetailService reportErpOutputRecordDetailService;

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

    @Override
    public List<ReportErpOutputRecordDetail> findByRid(Long id) {
        List<ReportErpOutputRecordDetail> list = dao.findByRid(id);
        return list;
    }

    @Override
    public ReportErpOutputRecordDetail findByCrystalNo(String crystalNo) {
        ReportErpOutputRecordDetail reportErpOutputRecordDetail = dao.findByCrystalNo(crystalNo);
        return reportErpOutputRecordDetail;
    }

    @Override
    public Result writeOffAccountsMethod(ReportErpOutputRecordDetailDto dto) {

        String crystalNo = dto.getCrystalNo();
        LongCrystal byCrystalNo = longCrystalService.findByCrystalNo(crystalNo);
        if(null != byCrystalNo){
            byCrystalNo.setRodPulled(0);
            byCrystalNo.setPushed(0);
            longCrystalService.saveOrUpdate(byCrystalNo);
        }
        ReportErpOutputRecordDetail reportErpOutputRecordDetail = reportErpOutputRecordDetailService.get(dto.getId());
        reportErpOutputRecordDetail.setWriteOffAccountsTime(new Date());
        reportErpOutputRecordDetail.setWriteOffAccountsUser(BaseContextHandler.getUsername());
        reportErpOutputRecordDetailService.saveOrUpdate(reportErpOutputRecordDetail);
        return ResultGenerator.success();
    }
}
